diff --git a/api/config/main-dev.php b/api/config/config-dev.php similarity index 100% rename from api/config/main-dev.php rename to api/config/config-dev.php diff --git a/api/config/main.php b/api/config/config.php similarity index 100% rename from api/config/main.php rename to api/config/config.php diff --git a/api/config/params.php b/api/config/params.php index d0b9c34..5412ed8 100644 --- a/api/config/params.php +++ b/api/config/params.php @@ -1,3 +1,4 @@ getenv('AUTHSERVER_HOST'), ]; diff --git a/api/modules/session/controllers/SessionController.php b/api/modules/session/controllers/SessionController.php index f335fa2..f120ba8 100644 --- a/api/modules/session/controllers/SessionController.php +++ b/api/modules/session/controllers/SessionController.php @@ -25,7 +25,7 @@ class SessionController extends ApiController { $behaviors['rateLimiting'] = [ 'class' => RateLimiter::class, 'only' => ['has-joined', 'has-joined-legacy'], - 'authserverDomain' => getenv('AUTHSERVER_HOST'), + 'authserverDomain' => Yii::$app->params['authserverHost'], ]; return $behaviors; diff --git a/api/modules/session/filters/RateLimiter.php b/api/modules/session/filters/RateLimiter.php index b5eb490..71cbf65 100644 --- a/api/modules/session/filters/RateLimiter.php +++ b/api/modules/session/filters/RateLimiter.php @@ -41,8 +41,7 @@ class RateLimiter extends \yii\filters\RateLimiter { * @inheritdoc */ public function checkRateLimit($user, $request, $response, $action) { - // TODO: теперь в authserverDomain хранится hostname без schema, а getHostInfo() возвращает с http(s). - if ($request->getHostInfo() === $this->authserverDomain) { + if (parse_url($request->getHostInfo(), PHP_URL_HOST) === $this->authserverDomain) { return; } diff --git a/api/web/index.php b/api/web/index.php index e94ef6e..38ed822 100644 --- a/api/web/index.php +++ b/api/web/index.php @@ -3,14 +3,12 @@ require __DIR__ . '/../../vendor/autoload.php'; defined('YII_DEBUG') or define('YII_DEBUG', (boolean)getenv('YII_DEBUG')); defined('YII_ENV') or define('YII_ENV', getenv('YII_ENV')); -defined('YII_APPLICATION_TYPE') or define('YII_APPLICATION_TYPE', 'web'); -defined('YII_APPLICATION_MODULE') or define('YII_APPLICATION_MODULE', 'api'); require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php'; require __DIR__ . '/../../common/config/bootstrap.php'; require __DIR__ . '/../config/bootstrap.php'; -$config = require __DIR__ . './../../common/config/config-loader.php'; +$config = \common\config\ConfigLoader::load('api'); $application = new yii\web\Application($config); $application->run(); diff --git a/common/config/.gitignore b/common/config/.gitignore index e7d849d..059a02c 100644 --- a/common/config/.gitignore +++ b/common/config/.gitignore @@ -1 +1 @@ -main-local.php +config-local.php diff --git a/common/config/ConfigLoader.php b/common/config/ConfigLoader.php new file mode 100644 index 0000000..c94c4e1 --- /dev/null +++ b/common/config/ConfigLoader.php @@ -0,0 +1,65 @@ +application = $application; + } + + public function getEnvironment() : string { + return YII_ENV; + } + + public function getConfig() : array { + $toMerge = [ + require __DIR__ . '/config.php', + ]; + + // Общие окружение-зависимые настройки + $path = __DIR__ . '/config-' . YII_ENV . '.php'; + if (file_exists($path)) { + $toMerge[] = require $path; + } + + // Общие локальные настройки + $path = __DIR__ . '/config-local.php'; + if (file_exists($path)) { + $toMerge[] = require $path; + } + + // Настройки конкретного приложения + $path = self::ROOT_PATH . '/' . $this->application . '/config/config.php'; + if (file_exists($path)) { + $toMerge[] = require $path; + } + + // Настройки конкретного приложения для действующего окружения + $path = self::ROOT_PATH . '/' . $this->application . '/config/main-' . YII_ENV . '.php'; + if (file_exists($path)) { + $toMerge[] = require $path; + } + + // Локальные настройки конкретного приложения + $path = self::ROOT_PATH . '/' . $this->application . '/config/config-local.php'; + if (file_exists($path)) { + $toMerge[] = require $path; + } + + return ArrayHelper::merge(...$toMerge); + } + + public static function load(string $application) : array { + return (new static($application))->getConfig(); + } + +} diff --git a/common/config/main-dev.php b/common/config/config-dev.php similarity index 100% rename from common/config/main-dev.php rename to common/config/config-dev.php diff --git a/common/config/config-loader.php b/common/config/config-loader.php deleted file mode 100644 index 906fee0..0000000 --- a/common/config/config-loader.php +++ /dev/null @@ -1,43 +0,0 @@ -mockRequest(); + Yii::$container->set(Validator::class, new class extends Validator { + public function validateValue($value) { + return null; + } + }); } protected function tearDown() { diff --git a/tests/codeception/api/unit/models/authentication/RepeatAccountActivationFormTest.php b/tests/codeception/api/unit/models/authentication/RepeatAccountActivationFormTest.php index 8bcf51f..e132ace 100644 --- a/tests/codeception/api/unit/models/authentication/RepeatAccountActivationFormTest.php +++ b/tests/codeception/api/unit/models/authentication/RepeatAccountActivationFormTest.php @@ -1,6 +1,7 @@ fileTransportCallback = function () { return 'testing_message.eml'; }; + Yii::$container->set(Validator::class, new class extends Validator { + public function validateValue($value) { + return null; + } + }); } protected function tearDown() { diff --git a/tests/codeception/api/unit/modules/session/filters/RateLimiterTest.php b/tests/codeception/api/unit/modules/session/filters/RateLimiterTest.php index e99a699..a0c3710 100644 --- a/tests/codeception/api/unit/modules/session/filters/RateLimiterTest.php +++ b/tests/codeception/api/unit/modules/session/filters/RateLimiterTest.php @@ -24,6 +24,9 @@ class RateLimiterTest extends TestCase { /** @var RateLimiter|\PHPUnit_Framework_MockObject_MockObject $filter */ $filter = $this->getMockBuilder(RateLimiter::class) + ->setConstructorArgs([[ + 'authserverDomain' => Yii::$app->params['authserverHost'] + ]]) ->setMethods(['getServer']) ->getMock(); @@ -54,7 +57,9 @@ class RateLimiterTest extends TestCase { ->method('getHostInfo') ->will($this->returnValue('http://authserver.ely.by')); - $filter = new RateLimiter(); + $filter = new RateLimiter([ + 'authserverDomain' => Yii::$app->params['authserverHost'] + ]); $filter->checkRateLimit(null, $request, null, null); } @@ -86,6 +91,7 @@ class RateLimiterTest extends TestCase { $filter = $this->getMockBuilder(RateLimiter::class) ->setConstructorArgs([[ 'limit' => 3, + 'authserverDomain' => Yii::$app->params['authserverHost'], ]]) ->setMethods(['getServer']) ->getMock(); diff --git a/tests/codeception/bin/_bootstrap.php b/tests/codeception/bin/_bootstrap.php index f1fbb72..8b78584 100644 --- a/tests/codeception/bin/_bootstrap.php +++ b/tests/codeception/bin/_bootstrap.php @@ -1,19 +1,10 @@ -rem @link http://www.yiiframework.com/ -rem @copyright Copyright (c) 2008 Yii Software LLC -rem @license http://www.yiiframework.com/license/ -rem ------------------------------------------------------------- - -@setlocal - -set YII_PATH=%~dp0 - -if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe - -"%PHP_COMMAND%" "%YII_PATH%yii" %* - -@endlocal diff --git a/tests/codeception/common/_bootstrap.php b/tests/codeception/common/_bootstrap.php index eee8f45..577fef2 100644 --- a/tests/codeception/common/_bootstrap.php +++ b/tests/codeception/common/_bootstrap.php @@ -1,12 +1,11 @@ [ - 'authserverDomain' => 'http://authserver.ely.by', + 'authserverHost' => 'authserver.ely.by', ], ]; diff --git a/tests/codeception/config/api/functional.php b/tests/codeception/config/api/functional.php index 8d2efde..d515572 100644 --- a/tests/codeception/config/api/functional.php +++ b/tests/codeception/config/api/functional.php @@ -2,16 +2,9 @@ $_SERVER['SCRIPT_FILENAME'] = API_ENTRY_FILE; $_SERVER['SCRIPT_NAME'] = API_ENTRY_URL; -/** - * Application configuration for api functional tests - */ return yii\helpers\ArrayHelper::merge( - require(YII_APP_BASE_PATH . '/common/config/main.php'), - require(YII_APP_BASE_PATH . '/common/config/main-local.php'), require(YII_APP_BASE_PATH . '/api/config/main.php'), - require(YII_APP_BASE_PATH . '/api/config/main-local.php'), - require(dirname(__DIR__) . '/config.php'), - require(dirname(__DIR__) . '/functional.php'), - require(__DIR__ . '/config.php'), - [ - ] + \common\config\ConfigLoader::load('api'), + require __DIR__ . '/../config.php', + require __DIR__ . '/../functional.php', + require __DIR__ . '/config.php' ); diff --git a/tests/codeception/config/api/unit.php b/tests/codeception/config/api/unit.php index 5bdca09..2e2ab12 100644 --- a/tests/codeception/config/api/unit.php +++ b/tests/codeception/config/api/unit.php @@ -1,15 +1,7 @@ 'app-common', 'basePath' => dirname(__DIR__), diff --git a/tests/codeception/config/config.php b/tests/codeception/config/config.php index 395f425..3fbe89e 100644 --- a/tests/codeception/config/config.php +++ b/tests/codeception/config/config.php @@ -3,7 +3,7 @@ return [ 'language' => 'en-US', 'controllerMap' => [ 'fixture' => [ - 'class' => 'yii\faker\FixtureController', + 'class' => yii\faker\FixtureController::class, 'fixtureDataPath' => '@tests/codeception/common/fixtures/data', 'templatePath' => '@tests/codeception/common/templates/fixtures', 'namespace' => 'tests\codeception\common\fixtures', diff --git a/tests/codeception/config/console/unit.php b/tests/codeception/config/console/unit.php index 4d3aeb0..b0ad993 100644 --- a/tests/codeception/config/console/unit.php +++ b/tests/codeception/config/console/unit.php @@ -1,14 +1,6 @@ [ 'request' => [ // it's not recommended to run functional tests with CSRF validation enabled + // TODO: у нас вроде и без того нет проверки csrf 'enableCsrfValidation' => false, + 'enableCookieValidation' => false, // but if you absolutely need it set cookie domain to localhost - /* - 'csrfCookie' => [ - 'domain' => 'localhost', - ], - */ ], ], -]; \ No newline at end of file +]; diff --git a/tests/codeception/config/unit.php b/tests/codeception/config/unit.php index 6bd08d3..bee73cf 100644 --- a/tests/codeception/config/unit.php +++ b/tests/codeception/config/unit.php @@ -1,7 +1,4 @@ run();