From 8c3982b192a927414c5f6f6e21dd5bc4f4f04383 Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Wed, 1 Aug 2018 20:54:53 +0300 Subject: [PATCH] fixes #11424 Enchament to email class error message --- lib/Cake/Network/Email/CakeEmail.php | 18 +++++++------ .../Test/Case/Network/Email/CakeEmailTest.php | 25 +++++++++++++------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 174301415..4b5c838df 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -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); diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index a0d15bec8..d0f018de3 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -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 + * + * @expectedException SocketException + * @expectedExceptionMessage Invalid email set for "_from". You passed "cake.@" + * @return void + */ + public function testInvalidFrom() { + $this->CakeEmail->from('cake.@'); } /** * testBuildInvalidData * - * @dataProvider invalidEmails * @expectedException SocketException + * @expectedExceptionMessage Invalid email set for "_to". You passed "1" * @return void */ - public function testInvalidEmailAdd($value) { - $this->CakeEmail->addTo($value); + 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();