mirror of
https://github.com/elyby/accounts.git
synced 2024-11-10 07:22:00 +05:30
Реализованы формы для шагов смены E-mail адреса, покрыты unit-тестами
У EmailActivation добавлено поле $_data и дописано поведение для работы с ним Упрощено подключение фикстур для EmailActivations
This commit is contained in:
parent
e2e31c3720
commit
50439fdaeb
10
api/mails/current-email-confirmation-html.php
Normal file
10
api/mails/current-email-confirmation-html.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/**
|
||||
* @var string $key
|
||||
*/
|
||||
?>
|
||||
|
||||
<p>
|
||||
Вы предприняли попытку сменить свой E-mail адрес. Это так? Если да, то код ниже позволит вам пройти 2 шаг.
|
||||
</p>
|
||||
<p>Код: <?= $key ?></p>
|
9
api/mails/current-email-confirmation-text.php
Normal file
9
api/mails/current-email-confirmation-text.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* @var string $key
|
||||
*/
|
||||
?>
|
||||
|
||||
Вы предприняли попытку сменить свой E-mail адрес. Это так? Если да, то код ниже позволит вам пройти 2 шаг.
|
||||
|
||||
Код активации <?= $key ?>
|
12
api/mails/new-email-confirmation-html.php
Normal file
12
api/mails/new-email-confirmation-html.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \common\models\Account $account
|
||||
* @var string $key
|
||||
*/
|
||||
?>
|
||||
|
||||
<p>
|
||||
Этот E-mail адрес был указан как новый для аккаунта <?= $account->username ?>. Чтобы подтвердить это E-mail,
|
||||
введите код ниже в форму на сайте.
|
||||
</p>
|
||||
<p>Код: <?= $key ?></p>
|
10
api/mails/new-email-confirmation-text.php
Normal file
10
api/mails/new-email-confirmation-text.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \common\models\Account $account
|
||||
* @var string $key
|
||||
*/
|
||||
?>
|
||||
|
||||
Этот E-mail адрес был указан как новый для аккаунта <?= $account->username ?>. Чтобы подтвердить это E-mail, введите код ниже в форму на сайте.
|
||||
|
||||
Код: <?= $key ?>
|
55
api/models/profile/ChangeEmail/ConfirmNewEmailForm.php
Normal file
55
api/models/profile/ChangeEmail/ConfirmNewEmailForm.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use common\models\Account;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\Exception;
|
||||
|
||||
class ConfirmNewEmailForm extends KeyConfirmationForm {
|
||||
|
||||
/**
|
||||
* @var Account
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getAccount() {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function changeEmail() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
/** @var \common\models\confirmations\NewEmailConfirmation $activation */
|
||||
$activation = $this->getActivationCodeModel();
|
||||
$activation->delete();
|
||||
|
||||
$account = $this->getAccount();
|
||||
$account->email = $activation->newEmail;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Cannot save new account email value');
|
||||
}
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
101
api/models/profile/ChangeEmail/InitStateForm.php
Normal file
101
api/models/profile/ChangeEmail/InitStateForm.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\Exception;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class InitStateForm extends ApiForm {
|
||||
|
||||
public $email;
|
||||
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
public function getAccount() {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function rules() {
|
||||
// TODO: поверить наличие уже отправленных подтверждений смены E-mail
|
||||
return [
|
||||
['!email', 'validateAccountPasswordHashStrategy'],
|
||||
];
|
||||
}
|
||||
|
||||
public function validateAccountPasswordHashStrategy($attribute) {
|
||||
$account = $this->getAccount();
|
||||
if ($account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY) {
|
||||
$this->addError($attribute, 'error.old_hash_strategy');
|
||||
}
|
||||
}
|
||||
|
||||
public function sendCurrentEmailConfirmation() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$activation = $this->createCode();
|
||||
$this->sendCode($activation);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CurrentEmailConfirmation
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function createCode() {
|
||||
$account = $this->getAccount();
|
||||
$emailActivation = new CurrentEmailConfirmation();
|
||||
$emailActivation->account_id = $account->id;
|
||||
if (!$emailActivation->save()) {
|
||||
throw new ErrorException('Cannot save email activation model');
|
||||
}
|
||||
|
||||
return $emailActivation;
|
||||
}
|
||||
|
||||
public function sendCode(EmailActivation $code) {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$fromEmail = Yii::$app->params['fromEmail'];
|
||||
if (!$fromEmail) {
|
||||
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
||||
}
|
||||
|
||||
$acceptor = $code->account;
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose([
|
||||
'html' => '@app/mails/current-email-confirmation-html',
|
||||
'text' => '@app/mails/current-email-confirmation-text',
|
||||
], [
|
||||
'key' => $code->key,
|
||||
])
|
||||
->setTo([$acceptor->email => $acceptor->username])
|
||||
->setFrom([$fromEmail => 'Ely.by Accounts'])
|
||||
->setSubject('Ely.by Account change E-mail confirmation');
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send email with activation code.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
112
api/models/profile/ChangeEmail/NewEmailForm.php
Normal file
112
api/models/profile/ChangeEmail/NewEmailForm.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\Exception;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class NewEmailForm extends KeyConfirmationForm {
|
||||
|
||||
public $email;
|
||||
|
||||
/**
|
||||
* @var Account
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getAccount() {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function rules() {
|
||||
return array_merge(parent::rules(), [
|
||||
['email', 'required', 'message' => 'error.email_required'],
|
||||
['email', 'validateEmail'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function validateEmail() {
|
||||
$account = new Account();
|
||||
$account->email = $this->email;
|
||||
if (!$account->validate(['email'])) {
|
||||
$this->addErrors($account->getErrors());
|
||||
}
|
||||
}
|
||||
|
||||
public function sendNewEmailConfirmation() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$previousActivation = $this->getActivationCodeModel();
|
||||
$previousActivation->delete();
|
||||
|
||||
$activation = $this->createCode();
|
||||
$this->sendCode($activation);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return NewEmailConfirmation
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function createCode() {
|
||||
$emailActivation = new NewEmailConfirmation();
|
||||
$emailActivation->account_id = $this->getAccount()->id;
|
||||
$emailActivation->newEmail = $this->email;
|
||||
if (!$emailActivation->save()) {
|
||||
throw new ErrorException('Cannot save email activation model');
|
||||
}
|
||||
|
||||
return $emailActivation;
|
||||
}
|
||||
|
||||
public function sendCode(EmailActivation $code) {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$fromEmail = Yii::$app->params['fromEmail'];
|
||||
if (!$fromEmail) {
|
||||
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
||||
}
|
||||
|
||||
$acceptor = $code->account;
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose([
|
||||
'html' => '@app/mails/new-email-confirmation-html',
|
||||
'text' => '@app/mails/new-email-confirmation-text',
|
||||
], [
|
||||
'key' => $code->key,
|
||||
'account' => $acceptor,
|
||||
])
|
||||
->setTo([$this->email => $acceptor->username])
|
||||
->setFrom([$fromEmail => 'Ely.by Accounts'])
|
||||
->setSubject('Ely.by Account new E-mail confirmation');
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send email with activation code.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
47
common/behaviors/DataBehavior.php
Normal file
47
common/behaviors/DataBehavior.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace common\behaviors;
|
||||
|
||||
use yii\base\Behavior;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class DataBehavior extends Behavior {
|
||||
/**
|
||||
* @var string имя атрибута, к которому будет применяться поведение
|
||||
*/
|
||||
public $attribute = '_data';
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
protected function setKey(string $key, $value) {
|
||||
$data = $this->getData();
|
||||
$data[$key] = $value;
|
||||
$this->owner->{$this->attribute} = serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getKey(string $key) {
|
||||
return ArrayHelper::getValue($this->getData(), $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws \yii\base\ErrorException Yii2 подхватит Notice от неправильной десериализаци и превратит его
|
||||
* в свой Exception, благодаря чему программа сможем продолжить нормально работать (вернее ловить Exception)
|
||||
*/
|
||||
private function getData() {
|
||||
$data = $this->owner->{$this->attribute};
|
||||
if (is_string($data)) {
|
||||
$data = unserialize($data);
|
||||
} else {
|
||||
$data = [];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace common\models;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
use common\behaviors\EmailActivationExpirationBehavior;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
@ -13,6 +15,7 @@ use yii\helpers\ArrayHelper;
|
||||
* @property string $key
|
||||
* @property integer $account_id
|
||||
* @property integer $type
|
||||
* @property string $_data
|
||||
* @property integer $created_at
|
||||
*
|
||||
* Отношения:
|
||||
@ -21,15 +24,14 @@ use yii\helpers\ArrayHelper;
|
||||
* Поведения:
|
||||
* @mixin TimestampBehavior
|
||||
* @mixin EmailActivationExpirationBehavior
|
||||
*
|
||||
* TODO: у модели могут быть проблемы с уникальностью, т.к. key является первичным и не автоинкрементом
|
||||
* TODO: мб стоит ловить beforeCreate и именно там генерировать уникальный ключ для модели.
|
||||
* Но опять же нужно продумать, а как пробросить формат и обеспечить преемлемую уникальность.
|
||||
* @mixin DataBehavior
|
||||
*/
|
||||
class EmailActivation extends ActiveRecord {
|
||||
|
||||
const TYPE_REGISTRATION_EMAIL_CONFIRMATION = 0;
|
||||
const TYPE_FORGOT_PASSWORD_KEY = 1;
|
||||
const TYPE_CURRENT_EMAIL_CONFIRMATION = 2;
|
||||
const TYPE_NEW_EMAIL_CONFIRMATION = 3;
|
||||
|
||||
public static function tableName() {
|
||||
return '{{%email_activations}}';
|
||||
@ -46,6 +48,10 @@ class EmailActivation extends ActiveRecord {
|
||||
'repeatTimeout' => 5 * 60,
|
||||
'expirationTimeout' => -1,
|
||||
],
|
||||
'dataBehavior' => [
|
||||
'class' => DataBehavior::class,
|
||||
'attribute' => '_data',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -74,7 +80,23 @@ class EmailActivation extends ActiveRecord {
|
||||
return [
|
||||
self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class,
|
||||
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
||||
self::TYPE_CURRENT_EMAIL_CONFIRMATION => confirmations\CurrentEmailConfirmation::class,
|
||||
self::TYPE_NEW_EMAIL_CONFIRMATION => confirmations\NewEmailConfirmation::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert) {
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->key === null) {
|
||||
do {
|
||||
$this->key = UserFriendlyRandomKey::make();
|
||||
} while (EmailActivation::find()->andWhere(['key' => $this->key])->exists());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
23
common/models/confirmations/CurrentEmailConfirmation.php
Normal file
23
common/models/confirmations/CurrentEmailConfirmation.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class CurrentEmailConfirmation extends EmailActivation {
|
||||
|
||||
public function behaviors() {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 6 * 60 * 60,
|
||||
'expirationTimeout' => 1 * 60 * 60,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
}
|
29
common/models/confirmations/NewEmailConfirmation.php
Normal file
29
common/models/confirmations/NewEmailConfirmation.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* Поведения:
|
||||
* @mixin NewEmailConfirmationBehavior
|
||||
*/
|
||||
class NewEmailConfirmation extends EmailActivation {
|
||||
|
||||
public function behaviors() {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 5 * 60,
|
||||
],
|
||||
'dataBehavior' => [
|
||||
'class' => NewEmailConfirmationBehavior::class,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
}
|
19
common/models/confirmations/NewEmailConfirmationBehavior.php
Normal file
19
common/models/confirmations/NewEmailConfirmationBehavior.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
|
||||
/**
|
||||
* @property string $newEmail
|
||||
*/
|
||||
class NewEmailConfirmationBehavior extends DataBehavior {
|
||||
|
||||
public function getNewEmail() : string {
|
||||
return $this->getKey('newEmail');
|
||||
}
|
||||
|
||||
public function setNewEmail(string $newEmail) {
|
||||
$this->setKey('newEmail', $newEmail);
|
||||
}
|
||||
|
||||
}
|
15
console/migrations/m160515_153724_email_activation_data.php
Normal file
15
console/migrations/m160515_153724_email_activation_data.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
use console\db\Migration;
|
||||
|
||||
class m160515_153724_email_activation_data extends Migration {
|
||||
|
||||
public function safeUp() {
|
||||
$this->addColumn('{{%email_activations}}', '_data', $this->text()->after('type'));
|
||||
}
|
||||
|
||||
public function safeDown() {
|
||||
$this->dropColumn('{{%email_activations}}', '_data');
|
||||
}
|
||||
|
||||
}
|
@ -17,10 +17,7 @@ class ConfirmEmailFormTest extends DbTestCase {
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -39,10 +39,7 @@ class ForgotPasswordFormTest extends DbTestCase {
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -17,10 +17,7 @@ class RecoverPasswordFormTest extends DbTestCase {
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -39,10 +39,7 @@ class RepeatAccountActivationFormTest extends DbTestCase {
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'activations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'activations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\profile\ChangeEmail\ConfirmNewEmailForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
use common\models\EmailActivation;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use tests\codeception\common\fixtures\EmailActivationFixture;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* @property AccountFixture $accounts
|
||||
* @property EmailActivationFixture $emailActivations
|
||||
*/
|
||||
class ConfirmNewEmailFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'accounts' => [
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testChangeEmail() {
|
||||
$this->specify('successfully change account email', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['account-with-change-email-finish-state']['id']);
|
||||
$model = new ConfirmNewEmailForm($account, [
|
||||
'key' => $this->emailActivations['newEmailConfirmation']['key'],
|
||||
]);
|
||||
expect($model->changeEmail())->true();
|
||||
expect(EmailActivation::findOne([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
||||
]))->null();
|
||||
$data = unserialize($this->emailActivations['newEmailConfirmation']['_data']);
|
||||
expect($account->email)->equals($data['newEmail']);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\profile\ChangeEmail\InitStateForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use tests\codeception\common\fixtures\EmailActivationFixture;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* @property AccountFixture $accounts
|
||||
* @property EmailActivationFixture $emailActivations
|
||||
*/
|
||||
class InitStateFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$mailer->fileTransportCallback = function () {
|
||||
return 'testing_message.eml';
|
||||
};
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
if (file_exists($this->getMessageFile())) {
|
||||
unlink($this->getMessageFile());
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'accounts' => [
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testValidateAccountPasswordHashStrategy() {
|
||||
$this->specify('we cannot change password on old password hash strategy', function() {
|
||||
$account = new Account();
|
||||
$account->password_hash_strategy = Account::PASS_HASH_STRATEGY_OLD_ELY;
|
||||
$model = new InitStateForm($account);
|
||||
$model->validateAccountPasswordHashStrategy('email');
|
||||
expect($model->getErrors('email'))->equals(['error.old_hash_strategy']);
|
||||
});
|
||||
|
||||
$this->specify('no errors on modern password hash strategy', function() {
|
||||
$account = new Account();
|
||||
$account->password_hash_strategy = Account::PASS_HASH_STRATEGY_YII2;
|
||||
$model = new InitStateForm($account);
|
||||
$model->validateAccountPasswordHashStrategy('email');
|
||||
expect($model->getErrors('email'))->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testCreateCode() {
|
||||
$this->specify('create valid code and store it to database', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
$model = new InitStateForm($account);
|
||||
$activationModel = $model->createCode();
|
||||
expect($activationModel)->isInstanceOf(CurrentEmailConfirmation::class);
|
||||
expect($activationModel->account_id)->equals($account->id);
|
||||
expect(EmailActivation::findOne($activationModel->key))->notNull();
|
||||
});
|
||||
}
|
||||
|
||||
public function testSendCurrentEmailConfirmation() {
|
||||
$this->specify('send email', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
$model = new InitStateForm($account);
|
||||
expect($model->sendCurrentEmailConfirmation())->true();
|
||||
expect(EmailActivation::find()->andWhere([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
|
||||
])->exists())->true();
|
||||
expect_file($this->getMessageFile())->exists();
|
||||
});
|
||||
}
|
||||
|
||||
private function getMessageFile() {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
|
||||
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\profile\ChangeEmail\NewEmailForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use tests\codeception\common\fixtures\EmailActivationFixture;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* @property AccountFixture $accounts
|
||||
* @property EmailActivationFixture $emailActivations
|
||||
*/
|
||||
class NewEmailFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$mailer->fileTransportCallback = function () {
|
||||
return 'testing_message.eml';
|
||||
};
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
if (file_exists($this->getMessageFile())) {
|
||||
unlink($this->getMessageFile());
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'accounts' => [
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testCreateCode() {
|
||||
$this->specify('create valid code and store it to database', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
$model = new NewEmailForm($account);
|
||||
$model->email = 'my-new-email@ely.by';
|
||||
$activationModel = $model->createCode();
|
||||
expect($activationModel)->isInstanceOf(NewEmailConfirmation::class);
|
||||
expect($activationModel->account_id)->equals($account->id);
|
||||
expect($activationModel->newEmail)->equals($model->email);
|
||||
expect(EmailActivation::findOne($activationModel->key))->notNull();
|
||||
});
|
||||
}
|
||||
|
||||
public function testSendNewEmailConfirmation() {
|
||||
$this->specify('send email', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
/** @var NewEmailForm $model */
|
||||
$model = new NewEmailForm($account, [
|
||||
'key' => $this->emailActivations['currentEmailConfirmation']['key'],
|
||||
'email' => 'my-new-email@ely.by',
|
||||
]);
|
||||
expect($model->sendNewEmailConfirmation())->true();
|
||||
expect(EmailActivation::findOne($this->emailActivations['currentEmailConfirmation']['key']))->null();
|
||||
expect(EmailActivation::findOne([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
||||
]))->notNull();
|
||||
expect_file($this->getMessageFile())->exists();
|
||||
});
|
||||
}
|
||||
|
||||
private function getMessageFile() {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
|
||||
}
|
||||
|
||||
}
|
@ -9,7 +9,7 @@ use tests\codeception\common\fixtures\AccountFixture;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* @property array $accounts
|
||||
* @property AccountFixture $accounts
|
||||
*/
|
||||
class ChangePasswordFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
@ -50,10 +50,7 @@ class FixtureHelper extends Module {
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
'oauthClients' => [
|
||||
'class' => OauthClientFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/oauth-clients.php',
|
||||
|
@ -75,4 +75,16 @@ return [
|
||||
'created_at' => 1462891612,
|
||||
'updated_at' => 1462891612,
|
||||
],
|
||||
'account-with-change-email-finish-state' => [
|
||||
'id' => 7,
|
||||
'uuid' => '4c34f2cc-4bd9-454b-9583-bb52f020ec16',
|
||||
'username' => 'CrafterGameplays',
|
||||
'email' => 'crafter@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,
|
||||
'created_at' => 1463349615,
|
||||
'updated_at' => 1463349615,
|
||||
],
|
||||
];
|
||||
|
@ -24,4 +24,17 @@ return [
|
||||
'type' => \common\models\EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
|
||||
'created_at' => time() - (new \common\models\confirmations\ForgotPassword())->repeatTimeout - 10,
|
||||
],
|
||||
'currentEmailConfirmation' => [
|
||||
'key' => 'H26HBDCHHAG2HGHGHS',
|
||||
'account_id' => 1,
|
||||
'type' => \common\models\EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
|
||||
'created_at' => time() - 10,
|
||||
],
|
||||
'newEmailConfirmation' => [
|
||||
'key' => 'H27HBDCHHAG2HGHGHS',
|
||||
'account_id' => 7,
|
||||
'type' => \common\models\EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
||||
'_data' => serialize(['newEmail' => 'my-new-email@ely.by']),
|
||||
'created_at' => time() - 10,
|
||||
],
|
||||
];
|
||||
|
79
tests/codeception/common/unit/behaviors/DataBehaviorTest.php
Normal file
79
tests/codeception/common/unit/behaviors/DataBehaviorTest.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace codeception\common\unit\behaviors;
|
||||
|
||||
use Codeception\Specify;
|
||||
use common\behaviors\DataBehavior;
|
||||
use tests\codeception\common\_support\ProtectedCaller;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\Model;
|
||||
|
||||
class DataBehaviorTest extends TestCase {
|
||||
use Specify;
|
||||
use ProtectedCaller;
|
||||
|
||||
public function testSetKey() {
|
||||
$this->specify('setting value should change model data field', function() {
|
||||
$model = $this->createModel();
|
||||
/** @var DataBehavior $behavior */
|
||||
$behavior = $model->behaviors['dataBehavior'];
|
||||
$this->callProtected($behavior, 'setKey', 'my-key', 'my-value');
|
||||
expect($model->_data)->equals(serialize(['my-key' => 'my-value']));
|
||||
});
|
||||
}
|
||||
|
||||
public function testGetKey() {
|
||||
$this->specify('getting value from exists data should work', function() {
|
||||
$model = $this->createModel();
|
||||
$model->_data = serialize(['some-key' => 'some-value']);
|
||||
/** @var DataBehavior $behavior */
|
||||
$behavior = $model->behaviors['dataBehavior'];
|
||||
expect($this->callProtected($behavior, 'getKey', 'some-key'))->equals('some-value');
|
||||
});
|
||||
}
|
||||
|
||||
public function testGetData() {
|
||||
$this->specify('getting value from null field should return empty array', function() {
|
||||
$model = $this->createModel();
|
||||
/** @var DataBehavior $behavior */
|
||||
$behavior = $model->behaviors['dataBehavior'];
|
||||
expect($this->callProtected($behavior, 'getData'))->equals([]);
|
||||
});
|
||||
|
||||
$this->specify('getting value from serialized data field should return encoded value', function() {
|
||||
$model = $this->createModel();
|
||||
$data = ['foo' => 'bar'];
|
||||
$model->_data = serialize($data);
|
||||
/** @var DataBehavior $behavior */
|
||||
$behavior = $model->behaviors['dataBehavior'];
|
||||
expect($this->callProtected($behavior, 'getData'))->equals($data);
|
||||
});
|
||||
|
||||
$this->specify('getting value from invalid serialization string', function() {
|
||||
$model = $this->createModel();
|
||||
$model->_data = 'this is invalid serialization of string';
|
||||
/** @var DataBehavior $behavior */
|
||||
$behavior = $model->behaviors['dataBehavior'];
|
||||
$this->expectException(ErrorException::class);
|
||||
$this->callProtected($behavior, 'getData');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Model
|
||||
*/
|
||||
private function createModel() {
|
||||
return new class extends Model {
|
||||
public $_data;
|
||||
|
||||
public function behaviors() {
|
||||
return [
|
||||
'dataBehavior' => [
|
||||
'class' => DataBehavior::class,
|
||||
],
|
||||
];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -13,10 +13,7 @@ class EmailActivationTest extends DbTestCase {
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -3,11 +3,12 @@ namespace codeception\common\unit\validators;
|
||||
|
||||
use Codeception\Specify;
|
||||
use common\validators\LanguageValidator;
|
||||
use ReflectionClass;
|
||||
use tests\codeception\common\_support\ProtectedCaller;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
|
||||
class LanguageValidatorTest extends TestCase {
|
||||
use Specify;
|
||||
use ProtectedCaller;
|
||||
|
||||
public function testGetFilesNames() {
|
||||
$this->specify('get list of 2 languages: ru and en', function() {
|
||||
@ -42,12 +43,4 @@ class LanguageValidatorTest extends TestCase {
|
||||
};
|
||||
}
|
||||
|
||||
private function callProtected($object, string $function, ...$args) {
|
||||
$class = new ReflectionClass($object);
|
||||
$method = $class->getMethod($function);
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invokeArgs($object, $args);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user