diff --git a/api/models/base/KeyConfirmationForm.php b/api/models/base/KeyConfirmationForm.php index 1ad128a..bc23c73 100644 --- a/api/models/base/KeyConfirmationForm.php +++ b/api/models/base/KeyConfirmationForm.php @@ -1,6 +1,7 @@ 'error.key_is_required'], - ['key', 'validateKey'], - ['key', 'validateKeyExpiration'], + ['key', EmailActivationKeyValidator::class], ]; } - public function validateKey($attribute) { - if (!$this->hasErrors()) { - if ($this->getActivationCodeModel() === null) { - $this->addError($attribute, "error.{$attribute}_not_exists"); - } - } - } - - public function validateKeyExpiration($attribute) { - if (!$this->hasErrors()) { - if ($this->getActivationCodeModel()->isExpired()) { - $this->addError($attribute, "error.{$attribute}_expire"); - } - } - } - /** * @return EmailActivation|null */ diff --git a/api/validators/EmailActivationKeyValidator.php b/api/validators/EmailActivationKeyValidator.php new file mode 100644 index 0000000..3154876 --- /dev/null +++ b/api/validators/EmailActivationKeyValidator.php @@ -0,0 +1,33 @@ +findEmailActivationModel($value)) === null) { + return [$this->notExist, []]; + } + + if ($model->isExpired()) { + return [$this->expired, []]; + } + + return null; + } + + /** + * @param string $key + * @return null|EmailActivation + */ + protected function findEmailActivationModel($key) { + return EmailActivation::findOne($key); + } + +} diff --git a/tests/codeception/api/functional.suite.yml b/tests/codeception/api/functional.suite.yml index 03824e8..eb80cc5 100644 --- a/tests/codeception/api/functional.suite.yml +++ b/tests/codeception/api/functional.suite.yml @@ -4,10 +4,10 @@ modules: - Filesystem - Yii2 - tests\codeception\common\_support\FixtureHelper - - REST: - depends: Yii2 - Redis - AMQP + - REST: + depends: Yii2 config: Yii2: configFile: '../config/api/functional.php' diff --git a/tests/codeception/api/unit.suite.yml b/tests/codeception/api/unit.suite.yml index a0582a5..1ba1eb4 100644 --- a/tests/codeception/api/unit.suite.yml +++ b/tests/codeception/api/unit.suite.yml @@ -1,6 +1 @@ -# Codeception Test Suite Configuration - -# suite for unit (internal) tests. -# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES. - class_name: UnitTester diff --git a/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php b/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php index e3a0799..4a93568 100644 --- a/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php +++ b/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php @@ -3,7 +3,6 @@ namespace tests\codeception\api\models\base; use api\models\base\KeyConfirmationForm; use Codeception\Specify; -use common\models\confirmations\ForgotPassword; use common\models\EmailActivation; use tests\codeception\api\unit\DbTestCase; use tests\codeception\common\fixtures\EmailActivationFixture; @@ -17,75 +16,10 @@ class KeyConfirmationFormTest extends DbTestCase { public function fixtures() { return [ - 'emailActivations' => [ - 'class' => EmailActivationFixture::class, - 'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php', - ], + 'emailActivations' => EmailActivationFixture::class, ]; } - 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([ - 'key' => [ - 'error.key_not_exists', - ], - ]); - }); - - $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(); - }); - } - - 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([ - 'key' => [ - 'error.key_expire', - ], - ]); - }); - - $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(); - }); - } - public function testGetActivationCodeModel() { $this->specify('should return model, based on passed key', function() { $model = new KeyConfirmationForm(); diff --git a/tests/codeception/api/unit/validators/EmailActivationKeyValidatorTest.php b/tests/codeception/api/unit/validators/EmailActivationKeyValidatorTest.php new file mode 100644 index 0000000..bf7baa5 --- /dev/null +++ b/tests/codeception/api/unit/validators/EmailActivationKeyValidatorTest.php @@ -0,0 +1,76 @@ + EmailActivationFixture::class, + ]; + } + + public function testFindEmailActivationModel() { + $this->specify('get EmailActivation model for exists key', function() { + $key = array_values($this->emailActivations->data)[0]['key']; + $model = new EmailActivationKeyValidator(); + /** @var EmailActivation $result */ + $result = $this->callProtected($model, 'findEmailActivationModel', $key); + expect($result)->isInstanceOf(EmailActivation::class); + expect($result->key)->equals($key); + }); + + $this->specify('get null model for exists key', function() { + $model = new EmailActivationKeyValidator(); + expect($this->callProtected($model, 'findEmailActivationModel', 'invalid-key'))->null(); + }); + } + + public function testValidateValue() { + $this->specify('get error.key_not_exists with validation wrong key', function () { + /** @var EmailActivationKeyValidator $model */ + $model = new class extends EmailActivationKeyValidator { + public function findEmailActivationModel($key) { + return null; + } + }; + expect($this->callProtected($model, 'validateValue', null))->equals([$model->notExist, []]); + }); + + $this->specify('get error.key_expire if we use old key', function () { + /** @var EmailActivationKeyValidator $model */ + $model = new class extends EmailActivationKeyValidator { + public function findEmailActivationModel($key) { + $codeModel = new ForgotPassword(); + $codeModel->created_at = time() - $codeModel->expirationTimeout - 10; + + return $codeModel; + } + }; + expect($this->callProtected($model, 'validateValue', null))->equals([$model->expired, []]); + }); + + $this->specify('no errors, if model exists and not expired', function () { + /** @var EmailActivationKeyValidator $model */ + $model = new class extends EmailActivationKeyValidator { + public function findEmailActivationModel($key) { + return new EmailActivation(); + } + }; + expect($this->callProtected($model, 'validateValue', null))->null(); + }); + } + +} diff --git a/tests/codeception/common/_support/ProtectedCaller.php b/tests/codeception/common/_support/ProtectedCaller.php new file mode 100644 index 0000000..e62b56b --- /dev/null +++ b/tests/codeception/common/_support/ProtectedCaller.php @@ -0,0 +1,17 @@ +getMethod($function); + $method->setAccessible(true); + + return $method->invokeArgs($object, $args); + } + +} diff --git a/tests/codeception/common/fixtures/EmailActivationFixture.php b/tests/codeception/common/fixtures/EmailActivationFixture.php index b9e0f70..8617fd9 100644 --- a/tests/codeception/common/fixtures/EmailActivationFixture.php +++ b/tests/codeception/common/fixtures/EmailActivationFixture.php @@ -8,6 +8,8 @@ class EmailActivationFixture extends ActiveFixture { public $modelClass = EmailActivation::class; + public $dataFile = '@tests/codeception/common/fixtures/data/email-activations.php'; + public $depends = [ AccountFixture::class, ];