mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Образован валидатор EmailValidator
This commit is contained in:
@@ -4,8 +4,6 @@ namespace tests\codeception\common\unit\models;
|
||||
use Codeception\Specify;
|
||||
use common\components\UserPass;
|
||||
use common\models\Account;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use tests\codeception\common\fixtures\MojangUsernameFixture;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use Yii;
|
||||
use const common\LATEST_RULES_VERSION;
|
||||
@@ -13,59 +11,12 @@ use const common\LATEST_RULES_VERSION;
|
||||
class AccountTest extends TestCase {
|
||||
use Specify;
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'accounts' => AccountFixture::class,
|
||||
'mojangAccounts' => MojangUsernameFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testValidateEmail() {
|
||||
// TODO: пропускать этот тест, если падает ошибка с недостпуностью интернет соединения
|
||||
$this->specify('email required', function() {
|
||||
$model = new Account(['email' => null]);
|
||||
expect($model->validate(['email']))->false();
|
||||
expect($model->getErrors('email'))->equals(['error.email_required']);
|
||||
});
|
||||
|
||||
$this->specify('email should be not more 255 symbols (I hope it\'s impossible to register)', function() {
|
||||
$model = new Account([
|
||||
'email' => 'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
||||
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
||||
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
||||
'emailemail', // = 256 symbols
|
||||
]);
|
||||
expect($model->validate(['email']))->false();
|
||||
expect($model->getErrors('email'))->equals(['error.email_too_long']);
|
||||
});
|
||||
|
||||
$this->specify('email should be email (it test can fail, if you don\'t have internet connection)', function() {
|
||||
$model = new Account(['email' => 'invalid_email']);
|
||||
expect($model->validate(['email']))->false();
|
||||
expect($model->getErrors('email'))->equals(['error.email_invalid']);
|
||||
});
|
||||
|
||||
$this->specify('email should be not tempmail', function() {
|
||||
$model = new Account(['email' => 'ibrpycwyjdnt@dropmail.me']);
|
||||
expect($model->validate(['email']))->false();
|
||||
expect($model->getErrors('email'))->equals(['error.email_is_tempmail']);
|
||||
});
|
||||
|
||||
$this->specify('email should be unique', function() {
|
||||
$model = new Account(['email' => $this->tester->grabFixture('accounts', 'admin')['email']]);
|
||||
expect($model->validate('email'))->false();
|
||||
expect($model->getErrors('email'))->equals(['error.email_not_available']);
|
||||
});
|
||||
}
|
||||
|
||||
public function testSetPassword() {
|
||||
$this->specify('calling method should change password and set latest password hash algorithm', function() {
|
||||
$model = new Account();
|
||||
$model->setPassword('12345678');
|
||||
expect('hash should be set', $model->password_hash)->notEmpty();
|
||||
expect('validation should be passed', $model->validatePassword('12345678'))->true();
|
||||
expect('latest password hash should be used', $model->password_hash_strategy)->equals(Account::PASS_HASH_STRATEGY_YII2);
|
||||
});
|
||||
$model = new Account();
|
||||
$model->setPassword('12345678');
|
||||
$this->assertNotEmpty($model->password_hash, 'hash should be set');
|
||||
$this->assertTrue($model->validatePassword('12345678'), 'validation should be passed');
|
||||
$this->assertEquals(Account::PASS_HASH_STRATEGY_YII2, $model->password_hash_strategy, 'latest password hash should be used');
|
||||
}
|
||||
|
||||
public function testValidatePassword() {
|
||||
|
109
tests/codeception/common/unit/validators/EmailValidatorTest.php
Normal file
109
tests/codeception/common/unit/validators/EmailValidatorTest.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
namespace codeception\common\unit\validators;
|
||||
|
||||
use common\validators\EmailValidator;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use yii\base\Model;
|
||||
|
||||
class EmailValidatorTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var EmailValidator
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->validator = new EmailValidator();
|
||||
}
|
||||
|
||||
public function testValidateAttributeRequired() {
|
||||
$model = $this->createModel('');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertEquals(['error.email_required'], $model->getErrors('field'));
|
||||
|
||||
$model = $this->createModel('email');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertNotEquals(['error.email_required'], $model->getErrors('field'));
|
||||
}
|
||||
|
||||
public function testValidateAttributeLength() {
|
||||
$model = $this->createModel(
|
||||
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
||||
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
||||
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
||||
'@gmail.com' // = 256 symbols
|
||||
);
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertEquals(['error.email_too_long'], $model->getErrors('field'));
|
||||
|
||||
$model = $this->createModel('some-email@gmail.com');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertNotEquals(['error.email_too_long'], $model->getErrors('field'));
|
||||
}
|
||||
|
||||
public function testValidateAttributeEmail() {
|
||||
$model = $this->createModel('non-email');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertEquals(['error.email_invalid'], $model->getErrors('field'));
|
||||
|
||||
$model = $this->createModel('non-email@etot-domen-ne-suschestrvyet.de');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertEquals(['error.email_invalid'], $model->getErrors('field'));
|
||||
|
||||
$model = $this->createModel('valid-email@gmail.com');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertNotEquals(['error.email_invalid'], $model->getErrors('field'));
|
||||
}
|
||||
|
||||
public function testValidateAttributeTempmail() {
|
||||
$model = $this->createModel('ibrpycwyjdnt@dropmail.me');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertEquals(['error.email_is_tempmail'], $model->getErrors('field'));
|
||||
|
||||
$model = $this->createModel('valid-email@gmail.com');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertNotEquals(['error.email_is_tempmail'], $model->getErrors('field'));
|
||||
}
|
||||
|
||||
public function testValidateAttributeUnique() {
|
||||
$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');
|
||||
$this->assertEquals(['error.email_not_available'], $model->getErrors('field'));
|
||||
|
||||
$model = $this->createModel($accountFixture->email);
|
||||
$this->validator->accountCallback = function() use ($accountFixture) {
|
||||
return $accountFixture->id;
|
||||
};
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertNotEquals(['error.email_not_available'], $model->getErrors('field'));
|
||||
$this->validator->accountCallback = null;
|
||||
|
||||
$model = $this->createModel('some-unique-email@gmail.com');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
$this->assertNotEquals(['error.email_not_available'], $model->getErrors('field'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fieldValue
|
||||
* @return Model
|
||||
*/
|
||||
private function createModel(string $fieldValue) : Model {
|
||||
$class = new class extends Model {
|
||||
public $field;
|
||||
};
|
||||
|
||||
$class->field = $fieldValue;
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user