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
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
class EmailValidatorTest extends TestCase {
|
|
|
|
|
2020-08-23 02:34:26 +05:30
|
|
|
private EmailValidator $validator;
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
public function _before() {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-01-07 21:38:40 +05:30
|
|
|
public function testValidateTrimming() {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
public function testValidateAttributeRequired() {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateAttributeLength() {
|
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(
|
|
|
|
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
|
|
|
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
|
|
|
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
|
|
|
'@gmail.com' // = 256 symbols
|
|
|
|
);
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2019-06-18 05:03:20 +05:30
|
|
|
public function testValidateAttributeEmailCaseNotExistsDomain() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateAttributeEmailCaseExistsDomainButWithoutMXRecord() {
|
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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateAttributeEmailCaseExistsDomainWithMXRecord() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateAttributeTempmail() {
|
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
|
|
|
|
*/
|
|
|
|
public function testValidateAttributeBlacklistedHost(string $email, bool $expectValid) {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValidateAttributeBlacklistedHostTestCases() {
|
|
|
|
yield 'seznam.cz' => ['user@seznam.cz', false];
|
|
|
|
yield 'valid' => ['valid@google.com', true];
|
|
|
|
}
|
|
|
|
|
2019-12-15 20:43:47 +05:30
|
|
|
/**
|
|
|
|
* @dataProvider getValidateAttributeIdnaTestCases
|
|
|
|
*/
|
|
|
|
public function testValidateAttributeIdna(string $input, string $expectedOutput) {
|
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
|
|
|
|
2019-12-15 20:43:47 +05:30
|
|
|
public function getValidateAttributeIdnaTestCases() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
public function testValidateAttributeUnique() {
|
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,
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var \common\models\Account $accountFixture */
|
|
|
|
$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);
|
|
|
|
$this->validator->accountCallback = function() use ($accountFixture) {
|
|
|
|
return $accountFixture->id;
|
|
|
|
};
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fieldValue
|
|
|
|
* @return Model
|
|
|
|
*/
|
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 {
|
|
|
|
public $field;
|
|
|
|
};
|
|
|
|
|
|
|
|
$class->field = $fieldValue;
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|