diff --git a/common/validators/EmailValidator.php b/common/validators/EmailValidator.php index 1a46fe9..e774728 100644 --- a/common/validators/EmailValidator.php +++ b/common/validators/EmailValidator.php @@ -38,6 +38,11 @@ class EmailValidator extends Validator { $tempmail = new TempmailValidator(); $tempmail->message = E::EMAIL_IS_TEMPMAIL; + $idnaDomain = new validators\FilterValidator(['filter' => function(string $value): string { + [$name, $domain] = explode('@', $value); + return $name . '@' . idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); + }]); + $unique = new validators\UniqueValidator(); $unique->message = E::EMAIL_NOT_AVAILABLE; $unique->targetClass = Account::class; @@ -53,12 +58,12 @@ class EmailValidator extends Validator { $this->executeValidation($length, $model, $attribute) && $this->executeValidation($email, $model, $attribute) && $this->executeValidation($tempmail, $model, $attribute) && + $this->executeValidation($idnaDomain, $model, $attribute) && $this->executeValidation($unique, $model, $attribute); } - protected function executeValidation(Validator $validator, Model $model, string $attribute) { + private function executeValidation(Validator $validator, Model $model, string $attribute): bool { $validator->validateAttribute($model, $attribute); - return !$model->hasErrors($attribute); } diff --git a/tests/codeception/common/unit/validators/EmailValidatorTest.php b/tests/codeception/common/unit/validators/EmailValidatorTest.php index caa24ce..7d33692 100644 --- a/tests/codeception/common/unit/validators/EmailValidatorTest.php +++ b/tests/codeception/common/unit/validators/EmailValidatorTest.php @@ -84,6 +84,18 @@ class EmailValidatorTest extends TestCase { $this->assertNotEquals(['error.email_is_tempmail'], $model->getErrors('field')); } + public function testValidateAttributeIdna() { + Mock::func(YiiEmailValidator::class, 'checkdnsrr')->times(2)->andReturnTrue(); + + $model = $this->createModel('qdushyantasunassm@❕.gq'); + $this->validator->validateAttribute($model, 'field'); + $this->assertSame('qdushyantasunassm@xn--bei.gq', $model->field); + + $model = $this->createModel('valid-email@gmail.com'); + $this->validator->validateAttribute($model, 'field'); + $this->assertSame('valid-email@gmail.com', $model->field); + } + public function testValidateAttributeUnique() { Mock::func(YiiEmailValidator::class, 'checkdnsrr')->times(3)->andReturnTrue();