fix test configuration

This commit is contained in:
Antonio Ramirez
2016-12-11 13:33:40 +01:00
parent 0b71a3e9b3
commit 4588d77129
20 changed files with 152 additions and 137 deletions

View File

@ -3,6 +3,7 @@
namespace Da\User; namespace Da\User;
use Da\User\Helper\ClassMapHelper; use Da\User\Helper\ClassMapHelper;
use Da\User\Model\User;
use Da\User\Validator\TimeZoneValidator; use Da\User\Validator\TimeZoneValidator;
use Yii; use Yii;
use yii\authclient\Collection; use yii\authclient\Collection;
@ -23,7 +24,7 @@ class Bootstrap implements BootstrapInterface
if ($app->hasModule('user') && $app->getModule('user') instanceof Module) { if ($app->hasModule('user') && $app->getModule('user') instanceof Module) {
$map = $this->buildClassMap($app->getModule('user')->classMap); $map = $this->buildClassMap($app->getModule('user')->classMap);
$this->initContainer($map); $this->initContainer($app,$map);
$this->initTranslations($app); $this->initTranslations($app);
$this->initMailServiceConfiguration($app, $app->getModule('user')); $this->initMailServiceConfiguration($app, $app->getModule('user'));
@ -41,9 +42,10 @@ class Bootstrap implements BootstrapInterface
/** /**
* Initialize container with module classes * Initialize container with module classes
* *
* @param \yii\base\Application $app
* @param array $map the previously built class map list * @param array $map the previously built class map list
*/ */
protected function initContainer($map) protected function initContainer($app, $map)
{ {
$di = Yii::$container; $di = Yii::$container;
try { try {
@ -99,9 +101,12 @@ try{
$name = (substr($class, strrpos($class, '\\') + 1)); $name = (substr($class, strrpos($class, '\\') + 1));
$modelClassMap[$class] = $model; $modelClassMap[$class] = $model;
if (in_array($name, ['User', 'Profile', 'Token', 'SocialNetworkAccount'])) { if (in_array($name, ['User', 'Profile', 'Token', 'SocialNetworkAccount'])) {
$di->set("Da\\User\\Query\\{$name}Query", function() use ($model) { $di->set(
"Da\\User\\Query\\{$name}Query",
function () use ($model) {
return $model::find(); return $model::find();
}); }
);
} }
} }
$di->setSingleton(ClassMapHelper::class, ClassMapHelper::class, [$modelClassMap]); $di->setSingleton(ClassMapHelper::class, ClassMapHelper::class, [$modelClassMap]);
@ -109,14 +114,15 @@ try{
// search class // search class
$di->set(Search\UserSearch::class, [$di->get(Query\UserQuery::class)]); $di->set(Search\UserSearch::class, [$di->get(Query\UserQuery::class)]);
if (php_sapi_name() !== 'cli') { if ($app instanceof WebApplication) {
// override Yii // override Yii
$di->set( $di->set(
'yii\web\User', 'yii\web\User',
[ [
'enableAutoLogin' => true, 'enableAutoLogin' => true,
'loginUrl' => ['/user/auth/login'], 'loginUrl' => ['/user/auth/login'],
'identityClass' => $di->get(ClassMapHelper::class)->get('User') 'identityClass' => $di->get(ClassMapHelper::class)->get(User::class)
] ]
); );
} }
@ -213,6 +219,7 @@ try{
protected function initControllerNamespace(WebApplication $app) protected function initControllerNamespace(WebApplication $app)
{ {
$app->getModule('user')->controllerNamespace = 'Da\User\Controller'; $app->getModule('user')->controllerNamespace = 'Da\User\Controller';
$app->getModule('user')->setViewPath('@Da/User/resources/views');
} }
/** /**
@ -229,7 +236,7 @@ try{
$defaults = [ $defaults = [
// --- models // --- models
'User' => 'Da\User\Model\User', 'User' => 'Da\User\Model\User',
'Account' => 'Da\User\Model\Account', 'SocialNetworkAccount' => 'Da\User\Model\SocialNetworkAccount',
'Profile' => 'Da\User\Model\Profile', 'Profile' => 'Da\User\Model\Profile',
'Token' => 'Da\User\Model\Token', 'Token' => 'Da\User\Model\Token',
// --- search // --- search
@ -245,7 +252,7 @@ try{
$routes = [ $routes = [
'Da\User\Model' => [ 'Da\User\Model' => [
'User', 'User',
'Account', 'SocialNetworkAccount',
'Profile', 'Profile',
'Token' 'Token'
], ],
@ -286,7 +293,7 @@ try{
return $route; return $route;
} }
} }
throw new Exception('Unknown configuration class name'); throw new Exception("Unknown configuration class name '{$name}'");
} }
} }

View File

@ -39,7 +39,7 @@ class AdminController extends Controller
* @param UserQuery $userQuery * @param UserQuery $userQuery
* @param array $config * @param array $config
*/ */
public function __construct($id, Module $module, UserQuery $userQuery, array $config) public function __construct($id, Module $module, UserQuery $userQuery, array $config = [])
{ {
$this->userQuery = $userQuery; $this->userQuery = $userQuery;
parent::__construct($id, $module, $config); parent::__construct($id, $module, $config);

View File

@ -20,7 +20,7 @@ class ProfileController extends Controller
* @param ProfileQuery $profileQuery * @param ProfileQuery $profileQuery
* @param array $config * @param array $config
*/ */
public function __construct($id, Module $module, ProfileQuery $profileQuery, array $config) public function __construct($id, Module $module, ProfileQuery $profileQuery, array $config = [])
{ {
$this->profileQuery = $profileQuery; $this->profileQuery = $profileQuery;
parent::__construct($id, $module, $config); parent::__construct($id, $module, $config);

View File

@ -34,7 +34,7 @@ class RecoveryController extends Controller
* @param TokenQuery $tokenQuery * @param TokenQuery $tokenQuery
* @param array $config * @param array $config
*/ */
public function __construct($id, Module $module, UserQuery $userQuery, TokenQuery $tokenQuery, array $config) public function __construct($id, Module $module, UserQuery $userQuery, TokenQuery $tokenQuery, array $config = [])
{ {
$this->userQuery = $userQuery; $this->userQuery = $userQuery;
$this->tokenQuery = $tokenQuery; $this->tokenQuery = $tokenQuery;
@ -90,7 +90,7 @@ class RecoveryController extends Controller
$this->trigger(FormEvent::EVENT_AFTER_REQUEST, $event); $this->trigger(FormEvent::EVENT_AFTER_REQUEST, $event);
return $this->render( return $this->render(
'/user/shared/message', '/shared/message',
[ [
'title' => Yii::t('user', 'Recovery message sent'), 'title' => Yii::t('user', 'Recovery message sent'),
'module' => $this->module, 'module' => $this->module,
@ -130,7 +130,7 @@ class RecoveryController extends Controller
); );
return $this->render( return $this->render(
'/user/shared/message', '/shared/message',
[ [
'title' => Yii::t('user', 'Invalid or expired link'), 'title' => Yii::t('user', 'Invalid or expired link'),
'module' => $this->module, 'module' => $this->module,
@ -149,7 +149,7 @@ class RecoveryController extends Controller
$this->trigger(ResetPasswordEvent::EVENT_AFTER_RESET, $event); $this->trigger(ResetPasswordEvent::EVENT_AFTER_RESET, $event);
return $this->render( return $this->render(
'/user/shared/message', '/shared/message',
[ [
'title' => Yii::t('user', 'Password has been changed'), 'title' => Yii::t('user', 'Password has been changed'),
'module' => $this->module, 'module' => $this->module,

View File

@ -47,7 +47,7 @@ class RegistrationController extends Controller
Module $module, Module $module,
UserQuery $userQuery, UserQuery $userQuery,
SocialNetworkAccountQuery $socialNetworkAccountQuery, SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config array $config = []
) { ) {
$this->userQuery = $userQuery; $this->userQuery = $userQuery;
@ -106,7 +106,7 @@ 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('/user/shared/message', [ return $this->render('/shared/message', [
'title' => Yii::t('user', 'Your account has been created') 'title' => Yii::t('user', 'Your account has been created')
]); ]);
} }
@ -186,7 +186,7 @@ class RegistrationController extends Controller
} }
return $this->render( return $this->render(
'/user/shared/message', '/shared/message',
[ [
'title' => Yii::t('user', 'Account confirmation'), 'title' => Yii::t('user', 'Account confirmation'),
'module' => $this->module, 'module' => $this->module,

View File

@ -36,7 +36,7 @@ class SecurityController extends Controller
$id, $id,
Module $module, Module $module,
SocialNetworkAccountQuery $socialNetworkAccountQuery, SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config array $config = []
) { ) {
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery; $this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
parent::__construct($id, $module, $config); parent::__construct($id, $module, $config);

View File

@ -54,7 +54,7 @@ class SettingsController extends Controller
ProfileQuery $profileQuery, ProfileQuery $profileQuery,
UserQuery $userQuery, UserQuery $userQuery,
SocialNetworkAccountQuery $socialNetworkAccountQuery, SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config array $config = []
) { ) {
$this->profileQuery = $profileQuery; $this->profileQuery = $profileQuery;
$this->userQuery = $userQuery; $this->userQuery = $userQuery;

View File

@ -33,7 +33,7 @@ class RegistrationForm extends Model
public function rules() public function rules()
{ {
/** @var User $user */ /** @var User $user */
$user = $this->getClassMap()->get('User'); $user = $this->getClassMap()->get(User::class);
return [ return [
// username rules // username rules

View File

@ -138,7 +138,7 @@ class Profile extends ActiveRecord
*/ */
public function getUser() public function getUser()
{ {
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']); return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
} }
/** /**

View File

@ -96,7 +96,7 @@ class SocialNetworkAccount extends ActiveRecord
*/ */
public function getUser() public function getUser()
{ {
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']); return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
} }
/** /**

View File

@ -74,7 +74,7 @@ class Token extends ActiveRecord
*/ */
public function getUser() public function getUser()
{ {
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']); return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
} }
/** /**

View File

@ -238,7 +238,7 @@ class User extends ActiveRecord implements IdentityInterface
*/ */
public function getProfile() public function getProfile()
{ {
return $this->hasOne($this->getClassMap()->get('Profile'), ['user_id' => 'id']); return $this->hasOne($this->getClassMap()->get(Profile::class), ['user_id' => 'id']);
} }
/** /**

View File

@ -14,6 +14,10 @@ class Module extends \yii\base\Module
* @var bool whether to force email confirmation to. * @var bool whether to force email confirmation to.
*/ */
public $enableEmailConfirmation = true; public $enableEmailConfirmation = true;
/**
* @var bool whether to display flash messages or not
*/
public $enableFlashMessages = true;
/** /**
* @var bool whether to generate passwords automatically and remove the password field from the registration form. * @var bool whether to generate passwords automatically and remove the password field from the registration form.
*/ */
@ -88,4 +92,6 @@ class Module extends \yii\base\Module
'recover/<id:\d+>/<code:[A-Za-z0-9_-]+>' => 'recovery/reset', 'recover/<id:\d+>/<code:[A-Za-z0-9_-]+>' => 'recovery/reset',
'settings/<action:\w+>' => 'settings/<action>' 'settings/<action:\w+>' => 'settings/<action>'
]; ];
public $viewPath = '@Da/User/resources/views';
} }

View File

@ -18,13 +18,13 @@ class AccountConfirmationService implements ServiceInterface
public function __construct( public function __construct(
$code, $code,
User $model, User $model,
TokenQuery $tokenQuery, UserConfirmationService $userConfirmationService,
UserConfirmationService $userConfirmationService TokenQuery $tokenQuery
) { ) {
$this->code = $code; $this->code = $code;
$this->model = $model; $this->model = $model;
$this->tokenQuery = $tokenQuery;
$this->userConfirmationService = $userConfirmationService; $this->userConfirmationService = $userConfirmationService;
$this->tokenQuery = $tokenQuery;
} }
public function run() public function run()

View File

@ -1,6 +1,6 @@
<?php <?php
use AssignmentsWidget; use Da\User\Widget\AssignmentsWidget;
/** /**
* @var yii\web\View $this * @var yii\web\View $this
@ -20,6 +20,6 @@ use AssignmentsWidget;
] ]
) ?> ) ?>
<?= Assignments::widget(['userId' => $user->id]) ?> <?= AssignmentsWidget::widget(['userId' => $user->id]) ?>
<?php $this->endContent() ?> <?php $this->endContent() ?>

View File

@ -1,18 +1,9 @@
<?php <?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\bootstrap\Alert; use yii\bootstrap\Alert;
/** /**
* @var dektrium\user\Module $module * @var \Da\User\Module $module
*/ */
?> ?>

View File

@ -10,6 +10,6 @@ $this->title = $title;
?> ?>
<?= $this->render('/_alert', [ <?= $this->render('_alert', [
'module' => $module, 'module' => $module,
]); ]);

2
tests/_app/assets/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -18,6 +18,9 @@ return [
], ],
], ],
'components' => [ 'components' => [
'assetManager' => [
'basePath' => __DIR__ . '/../assets'
],
'db' => require __DIR__ . '/db.php', 'db' => require __DIR__ . '/db.php',
'mailer' => [ 'mailer' => [
'useFileTransport' => true, 'useFileTransport' => true,

View File

@ -4,6 +4,7 @@ $time = time();
return [ return [
'user' => [ 'user' => [
'id' => 1,
'username' => 'user', 'username' => 'user',
'email' => 'user@example.com', 'email' => 'user@example.com',
'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui', 'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui',
@ -13,6 +14,7 @@ return [
'confirmed_at' => $time, 'confirmed_at' => $time,
], ],
'unconfirmed' => [ 'unconfirmed' => [
'id' => 2,
'username' => 'joe', 'username' => 'joe',
'email' => 'joe@example.com', 'email' => 'joe@example.com',
'password_hash' => '$2y$13$CIH1LSMPzU9xDCywt3QO8uovAu2axp8hwuXVa72oI.1G/USsGyMBS', 'password_hash' => '$2y$13$CIH1LSMPzU9xDCywt3QO8uovAu2axp8hwuXVa72oI.1G/USsGyMBS',
@ -21,6 +23,7 @@ return [
'updated_at' => $time, 'updated_at' => $time,
], ],
'unconfirmed_with_expired_token' => [ 'unconfirmed_with_expired_token' => [
'id' => 3,
'username' => 'john', 'username' => 'john',
'email' => 'john@example.com', 'email' => 'john@example.com',
'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui', 'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui',
@ -29,6 +32,7 @@ return [
'updated_at' => $time - 86401, 'updated_at' => $time - 86401,
], ],
'blocked' => [ 'blocked' => [
'id' => 4,
'username' => 'steven', 'username' => 'steven',
'email' => 'steven@example.com', 'email' => 'steven@example.com',
'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui', 'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui',
@ -39,6 +43,7 @@ return [
'confirmed_at' => $time, 'confirmed_at' => $time,
], ],
'user_with_expired_recovery_token' => [ 'user_with_expired_recovery_token' => [
'id' => 5,
'username' => 'andrew', 'username' => 'andrew',
'email' => 'andrew@example.com', 'email' => 'andrew@example.com',
'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui', 'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui',
@ -48,6 +53,7 @@ return [
'confirmed_at' => $time - 21601, 'confirmed_at' => $time - 21601,
], ],
'user_with_recovery_token' => [ 'user_with_recovery_token' => [
'id' => 6,
'username' => 'alex', 'username' => 'alex',
'email' => 'alex@example.com', 'email' => 'alex@example.com',
'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui', 'password_hash' => '$2y$13$qY.ImaYBppt66qez6B31QO92jc5DYVRzo5NxM1ivItkW74WsSG6Ui',