2016-03-13 23:54:49 +05:30
|
|
|
<?php
|
2019-08-02 18:27:17 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
namespace api\tests\unit\models\authentication;
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\authentication\RepeatAccountActivationForm;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2019-12-21 04:56:06 +05:30
|
|
|
use common\models\Account;
|
2019-12-21 03:53:58 +05:30
|
|
|
use common\models\confirmations\RegistrationConfirmation;
|
2017-11-27 04:59:15 +05:30
|
|
|
use common\tasks\SendRegistrationEmail;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountFixture;
|
|
|
|
use common\tests\fixtures\EmailActivationFixture;
|
2019-02-23 04:41:57 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
2016-03-13 23:54:49 +05:30
|
|
|
use Yii;
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class RepeatAccountActivationFormTest extends TestCase {
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
protected function setUp(): void {
|
2016-03-13 23:54:49 +05:30
|
|
|
parent::setUp();
|
2019-12-14 02:46:05 +05:30
|
|
|
Yii::$container->set(ReCaptchaValidator::class, new class($this->createMock(ClientInterface::class)) extends ReCaptchaValidator {
|
2024-12-02 15:40:55 +05:30
|
|
|
public function validateValue($value): ?array {
|
2016-09-19 03:31:19 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
2016-03-13 23:54:49 +05:30
|
|
|
}
|
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-03-13 23:54:49 +05:30
|
|
|
return [
|
2016-10-29 03:17:31 +05:30
|
|
|
'accounts' => AccountFixture::class,
|
2016-05-16 04:03:19 +05:30
|
|
|
'activations' => EmailActivationFixture::class,
|
2016-03-13 23:54:49 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateEmailForAccount(): void {
|
2019-12-21 04:56:06 +05:30
|
|
|
$model = $this->createWithAccount(null);
|
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
$this->assertSame(['error.email_not_found'], $model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2019-12-21 04:56:06 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->status = Account::STATUS_ACTIVE;
|
|
|
|
$model = $this->createWithAccount($account);
|
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
$this->assertSame(['error.account_already_activated'], $model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2019-12-21 04:56:06 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->status = Account::STATUS_REGISTERED;
|
|
|
|
$model = $this->createWithAccount($account);
|
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
$this->assertEmpty($model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateExistsActivation(): void {
|
2019-12-21 04:56:06 +05:30
|
|
|
$activation = new RegistrationConfirmation();
|
|
|
|
$activation->created_at = time() - 10;
|
|
|
|
$model = $this->createWithActivation($activation);
|
|
|
|
$model->validateExistsActivation('email');
|
|
|
|
$this->assertSame(['error.recently_sent_message'], $model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2019-12-21 04:56:06 +05:30
|
|
|
$activation = new RegistrationConfirmation();
|
|
|
|
$activation->created_at = time() - 60 * 60 * 24;
|
|
|
|
$model = $this->createWithActivation($activation);
|
|
|
|
$model->validateExistsActivation('email');
|
|
|
|
$this->assertEmpty($model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testSendRepeatMessage(): void {
|
2017-11-27 04:59:15 +05:30
|
|
|
$model = new RepeatAccountActivationForm();
|
|
|
|
$this->assertFalse($model->sendRepeatMessage(), 'no magic if we don\'t pass validation');
|
|
|
|
$this->assertEmpty($this->tester->grabQueueJobs());
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2017-11-27 04:59:15 +05:30
|
|
|
/** @var \common\models\Account $account */
|
|
|
|
$account = $this->tester->grabFixture('accounts', 'not-activated-account-with-expired-message');
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => $account->email]);
|
|
|
|
$this->assertTrue($model->sendRepeatMessage());
|
|
|
|
$activation = $model->getActivation();
|
|
|
|
$this->assertNotNull($activation);
|
|
|
|
/** @var SendRegistrationEmail $job */
|
|
|
|
$job = $this->tester->grabLastQueuedJob();
|
|
|
|
$this->assertInstanceOf(SendRegistrationEmail::class, $job);
|
|
|
|
$this->assertSame($account->username, $job->username);
|
|
|
|
$this->assertSame($account->email, $job->email);
|
|
|
|
$this->assertSame($account->lang, $job->locale);
|
|
|
|
$this->assertSame($activation->key, $job->code);
|
|
|
|
$this->assertSame('http://localhost/activation/' . $activation->key, $job->link);
|
2016-03-13 23:54:49 +05:30
|
|
|
}
|
|
|
|
|
2019-12-21 04:56:06 +05:30
|
|
|
private function createWithAccount(?Account $account): RepeatAccountActivationForm {
|
|
|
|
$model = $this->createPartialMock(RepeatAccountActivationForm::class, ['getAccount']);
|
|
|
|
$model->method('getAccount')->willReturn($account);
|
2016-05-14 05:17:17 +05:30
|
|
|
|
2019-12-21 04:56:06 +05:30
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createWithActivation(?RegistrationConfirmation $activation): RepeatAccountActivationForm {
|
|
|
|
$model = $this->createPartialMock(RepeatAccountActivationForm::class, ['getActivation']);
|
|
|
|
$model->method('getActivation')->willReturn($activation);
|
|
|
|
|
|
|
|
return $model;
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|