Egorka
6 years ago
71 changed files with 846 additions and 632 deletions
@ -0,0 +1,92 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 17.08.2018 |
||||
*/ |
||||
|
||||
namespace backend\controllers; |
||||
|
||||
|
||||
use core\entities\ModuleRecord; |
||||
use core\services\ModuleService; |
||||
use yii\web\Controller; |
||||
use yii\filters\VerbFilter; |
||||
use yii\filters\AccessControl; |
||||
use yii\web\NotFoundHttpException; |
||||
|
||||
class ModuleController extends Controller |
||||
{ |
||||
private $service; |
||||
|
||||
public function __construct( string $id, $module, ModuleService $service, array $config = [] ) { |
||||
parent::__construct( $id, $module, $config ); |
||||
$this->service = $service; |
||||
} |
||||
|
||||
public function behaviors(): array |
||||
{ |
||||
return [ |
||||
'access' => [ |
||||
'class' => AccessControl::class, |
||||
'rules' => [ |
||||
[ |
||||
'actions' => ['list', 'disable', 'enable', 'delete'], |
||||
'allow' => true, |
||||
'roles' => ['ModuleManagement'], |
||||
], |
||||
[ // all the action are accessible to admin |
||||
'allow' => true, |
||||
'roles' => ['admin'], |
||||
], |
||||
], |
||||
], |
||||
'verbs' => [ |
||||
'class' => VerbFilter::class, |
||||
'actions' => [ |
||||
'delete' => ['POST'], |
||||
'disable' => ['POST'], |
||||
'enable' => ['POST'], |
||||
], |
||||
], |
||||
]; |
||||
} |
||||
|
||||
public function actionList() |
||||
{ |
||||
//$modules = ModuleRecord::find()->all(); |
||||
$modules = \Yii::$app->moduleManager->getModules(); |
||||
|
||||
return $this->render('list', [ |
||||
'modules' => $modules, |
||||
]); |
||||
} |
||||
|
||||
public function actionDelete($id) |
||||
{ |
||||
$module = $this->findModel($id); |
||||
$this->service->delete($module); |
||||
return $this->redirect(['module/list']); |
||||
} |
||||
|
||||
public function actionDisable($id) |
||||
{ |
||||
$module = $this->findModel($id); |
||||
$this->service->disable($module); |
||||
return $this->redirect(['module/list']); |
||||
} |
||||
|
||||
public function actionEnable($id) |
||||
{ |
||||
$module = $this->findModel($id); |
||||
$this->service->enable($module); |
||||
return $this->redirect(['module/list']); |
||||
} |
||||
|
||||
protected function findModel($id): ModuleRecord |
||||
{ |
||||
if (($model = ModuleRecord::findOne($id)) !== null) { |
||||
return $model; |
||||
} |
||||
throw new NotFoundHttpException('The requested module does not exist.'); |
||||
} |
||||
} |
@ -0,0 +1,70 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 17.08.2018 |
||||
*/ |
||||
|
||||
use core\entities\ModuleRecord; |
||||
use yii\helpers\Html; |
||||
use core\helpers\ModuleHelper; |
||||
|
||||
/** |
||||
* @var $this \yii\web\View |
||||
* @var $modules \core\entities\ModuleRecord[] |
||||
*/ |
||||
|
||||
$this->title = Yii::t('main', 'Modules'); |
||||
$this->params['breadcrumbs'][] = $this->title; |
||||
?> |
||||
|
||||
<div class="row"> |
||||
|
||||
<?php foreach ($modules as $module): ?> |
||||
|
||||
<?php |
||||
|
||||
$color = $module->isEnabled() ? '#00aced' : '#cccccc'; |
||||
if ($module->isDisabled()) { |
||||
ModuleHelper::addModuleAdminTranslation($module->name); |
||||
} |
||||
|
||||
?> |
||||
|
||||
<div class="col-md-4"> |
||||
|
||||
<div class="box"> |
||||
<div class="box-header with-border"> |
||||
<h3 class="box-title"><i class="fa fa-power-off" aria-hidden="true" style="color: <?= $color ?>"></i> <?= Yii::t($module->name, $module->name) ?></h3>
|
||||
</div> |
||||
<div class="box-body"> |
||||
<?= Yii::t($module->name, $module->description) ?> |
||||
</div> |
||||
<div class="box-footer" style="text-align: right"> |
||||
<?php if ($module->isEnabled()): ?> |
||||
<?= Html::a(Yii::t('main', 'Disable'), ['module/disable', 'id' => $module->id], [ |
||||
'class' => 'btn btn-default btn-sm', |
||||
'data' => [ |
||||
'method' => 'post', |
||||
], |
||||
]) ?> |
||||
<?php else: ?> |
||||
<?= Html::a(Yii::t('main', 'Enable'), ['module/enable', 'id' => $module->id], [ |
||||
'class' => 'btn btn-default btn-sm', |
||||
'data' => [ |
||||
'method' => 'post', |
||||
], |
||||
]) ?> |
||||
<?php endif; ?> |
||||
|
||||
<?= Html::a(Yii::t('buttons', 'Delete'), ['module/delete', 'id' => $module->id], [ |
||||
'class' => 'btn btn-danger btn-sm', |
||||
'data' => [ |
||||
'confirm' => Yii::t('buttons', 'Are you sure you want to delete this item?'), |
||||
'method' => 'post', |
||||
], |
||||
]) ?> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
<?php endforeach; ?> |
||||
</div> |
@ -0,0 +1,63 @@
|
||||
<?php |
||||
|
||||
namespace common\modules\banners; |
||||
|
||||
use core\components\modules\ModuleInterface; |
||||
use yii\helpers\ArrayHelper; |
||||
|
||||
|
||||
/** |
||||
* blog module definition class |
||||
*/ |
||||
class BannersModule extends \yii\base\Module implements ModuleInterface |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public $controllerNamespace = 'common\modules\banners\controllers'; |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
|
||||
// custom initialization code goes here |
||||
} |
||||
|
||||
public function bootstrap($app) |
||||
{ |
||||
// add migration path |
||||
$app->controllerMap['migrate']['migrationPath'][] = '@common/modules/banners/migrations'; |
||||
|
||||
// add search rules |
||||
$app->params['search_rules'][] = "SELECT title, CONCAT('/banners/manage/banner/view/', id) AS url FROM {{banners}}"; |
||||
|
||||
$app->getUrlManager()->addRules([ |
||||
'banners/manage/banner/view/<id:\d+>' => 'banners/manage/banner/view', |
||||
]); |
||||
|
||||
// add languages |
||||
$app->getI18n()->translations = ArrayHelper::merge($app->getI18n()->translations, [ |
||||
'banners' => [ |
||||
'class' => 'yii\i18n\PhpMessageSource', |
||||
'basePath' => '@common/modules/banners/messages', |
||||
], |
||||
'banners_public' => [ |
||||
'class' => 'yii\i18n\PhpMessageSource', |
||||
'basePath' => '@common/modules/banners/messages', |
||||
], |
||||
]); |
||||
|
||||
// add menu items |
||||
if (basename($app->getBasePath()) === 'backend') { |
||||
$app->params['adminMenu'][] = [ |
||||
'label' => \Yii::t( 'banners', 'Banners' ), |
||||
'icon' => 'flag', |
||||
'url' => [ '/banners/manage/banner/index' ], |
||||
'visible' => \Yii::$app->user->can( 'admin' ) || \Yii::$app->user->can( 'BannersManagement' ) |
||||
]; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,8 @@
|
||||
<?php |
||||
|
||||
return [ |
||||
'version' => '1.0.1', |
||||
'name' => 'banners', |
||||
'description' => 'Banners widget for site', |
||||
'module' => 'BannersModule', |
||||
]; |
@ -0,0 +1,8 @@
|
||||
<?php |
||||
|
||||
return [ |
||||
'version' => '1.0.1', |
||||
'name' => 'blog', |
||||
'description' => 'Blog system for site with comments and slug', |
||||
'module' => 'BlogModule', |
||||
]; |
@ -0,0 +1,8 @@
|
||||
<?php |
||||
|
||||
return [ |
||||
'version' => '1.0.1', |
||||
'name' => 'forms', |
||||
'description' => 'Form widget generator for site', |
||||
'module' => 'FormsModule', |
||||
]; |
@ -1,5 +1,7 @@
|
||||
<?php |
||||
return [ |
||||
'forms' => 'Формы', |
||||
'Form widget generator for site' => 'Создание различных форм для сайта с отправкой на e-mail и уведомлениями', |
||||
'Form' => 'Форма', |
||||
'Forms' => 'Формы', |
||||
'Messages' => 'Сообщения', |
@ -0,0 +1,8 @@
|
||||
<?php |
||||
|
||||
return [ |
||||
'version' => '1.0.1', |
||||
'name' => 'links', |
||||
'description' => 'Custom links in web site menu', |
||||
'module' => 'LinksModule', |
||||
]; |
@ -1,4 +0,0 @@
|
||||
<?php |
||||
return [ |
||||
'Links' => 'Ссылки', |
||||
]; |
@ -0,0 +1,6 @@
|
||||
<?php |
||||
return [ |
||||
'links' => 'Ссылки', |
||||
'Custom links in web site menu' => 'Создание пунктов меню сайта, содержащих ссылки, указанные вручную', |
||||
'Links' => 'Ссылки', |
||||
]; |
@ -0,0 +1,8 @@
|
||||
<?php |
||||
|
||||
return [ |
||||
'version' => '1.0.1', |
||||
'name' => 'pages', |
||||
'description' => 'Custom pages on site with slug', |
||||
'module' => 'PagesModule', |
||||
]; |
@ -1,5 +1,7 @@
|
||||
<?php |
||||
return [ |
||||
'pages' => 'Страницы', |
||||
'Custom pages on site with slug' => 'Публикация информации в виде отдельных самостоятельных страниц сайта', |
||||
'Create Page' => 'Новая страница', |
||||
'Pages' => 'Страницы', |
||||
'Common' => 'Основное', |
@ -0,0 +1,90 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 20.08.2018 |
||||
*/ |
||||
|
||||
namespace core\components\modules; |
||||
|
||||
use core\entities\ModuleRecord; |
||||
use core\services\ModuleService; |
||||
use Yii; |
||||
use yii\helpers\FileHelper; |
||||
|
||||
class ModuleManager |
||||
{ |
||||
public $moduleNames = []; |
||||
public $modules = []; |
||||
|
||||
private $service; |
||||
|
||||
public function __construct(ModuleService $service) |
||||
{ |
||||
$this->service = $service; |
||||
} |
||||
|
||||
public function appendToMigrationTable($name) { |
||||
$time = time(); |
||||
$connection = Yii::$app->getDb(); |
||||
$command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')"); |
||||
$command->execute(); |
||||
} |
||||
|
||||
public function removeFromMigrationTable($name) { |
||||
$connection = Yii::$app->getDb(); |
||||
$command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'"); |
||||
$command->execute(); |
||||
} |
||||
|
||||
public function getModules() |
||||
{ |
||||
$modules = []; |
||||
$localModules = $this->getLocalModules(); |
||||
foreach ($localModules as $local_module) { |
||||
if (!$db_module = ModuleRecord::find()->andWhere(['name' => $local_module['name']])->one()) { |
||||
$db_module = $this->service->create($local_module['name'], "common\\modules\\".$local_module['name']."\\" . $local_module['module']); |
||||
$db_module->description = $local_module['description']; |
||||
$modules[] = $db_module; |
||||
} |
||||
else { |
||||
$db_module->description = $local_module['description']; |
||||
$modules[] = $db_module; |
||||
} |
||||
} |
||||
return $modules; |
||||
} |
||||
|
||||
public function getLocalModules() |
||||
{ |
||||
$this->_getLocalModulesNames(); |
||||
|
||||
if (empty($this->modules)) { |
||||
foreach ( $this->moduleNames as $module_name ) { |
||||
$manifest = Yii::getAlias( '@common/modules/' . $module_name . '/manifest.php' ); |
||||
if ( file_exists( $manifest ) ) { |
||||
$this->modules[] = require $manifest; |
||||
} |
||||
} |
||||
} |
||||
return $this->modules; |
||||
} |
||||
|
||||
private function _getLocalModulesNames(): void |
||||
{ |
||||
if (!empty($this->moduleNames)) { |
||||
return; |
||||
} |
||||
|
||||
$names = []; |
||||
$modulePath = Yii::getAlias('@common/modules'); |
||||
$modules = file_exists($modulePath) ? FileHelper::findDirectories($modulePath, [ |
||||
'recursive' => false, |
||||
]) : []; |
||||
foreach ($modules as $module) { |
||||
$module = basename($module); |
||||
$names[] = $module; |
||||
} |
||||
$this->moduleNames = $names; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,25 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 18.08.2018 |
||||
*/ |
||||
|
||||
namespace core\helpers; |
||||
|
||||
use yii\helpers\ArrayHelper; |
||||
use Yii; |
||||
|
||||
class ModuleHelper |
||||
{ |
||||
// add module translation without load module |
||||
public static function addModuleAdminTranslation($module_name) |
||||
{ |
||||
Yii::$app->getI18n()->translations = ArrayHelper::merge(Yii::$app->getI18n()->translations, [ |
||||
$module_name => [ |
||||
'class' => 'yii\i18n\PhpMessageSource', |
||||
'basePath' => '@common/modules/' . $module_name . '/messages', |
||||
], |
||||
]); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,30 @@
|
||||
<?php |
||||
|
||||
namespace core\repositories; |
||||
|
||||
use core\entities\ModuleRecord; |
||||
|
||||
class ModuleRepository |
||||
{ |
||||
public function get($id): ModuleRecord |
||||
{ |
||||
if (!$module = ModuleRecord::findOne($id)) { |
||||
throw new NotFoundException('Module is not found.'); |
||||
} |
||||
return $module; |
||||
} |
||||
|
||||
public function save(ModuleRecord $module): void |
||||
{ |
||||
if (!$module->save()) { |
||||
throw new \RuntimeException('Saving error.'); |
||||
} |
||||
} |
||||
|
||||
public function remove(ModuleRecord $module): void |
||||
{ |
||||
if (!$module->delete()) { |
||||
throw new \RuntimeException('Removing error.'); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,109 @@
|
||||
<?php |
||||
|
||||
namespace core\services; |
||||
|
||||
use core\entities\ModuleRecord; |
||||
use core\repositories\ModuleRepository; |
||||
use yii\helpers\FileHelper; |
||||
use Yii; |
||||
|
||||
class ModuleService |
||||
{ |
||||
private $modules; |
||||
|
||||
public function __construct(ModuleRepository $modules) |
||||
{ |
||||
$this->modules = $modules; |
||||
} |
||||
|
||||
public function create($name, $class, $type = 'common', $active = ModuleRecord::STATUS_DISABLED): ModuleRecord |
||||
{ |
||||
$module = new ModuleRecord(); |
||||
$module->name = $name; |
||||
$module->class = $class; |
||||
$module->type = $type; |
||||
$module->active = $active; |
||||
$this->modules->save($module); |
||||
return $module; |
||||
} |
||||
|
||||
public function delete(ModuleRecord $module) |
||||
{ |
||||
$migrations = $this->getMigrationFiles($module->name); |
||||
$migrations = array_reverse($migrations); |
||||
foreach ($migrations as $migrationPath) { |
||||
$migrationFile = basename($migrationPath); |
||||
$migration = str_replace('.php', '', $migrationFile); |
||||
if ($this->migrationExists($migration)) { |
||||
require $migrationPath; |
||||
$obj = new $migration; |
||||
if (method_exists($obj, 'safeDown')) { |
||||
$obj->safeDown(); |
||||
Yii::$app->moduleManager->removeFromMigrationTable($migration); |
||||
|
||||
} |
||||
elseif (method_exists($obj, 'down')) { |
||||
$obj->down(); |
||||
Yii::$app->moduleManager->removeFromMigrationTable($migration); |
||||
} |
||||
} |
||||
} |
||||
// delete files |
||||
$modulePath = Yii::getAlias('@common/modules/' . $module->name); |
||||
if (file_exists($modulePath)) { |
||||
FileHelper::removeDirectory($modulePath); |
||||
} |
||||
// delete module record |
||||
$this->modules->remove($module); |
||||
} |
||||
|
||||
public function disable(ModuleRecord $module) |
||||
{ |
||||
$module->active = ModuleRecord::STATUS_DISABLED; |
||||
$this->modules->save($module); |
||||
} |
||||
|
||||
public function enable(ModuleRecord $module) |
||||
{ |
||||
$module->active = ModuleRecord::STATUS_ENABLED; |
||||
|
||||
// migration if not exists |
||||
$migrations = $this->getMigrationFiles($module->name); |
||||
foreach ($migrations as $migrationPath) { |
||||
$migrationFile = basename($migrationPath); |
||||
$migration = str_replace('.php', '', $migrationFile); |
||||
if (!$this->migrationExists($migration)) { |
||||
// run migration |
||||
require $migrationPath; |
||||
$obj = new $migration; |
||||
if (method_exists($obj, 'safeUp')) { |
||||
$obj->safeUp(); |
||||
Yii::$app->moduleManager->appendToMigrationTable($migration); |
||||
|
||||
} |
||||
elseif (method_exists($obj, 'up')) { |
||||
$obj->up(); |
||||
Yii::$app->moduleManager->appendToMigrationTable($migration); |
||||
} |
||||
} |
||||
} |
||||
|
||||
$this->modules->save($module); |
||||
} |
||||
|
||||
private function getMigrationFiles($module) |
||||
{ |
||||
// migration if not exists |
||||
$migrationPath = Yii::getAlias('@common/modules/' . $module . '/migrations'); |
||||
return file_exists($migrationPath) ? FileHelper::findFiles($migrationPath) : []; |
||||
} |
||||
|
||||
private function migrationExists($name): bool |
||||
{ |
||||
// check record exists |
||||
$connection = Yii::$app->getDb(); |
||||
$command = $connection->createCommand("SELECT * FROM migration WHERE version = '$name'"); |
||||
$result = $command->queryAll(); |
||||
return $result ? true : false; |
||||
} |
||||
} |
@ -1,13 +0,0 @@
|
||||
<?php |
||||
return [ |
||||
[ |
||||
'username' => 'erau', |
||||
'auth_key' => 'tUu1qHcde0diwUol3xeI-18MuHkkprQI', |
||||
// password_0 |
||||
'password_hash' => '$2y$13$nJ1WDlBaGcbCdbNC5.5l4.sgy.OMEKCqtDQOdQ2OWpgiKRWYyzzne', |
||||
'password_reset_token' => 'RkD_Jw0_8HEedzLk7MM-ZKEFfYR7VbMr_1392559490', |
||||
'created_at' => '1392559490', |
||||
'updated_at' => '1392559490', |
||||
'email' => 'sfriesen@jenkins.info', |
||||
], |
||||
]; |
@ -1,23 +0,0 @@
|
||||
<?php |
||||
|
||||
return [ |
||||
[ |
||||
'username' => 'okirlin', |
||||
'auth_key' => 'iwTNae9t34OmnK6l4vT4IeaTk-YWI2Rv', |
||||
'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi', |
||||
'password_reset_token' => 't5GU9NwpuGYSfb7FEZMAxqtuz2PkEvv_' . time(), |
||||
'created_at' => '1391885313', |
||||
'updated_at' => '1391885313', |
||||
'email' => 'brady.renner@rutherford.com', |
||||
], |
||||
[ |
||||
'username' => 'troy.becker', |
||||
'auth_key' => 'EdKfXrx88weFMV0vIxuTMWKgfK2tS3Lp', |
||||
'password_hash' => '$2y$13$g5nv41Px7VBqhS3hVsVN2.MKfgT3jFdkXEsMC4rQJLfaMa7VaJqL2', |
||||
'password_reset_token' => '4BSNyiZNAuxjs5Mty990c47sVrgllIi_' . time(), |
||||
'created_at' => '1391885313', |
||||
'updated_at' => '1391885313', |
||||
'email' => 'nicolas.dianna@hotmail.com', |
||||
'status' => '0', |
||||
], |
||||
]; |
@ -1,20 +0,0 @@
|
||||
<?php |
||||
namespace frontend\tests\acceptance; |
||||
|
||||
use frontend\tests\AcceptanceTester; |
||||
use yii\helpers\Url; |
||||
|
||||
class HomeCest |
||||
{ |
||||
public function checkHome(AcceptanceTester $I) |
||||
{ |
||||
$I->amOnPage(Url::toRoute('/site/index')); |
||||
$I->see('My Application'); |
||||
|
||||
$I->seeLink('About'); |
||||
$I->click('About'); |
||||
$I->wait(2); // wait for page to be opened |
||||
|
||||
$I->see('This is the About page.'); |
||||
} |
||||
} |
@ -0,0 +1,15 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 13.08.2018 |
||||
*/ |
||||
|
||||
namespace frontend\tests\fixtures; |
||||
|
||||
|
||||
use yii\test\ActiveFixture; |
||||
|
||||
class UserFixture extends ActiveFixture |
||||
{ |
||||
public $modelClass = 'core\entites\user\User'; |
||||
} |
@ -0,0 +1,43 @@
|
||||
<?php |
||||
|
||||
return [ |
||||
'user1' => [ |
||||
"id" => 1, |
||||
"username" => "Error202", |
||||
"auth_key" => "I4MJAUm8ZZ4rennRmwhjfeZ7xV2-FpYn", |
||||
"password_hash" => "$2y$13$53Y5IhwZyUkvLirp.qq.o.J0XRDOPRFVCbkHSzYXKVb6.DbdM9uYy", |
||||
"password_reset_token" => null, |
||||
"email" => "error-202@mail.ru", |
||||
"email_confirm_token" => null, |
||||
"status" => 10, |
||||
"created_at" => 1516021751, |
||||
"updated_at" => 1530816306, |
||||
"user_pic" => "avatar_e188db6330ceab81865b3996f7bbb3d6_300.png" |
||||
], |
||||
'user2' => [ |
||||
"id" => 17, |
||||
"username" => "Error203", |
||||
"auth_key" => "wW7PWDP0yUvGVzEqy3dZepb0tj19ejv6", |
||||
"password_hash" => '$2y$13$Spf7/iHOj3aJARSORST4s.blouBXtUPLzMf7sD4AX9akSK1URGkfe', |
||||
"password_reset_token" => null, |
||||
"email" => "error-202@yandex.ru", |
||||
"email_confirm_token" => null, |
||||
"status" => 10, |
||||
"created_at" => 1518122129, |
||||
"updated_at" => 1518127758, |
||||
"user_pic" => null |
||||
], |
||||
'user3' => [ |
||||
"id" => 18, |
||||
"username" => "zz", |
||||
"auth_key" => "JbLkAlfA7jQ0XxbaWxUEPWQCzsVZ9gn8", |
||||
"password_hash" => '$2y$13$gyDo.ax9O962rq9pfh6sX.ZHrZ05cTZNAaX8VEMBL7n6UPsMr/vHa', |
||||
"password_reset_token" => null, |
||||
"email" => "zz@morework.ru", |
||||
"email_confirm_token" => "AcNTOgIMhefCq5tQaOyUqEIeIPLFqFdY", |
||||
"status" => 0, |
||||
"created_at" => 1518157352, |
||||
"updated_at" => 1518157352, |
||||
"user_pic" => null |
||||
], |
||||
]; |
@ -1,13 +0,0 @@
|
||||
<?php |
||||
namespace frontend\tests\functional; |
||||
|
||||
use frontend\tests\FunctionalTester; |
||||
|
||||
class AboutCest |
||||
{ |
||||
public function checkAbout(FunctionalTester $I) |
||||
{ |
||||
$I->amOnRoute('site/about'); |
||||
$I->see('About', 'h1'); |
||||
} |
||||
} |
@ -1,59 +0,0 @@
|
||||
<?php |
||||
namespace frontend\tests\functional; |
||||
|
||||
use frontend\tests\FunctionalTester; |
||||
|
||||
/* @var $scenario \Codeception\Scenario */ |
||||
|
||||
class ContactCest |
||||
{ |
||||
public function _before(FunctionalTester $I) |
||||
{ |
||||
$I->amOnPage(['site/contact']); |
||||
} |
||||
|
||||
public function checkContact(FunctionalTester $I) |
||||
{ |
||||
$I->see('Contact', 'h1'); |
||||
} |
||||
|
||||
public function checkContactSubmitNoData(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm('#contact-form', []); |
||||
$I->see('Contact', 'h1'); |
||||
$I->seeValidationError('Name cannot be blank'); |
||||
$I->seeValidationError('Email cannot be blank'); |
||||
$I->seeValidationError('Subject cannot be blank'); |
||||
$I->seeValidationError('Body cannot be blank'); |
||||
$I->seeValidationError('The verification code is incorrect'); |
||||
} |
||||
|
||||
public function checkContactSubmitNotCorrectEmail(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm('#contact-form', [ |
||||
'ContactForm[name]' => 'tester', |
||||
'ContactForm[email]' => 'tester.email', |
||||
'ContactForm[subject]' => 'test subject', |
||||
'ContactForm[body]' => 'test content', |
||||
'ContactForm[verifyCode]' => 'testme', |
||||
]); |
||||
$I->seeValidationError('Email is not a valid email address.'); |
||||
$I->dontSeeValidationError('Name cannot be blank'); |
||||
$I->dontSeeValidationError('Subject cannot be blank'); |
||||
$I->dontSeeValidationError('Body cannot be blank'); |
||||
$I->dontSeeValidationError('The verification code is incorrect'); |
||||
} |
||||
|
||||
public function checkContactSubmitCorrectData(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm('#contact-form', [ |
||||
'ContactForm[name]' => 'tester', |
||||
'ContactForm[email]' => 'tester@example.com', |
||||
'ContactForm[subject]' => 'test subject', |
||||
'ContactForm[body]' => 'test content', |
||||
'ContactForm[verifyCode]' => 'testme', |
||||
]); |
||||
$I->seeEmailIsSent(); |
||||
$I->see('Thank you for contacting us. We will respond to you as soon as possible.'); |
||||
} |
||||
} |
@ -1,17 +0,0 @@
|
||||
<?php |
||||
|
||||
namespace frontend\tests\functional; |
||||
|
||||
use frontend\tests\FunctionalTester; |
||||
|
||||
class HomeCest |
||||
{ |
||||
public function checkOpen(FunctionalTester $I) |
||||
{ |
||||
$I->amOnPage(\Yii::$app->homeUrl); |
||||
$I->see('My Application'); |
||||
$I->seeLink('About'); |
||||
$I->click('About'); |
||||
$I->see('This is the About page.'); |
||||
} |
||||
} |
@ -1,60 +0,0 @@
|
||||
<?php |
||||
|
||||
namespace frontend\tests\functional; |
||||
|
||||
use frontend\tests\FunctionalTester; |
||||
use common\fixtures\UserFixture; |
||||
|
||||
class LoginCest |
||||
{ |
||||
/** |
||||
* Load fixtures before db transaction begin |
||||
* Called in _before() |
||||
* @see \Codeception\Module\Yii2::_before() |
||||
* @see \Codeception\Module\Yii2::loadFixtures() |
||||
* @return array |
||||
*/ |
||||
public function _fixtures() |
||||
{ |
||||
return [ |
||||
'user' => [ |
||||
'class' => UserFixture::className(), |
||||
'dataFile' => codecept_data_dir() . 'login_data.php' |
||||
] |
||||
]; |
||||
} |
||||
|
||||
public function _before(FunctionalTester $I) |
||||
{ |
||||
$I->amOnRoute('site/login'); |
||||
} |
||||
|
||||
protected function formParams($login, $password) |
||||
{ |
||||
return [ |
||||
'LoginForm[username]' => $login, |
||||
'LoginForm[password]' => $password, |
||||
]; |
||||
} |
||||
|
||||
public function checkEmpty(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm('#login-form', $this->formParams('', '')); |
||||
$I->seeValidationError('Username cannot be blank.'); |
||||
$I->seeValidationError('Password cannot be blank.'); |
||||
} |
||||
|
||||
public function checkWrongPassword(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm('#login-form', $this->formParams('admin', 'wrong')); |
||||
$I->seeValidationError('Incorrect username or password.'); |
||||
} |
||||
|
||||
public function checkValidLogin(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm('#login-form', $this->formParams('erau', 'password_0')); |
||||
$I->see('Logout (erau)', 'form button[type=submit]'); |
||||
$I->dontSeeLink('Login'); |
||||
$I->dontSeeLink('Signup'); |
||||
} |
||||
} |
@ -1,57 +0,0 @@
|
||||
<?php |
||||
|
||||
namespace frontend\tests\functional; |
||||
|
||||
use frontend\tests\FunctionalTester; |
||||
|
||||
class SignupCest |
||||
{ |
||||
protected $formId = '#form-signup'; |
||||
|
||||
|
||||
public function _before(FunctionalTester $I) |
||||
{ |
||||
$I->amOnRoute('site/signup'); |
||||
} |
||||
|
||||
public function signupWithEmptyFields(FunctionalTester $I) |
||||
{ |
||||
$I->see('Signup', 'h1'); |
||||
$I->see('Please fill out the following fields to signup:'); |
||||
$I->submitForm($this->formId, []); |
||||
$I->seeValidationError('Username cannot be blank.'); |
||||
$I->seeValidationError('Email cannot be blank.'); |
||||
$I->seeValidationError('Password cannot be blank.'); |
||||
|
||||
} |
||||
|
||||
public function signupWithWrongEmail(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm( |
||||
$this->formId, [ |
||||
'SignupForm[username]' => 'tester', |
||||
'SignupForm[email]' => 'ttttt', |
||||
'SignupForm[password]' => 'tester_password', |
||||
] |
||||
); |
||||
$I->dontSee('Username cannot be blank.', '.help-block'); |
||||
$I->dontSee('Password cannot be blank.', '.help-block'); |
||||
$I->see('Email is not a valid email address.', '.help-block'); |
||||
} |
||||
|
||||
public function signupSuccessfully(FunctionalTester $I) |
||||
{ |
||||
$I->submitForm($this->formId, [ |
||||
'SignupForm[username]' => 'tester', |
||||
'SignupForm[email]' => 'tester.email@example.com', |
||||
'SignupForm[password]' => 'tester_password', |
||||
]); |
||||
|
||||
$I->seeRecord('common\models\User', [ |
||||
'username' => 'tester', |
||||
'email' => 'tester.email@example.com', |
||||
]); |
||||
|
||||
$I->see('Logout (tester)', 'form button[type=submit]'); |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 13.08.2018 |
||||
*/ |
||||
|
||||
namespace frontend\tests\unit; |
||||
|
||||
use Codeception\Test\Unit; |
||||
use frontend\tests\UnitTester; |
||||
use frontend\tests\fixtures\UserFixture; |
||||
|
||||
class UserTest extends Unit |
||||
{ |
||||
/** |
||||
* @var UnitTester |
||||
*/ |
||||
protected $tester; |
||||
|
||||
public function _fixtures() |
||||
{ |
||||
return [ |
||||
'users' => UserFixture::class, |
||||
]; |
||||
} |
||||
|
||||
protected function _before() { |
||||
|
||||
} |
||||
|
||||
protected function _after() { |
||||
|
||||
} |
||||
|
||||
// test |
||||
public function testTest() |
||||
{ |
||||
$user = $this->tester->grabFixture('users', 'user1'); |
||||
} |
||||
|
||||
} |
@ -1,32 +0,0 @@
|
||||
<?php |
||||
namespace frontend\tests\unit\models; |
||||
|
||||
use Yii; |
||||
use frontend\models\ContactForm; |
||||
|
||||
class ContactFormTest extends \Codeception\Test\Unit |
||||
{ |
||||
public function testSendEmail() |
||||
{ |
||||
$model = new ContactForm(); |
||||
|
||||
$model->attributes = [ |
||||
'name' => 'Tester', |
||||
'email' => 'tester@example.com', |
||||
'subject' => 'very important letter subject', |
||||
'body' => 'body of current message', |
||||
]; |
||||
|
||||
expect_that($model->sendEmail('admin@example.com')); |
||||
|
||||
// using Yii2 module actions to check email was sent |
||||
$this->tester->seeEmailIsSent(); |
||||
|
||||
$emailMessage = $this->tester->grabLastSentEmail(); |
||||
expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface'); |
||||
expect($emailMessage->getTo())->hasKey('admin@example.com'); |
||||
expect($emailMessage->getFrom())->hasKey('tester@example.com'); |
||||
expect($emailMessage->getSubject())->equals('very important letter subject'); |
||||
expect($emailMessage->toString())->contains('body of current message'); |
||||
} |
||||
} |
@ -1,59 +0,0 @@
|
||||
<?php |
||||
|
||||
namespace frontend\tests\unit\models; |
||||
|
||||
use Yii; |
||||
use frontend\models\PasswordResetRequestForm; |
||||
use common\fixtures\UserFixture as UserFixture; |
||||
use common\models\User; |
||||
|
||||
class PasswordResetRequestFormTest extends \Codeception\Test\Unit |
||||
{ |
||||
/** |
||||
* @var \frontend\tests\UnitTester |
||||
*/ |
||||
protected $tester; |
||||
|
||||
|
||||
public function _before() |
||||
{ |
||||
$this->tester->haveFixtures([ |
||||
'user' => [ |
||||
'class' => UserFixture::className(), |
||||
'dataFile' => codecept_data_dir() . 'user.php' |
||||
] |
||||
]); |
||||
} |
||||
|
||||
public function testSendMessageWithWrongEmailAddress() |
||||
{ |
||||
$model = new PasswordResetRequestForm(); |
||||
$model->email = 'not-existing-email@example.com'; |
||||
expect_not($model->sendEmail()); |
||||
} |
||||
|
||||
public function testNotSendEmailsToInactiveUser() |
||||
{ |
||||
$user = $this->tester->grabFixture('user', 1); |
||||
$model = new PasswordResetRequestForm(); |
||||
$model->email = $user['email']; |
||||
expect_not($model->sendEmail()); |
||||
} |
||||
|
||||
public function testSendEmailSuccessfully() |
||||
{ |
||||
$userFixture = $this->tester->grabFixture('user', 0); |
||||
|
||||
$model = new PasswordResetRequestForm(); |
||||
$model->email = $userFixture['email']; |
||||
$user = User::findOne(['password_reset_token' => $userFixture['password_reset_token']]); |
||||
|
||||
expect_that($model->sendEmail()); |
||||
expect_that($user->password_reset_token); |
||||
|
||||
$emailMessage = $this->tester->grabLastSentEmail(); |
||||
expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface'); |
||||
expect($emailMessage->getTo())->hasKey($model->email); |
||||
expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']); |
||||
} |
||||
} |
@ -1,44 +0,0 @@
|
||||
<?php |
||||
|
||||
namespace frontend\tests\unit\models; |
||||
|
||||
use common\fixtures\UserFixture; |
||||
use frontend\models\ResetPasswordForm; |
||||
|
||||
class ResetPasswordFormTest extends \Codeception\Test\Unit |
||||
{ |
||||
/** |
||||
* @var \frontend\tests\UnitTester |
||||
*/ |
||||
protected $tester; |
||||
|
||||
|
||||
public function _before() |
||||
{ |
||||
$this->tester->haveFixtures([ |
||||
'user' => [ |
||||
'class' => UserFixture::className(), |
||||
'dataFile' => codecept_data_dir() . 'user.php' |
||||
], |
||||
]); |
||||
} |
||||
|
||||
public function testResetWrongToken() |
||||
{ |
||||
$this->tester->expectException('yii\base\InvalidParamException', function() { |
||||
new ResetPasswordForm(''); |
||||
}); |
||||
|
||||
$this->tester->expectException('yii\base\InvalidParamException', function() { |
||||
new ResetPasswordForm('notexistingtoken_1391882543'); |
||||
}); |
||||
} |
||||
|
||||
public function testResetCorrectToken() |
||||
{ |
||||
$user = $this->tester->grabFixture('user', 0); |
||||
$form = new ResetPasswordForm($user['password_reset_token']); |
||||
expect_that($form->resetPassword()); |
||||
} |
||||
|
||||
} |
@ -1,59 +0,0 @@
|
||||
<?php |
||||
namespace frontend\tests\unit\models; |
||||
|
||||
use common\fixtures\UserFixture; |
||||
use frontend\models\SignupForm; |
||||
|
||||
class SignupFormTest extends \Codeception\Test\Unit |
||||
{ |
||||
/** |
||||
* @var \frontend\tests\UnitTester |
||||
*/ |
||||
protected $tester; |
||||
|
||||
|
||||
public function _before() |
||||
{ |
||||
$this->tester->haveFixtures([ |
||||
'user' => [ |
||||
'class' => UserFixture::className(), |
||||
'dataFile' => codecept_data_dir() . 'user.php' |
||||
] |
||||
]); |
||||
} |
||||
|
||||
public function testCorrectSignup() |
||||
{ |
||||
$model = new SignupForm([ |
||||
'username' => 'some_username', |
||||
'email' => 'some_email@example.com', |
||||
'password' => 'some_password', |
||||
]); |
||||
|
||||
$user = $model->signup(); |
||||
|
||||
expect($user)->isInstanceOf('common\models\User'); |
||||
|
||||
expect($user->username)->equals('some_username'); |
||||
expect($user->email)->equals('some_email@example.com'); |
||||
expect($user->validatePassword('some_password'))->true(); |
||||
} |
||||
|
||||
public function testNotCorrectSignup() |
||||
{ |
||||
$model = new SignupForm([ |
||||
'username' => 'troy.becker', |
||||
'email' => 'nicolas.dianna@hotmail.com', |
||||
'password' => 'some_password', |
||||
]); |
||||
|
||||
expect_not($model->signup()); |
||||
expect_that($model->getErrors('username')); |
||||
expect_that($model->getErrors('email')); |
||||
|
||||
expect($model->getFirstError('username')) |
||||
->equals(\Yii::t('auth', 'This username has already been taken.')); |
||||
expect($model->getFirstError('email')) |
||||
->equals(\Yii::t('auth', 'This email address has already been taken.')); |
||||
} |
||||
} |
Loading…
Reference in new issue