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