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\helpers\Mock;
|
|
|
|
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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EmailValidator
|
|
|
|
*/
|
|
|
|
private $validator;
|
|
|
|
|
|
|
|
public function _before() {
|
|
|
|
parent::_before();
|
2019-06-18 05:03:20 +05:30
|
|
|
|
|
|
|
Mock::define(YiiEmailValidator::class, 'checkdnsrr');
|
|
|
|
Mock::define(YiiEmailValidator::class, 'dns_get_record');
|
|
|
|
|
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
|
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(false);
|
|
|
|
|
|
|
|
$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-06-18 05:03:20 +05:30
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(false);
|
|
|
|
|
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() {
|
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(false);
|
|
|
|
Mock::func(YiiEmailValidator::class, 'dns_get_record')->times(0);
|
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() {
|
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturnValues([false, true]);
|
|
|
|
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['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() {
|
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
|
|
|
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['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-06-18 05:03:20 +05:30
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
|
|
|
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['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
|
|
|
}
|
|
|
|
|
2018-04-21 18:37:09 +05:30
|
|
|
public function testValidateAttributeIdna() {
|
2019-06-18 05:03:20 +05:30
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
|
|
|
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['127.0.0.1']);
|
2018-04-21 18:37:09 +05:30
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
public function testValidateAttributeUnique() {
|
2019-06-18 05:03:20 +05:30
|
|
|
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
|
|
|
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|