2016-05-15 04:03:21 +05:30
|
|
|
<?php
|
|
|
|
namespace codeception\api\unit\validators;
|
|
|
|
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2016-05-15 04:03:21 +05:30
|
|
|
use api\validators\EmailActivationKeyValidator;
|
2016-12-23 02:51:05 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-05-15 04:03:21 +05:30
|
|
|
use common\models\confirmations\ForgotPassword;
|
|
|
|
use common\models\EmailActivation;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\_support\ProtectedCaller;
|
|
|
|
use common\tests\fixtures\EmailActivationFixture;
|
2016-12-23 02:51:05 +05:30
|
|
|
use yii\base\Model;
|
2016-05-15 04:03:21 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class EmailActivationKeyValidatorTest extends TestCase {
|
2016-05-15 04:03:21 +05:30
|
|
|
use ProtectedCaller;
|
|
|
|
|
2016-12-23 02:51:05 +05:30
|
|
|
public function testValidateAttribute() {
|
|
|
|
/** @var Model $model */
|
|
|
|
$model = new class extends Model {
|
|
|
|
public $key;
|
|
|
|
};
|
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
/** @var EmailActivationKeyValidator|\PHPUnit\Framework\MockObject\MockObject $validator */
|
2016-12-23 02:51:05 +05:30
|
|
|
$validator = $this->getMockBuilder(EmailActivationKeyValidator::class)
|
|
|
|
->setMethods(['findEmailActivationModel'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$expiredActivation = new ForgotPassword();
|
2019-12-21 03:53:58 +05:30
|
|
|
$expiredActivation->created_at = time() - 60 * 60 - 10;
|
2016-12-23 02:51:05 +05:30
|
|
|
|
|
|
|
$validActivation = new EmailActivation();
|
|
|
|
|
|
|
|
$validator->expects($this->exactly(3))
|
|
|
|
->method('findEmailActivationModel')
|
|
|
|
->willReturnOnConsecutiveCalls(null, $expiredActivation, $validActivation);
|
|
|
|
|
|
|
|
$validator->validateAttribute($model, 'key');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame([E::KEY_REQUIRED], $model->getErrors('key'));
|
2016-12-23 02:51:05 +05:30
|
|
|
$this->assertNull($model->key);
|
|
|
|
|
|
|
|
$model->clearErrors();
|
|
|
|
$model->key = 'original value';
|
|
|
|
$validator->validateAttribute($model, 'key');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame([E::KEY_NOT_EXISTS], $model->getErrors('key'));
|
|
|
|
$this->assertSame('original value', $model->key);
|
2016-12-23 02:51:05 +05:30
|
|
|
|
|
|
|
$model->clearErrors();
|
|
|
|
$validator->validateAttribute($model, 'key');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame([E::KEY_EXPIRE], $model->getErrors('key'));
|
|
|
|
$this->assertSame('original value', $model->key);
|
2016-12-23 02:51:05 +05:30
|
|
|
|
|
|
|
$model->clearErrors();
|
|
|
|
$validator->validateAttribute($model, 'key');
|
|
|
|
$this->assertEmpty($model->getErrors('key'));
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame($validActivation, $model->key);
|
2016-05-15 04:03:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindEmailActivationModel() {
|
2016-12-23 02:51:05 +05:30
|
|
|
$this->tester->haveFixtures(['emailActivations' => EmailActivationFixture::class]);
|
|
|
|
|
|
|
|
$key = $this->tester->grabFixture('emailActivations', 'freshRegistrationConfirmation')['key'];
|
|
|
|
$model = new EmailActivationKeyValidator();
|
|
|
|
/** @var EmailActivation $result */
|
|
|
|
$result = $this->callProtected($model, 'findEmailActivationModel', $key);
|
|
|
|
$this->assertInstanceOf(EmailActivation::class, $result, 'valid key without specifying type must return model');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame($key, $result->key);
|
2016-12-23 02:51:05 +05:30
|
|
|
|
|
|
|
/** @var EmailActivation $result */
|
|
|
|
$result = $this->callProtected($model, 'findEmailActivationModel', $key, 0);
|
|
|
|
$this->assertInstanceOf(EmailActivation::class, $result, 'valid key with valid type must return model');
|
|
|
|
|
|
|
|
/** @var EmailActivation $result */
|
|
|
|
$result = $this->callProtected($model, 'findEmailActivationModel', $key, 1);
|
|
|
|
$this->assertNull($result, 'valid key, but invalid type must return null');
|
2016-05-15 04:03:21 +05:30
|
|
|
|
2016-12-23 02:51:05 +05:30
|
|
|
$model = new EmailActivationKeyValidator();
|
|
|
|
$result = $this->callProtected($model, 'findEmailActivationModel', 'invalid-key');
|
|
|
|
$this->assertNull($result, 'invalid key must return null');
|
2016-05-15 04:03:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|