Добавлен action для принятия правил + тесты для него

This commit is contained in:
ErickSkrauch 2016-08-06 19:22:09 +03:00
parent cf6c7bc88e
commit 16fc1ecb8c
6 changed files with 133 additions and 1 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace api\controllers; namespace api\controllers;
use api\models\profile\AcceptRulesForm;
use api\models\profile\ChangeEmail\ConfirmNewEmailForm; use api\models\profile\ChangeEmail\ConfirmNewEmailForm;
use api\models\profile\ChangeEmail\InitStateForm; use api\models\profile\ChangeEmail\InitStateForm;
use api\models\profile\ChangeEmail\NewEmailForm; use api\models\profile\ChangeEmail\NewEmailForm;
@ -21,7 +22,7 @@ class AccountsController extends Controller {
'class' => AccessControl::class, 'class' => AccessControl::class,
'rules' => [ 'rules' => [
[ [
'actions' => ['current'], 'actions' => ['current', 'accept-rules'],
'allow' => true, 'allow' => true,
'roles' => ['@'], 'roles' => ['@'],
], ],
@ -57,6 +58,7 @@ class AccountsController extends Controller {
'change-email-submit-new-email' => ['POST'], 'change-email-submit-new-email' => ['POST'],
'change-email-confirm-new-email' => ['POST'], 'change-email-confirm-new-email' => ['POST'],
'change-lang' => ['POST'], 'change-lang' => ['POST'],
'accept-rules' => ['POST'],
]; ];
} }
@ -185,4 +187,20 @@ class AccountsController extends Controller {
]; ];
} }
public function actionAcceptRules() {
$account = Yii::$app->user->identity;
$model = new AcceptRulesForm($account);
$model->load(Yii::$app->request->post());
if (!$model->agreeWithLatestRules()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
];
}
return [
'success' => true,
];
}
} }

View File

@ -0,0 +1,35 @@
<?php
namespace api\models\profile;
use api\models\base\ApiForm;
use common\models\Account;
use yii\base\ErrorException;
use const \common\LATEST_RULES_VERSION;
class AcceptRulesForm extends ApiForm {
/**
* @var Account
*/
private $account;
public function __construct(Account $account, array $config = []) {
$this->account = $account;
parent::__construct($config);
}
public function agreeWithLatestRules() : bool {
$account = $this->getAccount();
$account->rules_agreement_version = LATEST_RULES_VERSION;
if (!$account->save()) {
throw new ErrorException('Cannot set user rules version');
}
return true;
}
public function getAccount() : Account {
return $this->account;
}
}

View File

@ -59,4 +59,9 @@ class AccountsRoute extends BasePage {
]); ]);
} }
public function acceptRules() {
$this->route = ['accounts/accept-rules'];
$this->actor->sendPOST($this->getUrl());
}
} }

View File

@ -0,0 +1,28 @@
<?php
namespace tests\codeception\api\functional;
use tests\codeception\api\_pages\AccountsRoute;
use tests\codeception\api\FunctionalTester;
class AccountsAcceptRulesCest {
/**
* @var AccountsRoute
*/
private $route;
public function _before(FunctionalTester $I) {
$this->route = new AccountsRoute($I);
}
public function testCurrent(FunctionalTester $I) {
$I->loggedInAsActiveAccount('Veleyaba', 'password_0');
$this->route->acceptRules();
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'success' => true,
]);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace codeception\api\unit\models\profile;
use api\models\profile\AcceptRulesForm;
use Codeception\Specify;
use common\models\Account;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use const common\LATEST_RULES_VERSION;
/**
* @property AccountFixture $accounts
*/
class AcceptRulesFormTest extends DbTestCase {
use Specify;
public function fixtures() {
return [
'accounts' => AccountFixture::class,
];
}
public function testApplyLanguage() {
$this->specify('rules version bumped to latest', function() {
/** @var Account $account */
$account = Account::findOne($this->accounts['account-with-old-rules-version']);
$model = new AcceptRulesForm($account);
expect($model->agreeWithLatestRules())->true();
expect($account->rules_agreement_version)->equals(LATEST_RULES_VERSION);
});
}
}

View File

@ -107,4 +107,17 @@ return [
'created_at' => 1463349615, 'created_at' => 1463349615,
'updated_at' => 1463349615, 'updated_at' => 1463349615,
], ],
'account-with-old-rules-version' => [
'id' => 9,
'uuid' => '410462d3-8e71-47cc-bac6-64f77f88cf80',
'username' => 'Veleyaba',
'email' => 'veleyaba@gmail.com',
'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'lang' => 'en',
'status' => \common\models\Account::STATUS_ACTIVE,
'rules_agreement_version' => null,
'created_at' => 1470499952,
'updated_at' => 1470499952,
],
]; ];