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-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-03-20 04:55:26 +05:30
|
|
|
|
use tests\codeception\api\unit\DbTestCase;
|
|
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
2016-07-17 23:31:15 +05:30
|
|
|
|
use tests\codeception\common\fixtures\UsernameHistoryFixture;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
|
|
|
|
/**
|
2016-05-12 14:20:30 +05:30
|
|
|
|
* @property AccountFixture $accounts
|
2016-03-20 04:55:26 +05:30
|
|
|
|
*/
|
|
|
|
|
class ChangeUsernameFormTest extends DbTestCase {
|
|
|
|
|
use Specify;
|
|
|
|
|
|
|
|
|
|
public function fixtures() {
|
|
|
|
|
return [
|
2016-07-17 23:31:15 +05:30
|
|
|
|
'accounts' => AccountFixture::class,
|
|
|
|
|
'history' => UsernameHistoryFixture::class,
|
2016-03-20 04:55:26 +05:30
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChange() {
|
|
|
|
|
$this->specify('successfully change username to new one', function() {
|
2016-05-12 14:20:30 +05:30
|
|
|
|
$model = $this->createModel([
|
2016-03-20 04:55:26 +05:30
|
|
|
|
'password' => 'password_0',
|
|
|
|
|
'username' => 'my_new_nickname',
|
|
|
|
|
]);
|
|
|
|
|
expect($model->change())->true();
|
2016-05-12 14:20:30 +05:30
|
|
|
|
expect(Account::findOne($this->getAccountId())->username)->equals('my_new_nickname');
|
2016-04-24 00:14:10 +05:30
|
|
|
|
expect(UsernameHistory::findOne(['username' => 'my_new_nickname']))->isInstanceOf(UsernameHistory::class);
|
2016-03-20 04:55:26 +05:30
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:20:30 +05:30
|
|
|
|
public function testChangeWithoutChange() {
|
|
|
|
|
$this->specify('no new UsernameHistory record, if we don\'t change nickname', function() {
|
|
|
|
|
$model = $this->createModel([
|
|
|
|
|
'password' => 'password_0',
|
|
|
|
|
'username' => $this->accounts['admin']['username'],
|
|
|
|
|
]);
|
|
|
|
|
$callTime = time();
|
|
|
|
|
expect($model->change())->true();
|
|
|
|
|
expect(UsernameHistory::findOne([
|
|
|
|
|
'AND',
|
|
|
|
|
'username' => $this->accounts['admin']['username'],
|
2016-07-17 23:31:15 +05:30
|
|
|
|
['>=', 'applied_in', $callTime],
|
2016-05-12 14:20:30 +05:30
|
|
|
|
]))->null();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeCase() {
|
|
|
|
|
$this->specify('username should change, if we change case of some letters', function() {
|
|
|
|
|
$newUsername = mb_strtoupper($this->accounts['admin']['username']);
|
|
|
|
|
$model = $this->createModel([
|
|
|
|
|
'password' => 'password_0',
|
|
|
|
|
'username' => $newUsername,
|
|
|
|
|
]);
|
|
|
|
|
expect($model->change())->true();
|
|
|
|
|
expect(Account::findOne($this->getAccountId())->username)->equals($newUsername);
|
|
|
|
|
expect(UsernameHistory::findOne(['username' => $newUsername]))->isInstanceOf(UsernameHistory::class);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-05-12 14:20:30 +05:30
|
|
|
|
$model = $this->createModel([
|
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-05-12 14:20:30 +05:30
|
|
|
|
$model = $this->createModel([
|
2016-05-02 18:43:18 +05:30
|
|
|
|
'password' => 'password_0',
|
2016-05-12 14:20:30 +05:30
|
|
|
|
'username' => $this->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-05-12 14:20:30 +05:30
|
|
|
|
$model = $this->createModel();
|
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 createModel(array $params = []) : ChangeUsernameForm {
|
|
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
|
|
|
|
$params = array_merge($params, [
|
|
|
|
|
'accountId' => $this->getAccountId(),
|
|
|
|
|
]);
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
2016-05-12 14:20:30 +05:30
|
|
|
|
return new class($params) extends ChangeUsernameForm {
|
|
|
|
|
public $accountId;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
2016-05-12 14:20:30 +05:30
|
|
|
|
protected function getAccount() {
|
|
|
|
|
return Account::findOne($this->accountId);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
2016-05-12 14:20:30 +05:30
|
|
|
|
private function getAccountId() {
|
|
|
|
|
return $this->accounts['admin']['id'];
|
2016-03-20 04:55:26 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|