fixes #11424 Enchament to email class error message

This commit is contained in:
AlexAlexandru 2018-08-01 20:54:53 +03:00
parent a58eb706dc
commit 8c3982b192
2 changed files with 29 additions and 14 deletions

View file

@ -589,7 +589,7 @@ class CakeEmail {
*/ */
protected function _setEmail($varName, $email, $name) { protected function _setEmail($varName, $email, $name) {
if (!is_array($email)) { if (!is_array($email)) {
$this->_validateEmail($email); $this->_validateEmail($email, $varName);
if ($name === null) { if ($name === null) {
$name = $email; $name = $email;
} }
@ -601,7 +601,7 @@ class CakeEmail {
if (is_int($key)) { if (is_int($key)) {
$key = $value; $key = $value;
} }
$this->_validateEmail($key); $this->_validateEmail($key, $varName);
$list[$key] = $value; $list[$key] = $value;
} }
$this->{$varName} = $list; $this->{$varName} = $list;
@ -611,11 +611,12 @@ class CakeEmail {
/** /**
* Validate email address * Validate email address
* *
* @param string $email Email * @param string $email Email address to validate
* @param string $context Which property was set
* @return void * @return void
* @throws SocketException If email address does not validate * @throws SocketException If email address does not validate
*/ */
protected function _validateEmail($email) { protected function _validateEmail($email, $context) {
if ($this->_emailPattern === null) { if ($this->_emailPattern === null) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return; return;
@ -623,7 +624,10 @@ class CakeEmail {
} elseif (preg_match($this->_emailPattern, $email)) { } elseif (preg_match($this->_emailPattern, $email)) {
return; return;
} }
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email)); if ($email == '') {
throw new SocketException(__d('cake_dev', 'The email set for "%s" is empty.', $context));
}
throw new SocketException(__d('cake_dev', 'Invalid email set for "%s". You passed "%s".', $context, $email));
} }
/** /**
@ -659,7 +663,7 @@ class CakeEmail {
*/ */
protected function _addEmail($varName, $email, $name) { protected function _addEmail($varName, $email, $name) {
if (!is_array($email)) { if (!is_array($email)) {
$this->_validateEmail($email); $this->_validateEmail($email, $varName);
if ($name === null) { if ($name === null) {
$name = $email; $name = $email;
} }
@ -671,7 +675,7 @@ class CakeEmail {
if (is_int($key)) { if (is_int($key)) {
$key = $value; $key = $value;
} }
$this->_validateEmail($key); $this->_validateEmail($key, $varName);
$list[$key] = $value; $list[$key] = $value;
} }
$this->{$varName} = array_merge($this->{$varName}, $list); $this->{$varName} = array_merge($this->{$varName}, $list);

View file

@ -334,23 +334,34 @@ class CakeEmailTest extends CakeTestCase {
/** /**
* testBuildInvalidData * testBuildInvalidData
* *
* @dataProvider invalidEmails
* @expectedException SocketException * @expectedException SocketException
* @expectedExceptionMessage The email set for "_to" is empty.
* @return void * @return void
*/ */
public function testInvalidEmail($value) { public function testInvalidEmail() {
$this->CakeEmail->to($value); $this->CakeEmail->to('');
} }
/** /**
* testBuildInvalidData * testBuildInvalidData
* *
* @dataProvider invalidEmails
* @expectedException SocketException * @expectedException SocketException
* @expectedExceptionMessage Invalid email set for "_from". You passed "cake.@"
* @return void * @return void
*/ */
public function testInvalidEmailAdd($value) { public function testInvalidFrom() {
$this->CakeEmail->addTo($value); $this->CakeEmail->from('cake.@');
}
/**
* testBuildInvalidData
*
* @expectedException SocketException
* @expectedExceptionMessage Invalid email set for "_to". You passed "1"
* @return void
*/
public function testInvalidEmailAdd() {
$this->CakeEmail->addTo('1');
} }
/** /**
@ -423,7 +434,7 @@ class CakeEmailTest extends CakeTestCase {
* @return void * @return void
* *
* @expectedException SocketException * @expectedException SocketException
* @expectedExceptionMessage Invalid email: "fail.@example.com" * @expectedExceptionMessage Invalid email set for "_to". You passed "fail.@example.com"
*/ */
public function testUnsetEmailPattern() { public function testUnsetEmailPattern() {
$email = new CakeEmail(); $email = new CakeEmail();