-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from JWardee/v2.0.2
V2.0.2
- Loading branch information
Showing
8 changed files
with
142 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,13 +25,13 @@ public function testMail() | |
get_attached_file($pdfAttachmentId) | ||
]); | ||
|
||
$emailLogs = Logs::get(); | ||
$emailLogs = Logs::get(); | ||
|
||
$this->assertCount(1, $emailLogs); | ||
$this->assertCount(1, $emailLogs); | ||
$this->assertEquals($to, $emailLogs[0]['email_to']); | ||
$this->assertEquals($subject, $emailLogs[0]['subject']); | ||
$this->assertEquals($message, $emailLogs[0]['message']); | ||
$this->assertEquals(true, $emailLogs[0]['is_html']); | ||
$this->assertTrue($emailLogs[0]['is_html']); | ||
|
||
$this->assertEquals($additionalHeaders[0], $emailLogs[0]['additional_headers'][0]); | ||
$this->assertEquals($additionalHeaders[1], $emailLogs[0]['additional_headers'][1]); | ||
|
@@ -46,52 +46,128 @@ public function testMail() | |
wp_delete_attachment($pdfAttachmentId); | ||
} | ||
|
||
public function testEscapingToAndSubject() | ||
{ | ||
$to = 'hello <[email protected]>'; | ||
$subject = 'my subject <blah> " \''; | ||
|
||
wp_mail($to, $subject, 'message'); | ||
|
||
$log = Logs::getFirst(); | ||
|
||
$this->assertEquals($log['email_to'], htmlspecialchars($to)); | ||
$this->assertEquals($log['subject'], htmlspecialchars($subject)); | ||
} | ||
|
||
public function testCorrectTos() | ||
{ | ||
wp_mail('[email protected]', 'subject', 'message'); | ||
$this->assertTrue(Logs::get()[0]['status']); | ||
$this->assertTrue(Logs::getFirst()['status']); | ||
} | ||
|
||
public function testIncorrectTos() | ||
{ | ||
wp_mail('testtest.com', 'subject', 'message'); | ||
$this->assertFalse(Logs::get()[0]['status']); | ||
$this->assertFalse(Logs::getFirst()['status']); | ||
} | ||
|
||
public function testHtmlEmailSetViaFilter() | ||
{ | ||
public function testHtmlEmailSetViaFilter() | ||
{ | ||
$contentTypeFilterPriority = 999; | ||
$updateContentType = function() { | ||
$updateContentType = function () { | ||
return 'text/html'; | ||
}; | ||
|
||
add_filter('wp_mail_content_type', $updateContentType, $contentTypeFilterPriority); | ||
|
||
// Send an email without explicitly setting the html header | ||
wp_mail('[email protected]', 'subject', 'message'); | ||
wp_mail('[email protected]', 'subject', 'message'); | ||
|
||
remove_filter('wp_mail_content_type', $updateContentType, $contentTypeFilterPriority); | ||
|
||
$this->assertTrue(Logs::get()[0]['is_html']); | ||
} | ||
$this->assertTrue(Logs::get()[0]['is_html']); | ||
} | ||
|
||
public function testHtmlEmail() | ||
{ | ||
public function testHtmlEmail() | ||
{ | ||
// Test various formats | ||
wp_mail('[email protected]', 'subject', 'message', [GeneralHelper::$htmlEmailHeader]); | ||
wp_mail('[email protected]', 'subject', 'message', ['content-type:text/html']); | ||
wp_mail('[email protected]', 'subject', 'message', ['Content-Type: text/html']); | ||
wp_mail('[email protected]', 'subject', 'message', ['Content-Type: text/html;']); | ||
|
||
$this->assertTrue(Logs::get()[0]['is_html']); | ||
$this->assertTrue(Logs::get()[1]['is_html']); | ||
$this->assertTrue(Logs::get()[2]['is_html']); | ||
$this->assertTrue(Logs::get()[3]['is_html']); | ||
} | ||
|
||
public function testNonHtmlEmail() | ||
{ | ||
wp_mail('[email protected]', 'subject', 'message'); | ||
$this->assertFalse(Logs::get()[0]['is_html']); | ||
} | ||
wp_mail('[email protected]', 'subject', 'message', [GeneralHelper::$htmlEmailHeader]); | ||
wp_mail('[email protected]', 'subject', 'message', ['content-type:text/html']); | ||
wp_mail('[email protected]', 'subject', 'message', ['Content-Type: text/html']); | ||
wp_mail('[email protected]', 'subject', 'message', ['Content-Type: text/html;']); | ||
|
||
$this->assertTrue(Logs::get()[0]['is_html']); | ||
$this->assertTrue(Logs::get()[1]['is_html']); | ||
$this->assertTrue(Logs::get()[2]['is_html']); | ||
$this->assertTrue(Logs::get()[3]['is_html']); | ||
} | ||
|
||
public function testNonHtmlEmail() | ||
{ | ||
wp_mail('[email protected]', 'subject', 'message'); | ||
$this->assertFalse(Logs::get()[0]['is_html']); | ||
} | ||
|
||
public function testWpFiltersWithMailCatcherAreUnchanged() | ||
{ | ||
$originalTo = '[email protected]'; | ||
$originalSubject = 'subject'; | ||
$originalMessage = 'message'; | ||
$originalContentType = 'multipart/alternative'; | ||
$originalAdditionalHeaders = ['content-type: ' . $originalContentType, 'cc: [email protected]']; | ||
|
||
$isWpMailFilterCalled = false; | ||
$isWpMailContentFilterCalled = false; | ||
|
||
$wpMailFilter = function ($args) use (&$isWpMailFilterCalled, $originalTo, $originalSubject, $originalMessage, $originalAdditionalHeaders) { | ||
$isWpMailFilterCalled = true; | ||
|
||
$this->assertEquals($args['to'], $originalTo); | ||
$this->assertEquals($args['subject'], $originalSubject); | ||
$this->assertEquals($args['message'], $originalMessage); | ||
|
||
for ($i = 0; $i < count($args['headers']); $i++) { | ||
$this->assertEquals($args['headers'][$i], $originalAdditionalHeaders[$i]); | ||
} | ||
|
||
return $args; | ||
}; | ||
|
||
$wpMailContentFilter = function ($contentType) use (&$isWpMailContentFilterCalled, $originalContentType) { | ||
$isWpMailContentFilterCalled = true; | ||
$this->assertEquals($contentType, $originalContentType); | ||
return $originalContentType; | ||
}; | ||
|
||
// Add filters | ||
add_filter('wp_mail', $wpMailFilter); | ||
add_filter('wp_mail', $wpMailFilter, 9999991); | ||
add_filter('wp_mail_content_type', $wpMailContentFilter); | ||
add_filter('wp_mail_content_type', $wpMailContentFilter, 9999991); | ||
|
||
// Send message | ||
wp_mail($originalTo, $originalSubject, $originalMessage, $originalAdditionalHeaders); | ||
|
||
// Assert filters were called | ||
$this->assertTrue($isWpMailFilterCalled); | ||
$this->assertTrue($isWpMailContentFilterCalled); | ||
|
||
$emailLogs = Logs::get(); | ||
|
||
// Assert email was logged correctly | ||
$this->assertCount(1, $emailLogs); | ||
$this->assertEquals($originalTo, $emailLogs[0]['email_to']); | ||
$this->assertEquals($originalSubject, $emailLogs[0]['subject']); | ||
$this->assertEquals($originalMessage, $emailLogs[0]['message']); | ||
$this->assertFalse($emailLogs[0]['is_html']); | ||
|
||
$this->assertEquals($originalAdditionalHeaders[0], $emailLogs[0]['additional_headers'][0]); | ||
$this->assertEquals($originalAdditionalHeaders[1], $emailLogs[0]['additional_headers'][1]); | ||
|
||
// Tidy up | ||
remove_filter('wp_mail', $wpMailFilter); | ||
remove_filter('wp_mail', $wpMailFilter, 9999991); | ||
remove_filter('wp_mail_content_type', $wpMailContentFilter); | ||
remove_filter('wp_mail_content_type', $wpMailContentFilter, 9999991); | ||
} | ||
} |