Changeset 55321
- Timestamp:
- 02/13/2023 07:11:15 PM (2 years ago)
- Location:
- trunk/tests/phpunit/tests
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/comment.php
r55320 r55321 1237 1237 $this->assertSame( '1', $comment->comment_approved ); 1238 1238 } 1239 1240 /**1241 * Testing the `wp_comments_personal_data_exporter()` function.1242 *1243 * @group privacy1244 * @ticket 434401245 *1246 * @covers ::wp_comments_personal_data_exporter1247 */1248 public function test_wp_comments_personal_data_exporter() {1249 $args = array(1250 'comment_post_ID' => self::$post_id,1251 'comment_author' => 'Comment Author',1252 'comment_author_email' => 'personal@local.host',1253 'comment_author_url' => 'https://local.host/',1254 'comment_author_IP' => '192.168.0.1',1255 'comment_agent' => 'SOME_AGENT',1256 'comment_date' => '2018-03-28 20:05:00',1257 'comment_content' => 'Comment',1258 );1259 1260 $comment_id = self::factory()->comment->create( $args );1261 1262 $actual = wp_comments_personal_data_exporter( $args['comment_author_email'] );1263 $expected = $args;1264 1265 $this->assertTrue( $actual['done'] );1266 1267 // Number of exported comments.1268 $this->assertCount( 1, $actual['data'] );1269 1270 // Number of exported comment properties.1271 $this->assertCount( 8, $actual['data'][0]['data'] );1272 1273 // Exported group.1274 $this->assertSame( 'comments', $actual['data'][0]['group_id'] );1275 $this->assertSame( 'Comments', $actual['data'][0]['group_label'] );1276 1277 // Exported comment properties.1278 $this->assertSame( $expected['comment_author'], $actual['data'][0]['data'][0]['value'] );1279 $this->assertSame( $expected['comment_author_email'], $actual['data'][0]['data'][1]['value'] );1280 $this->assertSame( $expected['comment_author_url'], $actual['data'][0]['data'][2]['value'] );1281 $this->assertSame( $expected['comment_author_IP'], $actual['data'][0]['data'][3]['value'] );1282 $this->assertSame( $expected['comment_agent'], $actual['data'][0]['data'][4]['value'] );1283 $this->assertSame( $expected['comment_date'], $actual['data'][0]['data'][5]['value'] );1284 $this->assertSame( $expected['comment_content'], $actual['data'][0]['data'][6]['value'] );1285 $this->assertSame( esc_html( get_comment_link( $comment_id ) ), strip_tags( $actual['data'][0]['data'][7]['value'] ) );1286 }1287 1288 /**1289 * Testing the `wp_comments_personal_data_exporter()` function for no comments found.1290 *1291 * @group privacy1292 * @ticket 434401293 *1294 * @covers ::wp_comments_personal_data_exporter1295 */1296 public function test_wp_comments_personal_data_exporter_no_comments_found() {1297 1298 $actual = wp_comments_personal_data_exporter( 'nocommentsfound@local.host' );1299 1300 $expected = array(1301 'data' => array(),1302 'done' => true,1303 );1304 1305 $this->assertSame( $expected, $actual );1306 }1307 1308 /**1309 * Testing the `wp_comments_personal_data_exporter()` function for an empty comment property.1310 *1311 * @group privacy1312 * @ticket 434401313 *1314 * @covers ::wp_comments_personal_data_exporter1315 */1316 public function test_wp_comments_personal_data_exporter_empty_comment_prop() {1317 $args = array(1318 'comment_post_ID' => self::$post_id,1319 'comment_author' => 'Comment Author',1320 'comment_author_email' => 'personal@local.host',1321 'comment_author_url' => 'https://local.host/',1322 'comment_author_IP' => '192.168.0.1',1323 'comment_date' => '2018-03-28 20:05:00',1324 'comment_agent' => '',1325 'comment_content' => 'Comment',1326 );1327 1328 $c = self::factory()->comment->create( $args );1329 1330 $actual = wp_comments_personal_data_exporter( $args['comment_author_email'] );1331 1332 $this->assertTrue( $actual['done'] );1333 1334 // Number of exported comments.1335 $this->assertCount( 1, $actual['data'] );1336 1337 // Number of exported comment properties.1338 $this->assertCount( 7, $actual['data'][0]['data'] );1339 }1340 1341 /**1342 * Testing the `wp_comments_personal_data_exporter()` function with an empty second page.1343 *1344 * @group privacy1345 * @ticket 434401346 *1347 * @covers ::wp_comments_personal_data_exporter1348 */1349 public function test_wp_comments_personal_data_exporter_empty_second_page() {1350 $args = array(1351 'comment_post_ID' => self::$post_id,1352 'comment_author' => 'Comment Author',1353 'comment_author_email' => 'personal@local.host',1354 'comment_author_url' => 'https://local.host/',1355 'comment_author_IP' => '192.168.0.1',1356 'comment_date' => '2018-03-28 20:05:00',1357 'comment_agent' => 'SOME_AGENT',1358 'comment_content' => 'Comment',1359 );1360 1361 $c = self::factory()->comment->create( $args );1362 1363 $actual = wp_comments_personal_data_exporter( $args['comment_author_email'], 2 );1364 1365 $this->assertTrue( $actual['done'] );1366 1367 // Number of exported comments.1368 $this->assertCount( 0, $actual['data'] );1369 }1370 1239 } -
trunk/tests/phpunit/tests/comment/wpCommentsPersonalDataExporter.php
r55320 r55321 3 3 /** 4 4 * @group comment 5 * @group privacy 6 * 7 * @covers ::wp_comments_personal_data_exporter 5 8 */ 6 class Tests_Comment extends WP_UnitTestCase { 7 protected static $user_id; 8 protected static $post_id; 9 protected static $notify_message = ''; 10 11 protected $preprocess_comment_data = array(); 12 13 public function set_up() { 14 parent::set_up(); 15 reset_phpmailer_instance(); 16 } 17 18 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 19 self::$user_id = $factory->user->create( 20 array( 21 'role' => 'author', 22 'user_login' => 'test_wp_user_get', 23 'user_pass' => 'password', 24 'user_email' => 'test@test.com', 25 ) 26 ); 27 28 self::$post_id = $factory->post->create( 29 array( 30 'post_author' => self::$user_id, 31 ) 32 ); 33 } 34 35 /** 36 * @covers ::wp_update_comment 37 */ 38 public function test_wp_update_comment() { 39 $post = self::factory()->post->create_and_get( 40 array( 41 'post_title' => 'some-post', 42 'post_type' => 'post', 43 ) 44 ); 45 $post2 = self::factory()->post->create_and_get( 46 array( 47 'post_title' => 'some-post-2', 48 'post_type' => 'post', 49 ) 50 ); 51 52 $comments = self::factory()->comment->create_post_comments( $post->ID, 5 ); 53 54 $result = wp_update_comment( 55 array( 56 'comment_ID' => $comments[0], 57 'comment_parent' => $comments[1], 58 ) 59 ); 60 $this->assertSame( 1, $result ); 61 62 $comment = get_comment( $comments[0] ); 63 $this->assertEquals( $comments[1], $comment->comment_parent ); 64 65 $result = wp_update_comment( 66 array( 67 'comment_ID' => $comments[0], 68 'comment_parent' => $comments[1], 69 ) 70 ); 71 $this->assertSame( 0, $result ); 72 73 $result = wp_update_comment( 74 array( 75 'comment_ID' => $comments[0], 76 'comment_post_ID' => $post2->ID, 77 ) 78 ); 79 80 $comment = get_comment( $comments[0] ); 81 $this->assertEquals( $post2->ID, $comment->comment_post_ID ); 82 } 83 84 public function test_update_comment_from_privileged_user_by_privileged_user() { 85 $admin_id_1 = self::factory()->user->create( array( 'role' => 'administrator' ) ); 86 wp_set_current_user( $admin_id_1 ); 87 88 $comment_id = wp_new_comment( 89 array( 90 'comment_post_ID' => self::$post_id, 91 'comment_author' => 'Author', 92 'comment_author_url' => 'http://example.localhost/', 93 'comment_author_email' => 'test@test.com', 94 'user_id' => $admin_id_1, 95 'comment_content' => 'This is a comment', 96 ) 97 ); 98 99 wp_set_current_user( 0 ); 100 101 $admin_id_2 = self::factory()->user->create( 102 array( 103 'role' => 'administrator', 104 'user_login' => 'test_wp_admin_get', 105 'user_pass' => 'password', 106 'user_email' => 'testadmin@test.com', 107 ) 108 ); 109 110 wp_set_current_user( $admin_id_2 ); 111 112 wp_update_comment( 113 array( 114 'comment_ID' => $comment_id, 115 'comment_content' => 'new comment <img onerror=demo src=x>', 116 ) 117 ); 118 119 $comment = get_comment( $comment_id ); 120 $expected_content = is_multisite() 121 ? 'new comment ' 122 : 'new comment <img onerror=demo src=x>'; 123 124 $this->assertSame( $expected_content, $comment->comment_content ); 125 126 wp_set_current_user( 0 ); 127 } 128 129 public function test_update_comment_from_unprivileged_user_by_privileged_user() { 130 wp_set_current_user( self::$user_id ); 131 132 $comment_id = wp_new_comment( 133 array( 134 'comment_post_ID' => self::$post_id, 135 'comment_author' => 'Author', 136 'comment_author_url' => 'http://example.localhost/', 137 'comment_author_email' => 'test@test.com', 138 'user_id' => self::$user_id, 139 'comment_content' => '<a href="http://example.localhost/something.html">click</a>', 140 ) 141 ); 142 143 wp_set_current_user( 0 ); 144 145 $admin_id = self::factory()->user->create( 146 array( 147 'role' => 'administrator', 148 'user_login' => 'test_wp_admin_get', 149 'user_pass' => 'password', 150 'user_email' => 'testadmin@test.com', 151 ) 152 ); 153 154 wp_set_current_user( $admin_id ); 155 156 wp_update_comment( 157 array( 158 'comment_ID' => $comment_id, 159 'comment_content' => '<a href="http://example.localhost/something.html" disallowed=attribute>click</a>', 160 ) 161 ); 162 163 $comment = get_comment( $comment_id ); 164 $this->assertEquals( '<a href="http://example.localhost/something.html" rel="nofollow ugc">click</a>', $comment->comment_content, 'Comment: ' . $comment->comment_content ); 165 wp_set_current_user( 0 ); 166 } 167 168 /** 169 * @ticket 30627 170 * 171 * @covers ::wp_update_comment 172 */ 173 public function test_wp_update_comment_updates_comment_type() { 174 $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); 175 176 wp_update_comment( 177 array( 178 'comment_ID' => $comment_id, 179 'comment_type' => 'pingback', 180 ) 181 ); 182 183 $comment = get_comment( $comment_id ); 184 $this->assertSame( 'pingback', $comment->comment_type ); 185 } 186 187 /** 188 * @ticket 36784 189 * 190 * @covers ::wp_update_comment 191 */ 192 public function test_wp_update_comment_updates_comment_meta() { 193 $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); 194 195 wp_update_comment( 196 array( 197 'comment_ID' => $comment_id, 198 'comment_meta' => array( 199 'food' => 'taco', 200 'sauce' => 'fire', 201 ), 202 ) 203 ); 204 205 $this->assertSame( 'fire', get_comment_meta( $comment_id, 'sauce', true ) ); 206 } 207 208 /** 209 * @ticket 30307 210 * 211 * @covers ::wp_update_comment 212 */ 213 public function test_wp_update_comment_updates_user_id() { 214 $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); 215 216 wp_update_comment( 217 array( 218 'comment_ID' => $comment_id, 219 'user_id' => 1, 220 ) 221 ); 222 223 $comment = get_comment( $comment_id ); 224 $this->assertEquals( 1, $comment->user_id ); 225 } 226 227 /** 228 * @ticket 34954 229 * 230 * @covers ::wp_update_comment 231 */ 232 public function test_wp_update_comment_with_no_post_id() { 233 $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => 0 ) ); 234 235 $updated_comment_text = 'I should be able to update a comment with a Post ID of zero'; 236 237 $update = wp_update_comment( 238 array( 239 'comment_ID' => $comment_id, 240 'comment_content' => $updated_comment_text, 241 'comment_post_ID' => 0, 242 ) 243 ); 244 $this->assertSame( 1, $update ); 245 246 $comment = get_comment( $comment_id ); 247 $this->assertSame( $updated_comment_text, $comment->comment_content ); 248 } 249 250 /** 251 * @ticket 39732 252 * 253 * @covers ::wp_update_comment 254 */ 255 public function test_wp_update_comment_returns_false_for_invalid_comment_or_post_id() { 256 $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); 257 258 $update = wp_update_comment( 259 array( 260 'comment_ID' => -1, 261 'comment_post_ID' => self::$post_id, 262 ) 263 ); 264 $this->assertFalse( $update ); 265 266 $update = wp_update_comment( 267 array( 268 'comment_ID' => $comment_id, 269 'comment_post_ID' => -1, 270 ) 271 ); 272 $this->assertFalse( $update ); 273 } 274 275 /** 276 * @ticket 39732 277 * 278 * @covers ::wp_update_comment 279 */ 280 public function test_wp_update_comment_is_wp_error() { 281 $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); 282 283 add_filter( 'wp_update_comment_data', array( $this, 'wp_update_comment_data_filter' ), 10, 3 ); 284 285 $result = wp_update_comment( 286 array( 287 'comment_ID' => $comment_id, 288 'comment_type' => 'pingback', 289 ), 290 true 291 ); 292 293 remove_filter( 'wp_update_comment_data', array( $this, 'wp_update_comment_data_filter' ), 10, 3 ); 294 295 $this->assertWPError( $result ); 296 } 297 298 /** 299 * Blocks comments from being updated by returning WP_Error. 300 */ 301 public function wp_update_comment_data_filter( $data, $comment, $commentarr ) { 302 return new WP_Error( 'comment_wrong', 'wp_update_comment_data filter fails for this comment.', 500 ); 303 } 304 305 /** 306 * @covers ::get_approved_comments 307 */ 308 public function test_get_approved_comments() { 309 $ca1 = self::factory()->comment->create( 310 array( 311 'comment_post_ID' => self::$post_id, 312 'comment_approved' => '1', 313 ) 314 ); 315 $ca2 = self::factory()->comment->create( 316 array( 317 'comment_post_ID' => self::$post_id, 318 'comment_approved' => '1', 319 ) 320 ); 321 $ca3 = self::factory()->comment->create( 322 array( 323 'comment_post_ID' => self::$post_id, 324 'comment_approved' => '0', 325 ) 326 ); 327 $c2 = self::factory()->comment->create( 328 array( 329 'comment_post_ID' => self::$post_id, 330 'comment_approved' => '1', 331 'comment_type' => 'pingback', 332 ) 333 ); 334 $c3 = self::factory()->comment->create( 335 array( 336 'comment_post_ID' => self::$post_id, 337 'comment_approved' => '1', 338 'comment_type' => 'trackback', 339 ) 340 ); 341 $c4 = self::factory()->comment->create( 342 array( 343 'comment_post_ID' => self::$post_id, 344 'comment_approved' => '1', 345 'comment_type' => 'mario', 346 ) 347 ); 348 $c5 = self::factory()->comment->create( 349 array( 350 'comment_post_ID' => self::$post_id, 351 'comment_approved' => '1', 352 'comment_type' => 'luigi', 353 ) 354 ); 355 356 $found = get_approved_comments( self::$post_id ); 357 358 // All comment types will be returned. 359 $this->assertEquals( array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), wp_list_pluck( $found, 'comment_ID' ) ); 360 } 361 362 /** 363 * @ticket 30412 364 * 365 * @covers ::get_approved_comments 366 */ 367 public function test_get_approved_comments_with_post_id_0_should_return_empty_array() { 368 $ca1 = self::factory()->comment->create( 369 array( 370 'comment_post_ID' => self::$post_id, 371 'comment_approved' => '1', 372 ) 373 ); 374 375 $found = get_approved_comments( 0 ); 376 377 $this->assertSame( array(), $found ); 378 } 379 380 /** 381 * @ticket 14279 382 * 383 * @covers ::wp_new_comment 384 */ 385 public function test_wp_new_comment_respects_dates() { 386 $data = array( 387 'comment_post_ID' => self::$post_id, 388 'comment_author' => 'Comment Author', 389 'comment_author_url' => '', 390 'comment_author_email' => '', 391 'comment_type' => '', 392 'comment_content' => 'Comment', 393 'comment_date' => '2011-01-01 10:00:00', 394 'comment_date_gmt' => '2011-01-01 10:00:00', 395 ); 396 397 $id = wp_new_comment( $data ); 398 399 $comment = get_comment( $id ); 400 401 $this->assertSame( $data['comment_date'], $comment->comment_date ); 402 $this->assertSame( $data['comment_date_gmt'], $comment->comment_date_gmt ); 403 } 404 405 /** 406 * @ticket 14601 407 * 408 * @covers ::wp_new_comment 409 */ 410 public function test_wp_new_comment_respects_author_ip() { 411 $data = array( 412 'comment_post_ID' => self::$post_id, 413 'comment_author' => 'Comment Author', 414 'comment_author_IP' => '192.168.1.1', 415 'comment_author_url' => '', 416 'comment_author_email' => '', 417 'comment_type' => '', 418 'comment_content' => 'Comment', 419 ); 420 421 $id = wp_new_comment( $data ); 422 423 $comment = get_comment( $id ); 424 425 $this->assertSame( $data['comment_author_IP'], $comment->comment_author_IP ); 426 } 427 428 /** 429 * @ticket 14601 430 * 431 * @covers ::wp_new_comment 432 */ 433 public function test_wp_new_comment_respects_author_ip_empty_string() { 434 $data = array( 435 'comment_post_ID' => self::$post_id, 436 'comment_author' => 'Comment Author', 437 'comment_author_IP' => '', 438 'comment_author_url' => '', 439 'comment_author_email' => '', 440 'comment_type' => '', 441 'comment_content' => 'Comment', 442 ); 443 444 $id = wp_new_comment( $data ); 445 446 $comment = get_comment( $id ); 447 448 $this->assertSame( $data['comment_author_IP'], $comment->comment_author_IP ); 449 } 450 451 /** 452 * @ticket 14601 453 * 454 * @covers ::wp_new_comment 455 */ 456 public function test_wp_new_comment_respects_comment_agent() { 457 $data = array( 458 'comment_post_ID' => self::$post_id, 459 'comment_author' => 'Comment Author', 460 'comment_author_IP' => '', 461 'comment_author_url' => '', 462 'comment_author_email' => '', 463 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53', 464 'comment_type' => '', 465 'comment_content' => 'Comment', 466 ); 467 468 $id = wp_new_comment( $data ); 469 470 $comment = get_comment( $id ); 471 472 $this->assertSame( $data['comment_agent'], $comment->comment_agent ); 473 } 474 475 /** 476 * @ticket 14601 477 * 478 * @covers ::wp_new_comment 479 */ 480 public function test_wp_new_comment_should_trim_provided_comment_agent_to_254_chars() { 481 $data = array( 482 'comment_post_ID' => self::$post_id, 483 'comment_author' => 'Comment Author', 484 'comment_author_IP' => '', 485 'comment_author_url' => '', 486 'comment_author_email' => '', 487 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en; rv:1.8.1.4pre) Gecko/20070511 Camino/1.6pre', 488 'comment_type' => '', 489 'comment_content' => 'Comment', 490 ); 491 492 $id = wp_new_comment( $data ); 493 494 $comment = get_comment( $id ); 495 496 $this->assertSame( 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS ', $comment->comment_agent ); 497 } 498 499 /** 500 * @ticket 14601 501 * 502 * @covers ::wp_new_comment 503 */ 504 public function test_wp_new_comment_respects_comment_agent_empty_string() { 505 $data = array( 506 'comment_post_ID' => self::$post_id, 507 'comment_author' => 'Comment Author', 508 'comment_author_IP' => '', 509 'comment_author_url' => '', 510 'comment_author_email' => '', 511 'comment_agent' => '', 512 'comment_type' => '', 513 'comment_content' => 'Comment', 514 ); 515 516 $id = wp_new_comment( $data ); 517 518 $comment = get_comment( $id ); 519 520 $this->assertSame( $data['comment_agent'], $comment->comment_agent ); 521 } 522 523 /** 524 * @covers ::wp_new_comment 525 */ 526 public function test_wp_new_comment_respects_comment_field_lengths() { 527 $data = array( 528 'comment_post_ID' => self::$post_id, 529 'comment_author' => 'Comment Author', 530 'comment_author_url' => '', 531 'comment_author_email' => '', 532 'comment_type' => '', 533 'comment_content' => str_repeat( 'A', 65536 ), 534 'comment_date' => '2011-01-01 10:00:00', 535 'comment_date_gmt' => '2011-01-01 10:00:00', 536 ); 537 538 $id = wp_new_comment( $data ); 539 540 $comment = get_comment( $id ); 541 542 $this->assertSame( 65535, strlen( $comment->comment_content ) ); 543 } 544 545 /** 546 * @ticket 56244 547 * 548 * @covers ::wp_new_comment 549 */ 550 public function test_wp_new_comment_sends_all_expected_parameters_to_preprocess_comment_filter() { 551 $user = get_userdata( self::$user_id ); 552 wp_set_current_user( $user->ID ); 553 554 $data = array( 555 'comment_post_ID' => self::$post_id, 556 'comment_author' => $user->display_name, 557 'comment_author_email' => $user->user_email, 558 'comment_author_url' => $user->user_url, 559 'comment_content' => 'Comment', 560 'comment_type' => '', 561 'comment_parent' => 0, 562 'user_id' => $user->ID, 563 ); 564 565 add_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) ); 566 567 $comment = wp_new_comment( $data ); 568 569 $this->assertNotWPError( $comment ); 570 $this->assertSameSetsWithIndex( 571 array( 572 'comment_post_ID' => self::$post_id, 573 'comment_author' => $user->display_name, 574 'comment_author_email' => $user->user_email, 575 'comment_author_url' => $user->user_url, 576 'comment_content' => $data['comment_content'], 577 'comment_type' => '', 578 'comment_parent' => 0, 579 'user_ID' => $user->ID, 580 'user_id' => $user->ID, 581 'comment_author_IP' => '127.0.0.1', 582 'comment_agent' => '', 583 ), 584 $this->preprocess_comment_data 585 ); 586 587 } 588 589 public function filter_preprocess_comment( $commentdata ) { 590 $this->preprocess_comment_data = $commentdata; 591 return $commentdata; 592 } 593 594 /** 595 * @ticket 32566 596 * 597 * @covers ::wp_notify_moderator 598 */ 599 public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() { 600 $p = self::factory()->post->create( 601 array( 602 'post_author' => 0, 603 ) 604 ); 605 606 $c = self::factory()->comment->create( 607 array( 608 'comment_post_ID' => $p, 609 ) 610 ); 611 612 $this->assertTrue( wp_notify_moderator( $c ) ); 613 } 614 615 /** 616 * @covers ::wp_new_comment_notify_postauthor 617 */ 618 public function test_wp_new_comment_notify_postauthor_should_send_email_when_comment_is_approved() { 619 $c = self::factory()->comment->create( 620 array( 621 'comment_post_ID' => self::$post_id, 622 ) 623 ); 624 625 $sent = wp_new_comment_notify_postauthor( $c ); 626 $this->assertTrue( $sent ); 627 } 628 629 /** 630 * @covers ::wp_new_comment_notify_postauthor 631 */ 632 public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_is_unapproved() { 633 $c = self::factory()->comment->create( 634 array( 635 'comment_post_ID' => self::$post_id, 636 'comment_approved' => '0', 637 ) 638 ); 639 640 $sent = wp_new_comment_notify_postauthor( $c ); 641 $this->assertFalse( $sent ); 642 } 643 644 /** 645 * @ticket 33587 646 * 647 * @covers ::wp_new_comment_notify_postauthor 648 */ 649 public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_marked_as_spam() { 650 $c = self::factory()->comment->create( 651 array( 652 'comment_post_ID' => self::$post_id, 653 'comment_approved' => 'spam', 654 ) 655 ); 656 657 $sent = wp_new_comment_notify_postauthor( $c ); 658 $this->assertFalse( $sent ); 659 } 660 661 /** 662 * @ticket 35006 663 * 664 * @covers ::wp_new_comment_notify_postauthor 665 */ 666 public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_trashed() { 667 $c = self::factory()->comment->create( 668 array( 669 'comment_post_ID' => self::$post_id, 670 'comment_approved' => 'trash', 671 ) 672 ); 673 674 $sent = wp_new_comment_notify_postauthor( $c ); 675 $this->assertFalse( $sent ); 676 } 677 678 /** 679 * @ticket 43805 680 * 681 * @covers ::wp_new_comment_notify_postauthor 682 */ 683 public function test_wp_new_comment_notify_postauthor_content_should_include_link_to_parent() { 684 $c1 = self::factory()->comment->create( 685 array( 686 'comment_post_ID' => self::$post_id, 687 ) 688 ); 689 690 $c2 = self::factory()->comment->create( 691 array( 692 'comment_post_ID' => self::$post_id, 693 'comment_parent' => $c1, 694 ) 695 ); 696 697 add_filter( 'comment_notification_text', array( $this, 'save_comment_notification_text' ) ); 698 wp_new_comment_notify_postauthor( $c2 ); 699 remove_filter( 'comment_notification_text', array( $this, 'save_comment_notification_text' ) ); 700 701 $this->assertStringContainsString( admin_url( "comment.php?action=editcomment&c={$c1}" ), self::$notify_message ); 702 } 703 704 /** 705 * @ticket 43805 706 * 707 * @covers ::wp_new_comment_notify_moderator 708 */ 709 public function test_wp_new_comment_notify_moderator_content_should_include_link_to_parent() { 710 $c1 = self::factory()->comment->create( 711 array( 712 'comment_post_ID' => self::$post_id, 713 ) 714 ); 715 716 $c2 = self::factory()->comment->create( 717 array( 718 'comment_post_ID' => self::$post_id, 719 'comment_parent' => $c1, 720 'comment_approved' => '0', 721 ) 722 ); 723 724 add_filter( 'comment_moderation_text', array( $this, 'save_comment_notification_text' ) ); 725 wp_new_comment_notify_moderator( $c2 ); 726 remove_filter( 'comment_moderation_text', array( $this, 'save_comment_notification_text' ) ); 727 728 $this->assertStringContainsString( admin_url( "comment.php?action=editcomment&c={$c1}" ), self::$notify_message ); 729 } 730 731 /** 732 * Callback for the `comment_notification_text` & `comment_moderation_text` filters. 733 * 734 * @param string $notify_message The comment notification or moderation email text. 735 * @return string 736 */ 737 public function save_comment_notification_text( $notify_message = '' ) { 738 self::$notify_message = $notify_message; 739 return $notify_message; 740 } 741 742 /** 743 * @ticket 12431 744 * 745 * @covers ::get_comment_meta 746 */ 747 public function test_wp_new_comment_with_meta() { 748 $c = self::factory()->comment->create( 749 array( 750 'comment_approved' => '1', 751 'comment_meta' => array( 752 'food' => 'taco', 753 'sauce' => 'fire', 754 ), 755 ) 756 ); 757 758 $this->assertSame( 'fire', get_comment_meta( $c, 'sauce', true ) ); 759 } 760 761 /** 762 * @ticket 8071 763 * 764 * @covers WP_Comment::get_children 765 */ 766 public function test_wp_comment_get_children_should_fill_children() { 767 $c1 = self::factory()->comment->create( 768 array( 769 'comment_post_ID' => self::$post_id, 770 'comment_approved' => '1', 771 ) 772 ); 773 774 $c2 = self::factory()->comment->create( 775 array( 776 'comment_post_ID' => self::$post_id, 777 'comment_approved' => '1', 778 'comment_parent' => $c1, 779 ) 780 ); 781 782 $c3 = self::factory()->comment->create( 783 array( 784 'comment_post_ID' => self::$post_id, 785 'comment_approved' => '1', 786 'comment_parent' => $c2, 787 ) 788 ); 789 790 $c4 = self::factory()->comment->create( 791 array( 792 'comment_post_ID' => self::$post_id, 793 'comment_approved' => '1', 794 'comment_parent' => $c1, 795 ) 796 ); 797 798 $c5 = self::factory()->comment->create( 799 array( 800 'comment_post_ID' => self::$post_id, 801 'comment_approved' => '1', 802 ) 803 ); 804 805 $c6 = self::factory()->comment->create( 806 array( 807 'comment_post_ID' => self::$post_id, 808 'comment_approved' => '1', 809 'comment_parent' => $c5, 810 ) 811 ); 812 813 $comment = get_comment( $c1 ); 814 $children = $comment->get_children(); 815 816 // Direct descendants of $c1. 817 $this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $children, 'comment_ID' ) ) ); 818 819 // Direct descendants of $c2. 820 $this->assertEqualSets( array( $c3 ), array_values( wp_list_pluck( $children[ $c2 ]->get_children(), 'comment_ID' ) ) ); 821 } 822 823 /** 824 * @ticket 27571 825 * 826 * @covers ::get_comment 827 */ 828 public function test_post_properties_should_be_lazyloaded() { 829 $c = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); 830 831 $post = get_post( self::$post_id ); 832 $comment = get_comment( $c ); 833 834 $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' ); 835 836 foreach ( $post_fields as $pf ) { 837 $this->assertTrue( isset( $comment->$pf ), $pf ); 838 $this->assertSame( $post->$pf, $comment->$pf, $pf ); 839 } 840 } 841 842 843 /** 844 * Helper function to set up comment for 761 tests. 845 * 846 * @since 4.4.0 847 * @access public 848 */ 849 public function setup_notify_comment() { 850 /** 851 * Prevent flood alert from firing. 852 */ 853 add_filter( 'comment_flood_filter', '__return_false' ); 854 855 /** 856 * Set up a comment for testing. 857 */ 858 $post = self::factory()->post->create( 859 array( 860 'post_author' => self::$user_id, 861 ) 862 ); 863 864 $comment = self::factory()->comment->create( 865 array( 866 'comment_post_ID' => $post, 867 ) 868 ); 869 870 return array( 871 'post' => $post, 872 'comment' => $comment, 873 ); 874 } 875 876 /** 877 * @ticket 761 878 * 879 * @covers ::wp_new_comment 880 */ 881 public function test_wp_notify_moderator_filter_moderation_notify_option_true_filter_false() { 882 $comment_data = $this->setup_notify_comment(); 883 884 /** 885 * Test with moderator notification setting on, filter set to off. 886 * Should not send a notification. 887 */ 888 update_option( 'moderation_notify', 1 ); 889 add_filter( 'notify_moderator', '__return_false' ); 890 891 $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] ); 892 893 $this->assertFalse( $notification_sent, 'Moderator notification setting on, filter set to off' ); 894 895 remove_filter( 'notify_moderator', '__return_false' ); 896 remove_filter( 'comment_flood_filter', '__return_false' ); 897 } 898 899 /** 900 * @ticket 761 901 * 902 * @covers ::wp_new_comment 903 */ 904 public function test_wp_notify_moderator_filter_moderation_notify_option_false_filter_true() { 905 $comment_data = $this->setup_notify_comment(); 906 907 /** 908 * Test with moderator notification setting off, filter set to on. 909 * Should send a notification. 910 */ 911 update_option( 'moderation_notify', 0 ); 912 add_filter( 'notify_moderator', '__return_true' ); 913 914 $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] ); 915 916 $this->assertTrue( $notification_sent, 'Moderator notification setting off, filter set to on' ); 917 918 remove_filter( 'notify_moderator', '__return_true' ); 919 remove_filter( 'comment_flood_filter', '__return_false' ); 920 } 921 922 /** 923 * @ticket 761 924 * 925 * @covers ::wp_new_comment 926 */ 927 public function test_wp_notify_post_author_filter_comments_notify_option_true_filter_false() { 928 929 $comment_data = $this->setup_notify_comment(); 930 931 /** 932 * Test with author notification setting on, filter set to off. 933 * Should not send a notification. 934 */ 935 update_option( 'comments_notify', 1 ); 936 add_filter( 'notify_post_author', '__return_false' ); 937 938 $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] ); 939 940 $this->assertFalse( $notification_sent, 'Test with author notification setting on, filter set to off' ); 941 942 remove_filter( 'notify_post_author', '__return_false' ); 943 remove_filter( 'comment_flood_filter', '__return_false' ); 944 } 945 946 /** 947 * @ticket 761 948 * 949 * @covers ::wp_new_comment 950 */ 951 public function test_wp_notify_post_author_filter_comments_notify_option_false_filter_true() { 952 $comment_data = $this->setup_notify_comment(); 953 954 /** 955 * Test with author notification setting off, filter set to on. 956 * Should send a notification. 957 */ 958 update_option( 'comments_notify', 0 ); 959 add_filter( 'notify_post_author', '__return_true' ); 960 961 $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] ); 962 963 $this->assertTrue( $notification_sent, 'Test with author notification setting off, filter set to on' ); 964 965 remove_filter( 'notify_post_author', '__return_true' ); 966 remove_filter( 'comment_flood_filter', '__return_false' ); 967 } 968 969 /** 970 * Helper function to test moderator notifications. 971 * 972 * @since 4.4.0 973 * @access public 974 */ 975 public function try_sending_moderator_notification( $comment, $post ) { 976 977 // Don't approve comments, triggering notifications. 978 add_filter( 'pre_comment_approved', '__return_false' ); 979 980 // Moderators are notified when a new comment is added. 981 $data = array( 982 'comment_post_ID' => $post, 983 'comment_author' => 'Comment Author', 984 'comment_author_url' => '', 985 'comment_author_email' => '', 986 'comment_type' => '', 987 'comment_content' => 'Comment', 988 ); 989 wp_new_comment( $data ); 990 991 // Check to see if a notification email was sent to the moderator `admin@example.org`. 992 if ( isset( $GLOBALS['phpmailer']->mock_sent ) 993 && ! empty( $GLOBALS['phpmailer']->mock_sent ) 994 && WP_TESTS_EMAIL === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] 995 ) { 996 $email_sent_when_comment_added = true; 997 reset_phpmailer_instance(); 998 } else { 999 $email_sent_when_comment_added = false; 1000 } 1001 1002 return $email_sent_when_comment_added; 1003 } 1004 1005 /** 1006 * Helper function to test sending author notifications. 1007 * 1008 * @since 4.4.0 1009 * @access public 1010 */ 1011 public function try_sending_author_notification( $comment, $post ) { 1012 1013 // Approve comments, triggering notifications. 1014 add_filter( 'pre_comment_approved', '__return_true' ); 1015 1016 // Post authors possibly notified when a comment is approved on their post. 1017 wp_set_comment_status( $comment, 'approve' ); 1018 1019 // Check to see if a notification email was sent to the post author `test@test.com`. 1020 if ( isset( $GLOBALS['phpmailer']->mock_sent ) 1021 && ! empty( $GLOBALS['phpmailer']->mock_sent ) 1022 && 'test@test.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] 1023 ) { 1024 $email_sent_when_comment_approved = true; 1025 } else { 1026 $email_sent_when_comment_approved = false; 1027 } 1028 reset_phpmailer_instance(); 1029 1030 // Post authors are notified when a new comment is added to their post. 1031 $data = array( 1032 'comment_post_ID' => $post, 1033 'comment_author' => 'Comment Author', 1034 'comment_author_url' => '', 1035 'comment_author_email' => '', 1036 'comment_type' => '', 1037 'comment_content' => 'Comment', 1038 ); 1039 wp_new_comment( $data ); 1040 1041 // Check to see if a notification email was sent to the post author `test@test.com`. 1042 if ( isset( $GLOBALS['phpmailer']->mock_sent ) && 1043 ! empty( $GLOBALS['phpmailer']->mock_sent ) && 1044 'test@test.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) { 1045 $email_sent_when_comment_added = true; 1046 reset_phpmailer_instance(); 1047 } else { 1048 $email_sent_when_comment_added = false; 1049 } 1050 1051 return $email_sent_when_comment_approved || $email_sent_when_comment_added; 1052 } 1053 1054 /** 1055 * @covers ::_close_comments_for_old_post 1056 */ 1057 public function test_close_comments_for_old_post() { 1058 update_option( 'close_comments_for_old_posts', true ); 1059 // Close comments more than one day old. 1060 update_option( 'close_comments_days_old', 1 ); 1061 1062 $old_date = date_create( '-25 hours' ); 1063 $old_post_id = self::factory()->post->create( array( 'post_date' => date_format( $old_date, 'Y-m-d H:i:s' ) ) ); 1064 1065 $old_post_comment_status = _close_comments_for_old_post( true, $old_post_id ); 1066 $this->assertFalse( $old_post_comment_status ); 1067 1068 $new_post_comment_status = _close_comments_for_old_post( true, self::$post_id ); 1069 $this->assertTrue( $new_post_comment_status ); 1070 } 1071 1072 /** 1073 * @covers ::_close_comments_for_old_post 1074 */ 1075 public function test_close_comments_for_old_post_undated_draft() { 1076 $draft_id = self::factory()->post->create( 1077 array( 1078 'post_status' => 'draft', 1079 'post_type' => 'post', 1080 ) 1081 ); 1082 $draft_comment_status = _close_comments_for_old_post( true, $draft_id ); 1083 1084 $this->assertTrue( $draft_comment_status ); 1085 } 1086 1087 /** 1088 * @ticket 35276 1089 * 1090 * @covers ::wp_update_comment 1091 */ 1092 public function test_wp_update_comment_author_id_and_agent() { 1093 1094 $default_data = array( 1095 'comment_post_ID' => self::$post_id, 1096 'comment_author' => 'Comment Author', 1097 'comment_author_IP' => '192.168.0.1', 1098 'comment_agent' => 'WRONG_AGENT', 1099 'comment_author_url' => '', 1100 'comment_author_email' => '', 1101 'comment_type' => '', 1102 'comment_content' => 'Comment', 1103 ); 1104 1105 $comment_id = wp_new_comment( $default_data ); 1106 1107 // Confirm that the IP and Agent are correct on initial save. 1108 $save = get_comment( $comment_id ); 1109 $this->assertSame( $default_data['comment_author_IP'], $save->comment_author_IP ); 1110 $this->assertSame( $default_data['comment_agent'], $save->comment_agent ); 1111 1112 // Update the comment. 1113 wp_update_comment( 1114 array( 1115 'comment_ID' => $comment_id, 1116 'comment_author_IP' => '111.111.1.1', 1117 'comment_agent' => 'SHIELD_AGENT', 1118 ) 1119 ); 1120 1121 // Retrieve and check the new values. 1122 $updated = get_comment( $comment_id ); 1123 $this->assertSame( '111.111.1.1', $updated->comment_author_IP ); 1124 $this->assertSame( 'SHIELD_AGENT', $updated->comment_agent ); 1125 } 1126 1127 /** 1128 * @covers ::wp_get_comment_fields_max_lengths 1129 */ 1130 public function test_wp_get_comment_fields_max_lengths() { 1131 $expected = array( 1132 'comment_author' => 245, 1133 'comment_author_email' => 100, 1134 'comment_author_url' => 200, 1135 'comment_content' => 65525, 1136 ); 1137 1138 $lengths = wp_get_comment_fields_max_lengths(); 1139 1140 foreach ( $lengths as $field => $length ) { 1141 $this->assertSame( $expected[ $field ], $length ); 1142 } 1143 } 1144 1145 /** 1146 * @covers ::wp_update_comment 1147 */ 1148 public function test_update_should_invalidate_comment_cache() { 1149 global $wpdb; 1150 1151 $c = self::factory()->comment->create( array( 'comment_author' => 'Foo' ) ); 1152 1153 $comment = get_comment( $c ); 1154 $this->assertSame( 'Foo', $comment->comment_author ); 1155 1156 wp_update_comment( 1157 array( 1158 'comment_ID' => $c, 1159 'comment_author' => 'Bar', 1160 ) 1161 ); 1162 1163 $comment = get_comment( $c ); 1164 1165 $this->assertSame( 'Bar', $comment->comment_author ); 1166 } 1167 1168 /** 1169 * @covers ::wp_trash_comment 1170 */ 1171 public function test_trash_should_invalidate_comment_cache() { 1172 global $wpdb; 1173 1174 $c = self::factory()->comment->create(); 1175 1176 $comment = get_comment( $c ); 1177 1178 wp_trash_comment( $c ); 1179 1180 $comment = get_comment( $c ); 1181 1182 $this->assertSame( 'trash', $comment->comment_approved ); 1183 } 1184 1185 /** 1186 * @covers ::wp_untrash_comment 1187 */ 1188 public function test_untrash_should_invalidate_comment_cache() { 1189 global $wpdb; 1190 1191 $c = self::factory()->comment->create(); 1192 wp_trash_comment( $c ); 1193 1194 $comment = get_comment( $c ); 1195 $this->assertSame( 'trash', $comment->comment_approved ); 1196 1197 wp_untrash_comment( $c ); 1198 1199 $comment = get_comment( $c ); 1200 1201 $this->assertSame( '1', $comment->comment_approved ); 1202 } 1203 1204 /** 1205 * @covers ::wp_spam_comment 1206 */ 1207 public function test_spam_should_invalidate_comment_cache() { 1208 global $wpdb; 1209 1210 $c = self::factory()->comment->create(); 1211 1212 $comment = get_comment( $c ); 1213 1214 wp_spam_comment( $c ); 1215 1216 $comment = get_comment( $c ); 1217 1218 $this->assertSame( 'spam', $comment->comment_approved ); 1219 } 1220 1221 /** 1222 * @covers ::wp_unspam_comment 1223 */ 1224 public function test_unspam_should_invalidate_comment_cache() { 1225 global $wpdb; 1226 1227 $c = self::factory()->comment->create(); 1228 wp_spam_comment( $c ); 1229 1230 $comment = get_comment( $c ); 1231 $this->assertSame( 'spam', $comment->comment_approved ); 1232 1233 wp_unspam_comment( $c ); 1234 1235 $comment = get_comment( $c ); 1236 1237 $this->assertSame( '1', $comment->comment_approved ); 1238 } 9 class Tests_Comment_wpCommentsPersonalDataExporter extends WP_UnitTestCase { 1239 10 1240 11 /** 1241 12 * Testing the `wp_comments_personal_data_exporter()` function. 1242 13 * 1243 * @group privacy1244 14 * @ticket 43440 1245 *1246 * @covers ::wp_comments_personal_data_exporter1247 15 */ 1248 16 public function test_wp_comments_personal_data_exporter() { … … 1289 57 * Testing the `wp_comments_personal_data_exporter()` function for no comments found. 1290 58 * 1291 * @group privacy1292 59 * @ticket 43440 1293 *1294 * @covers ::wp_comments_personal_data_exporter1295 60 */ 1296 61 public function test_wp_comments_personal_data_exporter_no_comments_found() { … … 1309 74 * Testing the `wp_comments_personal_data_exporter()` function for an empty comment property. 1310 75 * 1311 * @group privacy1312 76 * @ticket 43440 1313 *1314 * @covers ::wp_comments_personal_data_exporter1315 77 */ 1316 78 public function test_wp_comments_personal_data_exporter_empty_comment_prop() { … … 1342 104 * Testing the `wp_comments_personal_data_exporter()` function with an empty second page. 1343 105 * 1344 * @group privacy1345 106 * @ticket 43440 1346 *1347 * @covers ::wp_comments_personal_data_exporter1348 107 */ 1349 108 public function test_wp_comments_personal_data_exporter_empty_second_page() {
Note: See TracChangeset
for help on using the changeset viewer.