Merge pull request #1386 from nojimage/cakeemail-custom-email-validation

CakeEmail class be able to setting custom email validation rule.
This commit is contained in:
Mark Story 2013-07-01 07:36:34 -07:00
commit 2219991d3b
2 changed files with 93 additions and 5 deletions

View file

@ -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 $_emailPattern = null;
/**
* Constructor
*
@ -521,6 +529,20 @@ class CakeEmail {
return $this->headerCharset = $charset;
}
/**
* EmailPattern setter/getter
*
* @param string $regex for email address validation
* @return string|CakeEmail
*/
public function emailPattern($regex = null) {
if ($regex === null) {
return $this->_emailPattern;
}
$this->_emailPattern = $regex;
return $this;
}
/**
* Set email
*
@ -532,7 +554,7 @@ class CakeEmail {
*/
protected function _setEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email)) {
if (!Validation::email($email, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) {
@ -546,7 +568,7 @@ class CakeEmail {
if (is_int($key)) {
$key = $value;
}
if (!Validation::email($key)) {
if (!Validation::email($key, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value;
@ -586,7 +608,7 @@ class CakeEmail {
*/
protected function _addEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email)) {
if (!Validation::email($email, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) {
@ -600,7 +622,7 @@ class CakeEmail {
if (is_int($key)) {
$key = $value;
}
if (!Validation::email($key)) {
if (!Validation::email($key, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value;
@ -1161,7 +1183,7 @@ class CakeEmail {
$simpleMethods = array(
'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
'transport', 'emailFormat', 'theme', 'helpers'
'transport', 'emailFormat', 'theme', 'helpers', 'emailPattern'
);
foreach ($simpleMethods as $method) {
if (isset($config[$method])) {
@ -1218,6 +1240,7 @@ class CakeEmail {
$this->headerCharset = null;
$this->_attachments = array();
$this->_config = array();
$this->_emailPattern = null;
return $this;
}

View file

@ -264,6 +264,69 @@ class CakeEmailTest extends CakeTestCase {
$this->CakeEmail->addTo($value);
}
/**
* test emailPattern method
*
* @return void
*/
public function testEmailPattern() {
$regex = '/.+@.+\..+/i';
$this->assertNull($this->CakeEmail->emailPattern());
$this->assertSame($regex, $this->CakeEmail->emailPattern($regex)->emailPattern());
}
/**
* Tests that it is possible to set email regex configuration to a CakeEmail object
*
* @return void
*/
public function testConfigEmailPattern() {
$regex = '/.+@.+\..+/i';
$email = new CakeEmail(array('emailPattern' => $regex));
$this->assertSame($regex, $email->emailPattern());
}
/**
* Tests that it is possible set custom email validation
*/
public function testCustomEmailValidation() {
$regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
$this->CakeEmail->emailPattern($regex)->to('pass.@example.com');
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->addTo('pass..old.docomo@example.com');
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->reset();
$emails = array(
'pass.@example.com',
'pass..old.docomo@example.com'
);
$additionalEmails = array(
'.extend.@example.com',
'.docomo@example.com'
);
$this->CakeEmail->emailPattern($regex)->to($emails);
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->addTo($additionalEmails);
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
'.extend.@example.com' => '.extend.@example.com',
'.docomo@example.com' => '.docomo@example.com',
), $this->CakeEmail->to());
}
/**
* testFormatAddress method
*
@ -1427,11 +1490,13 @@ class CakeEmailTest extends CakeTestCase {
public function testReset() {
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->theme('TestTheme');
$this->CakeEmail->emailPattern('/.+@.+\..+/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->emailPattern());
}
/**