From ccbfdc72a45c93f86be46d0f0c143b9da8500890 Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Mon, 2 Jul 2018 09:26:15 +0100 Subject: [PATCH] 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 8897e24077..e303858e4d 100644 --- a/CRM/Admin/Form/Options.php +++ b/CRM/Admin/Form/Options.php @@ -466,8 +466,7 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { //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 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { } } + 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 49e4fe28f8..3aa73301e1 100644 --- a/tests/phpunit/CRM/Core/OptionGroupTest.php +++ b/tests/phpunit/CRM/Core/OptionGroupTest.php @@ -91,4 +91,22 @@ class CRM_Core_OptionGroupTest extends CiviUnitTestCase { } } + + 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); + } + } -- 2.25.1