first commit
This commit is contained in:
41
tests/unit/models/ContactFormTest.php
Normal file
41
tests/unit/models/ContactFormTest.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace tests\unit\models;
|
||||
|
||||
use app\models\ContactForm;
|
||||
use yii\mail\MessageInterface;
|
||||
|
||||
class ContactFormTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
public $tester;
|
||||
|
||||
public function testEmailIsSentOnContact()
|
||||
{
|
||||
$model = new ContactForm();
|
||||
|
||||
$model->attributes = [
|
||||
'name' => 'Tester',
|
||||
'email' => 'tester@example.com',
|
||||
'subject' => 'very important letter subject',
|
||||
'body' => 'body of current message',
|
||||
'verifyCode' => 'testme',
|
||||
];
|
||||
|
||||
verify($model->contact('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');
|
||||
}
|
||||
}
|
||||
51
tests/unit/models/LoginFormTest.php
Normal file
51
tests/unit/models/LoginFormTest.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace tests\unit\models;
|
||||
|
||||
use app\models\LoginForm;
|
||||
|
||||
class LoginFormTest extends \Codeception\Test\Unit
|
||||
{
|
||||
private $model;
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
\Yii::$app->user->logout();
|
||||
}
|
||||
|
||||
public function testLoginNoUser()
|
||||
{
|
||||
$this->model = new LoginForm([
|
||||
'username' => 'not_existing_username',
|
||||
'password' => 'not_existing_password',
|
||||
]);
|
||||
|
||||
verify($this->model->login())->false();
|
||||
verify(\Yii::$app->user->isGuest)->true();
|
||||
}
|
||||
|
||||
public function testLoginWrongPassword()
|
||||
{
|
||||
$this->model = new LoginForm([
|
||||
'username' => 'demo',
|
||||
'password' => 'wrong_password',
|
||||
]);
|
||||
|
||||
verify($this->model->login())->false();
|
||||
verify(\Yii::$app->user->isGuest)->true();
|
||||
verify($this->model->errors)->arrayHasKey('password');
|
||||
}
|
||||
|
||||
public function testLoginCorrect()
|
||||
{
|
||||
$this->model = new LoginForm([
|
||||
'username' => 'demo',
|
||||
'password' => 'demo',
|
||||
]);
|
||||
|
||||
verify($this->model->login())->true();
|
||||
verify(\Yii::$app->user->isGuest)->false();
|
||||
verify($this->model->errors)->arrayHasNotKey('password');
|
||||
}
|
||||
|
||||
}
|
||||
44
tests/unit/models/UserTest.php
Normal file
44
tests/unit/models/UserTest.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace tests\unit\models;
|
||||
|
||||
use app\models\User;
|
||||
|
||||
class UserTest extends \Codeception\Test\Unit
|
||||
{
|
||||
public function testFindUserById()
|
||||
{
|
||||
verify($user = User::findIdentity(100))->notEmpty();
|
||||
verify($user->username)->equals('admin');
|
||||
|
||||
verify(User::findIdentity(999))->empty();
|
||||
}
|
||||
|
||||
public function testFindUserByAccessToken()
|
||||
{
|
||||
verify($user = User::findIdentityByAccessToken('100-token'))->notEmpty();
|
||||
verify($user->username)->equals('admin');
|
||||
|
||||
verify(User::findIdentityByAccessToken('non-existing'))->empty();
|
||||
}
|
||||
|
||||
public function testFindUserByUsername()
|
||||
{
|
||||
verify($user = User::findByUsername('admin'))->notEmpty();
|
||||
verify(User::findByUsername('not-admin'))->empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testFindUserByUsername
|
||||
*/
|
||||
public function testValidateUser()
|
||||
{
|
||||
$user = User::findByUsername('admin');
|
||||
verify($user->validateAuthKey('test100key'))->notEmpty();
|
||||
verify($user->validateAuthKey('test102key'))->empty();
|
||||
|
||||
verify($user->validatePassword('admin'))->notEmpty();
|
||||
verify($user->validatePassword('123456'))->empty();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user