| 682 | /** |
| 683 | * Testing wp_new_user_notification email statuses. |
| 684 | * |
| 685 | * Be sure when writing tests to unset any old instances of |
| 686 | * $GLOBALS['phpmailer'] or depending on array indices will be |
| 687 | * inaccurate. |
| 688 | * |
| 689 | * @dataProvider data_wp_new_user_notifications |
| 690 | * |
| 691 | * @ticket 33654 |
| 692 | * |
| 693 | * |
| 694 | */ |
| 695 | function test_wp_new_user_notification( $notify, $admin_email_sent_expected, $user_email_sent_expected ) { |
| 696 | |
| 697 | //unset any old instance of phpmailer |
| 698 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| 699 | |
| 700 | $was_admin_email_sent = false; |
| 701 | $was_user_email_sent = false; |
| 702 | |
| 703 | $user = $this->factory->user->create( $this->user_data ); |
| 704 | |
| 705 | wp_new_user_notification( $user, null, $notify ); |
| 706 | |
| 707 | /** |
| 708 | * Check to see if a notification email was sent to the |
| 709 | * post author `blackburn@battlefield3.com` and and site admin `admin@example.org`. |
| 710 | */ |
| 711 | if ( isset( $GLOBALS['phpmailer']->mock_sent ) && ! empty( $GLOBALS['phpmailer']->mock_sent ) ) { |
| 712 | $was_admin_email_sent = ( isset( $GLOBALS['phpmailer']->mock_sent[0] ) && 'admin@example.org' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) ? true : false; |
| 713 | $was_user_email_sent = ( isset( $GLOBALS['phpmailer']->mock_sent[1] ) && 'blackburn@battlefield3.com' == $GLOBALS['phpmailer']->mock_sent[1]['to'][0][0] ) ? true : false; |
| 714 | } |
| 715 | |
| 716 | $this->assertSame( $admin_email_sent_expected, $was_admin_email_sent, 'Admin email result was not as expected in test_wp_new_user_notification' ); |
| 717 | $this->assertSame( $user_email_sent_expected , $was_user_email_sent, 'User email result was not as expected in test_wp_new_user_notification' ); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Data provider. |
| 722 | * |
| 723 | * Passes the three available options for the $notify parameter and the expected email |
| 724 | * emails sent status as a bool. |
| 725 | * |
| 726 | * @since 4.4.0 |
| 727 | * |
| 728 | * @return array { |
| 729 | * @type array { |
| 730 | * @type string $post_args The arguments that will merged with the $_POST array. |
| 731 | * @type bool $admin_email_sent_expected The expected result of whether an email was sent to the admin. |
| 732 | * @type bool $user_email_sent_expected The expected result of whether an email was sent to the user. |
| 733 | * } |
| 734 | * } |
| 735 | */ |
| 736 | function data_wp_new_user_notifications() { |
| 737 | |
| 738 | return array( |
| 739 | array( |
| 740 | '', |
| 741 | true, |
| 742 | false, |
| 743 | ), |
| 744 | array( |
| 745 | 'admin', |
| 746 | true, |
| 747 | false, |
| 748 | ), |
| 749 | array( |
| 750 | 'both', |
| 751 | true, |
| 752 | true, |
| 753 | ), |
| 754 | ); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * @ticket 33654 |
| 759 | * @expectedDeprecated wp_new_user_notification |
| 760 | */ |
| 761 | function test_wp_new_user_notification_old_signature_throws_deprecated_warning() { |
| 762 | |
| 763 | $this->deprecated_called = false; |
| 764 | |
| 765 | /** |
| 766 | * Catch if the_deprecated_argument is called. |
| 767 | */ |
| 768 | add_action( |
| 769 | 'deprecated_argument_run', |
| 770 | function( $message, $version ) { |
| 771 | $this->deprecated_called = 'wp_new_user_notification' === $message; |
| 772 | }, |
| 773 | 10, |
| 774 | 2 |
| 775 | ); |
| 776 | |
| 777 | /** |
| 778 | * Set up a user and try sending a notification using the old, deprecated |
| 779 | * function signature `wp_new_user_notification( $user, 'plaintext_password' );` |
| 780 | */ |
| 781 | $user = $this->factory->user->create( |
| 782 | array( |
| 783 | 'role' => 'author', |
| 784 | 'user_login' => 'test_wp_new_user_notification', |
| 785 | 'user_pass' => 'password', |
| 786 | 'user_email' => 'test@test.com', |
| 787 | ) |
| 788 | ); |
| 789 | |
| 790 | wp_new_user_notification( $user, 'this_is_deprecated' ); |
| 791 | |
| 792 | $this->assertTrue( $this->deprecated_called ); |
| 793 | } |