first commit

This commit is contained in:
2023-05-06 20:40:02 +03:00
commit fdf3e1e602
221 changed files with 12262 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace frontend\tests\unit\models;
use frontend\models\ContactForm;
use yii\mail\MessageInterface;
class ContactFormTest extends \Codeception\Test\Unit
{
public function testSendEmail()
{
$model = new ContactForm();
$model->attributes = [
'name' => 'Tester',
'email' => 'tester@example.com',
'subject' => 'very important letter subject',
'body' => 'body of current message',
];
verify($model->sendEmail('admin@example.com'))->notEmpty();
// using Yii2 module actions to check email was sent
$this->tester->seeEmailIsSent();
/** @var MessageInterface $emailMessage */
$emailMessage = $this->tester->grabLastSentEmail();
verify($emailMessage)->instanceOf('yii\mail\MessageInterface');
verify($emailMessage->getTo())->arrayHasKey('admin@example.com');
verify($emailMessage->getFrom())->arrayHasKey('noreply@example.com');
verify($emailMessage->getReplyTo())->arrayHasKey('tester@example.com');
verify($emailMessage->getSubject())->equals('very important letter subject');
verify($emailMessage->toString())->stringContainsString('body of current message');
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace frontend\tests\unit\models;
use Yii;
use frontend\models\PasswordResetRequestForm;
use common\fixtures\UserFixture as UserFixture;
use common\models\User;
class PasswordResetRequestFormTest extends \Codeception\Test\Unit
{
/**
* @var \frontend\tests\UnitTester
*/
protected $tester;
public function _before()
{
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::class,
'dataFile' => codecept_data_dir() . 'user.php'
]
]);
}
public function testSendMessageWithWrongEmailAddress()
{
$model = new PasswordResetRequestForm();
$model->email = 'not-existing-email@example.com';
verify($model->sendEmail())->false();
}
public function testNotSendEmailsToInactiveUser()
{
$user = $this->tester->grabFixture('user', 1);
$model = new PasswordResetRequestForm();
$model->email = $user['email'];
verify($model->sendEmail())->false();
}
public function testSendEmailSuccessfully()
{
$userFixture = $this->tester->grabFixture('user', 0);
$model = new PasswordResetRequestForm();
$model->email = $userFixture['email'];
$user = User::findOne(['password_reset_token' => $userFixture['password_reset_token']]);
verify($model->sendEmail())->notEmpty();
verify($user->password_reset_token)->notEmpty();
$emailMessage = $this->tester->grabLastSentEmail();
verify($emailMessage)->instanceOf('yii\mail\MessageInterface');
verify($emailMessage->getTo())->arrayHasKey($model->email);
verify($emailMessage->getFrom())->arrayHasKey(Yii::$app->params['supportEmail']);
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace frontend\tests\unit\models;
use Codeception\Test\Unit;
use common\fixtures\UserFixture;
use frontend\models\ResendVerificationEmailForm;
class ResendVerificationEmailFormTest extends Unit
{
/**
* @var \frontend\tests\UnitTester
*/
protected $tester;
public function _before()
{
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::class,
'dataFile' => codecept_data_dir() . 'user.php'
]
]);
}
public function testWrongEmailAddress()
{
$model = new ResendVerificationEmailForm();
$model->attributes = [
'email' => 'aaa@bbb.cc'
];
verify($model->validate())->false();
verify($model->hasErrors())->true();
verify($model->getFirstError('email'))->equals('There is no user with this email address.');
}
public function testEmptyEmailAddress()
{
$model = new ResendVerificationEmailForm();
$model->attributes = [
'email' => ''
];
verify($model->validate())->false();
verify($model->hasErrors())->true();
verify($model->getFirstError('email'))->equals('Email cannot be blank.');
}
public function testResendToActiveUser()
{
$model = new ResendVerificationEmailForm();
$model->attributes = [
'email' => 'test2@mail.com'
];
verify($model->validate())->false();
verify($model->hasErrors())->true();
verify($model->getFirstError('email'))->equals('There is no user with this email address.');
}
public function testSuccessfullyResend()
{
$model = new ResendVerificationEmailForm();
$model->attributes = [
'email' => 'test@mail.com'
];
verify($model->validate())->true();
verify($model->hasErrors())->false();
verify($model->sendEmail())->true();
$this->tester->seeEmailIsSent();
$mail = $this->tester->grabLastSentEmail();
verify($mail)->instanceOf('yii\mail\MessageInterface');
verify($mail->getTo())->arrayHasKey('test@mail.com');
verify($mail->getFrom())->arrayHasKey(\Yii::$app->params['supportEmail']);
verify($mail->getSubject())->equals('Account registration at ' . \Yii::$app->name);
verify($mail->toString())->stringContainsString('4ch0qbfhvWwkcuWqjN8SWRq72SOw1KYT_1548675330');
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace frontend\tests\unit\models;
use common\fixtures\UserFixture;
use frontend\models\ResetPasswordForm;
class ResetPasswordFormTest extends \Codeception\Test\Unit
{
/**
* @var \frontend\tests\UnitTester
*/
protected $tester;
public function _before()
{
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::class,
'dataFile' => codecept_data_dir() . 'user.php'
],
]);
}
public function testResetWrongToken()
{
$this->tester->expectThrowable('\yii\base\InvalidArgumentException', function() {
new ResetPasswordForm('');
});
$this->tester->expectThrowable('\yii\base\InvalidArgumentException', function() {
new ResetPasswordForm('notexistingtoken_1391882543');
});
}
public function testResetCorrectToken()
{
$user = $this->tester->grabFixture('user', 0);
$form = new ResetPasswordForm($user['password_reset_token']);
verify($form->resetPassword())->notEmpty();
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace frontend\tests\unit\models;
use common\fixtures\UserFixture;
use frontend\models\SignupForm;
class SignupFormTest extends \Codeception\Test\Unit
{
/**
* @var \frontend\tests\UnitTester
*/
protected $tester;
public function _before()
{
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::class,
'dataFile' => codecept_data_dir() . 'user.php'
]
]);
}
public function testCorrectSignup()
{
$model = new SignupForm([
'username' => 'some_username',
'email' => 'some_email@example.com',
'password' => 'some_password',
]);
$user = $model->signup();
verify($user)->notEmpty();
/** @var \common\models\User $user */
$user = $this->tester->grabRecord('common\models\User', [
'username' => 'some_username',
'email' => 'some_email@example.com',
'status' => \common\models\User::STATUS_INACTIVE
]);
$this->tester->seeEmailIsSent();
$mail = $this->tester->grabLastSentEmail();
verify($mail)->instanceOf('yii\mail\MessageInterface');
verify($mail->getTo())->arrayHasKey('some_email@example.com');
verify($mail->getFrom())->arrayHasKey(\Yii::$app->params['supportEmail']);
verify($mail->getSubject())->equals('Account registration at ' . \Yii::$app->name);
verify($mail->toString())->stringContainsString($user->verification_token);
}
public function testNotCorrectSignup()
{
$model = new SignupForm([
'username' => 'troy.becker',
'email' => 'nicolas.dianna@hotmail.com',
'password' => 'some_password',
]);
verify($model->signup())->empty();
verify($model->getErrors('username'))->notEmpty();
verify($model->getErrors('email'))->notEmpty();
verify($model->getFirstError('username'))
->equals('This username has already been taken.');
verify($model->getFirstError('email'))
->equals('This email address has already been taken.');
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace frontend\tests\unit\models;
use common\fixtures\UserFixture;
use frontend\models\VerifyEmailForm;
class VerifyEmailFormTest extends \Codeception\Test\Unit
{
/**
* @var \frontend\tests\UnitTester
*/
protected $tester;
public function _before()
{
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::class,
'dataFile' => codecept_data_dir() . 'user.php'
]
]);
}
public function testVerifyWrongToken()
{
$this->tester->expectThrowable('\yii\base\InvalidArgumentException', function() {
new VerifyEmailForm('');
});
$this->tester->expectThrowable('\yii\base\InvalidArgumentException', function() {
new VerifyEmailForm('notexistingtoken_1391882543');
});
}
public function testAlreadyActivatedToken()
{
$this->tester->expectThrowable('\yii\base\InvalidArgumentException', function() {
new VerifyEmailForm('already_used_token_1548675330');
});
}
public function testVerifyCorrectToken()
{
$model = new VerifyEmailForm('4ch0qbfhvWwkcuWqjN8SWRq72SOw1KYT_1548675330');
$user = $model->verifyEmail();
verify($user)->instanceOf('common\models\User');
verify($user->username)->equals('test.test');
verify($user->email)->equals('test@mail.com');
verify($user->status)->equals(\common\models\User::STATUS_ACTIVE);
verify($user->validatePassword('Test1234'))->true();
}
}