2016-01-15 14:51:27 +05:30
|
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
|
namespace tests\codeception\api\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;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
use Codeception\Specify;
|
|
|
|
|
use common\models\Account;
|
|
|
|
|
use common\models\EmailActivation;
|
2016-09-21 13:43:43 +05:30
|
|
|
|
use common\models\UsernameHistory;
|
2016-10-29 03:17:31 +05:30
|
|
|
|
use tests\codeception\api\unit\TestCase;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
|
|
|
|
use Yii;
|
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
|
|
|
|
use Specify;
|
|
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
|
parent::setUp();
|
2016-08-18 05:25:52 +05:30
|
|
|
|
$this->mockRequest();
|
2016-10-29 03:17:31 +05:30
|
|
|
|
Yii::$container->set(ReCaptchaValidator::class, new 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
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
|
public function _fixtures() {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
return [
|
2016-10-29 03:17:31 +05:30
|
|
|
|
'accounts' => AccountFixture::class,
|
2016-01-15 14:51:27 +05:30
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
|
public function testValidatePasswordAndRePasswordMatch() {
|
|
|
|
|
$this->specify('error.rePassword_does_not_match if password and rePassword not match', function() {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
$model = new RegistrationForm([
|
2016-03-20 04:55:26 +05:30
|
|
|
|
'password' => 'enough-length',
|
|
|
|
|
'rePassword' => 'password',
|
2016-01-15 14:51:27 +05:30
|
|
|
|
]);
|
2016-03-20 04:55:26 +05:30
|
|
|
|
expect($model->validate(['rePassword']))->false();
|
|
|
|
|
expect($model->getErrors('rePassword'))->equals(['error.rePassword_does_not_match']);
|
|
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
|
$this->specify('no errors if password and rePassword match', function() {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
$model = new RegistrationForm([
|
2016-03-20 04:55:26 +05:30
|
|
|
|
'password' => 'enough-length',
|
|
|
|
|
'rePassword' => 'enough-length',
|
2016-01-15 14:51:27 +05:30
|
|
|
|
]);
|
2016-03-20 04:55:26 +05:30
|
|
|
|
expect($model->validate(['rePassword']))->true();
|
|
|
|
|
expect($model->getErrors('rePassword'))->isEmpty();
|
|
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
|
public function testSignup() {
|
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);
|
2016-10-29 03:17:31 +05:30
|
|
|
|
$this->assertEquals('ru', $account->lang, 'lang is set');
|
2016-05-13 14:33:00 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSignupWithDefaultLanguage() {
|
|
|
|
|
$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);
|
2016-10-29 03:17:31 +05:30
|
|
|
|
$this->assertEquals('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');
|
|
|
|
|
$this->assertEquals(LATEST_RULES_VERSION, $account->rules_agreement_version, 'actual rules version is set');
|
|
|
|
|
$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');
|
|
|
|
|
$this->assertTrue(EmailActivation::find()->andWhere([
|
2016-05-13 14:33:00 +05:30
|
|
|
|
'account_id' => $account->id,
|
2016-01-15 14:51:27 +05:30
|
|
|
|
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
2016-10-29 03:17:31 +05:30
|
|
|
|
])->exists(), 'email activation code exists in database');
|
|
|
|
|
$this->assertTrue(UsernameHistory::find()->andWhere([
|
2016-09-21 13:43:43 +05:30
|
|
|
|
'username' => $account->username,
|
|
|
|
|
'account_id' => $account->id,
|
|
|
|
|
'applied_in' => $account->created_at,
|
2016-10-29 03:17:31 +05:30
|
|
|
|
])->exists(), 'username history record exists in database');
|
|
|
|
|
$this->tester->canSeeEmailIsSent(1);
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
|
// TODO: там в самой форме есть метод sendMail(), который рано или поздно должен переехать. К нему нужны будут тоже тесты
|
|
|
|
|
|
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
|
|
|
|
|
->expects($this->any())
|
|
|
|
|
->method('getUserIP')
|
|
|
|
|
->will($this->returnValue($ip));
|
|
|
|
|
|
|
|
|
|
Yii::$app->set('request', $request);
|
|
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|