| | 290 | |
| | 291 | |
| | 292 | /** |
| | 293 | * Helper function to set up comment for 761 tests |
| | 294 | */ |
| | 295 | public function setup_notify_comment(){ |
| | 296 | /** |
| | 297 | * Mock some server variables. |
| | 298 | */ |
| | 299 | $_SERVER['SERVER_NAME'] = 'phpunit.wordpress.dev'; |
| | 300 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
| | 301 | |
| | 302 | /** |
| | 303 | * Prevent flood alert from firing. |
| | 304 | */ |
| | 305 | add_filter( 'comment_flood_filter', '__return_false' ); |
| | 306 | |
| | 307 | /** |
| | 308 | * Set up the post author to test notifying. |
| | 309 | */ |
| | 310 | $user = $this->factory->user->create( |
| | 311 | array( |
| | 312 | 'role' => 'author', |
| | 313 | 'user_login' => 'test_wp_user_get', |
| | 314 | 'user_pass' => 'password', |
| | 315 | 'user_email' => 'test@test.com', |
| | 316 | ) |
| | 317 | ); |
| | 318 | |
| | 319 | /** |
| | 320 | * Set up a comment for testing. |
| | 321 | */ |
| | 322 | $post = $this->factory->post->create( |
| | 323 | array( |
| | 324 | 'post_author' => $user, |
| | 325 | ) |
| | 326 | ); |
| | 327 | |
| | 328 | $comment = $this->factory->comment->create( |
| | 329 | array( |
| | 330 | 'comment_post_ID' => $post, |
| | 331 | ) |
| | 332 | ); |
| | 333 | |
| | 334 | return array( |
| | 335 | 'post' => $post, |
| | 336 | 'comment' => $comment, |
| | 337 | ); |
| | 338 | } |
| | 339 | |
| | 340 | /** |
| | 341 | * @ticket 761 |
| | 342 | */ |
| | 343 | public function test_wp_notify_moderator_filter() { |
| | 344 | |
| | 345 | $comment_data = $this->setup_notify_comment(); |
| | 346 | |
| | 347 | /** |
| | 348 | * Test with moderator notification setting on, filter set to off. |
| | 349 | * Should not send a notification. |
| | 350 | */ |
| | 351 | update_option( 'moderation_notify', 1 ); |
| | 352 | add_filter( 'wp_notify_moderator', '__return_false' ); |
| | 353 | $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] ); |
| | 354 | $this->assertFalse( $notification_sent, 'Moderator notification setting on, filter set to off' ); |
| | 355 | remove_filter( 'wp_notify_moderator', '__return_false' ); |
| | 356 | |
| | 357 | /** |
| | 358 | * Test with moderator notification setting off, filter set to on. |
| | 359 | * Should send a notification. |
| | 360 | */ |
| | 361 | update_option( 'moderation_notify', 0 ); |
| | 362 | add_filter( 'wp_notify_moderator', '__return_true' ); |
| | 363 | $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] ); |
| | 364 | $this->assertTrue( $notification_sent, 'Moderator notification setting off, filter set to on' ); |
| | 365 | remove_filter( 'wp_notify_moderator', '__return_true' ); |
| | 366 | |
| | 367 | } |
| | 368 | |
| | 369 | /** |
| | 370 | * @ticket 761 |
| | 371 | */ |
| | 372 | public function test_wp_notify_post_author_filter() { |
| | 373 | |
| | 374 | $comment_data = $this->setup_notify_comment(); |
| | 375 | |
| | 376 | /** |
| | 377 | * Test with author notification setting on, filter set to off. |
| | 378 | * Should not send a notification. |
| | 379 | */ |
| | 380 | update_option( 'comments_notify', 1 ); |
| | 381 | add_filter( 'wp_notify_post_author', '__return_false' ); |
| | 382 | $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] ); |
| | 383 | $this->assertFalse( $notification_sent, 'Test with author notification setting on, filter set to off' ); |
| | 384 | remove_filter( 'wp_notify_post_author', '__return_false' ); |
| | 385 | |
| | 386 | /** |
| | 387 | * Test with author notification setting off, filter set to on. |
| | 388 | * Should sent a notification. |
| | 389 | */ |
| | 390 | update_option( 'comments_notify', 0 ); |
| | 391 | add_filter( 'wp_notify_post_author', '__return_true' ); |
| | 392 | $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] ); |
| | 393 | $this->assertTrue( $notification_sent, 'Test with author notification setting off, filter set to on' ); |
| | 394 | remove_filter( 'wp_notify_post_author', '__return_true' ); |
| | 395 | |
| | 396 | } |
| | 397 | |
| | 398 | /** |
| | 399 | * Helper function to test moderator notifications. |
| | 400 | */ |
| | 401 | public function try_sending_moderator_notification( $comment, $post ) { |
| | 402 | |
| | 403 | /** |
| | 404 | * Don't approve comments, triggering notifications. |
| | 405 | */ |
| | 406 | add_filter( 'pre_comment_approved', '__return_false' ); |
| | 407 | |
| | 408 | /** |
| | 409 | * Moderators are notified when a new comment is added. |
| | 410 | */ |
| | 411 | $data = array( |
| | 412 | 'comment_post_ID' => $post, |
| | 413 | 'comment_author' => rand_str(), |
| | 414 | 'comment_author_url' => '', |
| | 415 | 'comment_author_email' => '', |
| | 416 | 'comment_type' => '', |
| | 417 | 'comment_content' => rand_str(), |
| | 418 | ); |
| | 419 | wp_new_comment( $data ); |
| | 420 | |
| | 421 | /** |
| | 422 | * Check to see if a notification email was sent to the moderator `admin@example.org`. |
| | 423 | */ |
| | 424 | if ( isset( $GLOBALS['phpmailer']->mock_sent ) && |
| | 425 | ! empty( $GLOBALS['phpmailer']->mock_sent ) && |
| | 426 | 'admin@example.org' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) { |
| | 427 | $email_sent_when_comment_added = true; |
| | 428 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| | 429 | } else { |
| | 430 | $email_sent_when_comment_added = false; |
| | 431 | } |
| | 432 | |
| | 433 | return $email_sent_when_comment_added; |
| | 434 | } |
| | 435 | |
| | 436 | /** |
| | 437 | * Helper function to test sending author notifications. |
| | 438 | */ |
| | 439 | public function try_sending_author_notification( $comment, $post ) { |
| | 440 | |
| | 441 | /** |
| | 442 | * Approve comments, triggering notifications. |
| | 443 | */ |
| | 444 | add_filter( 'pre_comment_approved', '__return_true' ); |
| | 445 | |
| | 446 | /** |
| | 447 | * Post authors possibly notified when a comment is approved on their post. |
| | 448 | */ |
| | 449 | wp_set_comment_status( $comment, 'approve' ); |
| | 450 | |
| | 451 | /** |
| | 452 | * Check to see if a notification email was sent to the post author `test@test.com`. |
| | 453 | */ |
| | 454 | if ( isset( $GLOBALS['phpmailer']->mock_sent ) && |
| | 455 | ! empty( $GLOBALS['phpmailer']->mock_sent ) && |
| | 456 | 'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) { |
| | 457 | $email_sent_when_comment_approved = true; |
| | 458 | } else { |
| | 459 | $email_sent_when_comment_approved = false; |
| | 460 | } |
| | 461 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| | 462 | |
| | 463 | /** |
| | 464 | * Post authors are notified when a new comment is added to their post. |
| | 465 | */ |
| | 466 | $data = array( |
| | 467 | 'comment_post_ID' => $post, |
| | 468 | 'comment_author' => rand_str(), |
| | 469 | 'comment_author_url' => '', |
| | 470 | 'comment_author_email' => '', |
| | 471 | 'comment_type' => '', |
| | 472 | 'comment_content' => rand_str(), |
| | 473 | ); |
| | 474 | wp_new_comment( $data ); |
| | 475 | |
| | 476 | /** |
| | 477 | * Check to see if a notification email was sent to the post author `test@test.com`. |
| | 478 | */ |
| | 479 | if ( isset( $GLOBALS['phpmailer']->mock_sent ) && |
| | 480 | ! empty( $GLOBALS['phpmailer']->mock_sent ) && |
| | 481 | 'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) { |
| | 482 | $email_sent_when_comment_added = true; |
| | 483 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| | 484 | } else { |
| | 485 | $email_sent_when_comment_added = false; |
| | 486 | } |
| | 487 | |
| | 488 | return $email_sent_when_comment_approved && $email_sent_when_comment_added; |
| | 489 | } |