Skip to content

Commit

Permalink
added tests to verify issue yiisoft#16484
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
cebe committed Jul 24, 2018
1 parent 60d79aa commit d0712e4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/framework/base/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,49 @@ public function testCreateControllerByID()
$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
Expand Down
28 changes: 28 additions & 0 deletions tests/framework/web/UrlManagerParseUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,32 @@ public function testRulesCacheIsUsed()
]);
}
}

/**
* 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' => [
"<module:$modules>" => '<module>',
"<module:$modules>/<controller>" => '<module>/<controller>',
"<module:$modules>/<controller>/<action>" => '<module>/<controller>/<action>',
'<url:[a-zA-Z0-9-/]+>' => '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);

}
}

0 comments on commit d0712e4

Please sign in to comment.