| 362 | |
| 363 | |
| 364 | /** |
| 365 | * Helper function to set up comment for 761 tests |
| 366 | */ |
| 367 | public function setup_notify_comment(){ |
| 368 | /** |
| 369 | * Mock some server variables. |
| 370 | */ |
| 371 | $_SERVER['SERVER_NAME'] = 'phpunit.wordpress.dev'; |
| 372 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
| 373 | |
| 374 | /** |
| 375 | * Prevent flood alert from firing. |
| 376 | */ |
| 377 | add_filter( 'comment_flood_filter', '__return_false' ); |
| 378 | |
| 379 | /** |
| 380 | * Set up the post author to test notifying. |
| 381 | */ |
| 382 | $user = $this->factory->user->create( |
| 383 | array( |
| 384 | 'role' => 'author', |
| 385 | 'user_login' => 'test_wp_user_get', |
| 386 | 'user_pass' => 'password', |
| 387 | 'user_email' => 'test@test.com', |
| 388 | ) |
| 389 | ); |
| 390 | |
| 391 | /** |
| 392 | * Set up a comment for testing. |
| 393 | */ |
| 394 | $post = $this->factory->post->create( |
| 395 | array( |
| 396 | 'post_author' => $user, |
| 397 | ) |
| 398 | ); |
| 399 | |
| 400 | $comment = $this->factory->comment->create( |
| 401 | array( |
| 402 | 'comment_post_ID' => $post, |
| 403 | ) |
| 404 | ); |
| 405 | |
| 406 | return array( |
| 407 | 'post' => $post, |
| 408 | 'comment' => $comment, |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * @ticket 761 |
| 414 | */ |
| 415 | public function test_wp_notify_moderator_filter() { |
| 416 | |
| 417 | $comment_data = $this->setup_notify_comment(); |
| 418 | |
| 419 | /** |
| 420 | * Test with moderator notification setting on, filter set to off. |
| 421 | * Should not send a notification. |
| 422 | */ |
| 423 | update_option( 'moderation_notify', 1 ); |
| 424 | add_filter( 'notify_moderator', '__return_false' ); |
| 425 | $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] ); |
| 426 | $this->assertFalse( $notification_sent, 'Moderator notification setting on, filter set to off' ); |
| 427 | remove_filter( 'notify_moderator', '__return_false' ); |
| 428 | |
| 429 | /** |
| 430 | * Test with moderator notification setting off, filter set to on. |
| 431 | * Should send a notification. |
| 432 | */ |
| 433 | update_option( 'moderation_notify', 0 ); |
| 434 | add_filter( 'notify_moderator', '__return_true' ); |
| 435 | $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] ); |
| 436 | $this->assertTrue( $notification_sent, 'Moderator notification setting off, filter set to on' ); |
| 437 | remove_filter( 'notify_moderator', '__return_true' ); |
| 438 | |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * @ticket 761 |
| 443 | */ |
| 444 | public function test_wp_notify_post_author_filter() { |
| 445 | |
| 446 | $comment_data = $this->setup_notify_comment(); |
| 447 | |
| 448 | /** |
| 449 | * Test with author notification setting on, filter set to off. |
| 450 | * Should not send a notification. |
| 451 | */ |
| 452 | update_option( 'comments_notify', 1 ); |
| 453 | add_filter( 'notify_post_author', '__return_false' ); |
| 454 | $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] ); |
| 455 | $this->assertFalse( $notification_sent, 'Test with author notification setting on, filter set to off' ); |
| 456 | remove_filter( 'notify_post_author', '__return_false' ); |
| 457 | |
| 458 | /** |
| 459 | * Test with author notification setting off, filter set to on. |
| 460 | * Should send a notification. |
| 461 | */ |
| 462 | update_option( 'comments_notify', 0 ); |
| 463 | add_filter( 'notify_post_author', '__return_true' ); |
| 464 | $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] ); |
| 465 | $this->assertTrue( $notification_sent, 'Test with author notification setting off, filter set to on' ); |
| 466 | remove_filter( 'notify_post_author', '__return_true' ); |
| 467 | |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Helper function to test moderator notifications. |
| 472 | * |
| 473 | * @since 4.4.0 |
| 474 | * @access public |
| 475 | */ |
| 476 | public function try_sending_moderator_notification( $comment, $post ) { |
| 477 | |
| 478 | // Don't approve comments, triggering notifications. |
| 479 | add_filter( 'pre_comment_approved', '__return_false' ); |
| 480 | |
| 481 | // Moderators are notified when a new comment is added. |
| 482 | $data = array( |
| 483 | 'comment_post_ID' => $post, |
| 484 | 'comment_author' => rand_str(), |
| 485 | 'comment_author_url' => '', |
| 486 | 'comment_author_email' => '', |
| 487 | 'comment_type' => '', |
| 488 | 'comment_content' => rand_str(), |
| 489 | ); |
| 490 | wp_new_comment( $data ); |
| 491 | |
| 492 | // Check to see if a notification email was sent to the moderator `admin@example.org`. |
| 493 | if ( isset( $GLOBALS['phpmailer']->mock_sent ) && |
| 494 | ! empty( $GLOBALS['phpmailer']->mock_sent ) && |
| 495 | 'admin@example.org' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) { |
| 496 | $email_sent_when_comment_added = true; |
| 497 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| 498 | } else { |
| 499 | $email_sent_when_comment_added = false; |
| 500 | } |
| 501 | |
| 502 | return $email_sent_when_comment_added; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Helper function to test sending author notifications. |
| 507 | * |
| 508 | * @since 4.4.0 |
| 509 | * @access public |
| 510 | */ |
| 511 | public function try_sending_author_notification( $comment, $post ) { |
| 512 | |
| 513 | // Approve comments, triggering notifications. |
| 514 | add_filter( 'pre_comment_approved', '__return_true' ); |
| 515 | |
| 516 | // Post authors possibly notified when a comment is approved on their post. |
| 517 | wp_set_comment_status( $comment, 'approve' ); |
| 518 | |
| 519 | // Check to see if a notification email was sent to the post author `test@test.com`. |
| 520 | if ( isset( $GLOBALS['phpmailer']->mock_sent ) && |
| 521 | ! empty( $GLOBALS['phpmailer']->mock_sent ) && |
| 522 | 'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) { |
| 523 | $email_sent_when_comment_approved = true; |
| 524 | } else { |
| 525 | $email_sent_when_comment_approved = false; |
| 526 | } |
| 527 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| 528 | |
| 529 | // Post authors are notified when a new comment is added to their post. |
| 530 | $data = array( |
| 531 | 'comment_post_ID' => $post, |
| 532 | 'comment_author' => rand_str(), |
| 533 | 'comment_author_url' => '', |
| 534 | 'comment_author_email' => '', |
| 535 | 'comment_type' => '', |
| 536 | 'comment_content' => rand_str(), |
| 537 | ); |
| 538 | wp_new_comment( $data ); |
| 539 | |
| 540 | // Check to see if a notification email was sent to the post author `test@test.com`. |
| 541 | if ( isset( $GLOBALS['phpmailer']->mock_sent ) && |
| 542 | ! empty( $GLOBALS['phpmailer']->mock_sent ) && |
| 543 | 'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) { |
| 544 | $email_sent_when_comment_added = true; |
| 545 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| 546 | } else { |
| 547 | $email_sent_when_comment_added = false; |
| 548 | } |
| 549 | |
| 550 | return $email_sent_when_comment_approved && $email_sent_when_comment_added; |
| 551 | } |