2016-01-15 14:51:27 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace tests\codeception\api\models;
|
|
|
|
|
|
|
|
|
|
use api\models\RegistrationForm;
|
|
|
|
|
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-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';
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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-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';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|