diff --git a/api/controllers/AccountsController.php b/api/controllers/AccountsController.php index 299d976..71ef948 100644 --- a/api/controllers/AccountsController.php +++ b/api/controllers/AccountsController.php @@ -4,6 +4,7 @@ namespace api\controllers; use api\models\profile\ChangeEmail\ConfirmNewEmailForm; use api\models\profile\ChangeEmail\InitStateForm; use api\models\profile\ChangeEmail\NewEmailForm; +use api\models\profile\ChangeLanguageForm; use api\models\profile\ChangePasswordForm; use api\models\profile\ChangeUsernameForm; use common\models\Account; @@ -30,6 +31,7 @@ class AccountsController extends Controller { 'change-email-initialize', 'change-email-submit-new-email', 'change-email-confirm-new-email', + 'change-lang', ], 'allow' => true, 'roles' => ['@'], @@ -52,6 +54,7 @@ class AccountsController extends Controller { 'change-email-initialize' => ['POST'], 'change-email-submit-new-email' => ['POST'], 'change-email-confirm-new-email' => ['POST'], + 'change-lang' => ['POST'], ]; } @@ -157,4 +160,21 @@ class AccountsController extends Controller { ]; } + public function actionChangeLang() { + /** @var Account $account */ + $account = Yii::$app->user->identity; + $model = new ChangeLanguageForm($account); + $model->load(Yii::$app->request->post()); + if (!$model->applyLanguage()) { + return [ + 'success' => false, + 'errors' => $this->normalizeModelErrors($model->getErrors()), + ]; + } + + return [ + 'success' => true, + ]; + } + } diff --git a/api/models/profile/ChangeLanguageForm.php b/api/models/profile/ChangeLanguageForm.php new file mode 100644 index 0000000..fa44d26 --- /dev/null +++ b/api/models/profile/ChangeLanguageForm.php @@ -0,0 +1,45 @@ +account = $account; + parent::__construct($config); + } + + public function rules() { + return [ + ['lang', 'required'], + ['lang', LanguageValidator::class], + ]; + } + + public function applyLanguage() : bool { + if (!$this->validate()) { + return false; + } + + $account = $this->getAccount(); + $account->lang = $this->lang; + if (!$account->save()) { + throw new ErrorException('Cannot change user language'); + } + + return true; + } + + public function getAccount() : Account { + return $this->account; + } + +} diff --git a/tests/codeception/api/_pages/AccountsRoute.php b/tests/codeception/api/_pages/AccountsRoute.php index 2e8232e..2f1bc05 100644 --- a/tests/codeception/api/_pages/AccountsRoute.php +++ b/tests/codeception/api/_pages/AccountsRoute.php @@ -50,4 +50,11 @@ class AccountsRoute extends BasePage { ]); } + public function changeLang($lang = null) { + $this->route = ['accounts/change-lang']; + $this->actor->sendPOST($this->getUrl(), [ + 'lang' => $lang, + ]); + } + } diff --git a/tests/codeception/api/functional/AccountsChangeLangCest.php b/tests/codeception/api/functional/AccountsChangeLangCest.php new file mode 100644 index 0000000..4fc16fb --- /dev/null +++ b/tests/codeception/api/functional/AccountsChangeLangCest.php @@ -0,0 +1,31 @@ +route = new AccountsRoute($I); + } + + public function testSubmitNewEmail(FunctionalTester $I) { + $I->wantTo('change my account language'); + $I->loggedInAsActiveAccount(); + + $this->route->changeLang('ru'); + $I->canSeeResponseCodeIs(200); + $I->canSeeResponseIsJson(); + $I->canSeeResponseContainsJson([ + 'success' => true, + ]); + } + +} diff --git a/tests/codeception/api/unit/models/profile/ChangeLanguageFormTest.php b/tests/codeception/api/unit/models/profile/ChangeLanguageFormTest.php new file mode 100644 index 0000000..eaa09c2 --- /dev/null +++ b/tests/codeception/api/unit/models/profile/ChangeLanguageFormTest.php @@ -0,0 +1,36 @@ + [ + 'class' => AccountFixture::class, + 'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php', + ], + ]; + } + + public function testApplyLanguage() { + $this->specify('language changed', function() { + /** @var Account $account */ + $account = Account::findOne($this->accounts['admin']); + $model = new ChangeLanguageForm($account); + $model->lang = 'ru'; + expect($model->applyLanguage())->true(); + expect($account->lang)->equals('ru'); + }); + } + +}