2016-11-02 02:27:42 +05:30
|
|
|
<?php
|
2019-06-18 05:03:20 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\validators;
|
2016-11-02 02:27:42 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
use common\models\Account;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountFixture;
|
|
|
|
use common\tests\unit\TestCase;
|
2019-02-23 04:41:57 +05:30
|
|
|
use common\validators\EmailValidator;
|
2024-12-02 15:40:55 +05:30
|
|
|
use Generator;
|
2016-11-02 02:27:42 +05:30
|
|
|
use yii\base\Model;
|
2017-09-19 22:36:16 +05:30
|
|
|
use yii\validators\EmailValidator as YiiEmailValidator;
|
2016-11-02 02:27:42 +05:30
|
|
|
|
2023-10-15 10:23:17 +05:30
|
|
|
/**
|
|
|
|
* @covers \common\validators\EmailValidator
|
|
|
|
*/
|
|
|
|
final class EmailValidatorTest extends TestCase {
|
2016-11-02 02:27:42 +05:30
|
|
|
|
2020-08-23 02:34:26 +05:30
|
|
|
private EmailValidator $validator;
|
2016-11-02 02:27:42 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function _before(): void {
|
2016-11-02 02:27:42 +05:30
|
|
|
parent::_before();
|
2019-06-18 05:03:20 +05:30
|
|
|
|
2019-12-14 02:46:05 +05:30
|
|
|
self::defineFunctionMock(YiiEmailValidator::class, 'checkdnsrr');
|
|
|
|
self::defineFunctionMock(YiiEmailValidator::class, 'dns_get_record');
|
2019-06-18 05:03:20 +05:30
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->validator = new EmailValidator();
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateTrimming(): void {
|
2018-01-07 21:38:40 +05:30
|
|
|
// Prevent it to access to db
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(false);
|
2018-01-07 21:38:40 +05:30
|
|
|
|
|
|
|
$model = $this->createModel("testemail@ely.by\u{feff}"); // Zero width no-break space (U+FEFF)
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['error.email_invalid'], $model->getErrors('field'));
|
|
|
|
$this->assertSame('testemail@ely.by', $model->field);
|
2018-01-07 21:38:40 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeRequired(): void {
|
2016-11-02 02:27:42 +05:30
|
|
|
$model = $this->createModel('');
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['error.email_required'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
$model = $this->createModel('email');
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertNotSame(['error.email_required'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeLength(): void {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(false);
|
2019-06-18 05:03:20 +05:30
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
$model = $this->createModel(
|
2024-12-02 15:40:55 +05:30
|
|
|
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail'
|
|
|
|
. 'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail'
|
|
|
|
. 'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail'
|
|
|
|
. '@gmail.com', // = 256 symbols
|
2016-11-02 02:27:42 +05:30
|
|
|
);
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['error.email_too_long'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
$model = $this->createModel('some-email@gmail.com');
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertNotSame(['error.email_too_long'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeEmailCaseNotExistsDomain(): void {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(false);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->never());
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2019-06-18 05:03:20 +05:30
|
|
|
$model = $this->createModel('non-email@this-domain-does-not-exists.de');
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['error.email_invalid'], $model->getErrors('field'));
|
2019-06-18 05:03:20 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeEmailCaseExistsDomainButWithoutMXRecord(): void {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->exactly(2))->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
2016-11-02 02:27:42 +05:30
|
|
|
|
2019-06-18 05:03:20 +05:30
|
|
|
$model = $this->createModel('non-email@this-domain-has-no-mx-record.de');
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-06-18 05:03:20 +05:30
|
|
|
$this->assertNotSame(['error.email_invalid'], $model->getErrors('field'));
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeEmailCaseExistsDomainWithMXRecord(): void {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['mx.google.com']);
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
$model = $this->createModel('valid-email@gmail.com');
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertNotSame(['error.email_invalid'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
}
|
|
|
|
|
2023-10-15 10:23:17 +05:30
|
|
|
public function testValidateAttributeStartingWithSlash(): void {
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['mx.google.com']);
|
|
|
|
|
2023-10-15 10:24:35 +05:30
|
|
|
$model = $this->createModel('/slash@gmail.com');
|
2023-10-15 10:23:17 +05:30
|
|
|
$this->validator->validateAttribute($model, 'field');
|
|
|
|
$this->assertSame(['error.email_invalid'], $model->getErrors('field'));
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeTempmail(): void {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
$model = $this->createModel('ibrpycwyjdnt@dropmail.me');
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['error.email_is_tempmail'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
$model = $this->createModel('valid-email@gmail.com');
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertNotSame(['error.email_is_tempmail'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
}
|
|
|
|
|
2020-08-23 02:34:26 +05:30
|
|
|
/**
|
|
|
|
* @dataProvider getValidateAttributeBlacklistedHostTestCases
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeBlacklistedHost(string $email, bool $expectValid): void {
|
2020-08-23 02:34:26 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
|
|
|
|
|
|
|
$model = $this->createModel($email);
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
|
|
|
$errors = $model->getErrors('field');
|
|
|
|
if ($expectValid) {
|
|
|
|
$this->assertEmpty($errors);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(['error.email_host_is_not_allowed'], $errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public static function getValidateAttributeBlacklistedHostTestCases(): Generator {
|
2020-08-23 02:34:26 +05:30
|
|
|
yield 'seznam.cz' => ['user@seznam.cz', false];
|
|
|
|
yield 'valid' => ['valid@google.com', true];
|
|
|
|
}
|
|
|
|
|
2019-12-15 20:43:47 +05:30
|
|
|
/**
|
|
|
|
* @dataProvider getValidateAttributeIdnaTestCases
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeIdna(string $input, string $expectedOutput): void {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
2018-04-21 18:37:09 +05:30
|
|
|
|
2019-12-15 20:43:47 +05:30
|
|
|
$model = $this->createModel($input);
|
2018-04-21 18:37:09 +05:30
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-12-15 20:43:47 +05:30
|
|
|
$this->assertSame($expectedOutput, $model->field);
|
|
|
|
}
|
2018-04-21 18:37:09 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public static function getValidateAttributeIdnaTestCases(): Generator {
|
2019-12-15 20:43:47 +05:30
|
|
|
yield ['qdushyantasunassm@❕.gq', 'qdushyantasunassm@xn--bei.gq'];
|
|
|
|
yield ['Rafaelaabraão@gmail.com', 'xn--rafaelaabrao-dcb@gmail.com'];
|
|
|
|
yield ['valid-email@gmail.com', 'valid-email@gmail.com'];
|
2018-04-21 18:37:09 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateAttributeUnique(): void {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->tester->haveFixtures([
|
|
|
|
'accounts' => AccountFixture::class,
|
|
|
|
]);
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @var Account $accountFixture */
|
2016-11-02 02:27:42 +05:30
|
|
|
$accountFixture = $this->tester->grabFixture('accounts', 'admin');
|
|
|
|
|
|
|
|
$model = $this->createModel($accountFixture->email);
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['error.email_not_available'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
$model = $this->createModel($accountFixture->email);
|
2024-12-02 15:40:55 +05:30
|
|
|
$this->validator->accountCallback = fn() => $accountFixture->id;
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertNotSame(['error.email_not_available'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->validator->accountCallback = null;
|
|
|
|
|
|
|
|
$model = $this->createModel('some-unique-email@gmail.com');
|
|
|
|
$this->validator->validateAttribute($model, 'field');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertNotSame(['error.email_not_available'], $model->getErrors('field'));
|
2016-11-02 02:27:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-02 15:40:55 +05:30
|
|
|
* @return Model&object{ field: mixed }
|
2016-11-02 02:27:42 +05:30
|
|
|
*/
|
2018-04-18 02:17:25 +05:30
|
|
|
private function createModel(string $fieldValue): Model {
|
2016-11-02 02:27:42 +05:30
|
|
|
$class = new class extends Model {
|
2024-12-02 15:40:55 +05:30
|
|
|
public string $field;
|
2016-11-02 02:27:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
$class->field = $fieldValue;
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|