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-12-04 22:26:49 +05:30
|
|
|
$this->tester->canSeeAmqpMessageIsCreated('events');
|
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],
|
2016-12-04 22:26:49 +05:30
|
|
|
]), 'no new UsernameHistory record, if we don\'t change username');
|
|
|
|
$this->tester->cantSeeAmqpMessageIsCreated('events');
|
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-12-04 22:26:49 +05:30
|
|
|
$this->tester->canSeeAmqpMessageIsCreated('events');
|
2016-05-12 14:20:30 +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-12-04 22:26:49 +05:30
|
|
|
$model->createEventTask(1, 'test1', 'test');
|
|
|
|
$message = $this->tester->grabLastSentAmqpMessage('events');
|
|
|
|
$body = json_decode($message->getBody(), true);
|
|
|
|
$this->assertEquals(1, $body['accountId']);
|
|
|
|
$this->assertEquals('test1', $body['newUsername']);
|
|
|
|
$this->assertEquals('test', $body['oldUsername']);
|
2016-04-24 00:14:10 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|