diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 5225b3d8e..32b224f79 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. This projec ## 1.0.13 (in progress) ++ [#1855](https://github.com/luyadev/luya/issues/1855) If create a url to an other module, don't replace the url with current module context. + ## 1.0.12 (8. October 2018) + [#1856](https://github.com/luyadev/luya/issues/1856) If application console method is runing, the cli determination should be forced. diff --git a/core/web/UrlManager.php b/core/web/UrlManager.php index 3fb6bc55e..57cc3413d 100644 --- a/core/web/UrlManager.php +++ b/core/web/UrlManager.php @@ -346,9 +346,9 @@ private function findModuleInRoute($route) private function urlReplaceModule($url, $navItemId, Composition $composition) { $route = $composition->removeFrom($this->removeBaseUrl($url)); - $module = $this->findModuleInRoute($route); + $moduleName = $this->findModuleInRoute($route); - if ($module === false || $this->menu === false) { + if ($moduleName === false || $this->menu === false) { return $url; } @@ -362,10 +362,15 @@ private function urlReplaceModule($url, $navItemId, Composition $composition) // another module which should not be modificated. // 2. If the current page (nav) context is the homepage, we have to keep the original link as it wont work because the homepage // does not have a route prefix. - if (($item->type == 2 && $module !== $item->moduleName) || $item->isHome) { + if (($item->type == 2 && $moduleName !== $item->moduleName) || $item->isHome) { return $url; } - - return preg_replace("/$module/", rtrim($item->link, '/'), ltrim($route, '/'), 1); + + // if current controller context has an other module as the requested url, its an outgoing link to another module which should not be modificated. + if ($moduleName !== Yii::$app->controller->module->id) { + return $url; + } + + return preg_replace("/$moduleName/", rtrim($item->link, '/'), ltrim($route, '/'), 1); } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 5a46faf8a..000000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - ./tests - - - - - ./vendor - ./tests - - - - - - - - \ No newline at end of file diff --git a/tests/core/web/UrlManagerTest.php b/tests/core/web/UrlManagerTest.php index fcb58021b..60e93c4e3 100644 --- a/tests/core/web/UrlManagerTest.php +++ b/tests/core/web/UrlManagerTest.php @@ -8,6 +8,10 @@ use luyatests\data\classes\UnitMenu; use luya\web\Composition; +class UrlStubController extends \luya\web\Controller +{ +} + /** * @author nadar */ @@ -212,11 +216,27 @@ public function testCreateMenuItemUrl() $urlManager = new UrlManager(); $menu = $urlManager->getMenu(); $this->assertNotFalse($menu); - + + Yii::$app->controller = new UrlStubController('stub', Yii::$app->getModule('unitmodule')); + $r = $urlManager->createMenuItemUrl(['unitmodule/controller/action'], 3); $this->assertSame('this-is-a-cms-link/controller/action', $r); } + + public function testCreateMenuItemUrlToOtherModule() + { + Yii::$app->set('menu', ['class' => UnitMenu::class]); + $urlManager = new UrlManager(); + $menu = $urlManager->getMenu(); + $this->assertNotFalse($menu); + + Yii::$app->controller = new UrlStubController('stub', Yii::$app->getModule('unitmodule')); + + $r = $urlManager->createMenuItemUrl(['othermodule/controller/action'], 3); + + $this->assertContains('/othermodule/controller/action', $r); + } public function testCreateMenuItemUrlWithHomeItem() { @@ -236,11 +256,27 @@ public function testCreateMenuItemUrlRedirectType2() $urlManager = new UrlManager(); $menu = $urlManager->getMenu(); $this->assertNotFalse($menu); - + + Yii::$app->controller = new UrlStubController('stub', Yii::$app->getModule('unitmodule')); + $r = $urlManager->createMenuItemUrl(['unitmodule/controller/action'], 2); $this->assertSame('this-is-a-module-type-page/controller/action', $r); } + + public function testCreateMenuItemUrlRedirectType2ToOtherModule() + { + Yii::$app->set('menu', ['class' => UnitMenu::class]); + $urlManager = new UrlManager(); + $menu = $urlManager->getMenu(); + $this->assertNotFalse($menu); + + Yii::$app->controller = new UrlStubController('stub', Yii::$app->getModule('unitmodule')); + + $r = $urlManager->createMenuItemUrl(['othermodule/controller/action'], 2); + + $this->assertContains('/othermodule/controller/action', $r); + } public function testCreateMenuItemUrlWithException() {