Форма смены ника теперь принимает аккаунт через конструктор

This commit is contained in:
ErickSkrauch 2017-06-17 22:06:04 +03:00
parent cb068b9dc0
commit 1169097adb
3 changed files with 29 additions and 18 deletions

View File

@ -90,7 +90,8 @@ class AccountsController extends Controller {
}
public function actionChangeUsername() {
$model = new ChangeUsernameForm();
$account = Yii::$app->user->identity;
$model = new ChangeUsernameForm($account);
$model->load(Yii::$app->request->post());
if (!$model->change()) {
return [

View File

@ -1,10 +1,10 @@
<?php
namespace api\models\profile;
use api\models\AccountIdentity;
use api\models\base\ApiForm;
use api\validators\PasswordRequiredValidator;
use common\helpers\Amqp;
use common\models\Account;
use common\models\amqp\UsernameChanged;
use common\models\UsernameHistory;
use common\validators\UsernameValidator;
@ -19,21 +19,31 @@ class ChangeUsernameForm extends ApiForm {
public $password;
public function rules() {
/**
* @var Account
*/
private $account;
public function __construct(Account $account, array $config = []) {
parent::__construct($config);
$this->account = $account;
}
public function rules(): array {
return [
['username', UsernameValidator::class, 'accountCallback' => function() {
return $this->getAccount()->id;
return $this->account->id;
}],
['password', PasswordRequiredValidator::class],
];
}
public function change() : bool {
public function change(): bool {
if (!$this->validate()) {
return false;
}
$account = $this->getAccount();
$account = $this->account;
if ($this->username === $account->username) {
return true;
}
@ -85,8 +95,4 @@ class ChangeUsernameForm extends ApiForm {
Amqp::sendToEventsExchange('accounts.username-changed', $message);
}
protected function getAccount() : AccountIdentity {
return Yii::$app->user->identity;
}
}

View File

@ -23,12 +23,11 @@ class ChangeUsernameFormTest extends TestCase {
public function setUp() {
parent::setUp();
$account = AccountIdentity::findOne($this->getAccountId());
Yii::$app->user->setIdentity($account);
Yii::$app->user->setIdentity($this->getAccount());
}
public function testChange() {
$model = new ChangeUsernameForm([
$model = new ChangeUsernameForm($this->getAccount(), [
'password' => 'password_0',
'username' => 'my_new_nickname',
]);
@ -39,8 +38,9 @@ class ChangeUsernameFormTest extends TestCase {
}
public function testChangeWithoutChange() {
$username = $this->tester->grabFixture('accounts', 'admin')['username'];
$model = new ChangeUsernameForm([
$account = $this->getAccount();
$username = $account->username;
$model = new ChangeUsernameForm($account, [
'password' => 'password_0',
'username' => $username,
]);
@ -56,7 +56,7 @@ class ChangeUsernameFormTest extends TestCase {
public function testChangeCase() {
$newUsername = mb_strtoupper($this->tester->grabFixture('accounts', 'admin')['username']);
$model = new ChangeUsernameForm([
$model = new ChangeUsernameForm($this->getAccount(), [
'password' => 'password_0',
'username' => $newUsername,
]);
@ -71,7 +71,7 @@ class ChangeUsernameFormTest extends TestCase {
}
public function testCreateTask() {
$model = new ChangeUsernameForm();
$model = new ChangeUsernameForm($this->getAccount());
$model->createEventTask(1, 'test1', 'test');
$message = $this->tester->grabLastSentAmqpMessage('events');
$body = json_decode($message->getBody(), true);
@ -80,8 +80,12 @@ class ChangeUsernameFormTest extends TestCase {
$this->assertEquals('test', $body['oldUsername']);
}
private function getAccount(): AccountIdentity {
return AccountIdentity::findOne($this->getAccountId());
}
private function getAccountId() {
return $this->tester->grabFixture('accounts', 'admin')['id'];
return $this->tester->grabFixture('accounts', 'admin')->id;
}
}