From d0712e49187be2812e7e91afc4dd7cb3f0fdd3da Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 24 Jul 2018 16:43:50 +0200 Subject: [PATCH] added tests to verify issue #16484 the case is to match all module names before a catch-all URL rule. module routes should work with "module name only", "module name+controller name" and "module/controller/action". --- tests/framework/base/ModuleTest.php | 43 +++++++++++++++++++ .../framework/web/UrlManagerParseUrlTest.php | 28 ++++++++++++ 2 files changed, 71 insertions(+) diff --git a/tests/framework/base/ModuleTest.php b/tests/framework/base/ModuleTest.php index 83a9a0cbc4..f93768b347 100644 --- a/tests/framework/base/ModuleTest.php +++ b/tests/framework/base/ModuleTest.php @@ -147,6 +147,49 @@ class ModuleTest extends TestCase $route = 'very---complex---name---test'; $this->assertNotInstanceOf(VeryComplexNameTestController::className(), $module->createControllerByID($route)); } + + public function testCreateController() + { + // app module has a submodule "base" which has two controllers: "default" and "other" + $module = new Module('app'); + $module->setModule('base', new Module('base')); + $defaultController = ['class' => 'yii\web\Controller']; + $otherController = ['class' => 'yii\web\Controller']; + $module->getModule('base')->controllerMap = [ + 'default' => $defaultController, + 'other' => $otherController, + ]; + + list($controller, $action) = $module->createController('base'); + $this->assertSame('', $action); + $this->assertSame('base/default', $controller->uniqueId); + + list($controller, $action) = $module->createController('base/default'); + $this->assertSame('', $action); + $this->assertSame('base/default', $controller->uniqueId); + + list($controller, $action) = $module->createController('base/other'); + $this->assertSame('', $action); + $this->assertSame('base/other', $controller->uniqueId); + + list($controller, $action) = $module->createController('base/default/index'); + $this->assertSame('index', $action); + $this->assertSame('base/default', $controller->uniqueId); + + list($controller, $action) = $module->createController('base/other/index'); + $this->assertSame('index', $action); + $this->assertSame('base/other', $controller->uniqueId); + + list($controller, $action) = $module->createController('base/other/someaction'); + $this->assertSame('someaction', $action); + $this->assertSame('base/other', $controller->uniqueId); + + $controller = $module->createController('bases/default/index'); + $this->assertFalse($controller); + + $controller = $module->createController('nocontroller'); + $this->assertFalse($controller); + } } class TestModule extends \yii\base\Module diff --git a/tests/framework/web/UrlManagerParseUrlTest.php b/tests/framework/web/UrlManagerParseUrlTest.php index 652336dad2..d2ab77b972 100644 --- a/tests/framework/web/UrlManagerParseUrlTest.php +++ b/tests/framework/web/UrlManagerParseUrlTest.php @@ -434,4 +434,32 @@ class UrlManagerParseUrlTest extends TestCase ]); } } + + /** + * Test a scenario where catch-all rule is used at the end for a CMS but module names should use the module actions and controllers. + */ + public function testModuleRoute() + { + $modules = 'user|my-admin'; + + $manager = $this->getUrlManager([ + 'rules' => [ + "" => '', + "/" => '/', + "//" => '//', + '' => 'site/index', + ], + ]); + + $result = $manager->parseRequest($this->getRequest('user')); + $this->assertEquals(['user', []], $result); + $result = $manager->parseRequest($this->getRequest('user/somecontroller')); + $this->assertEquals(['user/somecontroller', []], $result); + $result = $manager->parseRequest($this->getRequest('user/somecontroller/someaction')); + $this->assertEquals(['user/somecontroller/someaction', []], $result); + + $result = $manager->parseRequest($this->getRequest('users/somecontroller/someaction')); + $this->assertEquals(['site/index', ['url' => 'users/somecontroller/someaction']], $result); + + } }