Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.