Make WordPress Core

Changeset 36594


Ignore:
Timestamp:
02/20/2016 03:40:49 AM (9 years ago)
Author:
boonebgorges
Message:

Tests: Add decorators to PHPMailer mock object.

The new get_recipient() and get_sent() methods greatly simplify the
syntax required when writing tests for wp_mail().

Props welcher.
Fixes #34161.

Location:
trunk/tests/phpunit
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/mock-mailer.php

    r33124 r36594  
    1515    function postSend() {
    1616        $this->mock_sent[] = array(
    17             'to'     => $this->to,
    18             'cc'     => $this->cc,
    19             'bcc'    => $this->bcc,
    20             'header' => $this->MIMEHeader,
    21             'body'   => $this->MIMEBody,
     17            'to'      => $this->to,
     18            'cc'      => $this->cc,
     19            'bcc'     => $this->bcc,
     20            'header'  => $this->MIMEHeader,
     21            'subject' => $this->Subject,
     22            'body'    => $this->MIMEBody,
    2223        );
    2324
    2425        return true;
    2526    }
     27
     28    /**
     29     * Decorator to return the information for a sent mock.
     30     *
     31     * @since 4.5.0
     32     *
     33     * @param int $index Optional. Array index of mock_sent value.
     34     * @return object
     35     */
     36    public function get_sent( $index = 0 ) {
     37        $retval = false;
     38        if ( isset( $this->mock_sent[ $index ] ) ) {
     39            $retval = (object) $this->mock_sent[ $index ];
     40        }
     41        return $retval;
     42    }
     43
     44    /**
     45     * Get a recipient for a sent mock.
     46     *
     47     * @since 4.5.0
     48     *
     49     * @param string $address_type    The type of address for the email such as to, cc or bcc.
     50     * @param int    $mock_sent_index Optional. The sent_mock index we want to get the recipient for.
     51     * @param int    $recipient_index Optional. The recipient index in the array.
     52     * @return bool|object Returns object on success, or false if any of the indices don't exist.
     53     */
     54    public function get_recipient( $address_type, $mock_sent_index = 0, $recipient_index = 0 ) {
     55        $retval = false;
     56        $mock = $this->get_sent( $mock_sent_index );
     57        if ( $mock ) {
     58            if ( isset( $mock->{$address_type}[ $recipient_index ] ) ) {
     59                $address_index = $mock->{$address_type}[ $recipient_index ];
     60                $recipient_data = array(
     61                    'address' => ( isset( $address_index[0] ) && ! empty( $address_index[0] ) ) ? $address_index[0] : 'No address set',
     62                    'name'    => ( isset( $address_index[1] ) && ! empty( $address_index[1] ) ) ? $address_index[1] : 'No name set',
     63                );
     64
     65                $retval = (object) $recipient_data;
     66            }
     67        }
     68
     69        return $retval;
     70    }
    2671}
     72
     73/**
     74 * Helper method to return the global phpmailer instance defined in the bootstrap
     75 *
     76 * @since 4.4.0
     77 *
     78 * @return object|bool
     79 */
     80function tests_retrieve_phpmailer_instance() {
     81    $mailer = false;
     82    if ( isset( $GLOBALS['phpmailer'] ) ) {
     83        $mailer = $GLOBALS['phpmailer'];
     84    }
     85    return $mailer;
     86}
  • trunk/tests/phpunit/tests/mail.php

    r35617 r36594  
    7272        $body .= "\n";
    7373
    74         wp_mail($to, $subject, $body, $headers);
     74        wp_mail( $to, $subject, $body, $headers );
     75
     76        $mailer = tests_retrieve_phpmailer_instance();
    7577
    7678        // We need some better assertions here but these catch the failure for now.
    77         $this->assertEquals($body, $GLOBALS['phpmailer']->mock_sent[0]['body']);
    78         $this->assertTrue(strpos($GLOBALS['phpmailer']->mock_sent[0]['header'], 'boundary="----=_Part_4892_25692638.1192452070893"') > 0);
    79         $this->assertTrue(strpos($GLOBALS['phpmailer']->mock_sent[0]['header'], 'charset=') > 0);
     79        $this->assertEquals( $body, $mailer->get_sent()->body );
     80        $this->assertTrue( strpos( $mailer->get_sent()->header, 'boundary="----=_Part_4892_25692638.1192452070893"' ) > 0 );
     81        $this->assertTrue( strpos( $mailer->get_sent()->header, 'charset=' ) > 0 );
    8082    }
    8183
     
    8486     */
    8587    function test_wp_mail_rfc2822_addresses() {
    86         $to = "Name <address@tld.com>";
    87         $from = "Another Name <another_address@different-tld.com>";
    88         $cc = "The Carbon Guy <cc@cc.com>";
    89         $bcc = "The Blind Carbon Guy <bcc@bcc.com>";
    90         $subject = "RFC2822 Testing";
    91         $message = "My RFC822 Test Message";
     88        $to        = 'Name <address@tld.com>';
     89        $from      = 'Another Name <another_address@different-tld.com>';
     90        $cc        = 'The Carbon Guy <cc@cc.com>';
     91        $bcc       = 'The Blind Carbon Guy <bcc@bcc.com>';
     92        $subject   = 'RFC2822 Testing';
     93        $message   = 'My RFC822 Test Message';
    9294        $headers[] = "From: {$from}";
    9395        $headers[] = "CC: {$cc}";
     
    98100        // WordPress 3.2 and later correctly split the address into the two parts and send them seperately to PHPMailer
    99101        // Earlier versions of PHPMailer were not touchy about the formatting of these arguments.
    100         $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
    101         $this->assertEquals('Name', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][1]);
    102         $this->assertEquals('cc@cc.com', $GLOBALS['phpmailer']->mock_sent[0]['cc'][0][0]);
    103         $this->assertEquals('The Carbon Guy', $GLOBALS['phpmailer']->mock_sent[0]['cc'][0][1]);
    104         $this->assertEquals('bcc@bcc.com', $GLOBALS['phpmailer']->mock_sent[0]['bcc'][0][0]);
    105         $this->assertEquals('The Blind Carbon Guy', $GLOBALS['phpmailer']->mock_sent[0]['bcc'][0][1]);
    106         $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
     102
     103        //retrieve the mailer instance
     104        $mailer = tests_retrieve_phpmailer_instance();
     105        $this->assertEquals( 'address@tld.com',      $mailer->get_recipient( 'to' )->address );
     106        $this->assertEquals( 'Name',                 $mailer->get_recipient( 'to' )->name );
     107        $this->assertEquals( 'cc@cc.com',            $mailer->get_recipient( 'cc' )->address );
     108        $this->assertEquals( 'The Carbon Guy',       $mailer->get_recipient( 'cc' )->name );
     109        $this->assertEquals( 'bcc@bcc.com',          $mailer->get_recipient( 'bcc' )->address );
     110        $this->assertEquals( 'The Blind Carbon Guy', $mailer->get_recipient( 'bcc' )->name );
     111        $this->assertEquals( $message . "\n",        $mailer->get_sent()->body );
    107112    }
    108113
     
    111116     */
    112117    function test_wp_mail_multiple_rfc2822_to_addresses() {
    113         $to = "Name <address@tld.com>, Another Name <another_address@different-tld.com>";
    114         $subject = "RFC2822 Testing";
    115         $message = "My RFC822 Test Message";
     118        $to      = 'Name <address@tld.com>, Another Name <another_address@different-tld.com>';
     119        $subject = 'RFC2822 Testing';
     120        $message = 'My RFC822 Test Message';
    116121
    117122        wp_mail( $to, $subject, $message );
     
    119124        // WordPress 3.2 and later correctly split the address into the two parts and send them seperately to PHPMailer
    120125        // Earlier versions of PHPMailer were not touchy about the formatting of these arguments.
    121         $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
    122         $this->assertEquals('Name', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][1]);
    123         $this->assertEquals('another_address@different-tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][1][0]);
    124         $this->assertEquals('Another Name', $GLOBALS['phpmailer']->mock_sent[0]['to'][1][1]);
    125         $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
     126        $mailer = tests_retrieve_phpmailer_instance();
     127        $this->assertEquals( 'address@tld.com',                   $mailer->get_recipient( 'to' )->address );
     128        $this->assertEquals( 'Name',                              $mailer->get_recipient( 'to' )->name );
     129        $this->assertEquals( 'another_address@different-tld.com', $mailer->get_recipient( 'to', 0, 1 )->address );
     130        $this->assertEquals( 'Another Name',                      $mailer->get_recipient( 'to', 0, 1 )->name );
     131        $this->assertEquals( $message . "\n",                     $mailer->get_sent()->body );
    126132    }
    127133
    128134    function test_wp_mail_multiple_to_addresses() {
    129         $to = "address@tld.com, another_address@different-tld.com";
    130         $subject = "RFC2822 Testing";
    131         $message = "My RFC822 Test Message";
     135        $to      = 'address@tld.com, another_address@different-tld.com';
     136        $subject = 'RFC2822 Testing';
     137        $message = 'My RFC822 Test Message';
    132138
    133139        wp_mail( $to, $subject, $message );
    134140
    135         $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
    136         $this->assertEquals('another_address@different-tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][1][0]);
    137         $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
     141        $mailer = tests_retrieve_phpmailer_instance();
     142        $this->assertEquals( 'address@tld.com',                   $mailer->get_recipient( 'to' )->address );
     143        $this->assertEquals( 'another_address@different-tld.com', $mailer->get_recipient( 'to', 0, 1 )->address );
     144        $this->assertEquals( $message . "\n",                     $mailer->get_sent()->body );
    138145    }
    139146
     
    142149     */
    143150    function test_wp_mail_to_address_no_name() {
    144         $to = "<address@tld.com>";
    145         $subject = "RFC2822 Testing";
    146         $message = "My RFC822 Test Message";
     151        $to      = '<address@tld.com>';
     152        $subject = 'RFC2822 Testing';
     153        $message = 'My RFC822 Test Message';
    147154
    148155        wp_mail( $to, $subject, $message );
    149156
    150         $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
    151         $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
     157        $mailer = tests_retrieve_phpmailer_instance();
     158        $this->assertEquals( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
     159        $this->assertEquals( $message . "\n",    $mailer->get_sent()->body );
    152160    }
    153161
     
    170178     */
    171179    public function test_wp_mail_with_valid_from_header() {
    172         $to       = "address@tld.com";
    173         $subject  = "Testing";
    174         $message  = "Test Message";
    175         $headers  = "From: Foo <bar@example.com>";
    176         $expected = "From: Foo <bar@example.com>";
    177 
    178         wp_mail( $to, $subject, $message, $headers );
    179 
    180         $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     180        $to       = 'address@tld.com';
     181        $subject  = 'Testing';
     182        $message  = 'Test Message';
     183        $headers  = 'From: Foo <bar@example.com>';
     184        $expected = 'From: Foo <bar@example.com>';
     185
     186        wp_mail( $to, $subject, $message, $headers );
     187
     188        $mailer = tests_retrieve_phpmailer_instance();
     189        $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
    181190    }
    182191
     
    185194     */
    186195    public function test_wp_mail_with_empty_from_header() {
    187         $to       = "address@tld.com";
    188         $subject  = "Testing";
    189         $message  = "Test Message";
    190         $headers  = "From: ";
    191         $expected = "From: WordPress <wordpress@" . WP_TESTS_DOMAIN . ">";
    192 
    193         wp_mail( $to, $subject, $message, $headers );
    194 
    195         $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     196        $to       = 'address@tld.com';
     197        $subject  = 'Testing';
     198        $message  = 'Test Message';
     199        $headers  = 'From: ';
     200        $expected = 'From: WordPress <wordpress@' . WP_TESTS_DOMAIN . '>';
     201
     202        wp_mail( $to, $subject, $message, $headers );
     203
     204        $mailer = tests_retrieve_phpmailer_instance();
     205        $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
    196206    }
    197207
     
    200210     */
    201211    public function test_wp_mail_with_empty_from_name_for_the_from_header() {
    202         $to       = "address@tld.com";
    203         $subject  = "Testing";
    204         $message  = "Test Message";
    205         $headers  = "From: <wordpress@example.com>";
    206         $expected = "From: WordPress <wordpress@example.com>";
    207 
    208         wp_mail( $to, $subject, $message, $headers );
    209 
    210         $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     212        $to       = 'address@tld.com';
     213        $subject  = 'Testing';
     214        $message  = 'Test Message';
     215        $headers  = 'From: <wordpress@example.com>';
     216        $expected = 'From: WordPress <wordpress@example.com>';
     217
     218        wp_mail( $to, $subject, $message, $headers );
     219
     220        $mailer = tests_retrieve_phpmailer_instance();
     221        $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
    211222    }
    212223
     
    215226     */
    216227    public function test_wp_mail_with_valid_content_type_header() {
    217         $to       = "address@tld.com";
    218         $subject  = "Testing";
    219         $message  = "Test Message";
    220         $headers  = "Content-Type: text/html; charset=iso-8859-1";
    221         $expected = "Content-Type: text/html; charset=iso-8859-1";
    222 
    223         wp_mail( $to, $subject, $message, $headers );
    224 
    225         $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     228        $to       = 'address@tld.com';
     229        $subject  = 'Testing';
     230        $message  = 'Test Message';
     231        $headers  = 'Content-Type: text/html; charset=iso-8859-1';
     232        $expected = 'Content-Type: text/html; charset=iso-8859-1';
     233
     234        wp_mail( $to, $subject, $message, $headers );
     235
     236        $mailer = tests_retrieve_phpmailer_instance();
     237        $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
    226238    }
    227239
     
    230242     */
    231243    public function test_wp_mail_with_empty_content_type_header() {
    232         $to       = "address@tld.com";
    233         $subject  = "Testing";
    234         $message  = "Test Message";
    235         $headers  = "Content-Type: ";
    236         $expected = "Content-Type: text/plain; charset=UTF-8";
    237 
    238         wp_mail( $to, $subject, $message, $headers );
    239 
    240         $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     244        $to       = 'address@tld.com';
     245        $subject  = 'Testing';
     246        $message  = 'Test Message';
     247        $headers  = 'Content-Type: ';
     248        $expected = 'Content-Type: text/plain; charset=UTF-8';
     249
     250        wp_mail( $to, $subject, $message, $headers );
     251
     252        $mailer = tests_retrieve_phpmailer_instance();
     253        $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
    241254    }
    242255
     
    245258     */
    246259    public function test_wp_mail_with_empty_charset_for_the_content_type_header() {
    247         $to       = "address@tld.com";
    248         $subject  = "Testing";
    249         $message  = "Test Message";
    250         $headers  = "Content-Type: text/plain;";
    251         $expected = "Content-Type: text/plain; charset=UTF-8";
    252 
    253         wp_mail( $to, $subject, $message, $headers );
    254 
    255         $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     260        $to       = 'address@tld.com';
     261        $subject  = 'Testing';
     262        $message  = 'Test Message';
     263        $headers  = 'Content-Type: text/plain;';
     264        $expected = 'Content-Type: text/plain; charset=UTF-8';
     265
     266        wp_mail( $to, $subject, $message, $headers );
     267
     268        $mailer = tests_retrieve_phpmailer_instance();
     269        $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
    256270    }
    257271
  • trunk/tests/phpunit/tests/user.php

    r36482 r36594  
    10291029        wp_new_user_notification( self::$contrib_id, null, $notify );
    10301030
     1031        $mailer = tests_retrieve_phpmailer_instance();
     1032
    10311033        /*
    10321034         * Check to see if a notification email was sent to the
    10331035         * post author `blackburn@battlefield3.com` and and site admin `admin@example.org`.
    10341036         */
    1035         if ( ! empty( $GLOBALS['phpmailer']->mock_sent ) ) {
    1036             $was_admin_email_sent = ( isset( $GLOBALS['phpmailer']->mock_sent[0] ) && WP_TESTS_EMAIL == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] );
    1037             $was_user_email_sent = ( isset( $GLOBALS['phpmailer']->mock_sent[1] ) && 'blackburn@battlefield3.com' == $GLOBALS['phpmailer']->mock_sent[1]['to'][0][0] );
    1038         }
     1037        $admin_email = $mailer->get_recipient( 'to' );
     1038        $was_admin_email_sent = $admin_email && WP_TESTS_EMAIL === $admin_email->address;
     1039
     1040        $user_email = $mailer->get_recipient( 'to', 1 );
     1041        $was_user_email_sent = $user_email && 'blackburn@battlefield3.com' == $user_email->address;
     1042
    10391043
    10401044        $this->assertSame( $admin_email_sent_expected, $was_admin_email_sent, 'Admin email result was not as expected in test_wp_new_user_notification' );
Note: See TracChangeset for help on using the changeset viewer.