Skip to content

Commit 697bb53

Browse files
committed
refactor: unify tests and reduce redundancy
1 parent b545e92 commit 697bb53

File tree

1 file changed

+56
-51
lines changed

1 file changed

+56
-51
lines changed

tests/phpunit/tests/pluggable/wpMail.php

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -628,33 +628,24 @@ public function test_wp_mail_string_embeds() {
628628

629629
public function address_provider() {
630630
return array(
631-
'from encoded name' => array(
632-
'type' => 'From',
633-
'header' => 'From: =?UTF-8?B?VGVzdA==?= <test@example.com>',
634-
'expected' => array( 'test@example.com', 'Test' ),
635-
),
636-
'cc multiple encoded names' => array(
637-
'type' => 'Cc',
638-
'header' => 'Cc: =?UTF-8?B?Sm9obg==?= <john@example.com>, Jane <jane@example.com>',
631+
'single encoded name' => array(
632+
'content' => '=?UTF-8?B?Sm9obg==?= <john@example.com>',
639633
'expected' => array(
640634
array( 'john@example.com', 'John' ),
641-
array( 'jane@example.com', 'Jane' ),
642635
),
643636
),
644-
'bcc multiple encoded names' => array(
645-
'type' => 'Bcc',
646-
'header' => 'Bcc: =?UTF-8?B?Sm9obg==?= <john@example.com>, Jane <jane@example.com>',
637+
'multiple names with one encoded name' => array(
638+
'content' => '=?UTF-8?B?Sm9obg==?= <john@example.com>, Jane <jane@example.com>',
647639
'expected' => array(
648640
array( 'john@example.com', 'John' ),
649641
array( 'jane@example.com', 'Jane' ),
650642
),
651643
),
652-
'reply-to multiple encoded names' => array(
653-
'type' => 'Reply-To',
654-
'header' => 'Reply-To: =?UTF-8?B?SmFuZQ==?= <jane@example.com>, =?UTF-8?B?Sm9obg==?= <john@example.com>',
644+
'multiple encoded names' => array(
645+
'content' => '=?UTF-8?B?Sm9obg==?= <john@example.com>, =?UTF-8?B?SmFuZQ==?= <jane@example.com>',
655646
'expected' => array(
656-
'jane@example.com' => array( 'jane@example.com', 'Jane' ),
657-
'john@example.com' => array( 'john@example.com', 'John' ),
647+
array( 'john@example.com', 'John' ),
648+
array( 'jane@example.com', 'Jane' ),
658649
),
659650
),
660651
);
@@ -664,49 +655,63 @@ public function address_provider() {
664655
* @ticket 62940
665656
* @dataProvider address_provider
666657
*/
667-
public function test_wp_mail_single_line_utf8_header( $type, $header, $expected ) {
668-
wp_mail( 'test@example.com', 'subject', 'message', $header );
669-
$mailer = tests_retrieve_phpmailer_instance();
670-
671-
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
672-
switch ( $type ) {
673-
case 'From':
674-
$this->assertSame( $expected, array( $mailer->From, $mailer->FromName ) );
675-
break;
676-
677-
case 'Cc':
678-
$this->assertSame( $expected, $mailer->getCcAddresses() );
679-
break;
680-
681-
case 'Bcc':
682-
$this->assertSame( $expected, $mailer->getBccAddresses() );
683-
break;
684-
685-
case 'Reply-To':
686-
$this->assertSame( $expected, $mailer->getReplyToAddresses() );
687-
break;
688-
689-
default:
690-
$this->fail( "Unknown header type: {$type}" );
691-
break;
658+
public function test_wp_mail_single_line_utf8_header( $content, $expected ) {
659+
$headers = array( 'Cc', 'Bcc', 'Reply-To' );
660+
661+
foreach ( $headers as $header ) {
662+
$mail_header = sprintf( '%s: %s', $header, $content );
663+
wp_mail( 'test@example.com', 'subject', 'message', $mail_header );
664+
$mailer = tests_retrieve_phpmailer_instance();
665+
666+
switch ( $header ) {
667+
case 'Cc':
668+
$this->assertSame( $expected, $mailer->getCcAddresses() );
669+
break;
670+
671+
case 'Bcc':
672+
$this->assertSame( $expected, $mailer->getBccAddresses() );
673+
break;
674+
675+
case 'Reply-To':
676+
// Reply-To returns associative array, so modify expected data accordingly.
677+
$expected_reply_to = array();
678+
foreach ( $expected as $addr ) {
679+
$expected_reply_to[ $addr[0] ] = $addr;
680+
}
681+
$this->assertSame( $expected_reply_to, $mailer->getReplyToAddresses() );
682+
break;
683+
684+
default:
685+
$this->fail( "Unknown header type: {$header}" );
686+
break;
687+
}
688+
reset_phpmailer_instance();
692689
}
693-
// phpcs:enable
694690
}
695691

696692
/**
697693
* @ticket 62940
694+
* @dataProvider address_provider
698695
*/
699-
public function test_wp_mail_single_line_utf8_header_multiple_to() {
700-
$to = '=?UTF-8?B?Sm9obg==?= <john@example.com>, Jane <jane@example.com>';
701-
wp_mail( $to, 'subject', 'message' );
702-
$mailer = tests_retrieve_phpmailer_instance();
703-
$expected = array(
704-
array( 'john@example.com', 'John' ),
705-
array( 'jane@example.com', 'Jane' ),
706-
);
696+
public function test_wp_mail_single_line_utf8_header_multiple_to( $content, $expected ) {
697+
wp_mail( $content, 'subject', 'message' );
698+
$mailer = tests_retrieve_phpmailer_instance();
707699
$this->assertSame( $expected, $mailer->getToAddresses() );
708700
}
709701

702+
/**
703+
* @ticket 62940
704+
*/
705+
public function test_wp_mail_encoded_from() {
706+
$header = 'From: =?UTF-8?B?VGVzdA==?= <test@example.com>';
707+
$expected = array( 'test@example.com', 'Test' );
708+
wp_mail( 'john@example.com', 'subject', 'message', $header );
709+
$mailer = tests_retrieve_phpmailer_instance();
710+
711+
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
712+
$this->assertSame( $expected, array( $mailer->From, $mailer->FromName ) );
713+
}
714+
710715
public function addr_list_provider() {
711716
return array(
712717
'comma in quoted name' => array(

0 commit comments

Comments
 (0)