Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

fix getHref strategy in PageMvc #4282

Closed
Closed
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
33 changes: 32 additions & 1 deletion library/Zend/Navigation/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class Mvc extends AbstractPage
*/
protected $routeMatch;

/**
* If true and set routeMatch than getHref will use routeMatch params
* to assemble uri
* @var bool
*/
protected $useRouteMatch = false;

/**
* Router for assembling URLs
*
Expand Down Expand Up @@ -190,7 +197,7 @@ public function getHref()
);
}

if ($this->getRouteMatch() !== null) {
if ($this->getUseRouteMatch() && $this->getRouteMatch() !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a null value would be highly unlikely here; I think you can omit the extra condition.

$rmParams = $this->getRouteMatch()->getParams();

if (isset($rmParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
Expand Down Expand Up @@ -427,6 +434,30 @@ public function setRouteMatch(RouteMatch $matches)
return $this;
}

/**
* Get the useRouteMatch
*
* @return bool
*/
public function getUseRouteMatch()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For boolean getters like this, we can drop the get prefix, and it becomes simply useRouteMatch().

{
return $this->useRouteMatch;
}

/**
* Set whether the page should use route match params for assembling link uri
*
* @see getHref()
* @param bool $useRoteMatch [optional]
* @return Mvc
*/
public function setUseRouteMatch($useRoteMatch = true)
{
$this->useRouteMatch = (bool) $useRoteMatch;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Rote/Route/g (I can do this on merge.)

$this->hrefCache = null;
return $this;
}

/**
* Get the router.
*
Expand Down
34 changes: 30 additions & 4 deletions tests/ZendTest/Navigation/Page/MvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,34 @@ public function testNoExceptionForGetHrefIfDefaultRouterIsSet()
$page->setDefaultRouter(null);
}

public function testBoolSetAndGetUseRouteMatch()
{
$page = new Page\Mvc(array(
'useRouteMatch' => 2,
));
$this->assertSame(true, $page->getUseRouteMatch());

$page->setUseRouteMatch(null);
$this->assertSame(false, $page->getUseRouteMatch());

$page->setUseRouteMatch(false);
$this->assertSame(false, $page->getUseRouteMatch());

$page->setUseRouteMatch(true);
$this->assertSame(true, $page->getUseRouteMatch());

$page->setUseRouteMatch();
$this->assertSame(true, $page->getUseRouteMatch());
}

public function testMvcPageParamsInheritRouteMatchParams()
{
$page = new Page\Mvc(array(
'label' => 'lollerblades',
'route' => 'lollerblades'
'route' => 'lollerblades',
));

$route = new SegmentRoute('/lollerblades/view/:serialNumber');
$route = new SegmentRoute('/lollerblades/view[/:serialNumber]');

$router = new TreeRouteStack;
$router->addRoute('lollerblades', $route);
Expand All @@ -558,17 +578,20 @@ public function testMvcPageParamsInheritRouteMatchParams()
$page->setRouter($router);
$page->setRouteMatch($routeMatch);

$this->assertEquals('/lollerblades/view', $page->getHref());

$page->setUseRouteMatch(true);
$this->assertEquals('/lollerblades/view/23', $page->getHref());
}

public function testInheritedRouteMatchParamsWorkWithModuleRouteListener()
{
$page = new Page\Mvc(array(
'label' => 'mpinkstonwashere',
'route' => 'lmaoplane'
'route' => 'lmaoplane',
));

$route = new SegmentRoute('/lmaoplane/:controller');
$route = new SegmentRoute('/lmaoplane[/:controller]');

$router = new TreeRouteStack;
$router->addRoute('lmaoplane', $route);
Expand All @@ -589,6 +612,9 @@ public function testInheritedRouteMatchParamsWorkWithModuleRouteListener()
$page->setRouter($event->getRouter());
$page->setRouteMatch($event->getRouteMatch());

$this->assertEquals('/lmaoplane', $page->getHref());

$page->setUseRouteMatch(true);
$this->assertEquals('/lmaoplane/index', $page->getHref());
}
}