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) {
if (!is_array($email)) {
$this->_validateEmail($email);
$this->_validateEmail($email, $varName);
if ($name === null) {
$name = $email;
}
@ -601,7 +601,7 @@ class CakeEmail {
if (is_int($key)) {
$key = $value;
}
$this->_validateEmail($key);
$this->_validateEmail($key, $varName);
$list[$key] = $value;
}
$this->{$varName} = $list;
@ -611,11 +611,12 @@ class CakeEmail {
/**
* Validate email address
*
* @param string $email Email
* @param string $email Email address to validate
* @param string $context Which property was set
* @return void
* @throws SocketException If email address does not validate
*/
protected function _validateEmail($email) {
protected function _validateEmail($email, $context) {
if ($this->_emailPattern === null) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return;
@ -623,7 +624,10 @@ class CakeEmail {
} elseif (preg_match($this->_emailPattern, $email)) {
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) {
if (!is_array($email)) {
$this->_validateEmail($email);
$this->_validateEmail($email, $varName);
if ($name === null) {
$name = $email;
}
@ -671,7 +675,7 @@ class CakeEmail {
if (is_int($key)) {
$key = $value;
}
$this->_validateEmail($key);
$this->_validateEmail($key, $varName);
$list[$key] = $value;
}
$this->{$varName} = array_merge($this->{$varName}, $list);

View file

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