Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1855 #1861

Merged
merged 4 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 10 additions & 5 deletions core/web/UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
}
28 changes: 0 additions & 28 deletions phpunit.xml.dist

This file was deleted.

40 changes: 38 additions & 2 deletions tests/core/web/UrlManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use luyatests\data\classes\UnitMenu;
use luya\web\Composition;

class UrlStubController extends \luya\web\Controller
{
}

/**
* @author nadar
*/
Expand Down Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down