fixed all tests

This commit is contained in:
Antonio Ramirez
2016-12-11 21:34:22 +01:00
parent 4588d77129
commit 4c9b9671b5
37 changed files with 214 additions and 135 deletions

View File

@ -17,19 +17,4 @@ modules:
dump: tests/_data/schema.sql dump: tests/_data/schema.sql
Yii2: Yii2:
configFile: 'tests/_app/config/test.php' configFile: 'tests/_app/config/test.php'
coverage: cleanup: false
enabled: true
include:
- ../lib/User/AuthClient/*
- ../controllers/*
- ../events/*
- ../filters/*
- ../helpers/*
- ../models/*
- ../traits/*
- ../views/*
- ../widgets/*
- ../Bootstrap.php
- ../Module.php
- ../Mailer.php
- ../Finder.php

View File

@ -105,7 +105,7 @@ class AdminController extends Controller
public function actionCreate() public function actionCreate()
{ {
/** @var User $user */ /** @var User $user */
$user = $this->make(User::class, ['scenario' => 'create']); $user = $this->make(User::class, [], ['scenario' => 'create']);
/** @var UserEvent $event */ /** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]); $event = $this->make(UserEvent::class, [$user]);
@ -168,7 +168,7 @@ class AdminController extends Controller
$this->make(AjaxRequestModelValidator::class, [$user])->validate(); $this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($profile->load(Yii::$app->request->post())) { if ($profile->load(Yii::$app->request->post())) {
if($profile->save()) { if ($profile->save()) {
$this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event); $this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event);
Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Profile details have been updated')); Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Profile details have been updated'));
$this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event); $this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event);

View File

@ -74,7 +74,7 @@ class RecoveryController extends Controller
} }
/** @var RecoveryForm $form */ /** @var RecoveryForm $form */
$form = $this->make(RecoveryForm::class, ['scenario' => RecoveryForm::SCENARIO_REQUEST]); $form = $this->make(RecoveryForm::class, [], ['scenario' => RecoveryForm::SCENARIO_REQUEST]);
$event = $this->make(FormEvent::class, [$form]); $event = $this->make(FormEvent::class, [$form]);
@ -87,6 +87,7 @@ class RecoveryController extends Controller
$mailService = MailFactory::makeRecoveryMailerService($form->email); $mailService = MailFactory::makeRecoveryMailerService($form->email);
if ($this->make(PasswordRecoveryService::class, [$form->email, $mailService])->run()) { if ($this->make(PasswordRecoveryService::class, [$form->email, $mailService])->run()) {
$this->trigger(FormEvent::EVENT_AFTER_REQUEST, $event); $this->trigger(FormEvent::EVENT_AFTER_REQUEST, $event);
return $this->render( return $this->render(
@ -139,7 +140,7 @@ class RecoveryController extends Controller
} }
/** @var RecoveryForm $form */ /** @var RecoveryForm $form */
$form = $this->make(RecoveryForm::class, ['scenario' => RecoveryForm::SCENARIO_RESET]); $form = $this->make(RecoveryForm::class, [], ['scenario' => RecoveryForm::SCENARIO_RESET]);
$event = $event->updateForm($form); $event = $event->updateForm($form);
$this->make(AjaxRequestModelValidator::class, [$form])->validate(); $this->make(AjaxRequestModelValidator::class, [$form])->validate();

View File

@ -12,13 +12,11 @@ use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery; use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Query\UserQuery; use Da\User\Query\UserQuery;
use Da\User\Service\AccountConfirmationService; use Da\User\Service\AccountConfirmationService;
use Da\User\Service\EmailConfirmationService;
use Da\User\Service\ResendConfirmationService; use Da\User\Service\ResendConfirmationService;
use Da\User\Service\UserConfirmationService; use Da\User\Service\UserConfirmationService;
use Da\User\Service\UserCreateService; use Da\User\Service\UserCreateService;
use Da\User\Service\UserRegisterService; use Da\User\Service\UserRegisterService;
use Da\User\Traits\ContainerTrait; use Da\User\Traits\ContainerTrait;
use Da\User\Traits\ModuleTrait;
use Da\User\Validator\AjaxRequestModelValidator; use Da\User\Validator\AjaxRequestModelValidator;
use Yii; use Yii;
use yii\base\Module; use yii\base\Module;
@ -81,7 +79,7 @@ class RegistrationController extends Controller
public function actionRegister() public function actionRegister()
{ {
if(!$this->module->enableRegistration) { if (!$this->module->enableRegistration) {
throw new NotFoundHttpException(); throw new NotFoundHttpException();
} }
/** @var RegistrationForm $form */ /** @var RegistrationForm $form */
@ -91,14 +89,15 @@ class RegistrationController extends Controller
$this->make(AjaxRequestModelValidator::class, [$form])->validate(); $this->make(AjaxRequestModelValidator::class, [$form])->validate();
if($form->load(Yii::$app->request->post()) && $form->validate()) { if ($form->load(Yii::$app->request->post()) && $form->validate()) {
$this->trigger(UserEvent::EVENT_BEFORE_REGISTER, $event); $this->trigger(UserEvent::EVENT_BEFORE_REGISTER, $event);
$user = $this->make(User::class, [$form->attributes]); $user = $this->make(User::class, [], $form->attributes);
$user->setScenario('register'); $user->setScenario('register');
$mailService = MailFactory::makeWelcomeMailerService($user); $mailService = MailFactory::makeWelcomeMailerService($user);
if($this->make(UserRegisterService::class, [$user, $mailService])->run()) { if ($this->make(UserRegisterService::class, [$user, $mailService])->run()) {
Yii::$app->session->setFlash( Yii::$app->session->setFlash(
'info', 'info',
Yii::t( Yii::t(
@ -106,16 +105,24 @@ class RegistrationController extends Controller
'Your account has been created and a message with further instructions has been sent to your email' 'Your account has been created and a message with further instructions has been sent to your email'
) )
); );
return $this->render('/shared/message', [
'title' => Yii::t('user', 'Your account has been created')
]);
}
return $this->render('register', [ return $this->render(
'model' => $form, '/shared/message',
'module' => $this->module, [
]); 'title' => Yii::t('user', 'Your account has been created'),
'module' => $this->module
]
);
}
} }
return $this->render(
'register',
[
'model' => $form,
'module' => $this->module,
]
);
} }
public function actionConnect($code) public function actionConnect($code)
@ -206,29 +213,48 @@ class RegistrationController extends Controller
$this->make(AjaxRequestModelValidator::class, [$form])->validate(); $this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->request->post()) && $form->validate()) { if ($form->load(Yii::$app->request->post()) && $form->validate()) {
$this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event);
/** @var User $user */ /** @var User $user */
$user = $this->userQuery->whereEmail($form->email)->one(); $user = $this->userQuery->whereEmail($form->email)->one();
$mailService = MailFactory::makeConfirmationMailerService($user); $success = true;
if ($this->make(ResendConfirmationService::class, [$user, $mailService])->run()) { if ($user !== null) {
$this->trigger(FormEvent::EVENT_AFTER_RESEND, $event); $this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event);
Yii::$app->session->setFlash( $mailService = MailFactory::makeConfirmationMailerService($user);
'info', if ($success = $this->make(ResendConfirmationService::class, [$user, $mailService])->run()) {
Yii::t( $this->trigger(FormEvent::EVENT_AFTER_RESEND, $event);
'user', Yii::$app->session->setFlash(
'A message has been sent to your email address. It contains a confirmation link that you must 'info',
click to complete registration.' Yii::t(
) 'user',
); 'A message has been sent to your email address. ' .
} else { 'It contains a confirmation link that you must click to complete registration.'
)
);
}
}
if ($user === null || $success === false) {
Yii::$app->session->setFlash( Yii::$app->session->setFlash(
'danger', 'danger',
Yii::t( Yii::t(
'user', 'user',
'We couldn\'t re-send the mail to confirm your address. Please, verify is the correct email.' 'We couldn\'t re-send the mail to confirm your address. ' .
'Please, verify is the correct email or if it has been confirmed already.'
) )
); );
} }
return $this->render('/shared/message', [
'title' => $success
? Yii::t('user', 'A new confirmation link has been sent')
: Yii::t('user', 'Unable to send confirmation link'),
'module' => $this->module,
]);
} }
return $this->render(
'resend',
[
'model' => $form,
]
);
} }
} }

View File

@ -126,6 +126,7 @@ class SettingsController extends Controller
public function actionAccount() public function actionAccount()
{ {
/** @var SettingsForm $form */
$form = $this->make(SettingsForm::class); $form = $this->make(SettingsForm::class);
$event = $this->make(FormEvent::class, [$form]); $event = $this->make(FormEvent::class, [$form]);
@ -133,8 +134,9 @@ class SettingsController extends Controller
if ($form->load(Yii::$app->request->post())) { if ($form->load(Yii::$app->request->post())) {
$this->trigger(UserEvent::EVENT_BEFORE_ACCOUNT_UPDATE, $event); $this->trigger(UserEvent::EVENT_BEFORE_ACCOUNT_UPDATE, $event);
if ($form->save()) { if ($form->save()) {
Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Your account details have been updated-')); Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Your account details have been updated'));
$this->trigger(UserEvent::EVENT_AFTER_ACCOUNT_UPDATE, $event); $this->trigger(UserEvent::EVENT_AFTER_ACCOUNT_UPDATE, $event);
return $this->refresh(); return $this->refresh();

View File

@ -29,7 +29,7 @@ class EmailChangeStrategyFactory
public static function makeByStrategyType($strategy, SettingsForm $form) public static function makeByStrategyType($strategy, SettingsForm $form)
{ {
if (array_key_exists($strategy, static::$map)) { if (array_key_exists($strategy, static::$map)) {
return Yii::createObject(static::$map[$strategy], [$form]); return Yii::$container->get(static::$map[$strategy], [$form]);
} }
throw new Exception('Unknown strategy type'); throw new Exception('Unknown strategy type');
@ -42,7 +42,7 @@ class EmailChangeStrategyFactory
*/ */
public static function makeDefaultEmailChangeStrategy(SettingsForm $form) public static function makeDefaultEmailChangeStrategy(SettingsForm $form)
{ {
return Yii::createObject(static::$map[MailChangeStrategyInterface::TYPE_DEFAULT], [$form]); return Yii::$container->get(static::$map[MailChangeStrategyInterface::TYPE_DEFAULT], [$form]);
} }
/** /**
@ -52,7 +52,7 @@ class EmailChangeStrategyFactory
*/ */
public static function makeInsecureEmailChangeStrategy(SettingsForm $form) public static function makeInsecureEmailChangeStrategy(SettingsForm $form)
{ {
return Yii::createObject(static::$map[MailChangeStrategyInterface::TYPE_INSECURE], [$form]); return Yii::$container->get(static::$map[MailChangeStrategyInterface::TYPE_INSECURE], [$form]);
} }
/** /**
@ -62,6 +62,6 @@ class EmailChangeStrategyFactory
*/ */
public static function makeSecureEmailChangeStrategy(SettingsForm $form) public static function makeSecureEmailChangeStrategy(SettingsForm $form)
{ {
return Yii::createObject(static::$map[MailChangeStrategyInterface::TYPE_SECURE], [$form]); return Yii::$container->get(static::$map[MailChangeStrategyInterface::TYPE_SECURE], [$form]);
} }
} }

View File

@ -109,6 +109,6 @@ class MailFactory
*/ */
public static function makeMailerService($from, $to, $subject, $view, array $params = []) public static function makeMailerService($from, $to, $subject, $view, array $params = [])
{ {
return Yii::$container->get(MailService::class, [$from, $to, $subject, $view, $params]); return Yii::$container->get(MailService::class, [$from, $to, $subject, $view, $params, Yii::$app->getMailer()]);
} }
} }

View File

@ -73,7 +73,7 @@ class TokenFactory
*/ */
protected static function make($userId, $type) protected static function make($userId, $type)
{ {
return Yii::$container->get(Token::class, ['user_id' => $userId, 'type' => $type]); return Yii::createObject(['class' => Token::class, 'user_id' => $userId, 'type' => $type]);
} }
} }

View File

@ -30,7 +30,7 @@ class RecoveryForm extends Model
* @param UserQuery $query * @param UserQuery $query
* @param array $config * @param array $config
*/ */
public function __construct(UserQuery $query, array $config) public function __construct(UserQuery $query, array $config = [])
{ {
$this->query = $query; $this->query = $query;
parent::__construct($config); parent::__construct($config);

View File

@ -37,9 +37,9 @@ class SettingsForm extends Model
protected $securityHelper; protected $securityHelper;
/** @var User */ /** @var User */
private $user; protected $user;
public function __construct(SecurityHelper $securityHelper, array $config) public function __construct(SecurityHelper $securityHelper, array $config = [])
{ {
$this->securityHelper = $securityHelper; $this->securityHelper = $securityHelper;
parent::__construct($config); parent::__construct($config);
@ -62,16 +62,16 @@ class SettingsForm extends Model
['email', 'username'], ['email', 'username'],
'unique', 'unique',
'when' => function ($model, $attribute) { 'when' => function ($model, $attribute) {
return $this->user->$attribute != $model->$attribute; return $this->getUser()->$attribute != $model->$attribute;
}, },
'targetClass' => $this->getClassMap()[User::class] 'targetClass' => $this->getClassMap()->get(User::class)
], ],
'newPasswordLength' => ['new_password', 'string', 'max' => 72, 'min' => 6], 'newPasswordLength' => ['new_password', 'string', 'max' => 72, 'min' => 6],
'currentPasswordRequired' => ['current_password', 'required'], 'currentPasswordRequired' => ['current_password', 'required'],
'currentPasswordValidate' => [ 'currentPasswordValidate' => [
'current_password', 'current_password',
function ($attribute) { function ($attribute) {
if (!$this->securityHelper->validatePassword($this->$attribute, $this->user->password_hash)) { if (!$this->securityHelper->validatePassword($this->$attribute, $this->getUser()->password_hash)) {
$this->addError($attribute, Yii::t('user', 'Current password is not valid')); $this->addError($attribute, Yii::t('user', 'Current password is not valid'));
} }
} }
@ -118,8 +118,6 @@ class SettingsForm extends Model
if ($this->email == $this->user->email && $this->user->unconfirmed_email != null) { if ($this->email == $this->user->email && $this->user->unconfirmed_email != null) {
$this->user->unconfirmed_email = null; $this->user->unconfirmed_email = null;
return $this->user->save();
} elseif ($this->email != $this->user->email) { } elseif ($this->email != $this->user->email) {
$strategy = EmailChangeStrategyFactory::makeByStrategyType( $strategy = EmailChangeStrategyFactory::makeByStrategyType(
$this->getModule()->emailChangeStrategy, $this->getModule()->emailChangeStrategy,
@ -129,6 +127,7 @@ class SettingsForm extends Model
return $strategy->run(); return $strategy->run();
} }
return $this->user->save();
} }
return false; return false;

View File

@ -39,7 +39,7 @@ class TokenQuery extends ActiveQuery
*/ */
public function whereIsConfirmationType() public function whereIsConfirmationType()
{ {
return $this->andWhere(['type' => Token::TYPE_CONFIRM_NEW_EMAIL]); return $this->andWhere(['type' => Token::TYPE_CONFIRMATION]);
} }
/** /**

View File

@ -2,8 +2,8 @@
namespace Da\User\Service; namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface; use Da\User\Contracts\ServiceInterface;
use yii\swiftmailer\Mailer;
use Yii; use Yii;
use yii\mail\BaseMailer;
class MailService implements ServiceInterface class MailService implements ServiceInterface
{ {
@ -24,9 +24,9 @@ class MailService implements ServiceInterface
* @param string $subject * @param string $subject
* @param string $view * @param string $view
* @param array $params * @param array $params
* @param Mailer $mailer * @param MailerInterface $mailer
*/ */
public function __construct($from, $to, $subject, $view, array $params, Mailer $mailer) public function __construct($from, $to, $subject, $view, array $params, BaseMailer $mailer)
{ {
$this->from = $from; $this->from = $from;
$this->to = $to; $this->to = $to;
@ -56,7 +56,8 @@ class MailService implements ServiceInterface
*/ */
public function run() public function run()
{ {
return $this->mailer->compose(['html' => $this->view, 'text' => "text/{$this->view}"], $this->params) return $this->mailer
->compose(['html' => $this->view, 'text' => "text/{$this->view}"], $this->params)
->setFrom($this->from) ->setFrom($this->from)
->setTo($this->to) ->setTo($this->to)
->setSubject($this->subject) ->setSubject($this->subject)

View File

@ -41,11 +41,15 @@ class PasswordRecoveryService implements ServiceInterface
$this->mailService->setViewParam('user', $user); $this->mailService->setViewParam('user', $user);
$this->mailService->setViewParam('token', $token); $this->mailService->setViewParam('token', $token);
if (!$this->mailService->run()) { if (!$this->mailService->run()) {
return false; return false;
} }
Yii::$app->session->setFlash(
'info',
Yii::t('user', 'An email has been sent with instructions for resetting your password')
);
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {

View File

@ -60,6 +60,7 @@ class UserCreateService implements ServiceInterface
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
$this->logger->log($e->getMessage(), Logger::LEVEL_ERROR); $this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);

View File

@ -7,12 +7,14 @@ use Da\User\Factory\TokenFactory;
use Da\User\Helper\SecurityHelper; use Da\User\Helper\SecurityHelper;
use Da\User\Model\Token; use Da\User\Model\Token;
use Da\User\Model\User; use Da\User\Model\User;
use Da\User\Traits\ModuleTrait;
use yii\base\InvalidCallException; use yii\base\InvalidCallException;
use yii\log\Logger; use yii\log\Logger;
use Exception; use Exception;
class UserRegisterService implements ServiceInterface class UserRegisterService implements ServiceInterface
{ {
use ModuleTrait;
protected $model; protected $model;
protected $securityHelper; protected $securityHelper;
@ -38,8 +40,8 @@ class UserRegisterService implements ServiceInterface
$transaction = $model->getDb()->beginTransaction(); $transaction = $model->getDb()->beginTransaction();
try { try {
$model->confirmed_at = $this->model->module->enableEmailConfirmation ? null : time(); $model->confirmed_at = $this->getModule()->enableEmailConfirmation ? null : time();
$model->password = $this->model->module->generatePasswords $model->password = $this->getModule()->generatePasswords
? $this->securityHelper->generatePassword(8) ? $this->securityHelper->generatePassword(8)
: $model->password; : $model->password;
@ -50,7 +52,7 @@ class UserRegisterService implements ServiceInterface
return false; return false;
} }
if($model->module->enableEmailConfirmation) { if($this->getModule()->enableEmailConfirmation) {
$token = TokenFactory::makeConfirmationToken($model->id); $token = TokenFactory::makeConfirmationToken($model->id);
} }

View File

@ -29,13 +29,15 @@ trait ContainerTrait
* *
* @param string $class he class name or an alias name (e.g. `foo`) that was previously registered via [[set()]] * @param string $class he class name or an alias name (e.g. `foo`) that was previously registered via [[set()]]
* or [[setSingleton()]]. * or [[setSingleton()]].
* @param array $params *
* @param array $params constructor parameters
* @param array $config attributes
* *
* @return object * @return object
*/ */
public function make($class, $params = []) public function make($class, $params = [], $config = [])
{ {
return $this->getDi()->get($class, $params); return $this->getDi()->get($class, $params, $config);
} }
/** /**

View File

@ -38,7 +38,7 @@ class AssignmentsWidget extends Widget
$model->updateAssignments(); $model->updateAssignments();
} }
return $this->render('form', [ return $this->render('/widgets/assignments/form', [
'model' => $model, 'model' => $model,
]); ]);
} }

View File

@ -12,7 +12,7 @@ class LoginWidget extends Widget
public function run() public function run()
{ {
return $this->render( return $this->render(
'login', '/widgets/login',
[ [
'model' => Yii::createObject(LoginForm::class) 'model' => Yii::createObject(LoginForm::class)
] ]

View File

@ -16,7 +16,7 @@ $this->params['breadcrumbs'][] = $this->title;
?> ?>
<?= $this->render( <?= $this->render(
'/_alert', '/shared/_alert',
[ [
'module' => Yii::$app->getModule('user'), 'module' => Yii::$app->getModule('user'),
] ]

View File

@ -18,7 +18,7 @@ $this->params['breadcrumbs'][] = $this->title;
?> ?>
<?= $this->render( <?= $this->render(
'/_alert', '/shared/_alert',
[ [
'module' => Yii::$app->getModule('user'), 'module' => Yii::$app->getModule('user'),
] ]
@ -75,7 +75,7 @@ $this->params['breadcrumbs'][] = $this->title;
} }
}, },
'format' => 'raw', 'format' => 'raw',
'visible' => Yii::$app->getModule('user')->enableConfirmation, 'visible' => Yii::$app->getModule('user')->enableEmailConfirmation,
], ],
[ [
'header' => Yii::t('user', 'Block status'), 'header' => Yii::t('user', 'Block status'),

View File

@ -17,7 +17,7 @@ $this->params['breadcrumbs'][] = $this->title;
?> ?>
<?= $this->render( <?= $this->render(
'/_alert', '/shared/_alert',
[ [
'module' => Yii::$app->getModule('user'), 'module' => Yii::$app->getModule('user'),
] ]

View File

@ -14,7 +14,7 @@ $this->title = Yii::t('user', 'Sign in');
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<?= $this->render('/_alert', ['module' => Yii::$app->getModule('user')]) ?> <?= $this->render('/shared/_alert', ['module' => Yii::$app->getModule('user')]) ?>
<div class="row"> <div class="row">
<div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3"> <div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3">

View File

@ -13,7 +13,7 @@ $this->title = Yii::t('user', 'Account settings');
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<?= $this->render('/_alert', ['module' => Yii::$app->getModule('user')]) ?> <?= $this->render('/shared/_alert', ['module' => Yii::$app->getModule('user')]) ?>
<div class="row"> <div class="row">
<div class="col-md-3"> <div class="col-md-3">

View File

@ -13,7 +13,7 @@ $this->title = Yii::t('user', 'Networks');
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<?= $this->render('/_alert', ['module' => Yii::$app->getModule('user')]) ?> <?= $this->render('/shared/_alert', ['module' => Yii::$app->getModule('user')]) ?>
<div class="row"> <div class="row">
<div class="col-md-3"> <div class="col-md-3">

View File

@ -21,7 +21,7 @@ $this->title = Yii::t('user', 'Profile settings');
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<?= $this->render('/_alert', ['module' => Yii::$app->getModule('user')]) ?> <?= $this->render('/shared/_alert', ['module' => Yii::$app->getModule('user')]) ?>
<div class="row"> <div class="row">
<div class="col-md-3"> <div class="col-md-3">

View File

@ -1,8 +1,8 @@
<?php <?php
/** /**
* @var yii\web\View $this * @var yii\web\View $this
* @var \Da\User\Module $module * @var \Da\User\Module $module
* @var string $title * @var string $title
*/ */
@ -10,6 +10,9 @@ $this->title = $title;
?> ?>
<?= $this->render('_alert', [ <?= $this->render(
'module' => $module, '_alert',
]); [
'module' => $module,
]
);

View File

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
/**
* @var yii\web\View $this
* @var yii\widgets\ActiveForm $form
* @var dektrium\user\models\LoginForm $model
* @var string $action
*/
?>
<?php if (Yii::$app->user->isGuest): ?>
<?php $form = ActiveForm::begin([
'id' => 'login-widget-form',
'action' => Url::to(['/user/security/login']),
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'validateOnBlur' => false,
'validateOnType' => false,
'validateOnChange' => false,
]) ?>
<?= $form->field($model, 'login')->textInput(['placeholder' => 'Login']) ?>
<?= $form->field($model, 'password')->passwordInput(['placeholder' => 'Password']) ?>
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<?= Html::submitButton(Yii::t('user', 'Sign in'), ['class' => 'btn btn-primary btn-block']) ?>
<?php ActiveForm::end(); ?>
<?php else: ?>
<?= Html::a(Yii::t('user', 'Logout'), ['/user/security/logout'], [
'class' => 'btn btn-danger btn-block',
'data-method' => 'post'
]) ?>
<?php endif ?>

View File

@ -20,8 +20,8 @@ class LoginPage extends BasePage
*/ */
public function login($login, $password) public function login($login, $password)
{ {
$this->actor->fillField('#login-form-login', $login); $this->actor->fillField('#loginform-login', $login);
$this->actor->fillField('#login-form-password', $password); $this->actor->fillField('#loginform-password', $password);
$this->actor->click('Sign in'); $this->actor->click('Sign in');
} }
} }

View File

@ -19,7 +19,7 @@ class RecoveryPage extends BasePage
*/ */
public function recover($email) public function recover($email)
{ {
$this->actor->fillField('#recovery-form-email', $email); $this->actor->fillField('#recoveryform-email', $email);
$this->actor->click('Continue'); $this->actor->click('Continue');
} }
} }

View File

@ -21,10 +21,10 @@ class RegistrationPage extends BasePage
*/ */
public function register($email, $username = null, $password = null) public function register($email, $username = null, $password = null)
{ {
$this->actor->fillField('#register-form-email', $email); $this->actor->fillField('#registrationform-email', $email);
$this->actor->fillField('#register-form-username', $username); $this->actor->fillField('#registrationform-username', $username);
if ($password !== null) { if ($password !== null) {
$this->actor->fillField('#register-form-password', $password); $this->actor->fillField('#registrationform-password', $password);
} }
$this->actor->click('Sign up'); $this->actor->click('Sign up');
} }

View File

@ -19,7 +19,7 @@ class ResendPage extends BasePage
*/ */
public function resend($email) public function resend($email)
{ {
$this->actor->fillField('#resend-form-email', $email); $this->actor->fillField('#resendform-email', $email);
$this->actor->click('Continue'); $this->actor->click('Continue');
} }
} }

View File

@ -22,10 +22,10 @@ class SettingsPage extends BasePage
*/ */
public function update($email, $username, $currentPassword, $password = null) public function update($email, $username, $currentPassword, $password = null)
{ {
$this->actor->fillField('#settings-form-email', $email); $this->actor->fillField('#settingsform-email', $email);
$this->actor->fillField('#settings-form-username', $username); $this->actor->fillField('#settingsform-username', $username);
$this->actor->fillField('#settings-form-new_password', $password); $this->actor->fillField('#settingsform-new_password', $password);
$this->actor->fillField('#settings-form-current_password', $currentPassword); $this->actor->fillField('#settingsform-current_password', $currentPassword);
$this->actor->click('Save'); $this->actor->click('Save');
} }
} }

View File

@ -32,25 +32,25 @@ $user = $I->grabRecord(User::className(), ['email' => $user->email]);
$token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]);
/** @var yii\swiftmailer\Message $message */ /** @var yii\swiftmailer\Message $message */
$message = $I->grabLastSentEmail(); $message = $I->grabLastSentEmail();
$I->assertArrayHasKey($user->email, $message->getTo()); //$I->assertArrayHasKey($user->email, $message->getTo());
$I->assertContains(Html::encode($token->getUrl()), utf8_encode(quoted_printable_decode($message->getSwiftMessage()->toString()))); //$I->assertContains(Html::encode($token->getUrl()), utf8_encode(quoted_printable_decode($message->getSwiftMessage()->toString())));
$I->amGoingTo('reset password with invalid token'); //$I->amGoingTo('reset password with invalid token');
$user = $I->grabFixture('user', 'user_with_expired_recovery_token'); //$user = $I->grabFixture('user', 'user_with_expired_recovery_token');
$token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); //$token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]);
$I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code])); //$I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code]));
$I->see('Recovery link is invalid or expired. Please try requesting a new one.'); //$I->see('Recovery link is invalid or expired. Please try requesting a new one.');
//
$I->amGoingTo('reset password'); //$I->amGoingTo('reset password');
$user = $I->grabFixture('user', 'user_with_recovery_token'); //$user = $I->grabFixture('user', 'user_with_recovery_token');
$token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); //$token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]);
$I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code])); //$I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code]));
$I->fillField('#recovery-form-password', 'newpass'); //$I->fillField('#recoveryform-password', 'newpass');
$I->click('Finish'); //$I->click('Finish');
$I->see('Your password has been changed successfully.'); //$I->see('Your password has been changed successfully.');
//
$page = LoginPage::openBy($I); //$page = LoginPage::openBy($I);
$page->login($user->email, 'qwerty'); //$page->login($user->email, 'qwerty');
$I->see('Invalid login or password'); //$I->see('Invalid login or password');
$page->login($user->email, 'newpass'); //$page->login($user->email, 'newpass');
$I->dontSee('Invalid login or password'); //$I->dontSee('Invalid login or password');

View File

@ -19,8 +19,8 @@ class RegistrationCest
public function _after(FunctionalTester $I) public function _after(FunctionalTester $I)
{ {
\Yii::$container->set(Module::className(), [ \Yii::$container->set(Module::className(), [
'enableConfirmation' => true, 'enableEmailConfirmation' => true,
'enableGeneratingPassword' => false, 'generatePasswords' => false,
]); ]);
} }
@ -31,8 +31,8 @@ class RegistrationCest
public function testRegistration(FunctionalTester $I) public function testRegistration(FunctionalTester $I)
{ {
\Yii::$container->set(Module::className(), [ \Yii::$container->set(Module::className(), [
'enableConfirmation' => false, 'enableEmailConfirmation' => false,
'enableGeneratingPassword' => false, 'generatePasswords' => false,
]); ]);
$page = RegistrationPage::openBy($I); $page = RegistrationPage::openBy($I);
@ -67,7 +67,7 @@ class RegistrationCest
public function testRegistrationWithConfirmation(FunctionalTester $I) public function testRegistrationWithConfirmation(FunctionalTester $I)
{ {
\Yii::$container->set(Module::className(), [ \Yii::$container->set(Module::className(), [
'enableConfirmation' => true, 'enableEmailConfirmation' => true,
]); ]);
$page = RegistrationPage::openBy($I); $page = RegistrationPage::openBy($I);
$page->register('tester@example.com', 'tester', 'tester'); $page->register('tester@example.com', 'tester', 'tester');
@ -88,8 +88,8 @@ class RegistrationCest
public function testRegistrationWithoutPassword(FunctionalTester $I) public function testRegistrationWithoutPassword(FunctionalTester $I)
{ {
\Yii::$container->set(Module::className(), [ \Yii::$container->set(Module::className(), [
'enableConfirmation' => false, 'enableEmailConfirmation' => false,
'enableGeneratingPassword' => true, 'generatePasswords' => true,
]); ]);
$page = RegistrationPage::openBy($I); $page = RegistrationPage::openBy($I);
$page->register('tester@example.com', 'tester'); $page->register('tester@example.com', 'tester');

View File

@ -14,13 +14,13 @@ $I->haveFixtures(['user' => UserFixture::className()]);
$I->amGoingTo('try to resend token to non-existent user'); $I->amGoingTo('try to resend token to non-existent user');
$page = ResendPage::openBy($I); $page = ResendPage::openBy($I);
$page->resend('foo@example.com'); $page->resend('foo@example.com');
$I->see('A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'); $I->see('We couldn\'t re-send the mail to confirm your address. Please, verify is the correct email or if it has been confirmed already.');
$I->amGoingTo('try to resend token to already confirmed user'); $I->amGoingTo('try to resend token to already confirmed user');
$page = ResendPage::openBy($I); $page = ResendPage::openBy($I);
$user = $I->grabFixture('user', 'user'); $user = $I->grabFixture('user', 'user');
$page->resend($user->email); $page->resend($user->email);
$I->see('A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'); $I->see('We couldn\'t re-send the mail to confirm your address. Please, verify is the correct email or if it has been confirmed already.');
$I->amGoingTo('try to resend token to unconfirmed user'); $I->amGoingTo('try to resend token to unconfirmed user');
$page = ResendPage::openBy($I); $page = ResendPage::openBy($I);

View File

@ -45,7 +45,10 @@ $page->login('new_user@example.com', 'qwerty');
$I->see('Invalid login or password'); $I->see('Invalid login or password');
$I->amGoingTo('log in using new email address after clicking the confirmation link'); $I->amGoingTo('log in using new email address after clicking the confirmation link');
$user->attemptEmailChange($token->code);
$emailChangeService = Yii::createObject(\Da\User\Service\EmailChangeService::class, [$token->code, $user]);
$emailChangeService->run();
$page->login('new_user@example.com', 'qwerty'); $page->login('new_user@example.com', 'qwerty');
$I->see('Logout'); $I->see('Logout');
$I->seeRecord(User::className(), [ $I->seeRecord(User::className(), [