2013-12-02 19:25:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace tests\unit\models;
|
|
|
|
|
2014-01-01 03:02:10 +08:00
|
|
|
use Yii;
|
2013-12-17 07:27:33 +08:00
|
|
|
use yii\codeception\TestCase;
|
2013-12-31 04:17:36 +08:00
|
|
|
use app\models\User;
|
2013-12-02 19:25:21 +08:00
|
|
|
|
2013-12-17 07:27:33 +08:00
|
|
|
class LoginFormTest extends TestCase
|
|
|
|
{
|
2013-12-31 04:17:36 +08:00
|
|
|
use \Codeception\Specify;
|
|
|
|
|
2014-02-14 22:08:07 +08:00
|
|
|
protected function tearDown()
|
2014-02-14 22:05:56 +08:00
|
|
|
{
|
|
|
|
Yii::$app->user->logout();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2013-12-31 04:17:36 +08:00
|
|
|
public function testLoginNoUser()
|
|
|
|
{
|
2014-01-01 03:02:10 +08:00
|
|
|
$model = $this->mockUser(null);
|
2013-12-31 04:17:36 +08:00
|
|
|
|
|
|
|
$model->username = 'some_username';
|
|
|
|
$model->password = 'some_password';
|
|
|
|
|
2014-02-24 23:45:35 +08:00
|
|
|
$this->specify('user should not be able to login, when there is no identity', function () use ($model) {
|
2014-01-05 07:12:40 +08:00
|
|
|
expect('model should not login user', $model->login())->false();
|
|
|
|
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
|
2013-12-31 04:17:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginWrongPassword()
|
|
|
|
{
|
2014-01-01 03:02:10 +08:00
|
|
|
$model = $this->mockUser(new User);
|
2013-12-31 04:17:36 +08:00
|
|
|
|
|
|
|
$model->username = 'demo';
|
|
|
|
$model->password = 'wrong-password';
|
|
|
|
|
2014-01-01 03:02:10 +08:00
|
|
|
$this->specify('user should not be able to login with wrong password', function () use ($model) {
|
2014-01-05 07:12:40 +08:00
|
|
|
expect('model should not login user', $model->login())->false();
|
|
|
|
expect('error message should be set', $model->errors)->hasKey('password');
|
|
|
|
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
|
2013-12-31 04:17:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginCorrect()
|
|
|
|
{
|
2014-01-01 03:02:10 +08:00
|
|
|
$model = $this->mockUser(new User(['password' => 'demo']));
|
2013-12-31 04:17:36 +08:00
|
|
|
|
|
|
|
$model->username = 'demo';
|
|
|
|
$model->password = 'demo';
|
|
|
|
|
2014-02-24 23:45:35 +08:00
|
|
|
$this->specify('user should be able to login with correct credentials', function () use ($model) {
|
2014-01-05 07:12:40 +08:00
|
|
|
expect('model should login user', $model->login())->true();
|
|
|
|
expect('error message should not be set', $model->errors)->hasntKey('password');
|
|
|
|
expect('user should be logged in', Yii::$app->user->isGuest)->false();
|
2013-12-31 04:17:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function mockUser($user)
|
|
|
|
{
|
2014-02-24 23:45:35 +08:00
|
|
|
$loginForm = $this->getMock('app\models\LoginForm', ['getUser']);
|
2014-01-01 03:02:10 +08:00
|
|
|
$loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user));
|
|
|
|
return $loginForm;
|
2013-12-31 04:17:36 +08:00
|
|
|
}
|
2014-01-20 00:35:36 +08:00
|
|
|
}
|