Добавлено поле username в модель Account, скорретирована форма входа и её тесты

This commit is contained in:
ErickSkrauch
2016-01-04 18:31:14 +03:00
parent c6a6f35be6
commit 45c31dfbbe
5 changed files with 76 additions and 35 deletions

View File

@ -25,60 +25,60 @@ class AuthenticationFormTest extends DbTestCase {
];
}
protected function createModel($email = '', $password = '') {
protected function createModel($login = '', $password = '') {
return new AuthenticationForm([
'email' => $email,
'login' => $login,
'password' => $password,
]);
}
public function testLoginEmail() {
public function testLoginEmailOrUsername() {
$model = $this->createModel();
$this->specify('error.email_required expected if email is not set', function() use ($model) {
$this->specify('error.login_required expected if login is not set', function() use ($model) {
expect($model->login())->false();
expect($model->getErrors('email'))->equals(['error.email_required']);
expect($model->getErrors('login'))->equals(['error.login_required']);
expect(Yii::$app->user->isGuest)->true();
});
$model = $this->createModel('wrong-email-string');
$this->specify('error.email_invalid expected if email not correct', function() use ($model) {
$model = $this->createModel('non-exist-username');
$this->specify('error.login_not_exist expected if username not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_invalid']);
});
$model = $this->createModel('wrong@email');
$this->specify('error.email_invalid expected if email not correct', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_invalid']);
expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$model = $this->createModel('not-exist@user.com');
$this->specify('error.email_not_exist expected if email not exists in database', function() use ($model) {
$this->specify('error.login_not_exist expected if email not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_not_exist']);
expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$model = $this->createModel('Admin');
$this->specify('no errors on login field if username is correct and exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('login'))->isEmpty();
});
$model = $this->createModel('admin@ely.by');
$this->specify('no errors on email field if email is correct and exists in database', function() use ($model) {
$this->specify('no errors on login field if email is correct and exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->isEmpty();
expect($model->getErrors('login'))->isEmpty();
});
}
public function testLoginPassword() {
$model = $this->createModel();
$this->specify('password don\'t has errors if email not set', function() use ($model) {
$this->specify('password don\'t has errors if email or username not set', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
});
$model = $this->createModel('wrong-email-string', 'random-password');
$this->specify('password don\'t has errors if email invalid', function() use ($model) {
$model = $this->createModel('non-exist-username', 'random-password');
$this->specify('password don\'t has errors if username not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
@ -97,11 +97,27 @@ class AuthenticationFormTest extends DbTestCase {
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
});
$model = $this->createModel('Admin', 'wrong-password');
$this->specify('error.password_incorrect expected if username correct, but password wrong', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
});
}
public function testLoginCorrect() {
public function testLoginByUsernameCorrect() {
$model = $this->createModel('Admin', 'password_0');
$this->specify('user should be able to login with correct username and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
public function testLoginByEmailCorrect() {
$model = $this->createModel('admin@ely.by', 'password_0');
$this->specify('user should be able to login with correct credentials', function () use ($model) {
$this->specify('user should be able to login with correct email and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();