2016-03-20 04:55:26 +05:30
|
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
|
namespace tests\codeception\api\models\profile;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
|
use api\models\AccountIdentity;
|
2016-05-14 05:17:17 +05:30
|
|
|
|
use api\models\profile\ChangeUsernameForm;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
use Codeception\Specify;
|
|
|
|
|
use common\models\Account;
|
2016-04-24 00:14:10 +05:30
|
|
|
|
use common\models\UsernameHistory;
|
2016-10-29 03:17:31 +05:30
|
|
|
|
use tests\codeception\api\unit\TestCase;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
2016-07-17 23:31:15 +05:30
|
|
|
|
use tests\codeception\common\fixtures\UsernameHistoryFixture;
|
2016-07-25 16:37:14 +05:30
|
|
|
|
use Yii;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
|
class ChangeUsernameFormTest extends TestCase {
|
2016-03-20 04:55:26 +05:30
|
|
|
|
use Specify;
|
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
|
public function _fixtures() {
|
2016-03-20 04:55:26 +05:30
|
|
|
|
return [
|
2016-07-17 23:31:15 +05:30
|
|
|
|
'accounts' => AccountFixture::class,
|
|
|
|
|
'history' => UsernameHistoryFixture::class,
|
2016-03-20 04:55:26 +05:30
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
|
public function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$account = AccountIdentity::findOne($this->getAccountId());
|
|
|
|
|
Yii::$app->user->setIdentity($account);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
|
public function testChange() {
|
2016-10-29 03:17:31 +05:30
|
|
|
|
$model = new ChangeUsernameForm([
|
|
|
|
|
'password' => 'password_0',
|
|
|
|
|
'username' => 'my_new_nickname',
|
|
|
|
|
]);
|
|
|
|
|
$this->assertTrue($model->change());
|
|
|
|
|
$this->assertEquals('my_new_nickname', Account::findOne($this->getAccountId())->username);
|
|
|
|
|
$this->assertInstanceOf(UsernameHistory::class, UsernameHistory::findOne(['username' => 'my_new_nickname']));
|
2016-03-20 04:55:26 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:20:30 +05:30
|
|
|
|
public function testChangeWithoutChange() {
|
2016-10-29 03:17:31 +05:30
|
|
|
|
$username = $this->tester->grabFixture('accounts', 'admin')['username'];
|
|
|
|
|
$model = new ChangeUsernameForm([
|
|
|
|
|
'password' => 'password_0',
|
|
|
|
|
'username' => $username,
|
|
|
|
|
]);
|
|
|
|
|
$callTime = time();
|
|
|
|
|
$this->assertTrue($model->change());
|
|
|
|
|
$this->assertNull(UsernameHistory::findOne([
|
|
|
|
|
'AND',
|
|
|
|
|
'username' => $username,
|
|
|
|
|
['>=', 'applied_in', $callTime],
|
|
|
|
|
]), 'no new UsernameHistory record, if we don\'t change nickname');
|
2016-05-12 14:20:30 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeCase() {
|
2016-10-29 03:17:31 +05:30
|
|
|
|
$newUsername = mb_strtoupper($this->tester->grabFixture('accounts', 'admin')['username']);
|
|
|
|
|
$model = new ChangeUsernameForm([
|
|
|
|
|
'password' => 'password_0',
|
|
|
|
|
'username' => $newUsername,
|
|
|
|
|
]);
|
|
|
|
|
$this->assertTrue($model->change());
|
|
|
|
|
$this->assertEquals($newUsername, Account::findOne($this->getAccountId())->username);
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
UsernameHistory::class,
|
|
|
|
|
UsernameHistory::findOne(['username' => $newUsername]),
|
|
|
|
|
'username should change, if we change case of some letters'
|
|
|
|
|
);
|
2016-05-12 14:20:30 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValidateUsername() {
|
2016-05-02 18:43:18 +05:30
|
|
|
|
$this->specify('error.username_not_available expected if username is already taken', function() {
|
2016-07-25 16:37:14 +05:30
|
|
|
|
$model = new ChangeUsernameForm([
|
2016-05-02 18:43:18 +05:30
|
|
|
|
'password' => 'password_0',
|
|
|
|
|
'username' => 'Jon',
|
|
|
|
|
]);
|
2016-05-12 14:20:30 +05:30
|
|
|
|
$model->validateUsername('username');
|
2016-05-02 18:43:18 +05:30
|
|
|
|
expect($model->getErrors('username'))->equals(['error.username_not_available']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->specify('error.username_not_available is NOT expected if username is already taken by CURRENT user', function() {
|
2016-07-25 16:37:14 +05:30
|
|
|
|
$model = new ChangeUsernameForm([
|
2016-05-02 18:43:18 +05:30
|
|
|
|
'password' => 'password_0',
|
2016-10-29 03:17:31 +05:30
|
|
|
|
'username' => $this->tester->grabFixture('accounts', 'admin')['username'],
|
2016-05-02 18:43:18 +05:30
|
|
|
|
]);
|
2016-05-12 14:20:30 +05:30
|
|
|
|
$model->validateUsername('username');
|
|
|
|
|
expect($model->getErrors('username'))->isEmpty();
|
2016-05-02 18:43:18 +05:30
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
|
public function testCreateTask() {
|
2016-07-25 16:37:14 +05:30
|
|
|
|
$model = new ChangeUsernameForm();
|
2016-07-17 20:55:24 +05:30
|
|
|
|
$model->createEventTask('1', 'test1', 'test');
|
2016-04-24 00:14:10 +05:30
|
|
|
|
// TODO: у меня пока нет идей о том, чтобы это как-то успешно протестировать, увы
|
|
|
|
|
// но по крайней мере можно убедиться, что оно не падает где-то на этом шаге
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:20:30 +05:30
|
|
|
|
private function getAccountId() {
|
2016-10-29 03:17:31 +05:30
|
|
|
|
return $this->tester->grabFixture('accounts', 'admin')['id'];
|
2016-03-20 04:55:26 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|