From c65ce8978a496e1a2d678868fda1c480aee4c15e Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Wed, 20 Jun 2018 20:04:01 +0100 Subject: [PATCH 1/2] dev/mail/15 deal better with spaces in from email address --- CRM/Admin/Form/Options.php | 5 +++-- ang/crmMailing/services.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CRM/Admin/Form/Options.php b/CRM/Admin/Form/Options.php index f68718d881b5..8897e240774c 100644 --- a/CRM/Admin/Form/Options.php +++ b/CRM/Admin/Form/Options.php @@ -464,9 +464,10 @@ public function postProcess() { $params['reset_default_for'] = array('filter' => "0, " . $params['filter']); } - //make sure we should has to have space, CRM-6977 + //make sure we only have a single space, CRM-6977 and dev/mail/15 if ($this->_gName == 'from_email_address') { - $params['label'] = str_replace('"<', '" <', $params['label']); + preg_match("/^\"(.*)\" *<([^@>]*@[^@>]*)>$/", $params['label'], $parts); + $params['label'] = "\"{$parts[1]}\" <$parts[2]>"; } } diff --git a/ang/crmMailing/services.js b/ang/crmMailing/services.js index cfaaeda2b1d1..e50a0a0f7a7a 100644 --- a/ang/crmMailing/services.js +++ b/ang/crmMailing/services.js @@ -5,7 +5,7 @@ // the available "From:" addrs. Records are like the underlying OptionValues -- but add "email" // and "author". angular.module('crmMailing').factory('crmFromAddresses', function ($q, crmApi) { - var emailRegex = /^"(.*)" <([^@>]*@[^@>]*)>$/; + var emailRegex = /^"(.*)" *<([^@>]*@[^@>]*)>$/; var addrs = _.map(CRM.crmMailing.fromAddress, function (addr) { var match = emailRegex.exec(addr.label); return angular.extend({}, addr, { From ccbfdc72a45c93f86be46d0f0c143b9da8500890 Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Mon, 2 Jul 2018 09:26:15 +0100 Subject: [PATCH 2/2] dev/mail/15 move santize to function and add unit test --- CRM/Admin/Form/Options.php | 8 ++++++-- tests/phpunit/CRM/Core/OptionGroupTest.php | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CRM/Admin/Form/Options.php b/CRM/Admin/Form/Options.php index 8897e240774c..e303858e4d15 100644 --- a/CRM/Admin/Form/Options.php +++ b/CRM/Admin/Form/Options.php @@ -466,8 +466,7 @@ public function postProcess() { //make sure we only have a single space, CRM-6977 and dev/mail/15 if ($this->_gName == 'from_email_address') { - preg_match("/^\"(.*)\" *<([^@>]*@[^@>]*)>$/", $params['label'], $parts); - $params['label'] = "\"{$parts[1]}\" <$parts[2]>"; + $params['label'] = $this->sanitizeFromEmailAddress($params['label']); } } @@ -509,4 +508,9 @@ public function postProcess() { } } + public function sanitizeFromEmailAddress($email) { + preg_match("/^\"(.*)\" *<([^@>]*@[^@>]*)>$/", $email, $parts); + return "\"{$parts[1]}\" <$parts[2]>"; + } + } diff --git a/tests/phpunit/CRM/Core/OptionGroupTest.php b/tests/phpunit/CRM/Core/OptionGroupTest.php index 49e4fe28f846..3aa73301e1b7 100644 --- a/tests/phpunit/CRM/Core/OptionGroupTest.php +++ b/tests/phpunit/CRM/Core/OptionGroupTest.php @@ -91,4 +91,22 @@ public function testsOptionGroupDataType($optionGroup, $expectedDataType) { } } + + public function emailAddressTests() { + $tests[] = array('"Name"', '"Name" '); + $tests[] = array('"Name" ', '"Name" '); + $tests[] = array('"Name" ', '"Name" '); + return $tests; + } + + + /** + * @dataProvider emailAddressTests + */ + public function testSanitizeFromEmailAddress($dirty, $clean) { + $form = new CRM_Admin_Form_Options(); + $actual = $form->sanitizeFromEmailAddress($dirty); + $this->assertEquals($actual, $clean); + } + }