2016-03-13 23:54:49 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace tests\codeception\api\models\authentication;
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\authentication\RepeatAccountActivationForm;
|
2016-03-13 23:54:49 +05:30
|
|
|
use Codeception\Specify;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\models\EmailActivation;
|
2016-03-13 23:54:49 +05:30
|
|
|
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 $activations
|
|
|
|
*/
|
|
|
|
class RepeatAccountActivationFormTest 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 [
|
|
|
|
'accounts' => [
|
|
|
|
'class' => AccountFixture::class,
|
|
|
|
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
|
|
|
],
|
2016-05-16 04:03:19 +05:30
|
|
|
'activations' => EmailActivationFixture::class,
|
2016-03-13 23:54:49 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateEmailForAccount() {
|
|
|
|
$this->specify('error.email_not_found if passed valid email, but it don\'t exists in database', function() {
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => 'me-is-not@exists.net']);
|
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
expect($model->getErrors('email'))->equals(['error.email_not_found']);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('error.account_already_activated if passed valid email, but account already activated', function() {
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => $this->accounts['admin']['email']]);
|
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
expect($model->getErrors('email'))->equals(['error.account_already_activated']);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed valid email for not activated account', function() {
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => $this->accounts['not-activated-account']['email']]);
|
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
expect($model->getErrors('email'))->isEmpty();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateExistsActivation() {
|
|
|
|
$this->specify('error.recently_sent_message if passed email has recently sent message', function() {
|
2016-05-14 05:17:17 +05:30
|
|
|
$model = $this->createModel([
|
2016-05-11 01:10:06 +05:30
|
|
|
'emailKey' => $this->activations['freshRegistrationConfirmation']['key'],
|
|
|
|
]);
|
2016-03-13 23:54:49 +05:30
|
|
|
$model->validateExistsActivation('email');
|
|
|
|
expect($model->getErrors('email'))->equals(['error.recently_sent_message']);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed email has expired activation message', function() {
|
2016-05-14 05:17:17 +05:30
|
|
|
$model = $this->createModel([
|
2016-05-11 01:10:06 +05:30
|
|
|
'emailKey' => $this->activations['oldRegistrationConfirmation']['key'],
|
|
|
|
]);
|
2016-03-13 23:54:49 +05:30
|
|
|
$model->validateExistsActivation('email');
|
|
|
|
expect($model->getErrors('email'))->isEmpty();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendRepeatMessage() {
|
|
|
|
$this->specify('no magic if we don\'t pass validation', function() {
|
|
|
|
$model = new RepeatAccountActivationForm();
|
|
|
|
expect($model->sendRepeatMessage())->false();
|
|
|
|
expect_file($this->getMessageFile())->notExists();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('successfully send new message if previous message has expired', function() {
|
|
|
|
$email = $this->accounts['not-activated-account-with-expired-message']['email'];
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => $email]);
|
|
|
|
expect($model->sendRepeatMessage())->true();
|
2016-05-11 01:10:06 +05:30
|
|
|
expect($model->getActivation())->notNull();
|
2016-03-13 23:54:49 +05:30
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @return RepeatAccountActivationForm
|
|
|
|
*/
|
|
|
|
private function createModel(array $params = []) {
|
|
|
|
return new class($params) extends RepeatAccountActivationForm {
|
|
|
|
public $emailKey;
|
|
|
|
|
|
|
|
public function getActivation() {
|
|
|
|
return EmailActivation::findOne($this->emailKey);
|
|
|
|
}
|
|
|
|
};
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|