2016-01-21 02:44:29 +05:30
|
|
|
<?php
|
2016-03-20 05:03:49 +05:30
|
|
|
namespace tests\codeception\api\models\base;
|
2016-01-21 02:44:29 +05:30
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
use api\models\base\KeyConfirmationForm;
|
2016-01-21 02:44:29 +05:30
|
|
|
use Codeception\Specify;
|
2016-05-12 03:38:40 +05:30
|
|
|
use common\models\confirmations\ForgotPassword;
|
|
|
|
use common\models\EmailActivation;
|
2016-01-21 02:44:29 +05:30
|
|
|
use tests\codeception\api\unit\DbTestCase;
|
|
|
|
use tests\codeception\common\fixtures\EmailActivationFixture;
|
|
|
|
use Yii;
|
|
|
|
|
|
|
|
/**
|
2016-05-12 03:38:40 +05:30
|
|
|
* @property EmailActivationFixture $emailActivations
|
2016-01-21 02:44:29 +05:30
|
|
|
*/
|
2016-03-20 05:03:49 +05:30
|
|
|
class KeyConfirmationFormTest extends DbTestCase {
|
2016-01-21 02:44:29 +05:30
|
|
|
use Specify;
|
|
|
|
|
|
|
|
public function fixtures() {
|
|
|
|
return [
|
|
|
|
'emailActivations' => [
|
|
|
|
'class' => EmailActivationFixture::class,
|
|
|
|
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:38:40 +05:30
|
|
|
public function testValidateKey() {
|
|
|
|
$this->specify('get error.key_not_exists with validation wrong key', function () {
|
|
|
|
/** @var KeyConfirmationForm $model */
|
|
|
|
$model = new class extends KeyConfirmationForm {
|
|
|
|
public function getActivationCodeModel() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$model->validateKey('key');
|
|
|
|
expect($model->errors)->equals([
|
2016-01-21 02:44:29 +05:30
|
|
|
'key' => [
|
2016-05-12 03:38:40 +05:30
|
|
|
'error.key_not_exists',
|
2016-01-21 02:44:29 +05:30
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
2016-05-12 03:38:40 +05:30
|
|
|
|
|
|
|
$this->specify('no errors, if model exists', function () {
|
|
|
|
/** @var KeyConfirmationForm $model */
|
|
|
|
$model = new class extends KeyConfirmationForm {
|
|
|
|
public function getActivationCodeModel() {
|
|
|
|
return new EmailActivation();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$model->validateKey('key');
|
|
|
|
expect($model->errors)->isEmpty();
|
|
|
|
});
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
2016-05-12 03:38:40 +05:30
|
|
|
public function testValidateKeyExpiration() {
|
|
|
|
$this->specify('get error.key_expire if we use old key', function () {
|
|
|
|
/** @var KeyConfirmationForm $model */
|
|
|
|
$model = new class extends KeyConfirmationForm {
|
|
|
|
public function getActivationCodeModel() {
|
|
|
|
$codeModel = new ForgotPassword();
|
|
|
|
$codeModel->created_at = time() - $codeModel->expirationTimeout - 10;
|
|
|
|
|
|
|
|
return $codeModel;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$model->validateKeyExpiration('key');
|
|
|
|
expect($model->errors)->equals([
|
2016-01-21 02:44:29 +05:30
|
|
|
'key' => [
|
2016-05-12 03:38:40 +05:30
|
|
|
'error.key_expire',
|
2016-01-21 02:44:29 +05:30
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
2016-05-12 03:38:40 +05:30
|
|
|
|
|
|
|
$this->specify('no errors if key is not yet expired', function () {
|
|
|
|
/** @var KeyConfirmationForm $model */
|
|
|
|
$model = new class extends KeyConfirmationForm {
|
|
|
|
public function getActivationCodeModel() {
|
|
|
|
$codeModel = new ForgotPassword();
|
|
|
|
$codeModel->created_at = time() - $codeModel->expirationTimeout + 10;
|
|
|
|
|
|
|
|
return $codeModel;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$model->validateKeyExpiration('key');
|
|
|
|
expect($model->errors)->isEmpty();
|
|
|
|
});
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
2016-05-12 03:38:40 +05:30
|
|
|
public function testGetActivationCodeModel() {
|
|
|
|
$this->specify('should return model, based on passed key', function() {
|
|
|
|
$model = new KeyConfirmationForm();
|
|
|
|
$model->key = array_values($this->emailActivations->data)[0]['key'];
|
|
|
|
expect($model->getActivationCodeModel())->isInstanceOf(EmailActivation::class);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('should return null, if passed key is invalid', function() {
|
|
|
|
$model = new KeyConfirmationForm();
|
|
|
|
$model->key = 'this-is-invalid-key';
|
|
|
|
expect($model->getActivationCodeModel())->null();
|
2016-01-21 02:44:29 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|