From 202b753c63703ea513744a060f7a60ae64a0a090 Mon Sep 17 00:00:00 2001 From: nojimage Date: Sat, 29 Jun 2013 02:44:55 +0900 Subject: [PATCH] Add emailRegex property to CakeEmail --- lib/Cake/Network/Email/CakeEmail.php | 25 ++++++++++++++++++- .../Test/Case/Network/Email/CakeEmailTest.php | 24 ++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 66be98884b7..0bcbcd16571 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -318,6 +318,14 @@ class CakeEmail { 'ISO-2022-JP-MS' => 'ISO-2022-JP' ); +/** + * Regex for email validation + * If null, it will use built in regex + * + * @var string + */ + protected $_emailRegex = null; + /** * Constructor * @@ -521,6 +529,20 @@ public function headerCharset($charset = null) { return $this->headerCharset = $charset; } +/** + * EmailRegex setter/getter + * + * @param string $regexp + * @return string|CakeEmail + */ + public function emailRegex($regex = false) { + if ($regex === false) { + return $this->_emailRegex; + } + $this->_emailRegex = $regex; + return $this; + } + /** * Set email * @@ -1161,7 +1183,7 @@ protected function _applyConfig($config) { $simpleMethods = array( 'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc', 'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments', - 'transport', 'emailFormat', 'theme', 'helpers' + 'transport', 'emailFormat', 'theme', 'helpers', 'emailRegex' ); foreach ($simpleMethods as $method) { if (isset($config[$method])) { @@ -1218,6 +1240,7 @@ public function reset() { $this->headerCharset = null; $this->_attachments = array(); $this->_config = array(); + $this->_emailRegex = null; return $this; } diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index a490f94e41d..1020ff855de 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -264,6 +264,28 @@ public function testInvalidEmailAdd($value) { $this->CakeEmail->addTo($value); } +/** + * test emailRegex method + * + * @return void + */ + public function testEmailRegex() { + $regex = '/.+@.+\..+/i'; + $this->assertNull($this->CakeEmail->emailRegex()); + $this->assertSame($regex, $this->CakeEmail->emailRegex($regex)->emailRegex()); + } + +/** + * Tests that it is possible to set email regex configuration to a CakeEmail object + * + * @return void + */ + public function testConfigEmailRegex() { + $regex = '/.+@.+\..+/i'; + $email = new CakeEmail(array('emailRegex' => $regex)); + $this->assertSame($regex, $email->emailRegex()); + } + /** * testFormatAddress method * @@ -1427,11 +1449,13 @@ public function testMessage() { public function testReset() { $this->CakeEmail->to('cake@cakephp.org'); $this->CakeEmail->theme('TestTheme'); + $this->CakeEmail->emailRegex('/.+@.+\..+/i'); $this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org')); $this->CakeEmail->reset(); $this->assertSame($this->CakeEmail->to(), array()); $this->assertSame(null, $this->CakeEmail->theme()); + $this->assertSame(null, $this->CakeEmail->emailRegex()); } /**