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

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 <?php
namespace api\controllers; namespace api\controllers;
use api\models\ChangePasswordForm; use api\models\profile\ChangePasswordForm;
use api\models\ChangeUsernameForm; use api\models\profile\ChangeUsernameForm;
use common\models\Account; use common\models\Account;
use Yii; use Yii;
use yii\filters\AccessControl; use yii\filters\AccessControl;

View File

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

View File

@ -1,9 +1,9 @@
<?php <?php
namespace api\controllers; namespace api\controllers;
use api\models\ConfirmEmailForm; use api\models\authentication\ConfirmEmailForm;
use api\models\RepeatAccountActivationForm; use api\models\authentication\RepeatAccountActivationForm;
use api\models\RegistrationForm; use api\models\authentication\RegistrationForm;
use Yii; use Yii;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\helpers\ArrayHelper; 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 <?php
namespace api\models; namespace api\models\authentication;
use api\models\base\KeyConfirmationForm; use api\models\base\KeyConfirmationForm;
use common\models\Account; use common\models\Account;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
<?php <?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 Codeception\Specify;
use common\models\Account; use common\models\Account;
use common\models\EmailActivation; use common\models\EmailActivation;
@ -10,7 +10,7 @@ use tests\codeception\common\fixtures\EmailActivationFixture;
use Yii; use Yii;
/** /**
* @property array $emailActivations * @property EmailActivationFixture $emailActivations
*/ */
class ConfirmEmailFormTest extends DbTestCase { class ConfirmEmailFormTest extends DbTestCase {
use Specify; use Specify;

View File

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

View File

@ -1,18 +1,18 @@
<?php <?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 Codeception\Specify;
use common\models\Account; use common\models\Account;
use tests\codeception\api\unit\DbTestCase; use tests\codeception\api\unit\TestCase;
use Yii; use Yii;
class LoginFormTest extends DbTestCase { class LoginFormTest extends TestCase {
use Specify; use Specify;
public function testValidateLogin() { public function testValidateLogin() {
$this->specify('error.login_not_exist if login not exists', function() { $this->specify('error.login_not_exist if login not exists', function () {
$model = new DummyLoginForm([ $model = $this->createModel([
'login' => 'mr-test', 'login' => 'mr-test',
'account' => null, 'account' => null,
]); ]);
@ -20,8 +20,8 @@ class LoginFormTest extends DbTestCase {
expect($model->getErrors('login'))->equals(['error.login_not_exist']); expect($model->getErrors('login'))->equals(['error.login_not_exist']);
}); });
$this->specify('no errors if login exists', function() { $this->specify('no errors if login exists', function () {
$model = new DummyLoginForm([ $model = $this->createModel([
'login' => 'mr-test', 'login' => 'mr-test',
'account' => new Account(), 'account' => new Account(),
]); ]);
@ -31,8 +31,8 @@ class LoginFormTest extends DbTestCase {
} }
public function testValidatePassword() { public function testValidatePassword() {
$this->specify('error.password_incorrect if password invalid', function() { $this->specify('error.password_incorrect if password invalid', function () {
$model = new DummyLoginForm([ $model = $this->createModel([
'password' => '87654321', 'password' => '87654321',
'account' => new Account(['password' => '12345678']), 'account' => new Account(['password' => '12345678']),
]); ]);
@ -40,8 +40,8 @@ class LoginFormTest extends DbTestCase {
expect($model->getErrors('password'))->equals(['error.password_incorrect']); expect($model->getErrors('password'))->equals(['error.password_incorrect']);
}); });
$this->specify('no errors if password valid', function() { $this->specify('no errors if password valid', function () {
$model = new DummyLoginForm([ $model = $this->createModel([
'password' => '12345678', 'password' => '12345678',
'account' => new Account(['password' => '12345678']), 'account' => new Account(['password' => '12345678']),
]); ]);
@ -51,16 +51,16 @@ class LoginFormTest extends DbTestCase {
} }
public function testValidateActivity() { public function testValidateActivity() {
$this->specify('error.account_not_activated if account in not activated state', function() { $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]), 'account' => new Account(['status' => Account::STATUS_REGISTERED]),
]); ]);
$model->validateActivity('login'); $model->validateActivity('login');
expect($model->getErrors('login'))->equals(['error.account_not_activated']); expect($model->getErrors('login'))->equals(['error.account_not_activated']);
}); });
$this->specify('no errors if account active', function() { $this->specify('no errors if account active', function () {
$model = new DummyLoginForm([ $model = $this->createModel([
'account' => new Account(['status' => Account::STATUS_ACTIVE]), 'account' => new Account(['status' => Account::STATUS_ACTIVE]),
]); ]);
$model->validateActivity('login'); $model->validateActivity('login');
@ -70,7 +70,7 @@ class LoginFormTest extends DbTestCase {
public function testLogin() { public function testLogin() {
$this->specify('user should be able to login with correct username and password', function () { $this->specify('user should be able to login with correct username and password', function () {
$model = new DummyLoginForm([ $model = $this->createModel([
'login' => 'erickskrauch', 'login' => 'erickskrauch',
'password' => '12345678', 'password' => '12345678',
'account' => new Account([ 'account' => new Account([
@ -84,18 +84,22 @@ class LoginFormTest extends DbTestCase {
}); });
} }
} /**
* @param array $params
* @return LoginForm
*/
private function createModel(array $params = []) {
return new class($params) extends LoginForm {
private $_account;
class DummyLoginForm extends LoginForm { public function setAccount($value) {
$this->_account = $value;
}
private $_account; public function getAccount() {
return $this->_account;
public function setAccount($value) { }
$this->_account = $value; };
}
public function getAccount() {
return $this->_account;
} }
} }

View File

@ -1,7 +1,7 @@
<?php <?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 Codeception\Specify;
use common\models\Account; use common\models\Account;
use common\models\EmailActivation; use common\models\EmailActivation;
@ -10,7 +10,7 @@ use tests\codeception\common\fixtures\EmailActivationFixture;
use Yii; use Yii;
/** /**
* @property array $emailActivations * @property EmailActivationFixture $emailActivations
*/ */
class RecoverPasswordFormTest extends DbTestCase { class RecoverPasswordFormTest extends DbTestCase {
use Specify; use Specify;

View File

@ -1,7 +1,7 @@
<?php <?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 Codeception\Specify;
use common\models\Account; use common\models\Account;
use common\models\EmailActivation; use common\models\EmailActivation;

View File

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

View File

@ -1,7 +1,7 @@
<?php <?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 Codeception\Specify;
use common\models\Account; use common\models\Account;
use tests\codeception\api\unit\DbTestCase; use tests\codeception\api\unit\DbTestCase;

View File

@ -1,7 +1,7 @@
<?php <?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 Codeception\Specify;
use common\models\Account; use common\models\Account;
use common\models\UsernameHistory; use common\models\UsernameHistory;