Образован trait AccountFinder для поиска пользователя по его нику\мылу

Модель EmailActivation теперь умеет автоматически создавать своих правильных потомков по соответствующему типу
Добавлена форма восстановления пароля и её обработчик (без контроллера)
This commit is contained in:
ErickSkrauch
2016-05-10 22:40:06 +03:00
parent ce2e68faf6
commit a29cb76cbf
21 changed files with 664 additions and 62 deletions

View File

@ -31,7 +31,7 @@ class ConfirmEmailFormTest extends DbTestCase {
}
public function testConfirm() {
$fixture = $this->emailActivations[0];
$fixture = $this->emailActivations['freshRegistrationConfirmation'];
$model = $this->createModel($fixture['key']);
$this->specify('expect true result', function() use ($model, $fixture) {
expect('model return successful result', $model->confirm())->notEquals(false);

View File

@ -0,0 +1,149 @@
<?php
namespace codeception\api\unit\models;
use api\models\ForgotPasswordForm;
use Codeception\Specify;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use tests\codeception\common\fixtures\EmailActivationFixture;
use Yii;
/**
* @property array $accounts
* @property array $emailActivations
*/
class ForgotPasswordFormTest extends DbTestCase {
use Specify;
protected 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 [
'accounts' => [
'class' => AccountFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
'emailActivations' => [
'class' => EmailActivationFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
],
];
}
public function testValidateAccount() {
$this->specify('error.login_not_exist if login is invalid', function() {
$model = new ForgotPasswordForm(['login' => 'unexist']);
$model->validateLogin('login');
expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$this->specify('empty errors if login is exists', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['admin']['username']]);
$model->validateLogin('login');
expect($model->getErrors('login'))->isEmpty();
});
}
public function testValidateActivity() {
$this->specify('error.account_not_activated if account is not confirmed', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['not-activated-account']['username']]);
$model->validateActivity('login');
expect($model->getErrors('login'))->equals(['error.account_not_activated']);
});
$this->specify('empty errors if login is exists', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['admin']['username']]);
$model->validateLogin('login');
expect($model->getErrors('login'))->isEmpty();
});
}
public function testValidateFrequency() {
$this->specify('error.account_not_activated if recently was message', function() {
$model = new DummyForgotPasswordForm([
'login' => $this->accounts['admin']['username'],
'key' => $this->emailActivations['freshPasswordRecovery']['key'],
]);
$model->validateFrequency('login');
expect($model->getErrors('login'))->equals(['error.email_frequency']);
});
$this->specify('empty errors if email was sent a long time ago', function() {
$model = new DummyForgotPasswordForm([
'login' => $this->accounts['admin']['username'],
'key' => $this->emailActivations['oldPasswordRecovery']['key'],
]);
$model->validateFrequency('login');
expect($model->getErrors('login'))->isEmpty();
});
$this->specify('empty errors if previous confirmation model not founded', function() {
$model = new DummyForgotPasswordForm([
'login' => $this->accounts['admin']['username'],
'key' => 'invalid-key',
]);
$model->validateFrequency('login');
expect($model->getErrors('login'))->isEmpty();
});
}
public function testForgotPassword() {
$this->specify('successfully send message with restore password key', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['admin']['username']]);
expect($model->forgotPassword())->true();
expect($model->getEmailActivation())->notNull();
expect_file($this->getMessageFile())->exists();
});
}
public function testForgotPasswordResend() {
$this->specify('successfully renew and send message with restore password key', function() {
$model = new ForgotPasswordForm([
'login' => $this->accounts['account-with-expired-forgot-password-message']['username'],
]);
$callTime = time();
expect($model->forgotPassword())->true();
$emailActivation = $model->getEmailActivation();
expect($emailActivation)->notNull();
expect($emailActivation->created_at)->greaterOrEquals($callTime);
expect_file($this->getMessageFile())->exists();
});
}
private function getMessageFile() {
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
}
}
class DummyForgotPasswordForm extends ForgotPasswordForm {
public $key;
public function getEmailActivation() {
return EmailActivation::findOne($this->key);
}
}

View File

@ -84,18 +84,6 @@ class LoginFormTest extends DbTestCase {
});
}
public function testGetLoginAttribute() {
$this->specify('email if login look like email value', function() {
$model = new DummyLoginForm(['login' => 'erickskrauch@ely.by']);
expect($model->getLoginAttribute())->equals('email');
});
$this->specify('username in any other case', function() {
$model = new DummyLoginForm(['login' => 'erickskrauch']);
expect($model->getLoginAttribute())->equals('username');
});
}
}
class DummyLoginForm extends LoginForm {

View File

@ -3,6 +3,7 @@ namespace tests\codeception\api\models;
use api\models\RepeatAccountActivationForm;
use Codeception\Specify;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use tests\codeception\common\fixtures\EmailActivationFixture;
@ -67,14 +68,17 @@ class RepeatAccountActivationFormTest extends DbTestCase {
public function testValidateExistsActivation() {
$this->specify('error.recently_sent_message if passed email has recently sent message', function() {
$model = new RepeatAccountActivationForm(['email' => $this->accounts['not-activated-account']['email']]);
$model = new DummyRepeatAccountActivationForm([
'emailKey' => $this->activations['freshRegistrationConfirmation']['key'],
]);
$model->validateExistsActivation('email');
expect($model->getErrors('email'))->equals(['error.recently_sent_message']);
});
$this->specify('no errors if passed email has expired activation message', function() {
$email = $this->accounts['not-activated-account-with-expired-message']['email'];
$model = new RepeatAccountActivationForm(['email' => $email]);
$model = new DummyRepeatAccountActivationForm([
'emailKey' => $this->activations['oldRegistrationConfirmation']['key'],
]);
$model->validateExistsActivation('email');
expect($model->getErrors('email'))->isEmpty();
});
@ -91,7 +95,7 @@ class RepeatAccountActivationFormTest extends DbTestCase {
$email = $this->accounts['not-activated-account-with-expired-message']['email'];
$model = new RepeatAccountActivationForm(['email' => $email]);
expect($model->sendRepeatMessage())->true();
expect($model->getActiveActivation())->notNull();
expect($model->getActivation())->notNull();
expect_file($this->getMessageFile())->exists();
});
}
@ -104,3 +108,13 @@ class RepeatAccountActivationFormTest extends DbTestCase {
}
}
class DummyRepeatAccountActivationForm extends RepeatAccountActivationForm {
public $emailKey;
public function getActivation() {
return EmailActivation::findOne($this->emailKey);
}
}

View File

@ -53,7 +53,7 @@ class KeyConfirmationFormTest extends DbTestCase {
}
public function testCorrectKey() {
$model = $this->createModel($this->emailActivations[0]['key']);
$model = $this->createModel($this->emailActivations['freshRegistrationConfirmation']['key']);
$this->specify('no errors if key exists', function () use ($model) {
expect('model should pass validation', $model->validate())->true();
});