Реализованы формы для шагов смены E-mail адреса, покрыты unit-тестами

У EmailActivation добавлено поле $_data и дописано поведение для работы с ним
Упрощено подключение фикстур для EmailActivations
This commit is contained in:
ErickSkrauch
2016-05-16 01:33:19 +03:00
parent e2e31c3720
commit 50439fdaeb
27 changed files with 818 additions and 42 deletions

View File

@@ -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,
];
}

View File

@@ -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,
];
}

View File

@@ -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,
];
}

View File

@@ -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,
];
}

View File

@@ -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']);
});
}
}

View File

@@ -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';
}
}

View File

@@ -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';
}
}

View File

@@ -9,7 +9,7 @@ use tests\codeception\common\fixtures\AccountFixture;
use Yii;
/**
* @property array $accounts
* @property AccountFixture $accounts
*/
class ChangePasswordFormTest extends DbTestCase {
use Specify;

View File

@@ -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',

View File

@@ -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,
],
];

View File

@@ -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,
],
];

View 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,
],
];
}
};
}
}

View File

@@ -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,
];
}

View File

@@ -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);
}
}