Наведён порядок в моделях проекта

This commit is contained in:
ErickSkrauch 2016-05-14 02:47:17 +03:00
parent ecd92b8fda
commit 0ba1be27e8
21 changed files with 93 additions and 204 deletions

View File

@ -1,8 +1,8 @@
<?php
namespace api\controllers;
use api\models\ChangePasswordForm;
use api\models\ChangeUsernameForm;
use api\models\profile\ChangePasswordForm;
use api\models\profile\ChangeUsernameForm;
use common\models\Account;
use Yii;
use yii\filters\AccessControl;

View File

@ -1,9 +1,9 @@
<?php
namespace api\controllers;
use api\models\ForgotPasswordForm;
use api\models\LoginForm;
use api\models\RecoverPasswordForm;
use api\models\authentication\ForgotPasswordForm;
use api\models\authentication\LoginForm;
use api\models\authentication\RecoverPasswordForm;
use common\helpers\StringHelper;
use Yii;
use yii\filters\AccessControl;

View File

@ -1,9 +1,9 @@
<?php
namespace api\controllers;
use api\models\ConfirmEmailForm;
use api\models\RepeatAccountActivationForm;
use api\models\RegistrationForm;
use api\models\authentication\ConfirmEmailForm;
use api\models\authentication\RepeatAccountActivationForm;
use api\models\authentication\RegistrationForm;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;

View File

@ -1,59 +0,0 @@
<?php
namespace api\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* @inheritdoc
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
*
* @param string $email the target email address
* @return boolean whether the email was sent
*/
public function sendEmail($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
}
}

View File

@ -1,65 +0,0 @@
<?php
namespace api\models;
use common\models\Account;
use yii\base\InvalidParamException;
use yii\base\Model;
use Yii;
/**
* Password reset form
*/
class ResetPasswordForm extends Model
{
public $password;
/**
* @var \common\models\Account
*/
private $_user;
/**
* Creates a form model given a token.
*
* @param string $token
* @param array $config name-value pairs that will be used to initialize the object properties
* @throws \yii\base\InvalidParamException if token is empty or not valid
*/
public function __construct($token, $config = [])
{
if (empty($token) || !is_string($token)) {
throw new InvalidParamException('Password reset token cannot be blank.');
}
$this->_user = Account::findByPasswordResetToken($token);
if (!$this->_user) {
throw new InvalidParamException('Wrong password reset token.');
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* Resets password.
*
* @return boolean if password was reset.
*/
public function resetPassword()
{
$user = $this->_user;
$user->setPassword($this->password);
$user->removePasswordResetToken();
return $user->save(false);
}
}

View File

@ -1,5 +1,5 @@
<?php
namespace api\models;
namespace api\models\authentication;
use api\models\base\KeyConfirmationForm;
use common\models\Account;

View File

@ -1,5 +1,5 @@
<?php
namespace api\models;
namespace api\models\authentication;
use api\models\base\ApiForm;
use api\traits\AccountFinder;

View File

@ -1,5 +1,5 @@
<?php
namespace api\models;
namespace api\models\authentication;
use api\models\base\ApiForm;
use api\traits\AccountFinder;

View File

@ -1,5 +1,5 @@
<?php
namespace api\models;
namespace api\models\authentication;
use api\models\base\KeyConfirmationForm;
use common\models\EmailActivation;

View File

@ -1,8 +1,9 @@
<?php
namespace api\models;
namespace api\models\authentication;
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
use api\models\base\ApiForm;
use api\models\profile\ChangeUsernameForm;
use common\components\UserFriendlyRandomKey;
use common\models\Account;
use common\models\confirmations\RegistrationConfirmation;

View File

@ -1,5 +1,5 @@
<?php
namespace api\models;
namespace api\models\authentication;
use api\models\base\ApiForm;
use common\components\UserFriendlyRandomKey;

View File

@ -1,5 +1,5 @@
<?php
namespace api\models;
namespace api\models\profile;
use api\models\base\PasswordProtectedForm;
use common\models\Account;

View File

@ -1,5 +1,5 @@
<?php
namespace api\models;
namespace api\models\profile;
use api\models\base\PasswordProtectedForm;
use common\helpers\Amqp;

View File

@ -1,7 +1,7 @@
<?php
namespace tests\codeception\api\models;
namespace tests\codeception\api\models\authentication;
use api\models\ConfirmEmailForm;
use api\models\authentication\ConfirmEmailForm;
use Codeception\Specify;
use common\models\Account;
use common\models\EmailActivation;
@ -10,7 +10,7 @@ use tests\codeception\common\fixtures\EmailActivationFixture;
use Yii;
/**
* @property array $emailActivations
* @property EmailActivationFixture $emailActivations
*/
class ConfirmEmailFormTest extends DbTestCase {
use Specify;

View File

@ -1,7 +1,7 @@
<?php
namespace codeception\api\unit\models;
namespace codeception\api\unit\models\authentication;
use api\models\ForgotPasswordForm;
use api\models\authentication\ForgotPasswordForm;
use Codeception\Specify;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
@ -10,8 +10,8 @@ use tests\codeception\common\fixtures\EmailActivationFixture;
use Yii;
/**
* @property array $accounts
* @property array $emailActivations
* @property AccountFixture $accounts
* @property EmailActivationFixture $emailActivations
*/
class ForgotPasswordFormTest extends DbTestCase {
use Specify;
@ -76,7 +76,7 @@ class ForgotPasswordFormTest extends DbTestCase {
public function testValidateFrequency() {
$this->specify('error.account_not_activated if recently was message', function() {
$model = new DummyForgotPasswordForm([
$model = $this->createModel([
'login' => $this->accounts['admin']['username'],
'key' => $this->emailActivations['freshPasswordRecovery']['key'],
]);
@ -86,7 +86,7 @@ class ForgotPasswordFormTest extends DbTestCase {
});
$this->specify('empty errors if email was sent a long time ago', function() {
$model = new DummyForgotPasswordForm([
$model = $this->createModel([
'login' => $this->accounts['admin']['username'],
'key' => $this->emailActivations['oldPasswordRecovery']['key'],
]);
@ -96,7 +96,7 @@ class ForgotPasswordFormTest extends DbTestCase {
});
$this->specify('empty errors if previous confirmation model not founded', function() {
$model = new DummyForgotPasswordForm([
$model = $this->createModel([
'login' => $this->accounts['admin']['username'],
'key' => 'invalid-key',
]);
@ -136,14 +136,18 @@ class ForgotPasswordFormTest extends DbTestCase {
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
}
}
class DummyForgotPasswordForm extends ForgotPasswordForm {
/**
* @param array $params
* @return ForgotPasswordForm
*/
private function createModel(array $params = []) {
return new class($params) extends ForgotPasswordForm {
public $key;
public function getEmailActivation() {
return EmailActivation::findOne($this->key);
}
};
}
}

View File

@ -1,18 +1,18 @@
<?php
namespace tests\codeception\api\models;
namespace tests\codeception\api\models\authentication;
use api\models\LoginForm;
use api\models\authentication\LoginForm;
use Codeception\Specify;
use common\models\Account;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\api\unit\TestCase;
use Yii;
class LoginFormTest extends DbTestCase {
class LoginFormTest extends TestCase {
use Specify;
public function testValidateLogin() {
$this->specify('error.login_not_exist if login not exists', function () {
$model = new DummyLoginForm([
$model = $this->createModel([
'login' => 'mr-test',
'account' => null,
]);
@ -21,7 +21,7 @@ class LoginFormTest extends DbTestCase {
});
$this->specify('no errors if login exists', function () {
$model = new DummyLoginForm([
$model = $this->createModel([
'login' => 'mr-test',
'account' => new Account(),
]);
@ -32,7 +32,7 @@ class LoginFormTest extends DbTestCase {
public function testValidatePassword() {
$this->specify('error.password_incorrect if password invalid', function () {
$model = new DummyLoginForm([
$model = $this->createModel([
'password' => '87654321',
'account' => new Account(['password' => '12345678']),
]);
@ -41,7 +41,7 @@ class LoginFormTest extends DbTestCase {
});
$this->specify('no errors if password valid', function () {
$model = new DummyLoginForm([
$model = $this->createModel([
'password' => '12345678',
'account' => new Account(['password' => '12345678']),
]);
@ -52,7 +52,7 @@ class LoginFormTest extends DbTestCase {
public function testValidateActivity() {
$this->specify('error.account_not_activated if account in not activated state', function () {
$model = new DummyLoginForm([
$model = $this->createModel([
'account' => new Account(['status' => Account::STATUS_REGISTERED]),
]);
$model->validateActivity('login');
@ -60,7 +60,7 @@ class LoginFormTest extends DbTestCase {
});
$this->specify('no errors if account active', function () {
$model = new DummyLoginForm([
$model = $this->createModel([
'account' => new Account(['status' => Account::STATUS_ACTIVE]),
]);
$model->validateActivity('login');
@ -70,7 +70,7 @@ class LoginFormTest extends DbTestCase {
public function testLogin() {
$this->specify('user should be able to login with correct username and password', function () {
$model = new DummyLoginForm([
$model = $this->createModel([
'login' => 'erickskrauch',
'password' => '12345678',
'account' => new Account([
@ -84,10 +84,12 @@ class LoginFormTest extends DbTestCase {
});
}
}
class DummyLoginForm extends LoginForm {
/**
* @param array $params
* @return LoginForm
*/
private function createModel(array $params = []) {
return new class($params) extends LoginForm {
private $_account;
public function setAccount($value) {
@ -97,5 +99,7 @@ class DummyLoginForm extends LoginForm {
public function getAccount() {
return $this->_account;
}
};
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace tests\codeception\api\models;
namespace tests\codeception\api\models\authentication;
use api\models\RecoverPasswordForm;
use api\models\authentication\RecoverPasswordForm;
use Codeception\Specify;
use common\models\Account;
use common\models\EmailActivation;
@ -10,7 +10,7 @@ use tests\codeception\common\fixtures\EmailActivationFixture;
use Yii;
/**
* @property array $emailActivations
* @property EmailActivationFixture $emailActivations
*/
class RecoverPasswordFormTest extends DbTestCase {
use Specify;

View File

@ -1,7 +1,7 @@
<?php
namespace tests\codeception\api\models;
namespace tests\codeception\api\models\authentication;
use api\models\RegistrationForm;
use api\models\authentication\RegistrationForm;
use Codeception\Specify;
use common\models\Account;
use common\models\EmailActivation;

View File

@ -1,7 +1,7 @@
<?php
namespace tests\codeception\api\models;
namespace tests\codeception\api\models\authentication;
use api\models\RepeatAccountActivationForm;
use api\models\authentication\RepeatAccountActivationForm;
use Codeception\Specify;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
@ -68,7 +68,7 @@ class RepeatAccountActivationFormTest extends DbTestCase {
public function testValidateExistsActivation() {
$this->specify('error.recently_sent_message if passed email has recently sent message', function() {
$model = new DummyRepeatAccountActivationForm([
$model = $this->createModel([
'emailKey' => $this->activations['freshRegistrationConfirmation']['key'],
]);
$model->validateExistsActivation('email');
@ -76,7 +76,7 @@ class RepeatAccountActivationFormTest extends DbTestCase {
});
$this->specify('no errors if passed email has expired activation message', function() {
$model = new DummyRepeatAccountActivationForm([
$model = $this->createModel([
'emailKey' => $this->activations['oldRegistrationConfirmation']['key'],
]);
$model->validateExistsActivation('email');
@ -107,14 +107,18 @@ class RepeatAccountActivationFormTest extends DbTestCase {
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
}
}
class DummyRepeatAccountActivationForm extends RepeatAccountActivationForm {
/**
* @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);
}
};
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace tests\codeception\api\models;
namespace tests\codeception\api\models\profile;
use api\models\ChangePasswordForm;
use api\models\profile\ChangePasswordForm;
use Codeception\Specify;
use common\models\Account;
use tests\codeception\api\unit\DbTestCase;

View File

@ -1,7 +1,7 @@
<?php
namespace tests\codeception\api\models;
namespace tests\codeception\api\models\profile;
use api\models\ChangeUsernameForm;
use api\models\profile\ChangeUsernameForm;
use Codeception\Specify;
use common\models\Account;
use common\models\UsernameHistory;