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-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;
|
|
|
|
|
use tests\codeception\api\unit\DbTestCase;
|
|
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
|
|
|
|
use Yii;
|
2016-08-06 21:22:03 +05:30
|
|
|
|
use const common\LATEST_RULES_VERSION;
|
2016-08-18 05:25:52 +05:30
|
|
|
|
use yii\web\Request;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-01-21 02:44:29 +05:30
|
|
|
|
/**
|
|
|
|
|
* @property array $accounts
|
|
|
|
|
*/
|
2016-01-15 14:51:27 +05:30
|
|
|
|
class RegistrationFormTest extends DbTestCase {
|
|
|
|
|
use Specify;
|
|
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
/** @var \yii\swiftmailer\Mailer $mailer */
|
|
|
|
|
$mailer = Yii::$app->mailer;
|
|
|
|
|
$mailer->fileTransportCallback = function () {
|
|
|
|
|
return 'testing_message.eml';
|
|
|
|
|
};
|
2016-08-18 05:25:52 +05:30
|
|
|
|
$this->mockRequest();
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function tearDown() {
|
|
|
|
|
if (file_exists($this->getMessageFile())) {
|
|
|
|
|
unlink($this->getMessageFile());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function fixtures() {
|
|
|
|
|
return [
|
2016-01-21 02:44:29 +05:30
|
|
|
|
'accounts' => [
|
|
|
|
|
'class' => AccountFixture::class,
|
2016-01-15 14:51:27 +05:30
|
|
|
|
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
expect('lang is set', $account->lang)->equals('ru');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
expect('lang is set', $account->lang)->equals('en');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Account|null $account
|
|
|
|
|
*/
|
|
|
|
|
private function expectSuccessRegistration($account) {
|
|
|
|
|
expect('user should be valid', $account)->isInstanceOf(Account::class);
|
|
|
|
|
expect('password should be correct', $account->validatePassword('some_password'))->true();
|
|
|
|
|
expect('uuid is set', $account->uuid)->notEmpty();
|
2016-08-18 05:25:52 +05:30
|
|
|
|
expect('registration_ip is set', $account->registration_ip)->notNull();
|
2016-08-06 21:22:03 +05:30
|
|
|
|
expect('actual rules version is set', $account->rules_agreement_version)->equals(LATEST_RULES_VERSION);
|
2016-01-15 14:51:27 +05:30
|
|
|
|
expect('user model exists in database', Account::find()->andWhere([
|
|
|
|
|
'username' => 'some_username',
|
|
|
|
|
'email' => 'some_email@example.com',
|
|
|
|
|
])->exists())->true();
|
|
|
|
|
expect('email activation code exists in database', 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,
|
|
|
|
|
])->exists())->true();
|
|
|
|
|
expect_file('message file exists', $this->getMessageFile())->exists();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
private function getMessageFile() {
|
|
|
|
|
/** @var \yii\swiftmailer\Mailer $mailer */
|
|
|
|
|
$mailer = Yii::$app->mailer;
|
|
|
|
|
|
|
|
|
|
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|