From 0ba1be27e866dd5f4983fe0dfeb410340593f651 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sat, 14 May 2016 02:47:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=B2=D0=B5=D0=B4=D1=91=D0=BD=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=80=D1=8F=D0=B4=D0=BE=D0=BA=20=D0=B2=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=D0=BB=D1=8F=D1=85=20=D0=BF=D1=80=D0=BE=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controllers/AccountsController.php | 4 +- api/controllers/AuthenticationController.php | 6 +- api/controllers/SignupController.php | 6 +- api/models/ContactForm.php | 59 ----------------- api/models/ResetPasswordForm.php | 65 ------------------- .../{ => authentication}/ConfirmEmailForm.php | 2 +- .../ForgotPasswordForm.php | 2 +- api/models/{ => authentication}/LoginForm.php | 2 +- .../RecoverPasswordForm.php | 2 +- .../{ => authentication}/RegistrationForm.php | 3 +- .../RepeatAccountActivationForm.php | 2 +- .../{ => profile}/ChangePasswordForm.php | 2 +- .../{ => profile}/ChangeUsernameForm.php | 2 +- .../ConfirmEmailFormTest.php | 6 +- .../ForgotPasswordFormTest.php | 32 +++++---- .../{ => authentication}/LoginFormTest.php | 58 +++++++++-------- .../RecoverPasswordFormTest.php | 6 +- .../RegistrationFormTest.php | 4 +- .../RepeatAccountActivationFormTest.php | 26 ++++---- .../{ => profile}/ChangePasswordFormTest.php | 4 +- .../{ => profile}/ChangeUsernameFormTest.php | 4 +- 21 files changed, 93 insertions(+), 204 deletions(-) delete mode 100644 api/models/ContactForm.php delete mode 100644 api/models/ResetPasswordForm.php rename api/models/{ => authentication}/ConfirmEmailForm.php (97%) rename api/models/{ => authentication}/ForgotPasswordForm.php (99%) rename api/models/{ => authentication}/LoginForm.php (98%) rename api/models/{ => authentication}/RecoverPasswordForm.php (98%) rename api/models/{ => authentication}/RegistrationForm.php (98%) rename api/models/{ => authentication}/RepeatAccountActivationForm.php (98%) rename api/models/{ => profile}/ChangePasswordForm.php (98%) rename api/models/{ => profile}/ChangeUsernameForm.php (98%) rename tests/codeception/api/unit/models/{ => authentication}/ConfirmEmailFormTest.php (90%) rename tests/codeception/api/unit/models/{ => authentication}/ForgotPasswordFormTest.php (88%) rename tests/codeception/api/unit/models/{ => authentication}/LoginFormTest.php (68%) rename tests/codeception/api/unit/models/{ => authentication}/RecoverPasswordFormTest.php (89%) rename tests/codeception/api/unit/models/{ => authentication}/RegistrationFormTest.php (97%) rename tests/codeception/api/unit/models/{ => authentication}/RepeatAccountActivationFormTest.php (87%) rename tests/codeception/api/unit/models/{ => profile}/ChangePasswordFormTest.php (97%) rename tests/codeception/api/unit/models/{ => profile}/ChangeUsernameFormTest.php (98%) diff --git a/api/controllers/AccountsController.php b/api/controllers/AccountsController.php index fefc292..8e590e7 100644 --- a/api/controllers/AccountsController.php +++ b/api/controllers/AccountsController.php @@ -1,8 +1,8 @@ '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(); - } -} diff --git a/api/models/ResetPasswordForm.php b/api/models/ResetPasswordForm.php deleted file mode 100644 index a8ea443..0000000 --- a/api/models/ResetPasswordForm.php +++ /dev/null @@ -1,65 +0,0 @@ -_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); - } -} diff --git a/api/models/ConfirmEmailForm.php b/api/models/authentication/ConfirmEmailForm.php similarity index 97% rename from api/models/ConfirmEmailForm.php rename to api/models/authentication/ConfirmEmailForm.php index 7728278..c555e2d 100644 --- a/api/models/ConfirmEmailForm.php +++ b/api/models/authentication/ConfirmEmailForm.php @@ -1,5 +1,5 @@ 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'; } -} + /** + * @param array $params + * @return ForgotPasswordForm + */ + private function createModel(array $params = []) { + return new class($params) extends ForgotPasswordForm { + public $key; -class DummyForgotPasswordForm extends ForgotPasswordForm { - - public $key; - - public function getEmailActivation() { - return EmailActivation::findOne($this->key); + public function getEmailActivation() { + return EmailActivation::findOne($this->key); + } + }; } } diff --git a/tests/codeception/api/unit/models/LoginFormTest.php b/tests/codeception/api/unit/models/authentication/LoginFormTest.php similarity index 68% rename from tests/codeception/api/unit/models/LoginFormTest.php rename to tests/codeception/api/unit/models/authentication/LoginFormTest.php index 71af2d1..f1c131e 100644 --- a/tests/codeception/api/unit/models/LoginFormTest.php +++ b/tests/codeception/api/unit/models/authentication/LoginFormTest.php @@ -1,18 +1,18 @@ specify('error.login_not_exist if login not exists', function() { - $model = new DummyLoginForm([ + $this->specify('error.login_not_exist if login not exists', function () { + $model = $this->createModel([ 'login' => 'mr-test', 'account' => null, ]); @@ -20,8 +20,8 @@ class LoginFormTest extends DbTestCase { expect($model->getErrors('login'))->equals(['error.login_not_exist']); }); - $this->specify('no errors if login exists', function() { - $model = new DummyLoginForm([ + $this->specify('no errors if login exists', function () { + $model = $this->createModel([ 'login' => 'mr-test', 'account' => new Account(), ]); @@ -31,8 +31,8 @@ class LoginFormTest extends DbTestCase { } public function testValidatePassword() { - $this->specify('error.password_incorrect if password invalid', function() { - $model = new DummyLoginForm([ + $this->specify('error.password_incorrect if password invalid', function () { + $model = $this->createModel([ 'password' => '87654321', 'account' => new Account(['password' => '12345678']), ]); @@ -40,8 +40,8 @@ class LoginFormTest extends DbTestCase { expect($model->getErrors('password'))->equals(['error.password_incorrect']); }); - $this->specify('no errors if password valid', function() { - $model = new DummyLoginForm([ + $this->specify('no errors if password valid', function () { + $model = $this->createModel([ 'password' => '12345678', 'account' => new Account(['password' => '12345678']), ]); @@ -51,16 +51,16 @@ class LoginFormTest extends DbTestCase { } public function testValidateActivity() { - $this->specify('error.account_not_activated if account in not activated state', function() { - $model = new DummyLoginForm([ + $this->specify('error.account_not_activated if account in not activated state', function () { + $model = $this->createModel([ 'account' => new Account(['status' => Account::STATUS_REGISTERED]), ]); $model->validateActivity('login'); expect($model->getErrors('login'))->equals(['error.account_not_activated']); }); - $this->specify('no errors if account active', function() { - $model = new DummyLoginForm([ + $this->specify('no errors if account active', function () { + $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,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 setAccount($value) { - $this->_account = $value; - } - - public function getAccount() { - return $this->_account; + public function getAccount() { + return $this->_account; + } + }; } } diff --git a/tests/codeception/api/unit/models/RecoverPasswordFormTest.php b/tests/codeception/api/unit/models/authentication/RecoverPasswordFormTest.php similarity index 89% rename from tests/codeception/api/unit/models/RecoverPasswordFormTest.php rename to tests/codeception/api/unit/models/authentication/RecoverPasswordFormTest.php index 1409ad3..cf5faba 100644 --- a/tests/codeception/api/unit/models/RecoverPasswordFormTest.php +++ b/tests/codeception/api/unit/models/authentication/RecoverPasswordFormTest.php @@ -1,7 +1,7 @@ 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'; } -} + /** + * @param array $params + * @return RepeatAccountActivationForm + */ + private function createModel(array $params = []) { + return new class($params) extends RepeatAccountActivationForm { + public $emailKey; -class DummyRepeatAccountActivationForm extends RepeatAccountActivationForm { - - public $emailKey; - - public function getActivation() { - return EmailActivation::findOne($this->emailKey); + public function getActivation() { + return EmailActivation::findOne($this->emailKey); + } + }; } } diff --git a/tests/codeception/api/unit/models/ChangePasswordFormTest.php b/tests/codeception/api/unit/models/profile/ChangePasswordFormTest.php similarity index 97% rename from tests/codeception/api/unit/models/ChangePasswordFormTest.php rename to tests/codeception/api/unit/models/profile/ChangePasswordFormTest.php index acc310d..3c9665b 100644 --- a/tests/codeception/api/unit/models/ChangePasswordFormTest.php +++ b/tests/codeception/api/unit/models/profile/ChangePasswordFormTest.php @@ -1,7 +1,7 @@