From 1695297c3e584c74df3efdf19ac28592ec628c17 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 7 Oct 2023 09:57:29 +1300 Subject: [PATCH] Trim text emails when checking if empty I was concerned that inadvertant white space might block the addiiton of a text version. The unit test showed that to be the case so I added the extra trim --- CRM/Utils/Mail.php | 2 +- tests/phpunit/CRM/Utils/MailTest.php | 37 +++++++++++++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/CRM/Utils/Mail.php b/CRM/Utils/Mail.php index 63e14b7000..71d71fbb99 100644 --- a/CRM/Utils/Mail.php +++ b/CRM/Utils/Mail.php @@ -190,7 +190,7 @@ class CRM_Utils_Mail { $htmlMessage = FALSE; } $attachments = $params['attachments'] ?? NULL; - if (!empty($params['text'])) { + if (!empty($params['text']) && trim($params['text'])) { $textMessage = $params['text']; } else { diff --git a/tests/phpunit/CRM/Utils/MailTest.php b/tests/phpunit/CRM/Utils/MailTest.php index 8e32fac889..b195bf28fb 100644 --- a/tests/phpunit/CRM/Utils/MailTest.php +++ b/tests/phpunit/CRM/Utils/MailTest.php @@ -2,6 +2,7 @@ /** * Class CRM_Utils_MailTest + * * @group headless */ class CRM_Utils_MailTest extends CiviUnitTestCase { @@ -12,41 +13,40 @@ class CRM_Utils_MailTest extends CiviUnitTestCase { } /** - * Test case for add( ) * test with empty params. */ public function testFormatRFC822(): void { $values = [ [ - 'name' => "Test User", - 'email' => "foo@bar.com", - 'result' => "Test User ", + 'name' => 'Test User', + 'email' => 'foo@bar.com', + 'result' => 'Test User ', ], [ 'name' => '"Test User"', - 'email' => "foo@bar.com", - 'result' => "Test User ", + 'email' => 'foo@bar.com', + 'result' => 'Test User ', ], [ - 'name' => "User, Test", - 'email' => "foo@bar.com", + 'name' => 'User, Test', + 'email' => 'foo@bar.com', 'result' => '"User, Test" ', ], [ 'name' => '"User, Test"', - 'email' => "foo@bar.com", + 'email' => 'foo@bar.com', 'result' => '"User, Test" ', ], [ 'name' => '"Test User"', - 'email' => "foo@bar.com", + 'email' => 'foo@bar.com', 'result' => '"Test User" ', 'useQuote' => TRUE, ], [ - 'name' => "User, Test", - 'email' => "foo@bar.com", + 'name' => 'User, Test', + 'email' => 'foo@bar.com', 'result' => '"User, Test" ', 'useQuote' => TRUE, ], @@ -77,6 +77,19 @@ class CRM_Utils_MailTest extends CiviUnitTestCase { $this->assertEquals('Unable to send email. Please report this message to the site administrator', CRM_Core_Session::singleton()->getStatus()[0]['text']); } + public function testEmptyText(): void { + $mailHelper = new CiviMailUtils($this); + $params = [ + 'toEmail' => 'a@example.com', + 'from' => 'b@example.com', + 'html' => '

hi

How are you

', + 'text' => " \n\t", + 'subject' => 'hey', + ]; + CRM_Utils_Mail::send($params); + $mailHelper->checkAllMailLog(['How are you']); + } + /** * Mimic exception in mailer class. * -- 2.25.1