2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
2019-06-18 05:03:20 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests\_support\models\authentication;
|
2016-01-15 14:51:27 +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\RegistrationForm;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2016-01-15 14:51:27 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\EmailActivation;
|
2016-09-21 13:43:43 +05:30
|
|
|
use common\models\UsernameHistory;
|
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;
|
|
|
|
use common\tests\fixtures\UsernameHistoryFixture;
|
2019-02-23 04:41:57 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
2016-01-15 14:51:27 +05:30
|
|
|
use Yii;
|
2017-09-19 22:36:16 +05:30
|
|
|
use yii\validators\EmailValidator;
|
2016-08-18 05:25:52 +05:30
|
|
|
use yii\web\Request;
|
2016-10-29 03:17:31 +05:30
|
|
|
use const common\LATEST_RULES_VERSION;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class RegistrationFormTest extends TestCase {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
protected function setUp(): void {
|
2016-01-15 14:51:27 +05:30
|
|
|
parent::setUp();
|
2016-08-18 05:25:52 +05:30
|
|
|
$this->mockRequest();
|
2019-12-14 02:46:05 +05:30
|
|
|
Yii::$container->set(ReCaptchaValidator::class, new class($this->createMock(ClientInterface::class)) extends ReCaptchaValidator {
|
2016-09-19 03:31:19 +05:30
|
|
|
public function validateValue($value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-01-15 14:51:27 +05:30
|
|
|
return [
|
2016-10-29 03:17:31 +05:30
|
|
|
'accounts' => AccountFixture::class,
|
2017-08-03 00:25:46 +05:30
|
|
|
'emailActivations' => EmailActivationFixture::class,
|
|
|
|
'usernameHistory' => UsernameHistoryFixture::class,
|
2016-01-15 14:51:27 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
public function testValidatePasswordAndRePasswordMatch() {
|
2017-11-27 04:59:15 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'password' => 'enough-length',
|
|
|
|
'rePassword' => 'but-mismatch',
|
|
|
|
]);
|
|
|
|
$this->assertFalse($model->validate(['rePassword']));
|
|
|
|
$this->assertSame(['error.rePassword_does_not_match'], $model->getErrors('rePassword'));
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2017-11-27 04:59:15 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'password' => 'enough-length',
|
|
|
|
'rePassword' => 'enough-length',
|
|
|
|
]);
|
|
|
|
$this->assertTrue($model->validate(['rePassword']));
|
|
|
|
$this->assertEmpty($model->getErrors('rePassword'));
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
public function testSignup() {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(EmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(EmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['']);
|
2016-01-15 14:51:27 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'username' => 'some_username',
|
|
|
|
'email' => 'some_email@example.com',
|
|
|
|
'password' => 'some_password',
|
|
|
|
'rePassword' => 'some_password',
|
|
|
|
'rulesAgreement' => true,
|
2016-05-13 14:33:00 +05:30
|
|
|
'lang' => 'ru',
|
2016-01-15 14:51:27 +05:30
|
|
|
]);
|
|
|
|
|
2016-05-13 14:33:00 +05:30
|
|
|
$account = $model->signup();
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-05-13 14:33:00 +05:30
|
|
|
$this->expectSuccessRegistration($account);
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('ru', $account->lang, 'lang is set');
|
2016-05-13 14:33:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testSignupWithDefaultLanguage() {
|
2019-12-14 02:46:05 +05:30
|
|
|
$this->getFunctionMock(EmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
|
|
|
$this->getFunctionMock(EmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['']);
|
2016-05-13 14:33:00 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'username' => 'some_username',
|
|
|
|
'email' => 'some_email@example.com',
|
|
|
|
'password' => 'some_password',
|
|
|
|
'rePassword' => 'some_password',
|
|
|
|
'rulesAgreement' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$account = $model->signup();
|
|
|
|
|
|
|
|
$this->expectSuccessRegistration($account);
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('en', $account->lang, 'lang is set');
|
2016-05-13 14:33:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account|null $account
|
|
|
|
*/
|
|
|
|
private function expectSuccessRegistration($account) {
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->assertInstanceOf(Account::class, $account, 'user should be valid');
|
|
|
|
$this->assertTrue($account->validatePassword('some_password'), 'password should be correct');
|
|
|
|
$this->assertNotEmpty($account->uuid, 'uuid is set');
|
|
|
|
$this->assertNotNull($account->registration_ip, 'registration_ip is set');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(LATEST_RULES_VERSION, $account->rules_agreement_version, 'actual rules version is set');
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->assertTrue(Account::find()->andWhere([
|
2016-01-15 14:51:27 +05:30
|
|
|
'username' => 'some_username',
|
|
|
|
'email' => 'some_email@example.com',
|
2016-10-29 03:17:31 +05:30
|
|
|
])->exists(), 'user model exists in database');
|
2017-06-11 23:36:55 +05:30
|
|
|
/** @var EmailActivation $activation */
|
|
|
|
$activation = EmailActivation::find()
|
|
|
|
->andWhere([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
|
|
|
])
|
|
|
|
->one();
|
|
|
|
$this->assertInstanceOf(EmailActivation::class, $activation, 'email activation code exists in database');
|
2019-12-13 16:25:09 +05:30
|
|
|
$this->assertTrue(
|
|
|
|
UsernameHistory::find()
|
|
|
|
->andWhere(['username' => $account->username])
|
|
|
|
->andWhere(['account_id' => $account->id])
|
|
|
|
->andWhere(['>=', 'applied_in', $account->created_at])
|
|
|
|
->exists(),
|
|
|
|
'username history record exists in database'
|
|
|
|
);
|
2017-11-27 04:59:15 +05:30
|
|
|
|
|
|
|
/** @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-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-08-18 05:25:52 +05:30
|
|
|
private function mockRequest($ip = '88.225.20.236') {
|
|
|
|
$request = $this->getMockBuilder(Request::class)
|
|
|
|
->setMethods(['getUserIP'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request
|
|
|
|
->method('getUserIP')
|
2019-02-21 01:28:52 +05:30
|
|
|
->willReturn($ip);
|
2016-08-18 05:25:52 +05:30
|
|
|
|
|
|
|
Yii::$app->set('request', $request);
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|