From 25d39aa81988762461bd5274c23e998aea7783ff Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 23 Jun 2016 08:36:47 +0200 Subject: [PATCH] support egulias/email-validator 2.x --- composer.json | 2 +- .../Validator/Constraints/EmailValidator.php | 11 ++- .../Tests/Constraints/EmailValidatorTest.php | 74 +++++++++++++++++++ src/Symfony/Component/Validator/composer.json | 2 +- 4 files changed, 86 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 05c98d8318..4bd1dce4a3 100644 --- a/composer.json +++ b/composer.json @@ -84,7 +84,7 @@ "monolog/monolog": "~1.11", "ocramius/proxy-manager": "~0.4|~1.0|~2.0", "predis/predis": "~1.0", - "egulias/email-validator": "~1.2", + "egulias/email-validator": "~1.2,>=1.2.8|~2.0", "symfony/polyfill-apcu": "~1.1", "symfony/security-acl": "~2.8|~3.0", "phpdocumentor/reflection-docblock": "^3.0" diff --git a/src/Symfony/Component/Validator/Constraints/EmailValidator.php b/src/Symfony/Component/Validator/Constraints/EmailValidator.php index 5bb7b10099..2be9c65eef 100644 --- a/src/Symfony/Component/Validator/Constraints/EmailValidator.php +++ b/src/Symfony/Component/Validator/Constraints/EmailValidator.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Validator\Constraints; +use Egulias\EmailValidator\Validation\EmailValidation; +use Egulias\EmailValidator\Validation\RFCValidation; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\RuntimeException; @@ -61,7 +63,14 @@ class EmailValidator extends ConstraintValidator $strictValidator = new \Egulias\EmailValidator\EmailValidator(); - if (!$strictValidator->isValid($value, false, true)) { + if (interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, new RFCValidation())) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode(Email::INVALID_FORMAT_ERROR) + ->addViolation(); + + return; + } elseif (!interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, false, true)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Email::INVALID_FORMAT_ERROR) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php index 3b4875b794..bb69e694b3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php @@ -103,6 +103,80 @@ class EmailValidatorTest extends ConstraintValidatorTestCase $this->assertNoViolation(); } + /** + * @dataProvider getInvalidEmailsForStrictChecks + */ + public function testStrictWithInvalidEmails($email) + { + $constraint = new Email(array( + 'message' => 'myMessage', + 'strict' => true, + )); + + $this->validator->validate($email, $constraint); + + $this + ->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$email.'"') + ->setCode(Email::INVALID_FORMAT_ERROR) + ->assertRaised(); + } + + /** + * @link https://github.com/egulias/EmailValidator/blob/1.2.8/tests/egulias/Tests/EmailValidator/EmailValidatorTest.php + */ + public function getInvalidEmailsForStrictChecks() + { + return array( + array('test@example.com test'), + array('user name@example.com'), + array('user name@example.com'), + array('example.@example.co.uk'), + array('example@example@example.co.uk'), + array('(test_exampel@example.fr)'), + array('example(example)example@example.co.uk'), + array('.example@localhost'), + array('ex\ample@localhost'), + array('example@local\host'), + array('example@localhost.'), + array('user name@example.com'), + array('username@ example . com'), + array('example@(fake).com'), + array('example@(fake.com'), + array('username@example,com'), + array('usern,ame@example.com'), + array('user[na]me@example.com'), + array('"""@iana.org'), + array('"\"@iana.org'), + array('"test"test@iana.org'), + array('"test""test"@iana.org'), + array('"test"."test"@iana.org'), + array('"test".test@iana.org'), + array('"test"'.chr(0).'@iana.org'), + array('"test\"@iana.org'), + array(chr(226).'@iana.org'), + array('test@'.chr(226).'.org'), + array('\r\ntest@iana.org'), + array('\r\n test@iana.org'), + array('\r\n \r\ntest@iana.org'), + array('\r\n \r\ntest@iana.org'), + array('\r\n \r\n test@iana.org'), + array('test@iana.org \r\n'), + array('test@iana.org \r\n '), + array('test@iana.org \r\n \r\n'), + array('test@iana.org \r\n\r\n'), + array('test@iana.org \r\n\r\n '), + array('test@iana/icann.org'), + array('test@foo;bar.com'), + array('test;123@foobar.com'), + array('test@example..com'), + array('email.email@email."'), + array('test@email>'), + array('test@email<'), + array('test@email{'), + ); + } + /** * @dataProvider getDnsChecks * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts diff --git a/src/Symfony/Component/Validator/composer.json b/src/Symfony/Component/Validator/composer.json index 809dc704cf..e4b2f61fae 100644 --- a/src/Symfony/Component/Validator/composer.json +++ b/src/Symfony/Component/Validator/composer.json @@ -29,7 +29,7 @@ "symfony/cache": "~3.1", "doctrine/annotations": "~1.0", "doctrine/cache": "~1.0", - "egulias/email-validator": "~1.2,>=1.2.1" + "egulias/email-validator": "~1.2,>=1.2.8|~2.0" }, "suggest": { "psr/cache-implementation": "For using the metadata cache.",