Skip to content

Commit

Permalink
Fix currentAsLink not working when determining state via voters. (#328
Browse files Browse the repository at this point in the history
)

The renderer was checking the items "current" state using its
`isCurrent()` method instead of the matcher, hence it would only work
when the state was manually set on the item via its `setCurrent()`
method.
  • Loading branch information
ndm2 committed Apr 22, 2020
1 parent 85b6546 commit 254fb64
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Knp/Menu/Renderer/ListRenderer.php
Expand Up @@ -188,7 +188,7 @@ protected function renderItem(ItemInterface $item, array $options): string
*/
protected function renderLink(ItemInterface $item, array $options = []): string
{
if ($item->getUri() && (!$item->isCurrent() || $options['currentAsLink'])) {
if ($item->getUri() && (!$this->matcher->isCurrent($item) || $options['currentAsLink'])) {
$text = $this->renderLinkElement($item, $options);
} else {
$text = $this->renderSpanElement($item, $options);
Expand Down
25 changes: 25 additions & 0 deletions tests/Knp/Menu/Tests/Renderer/AbstractRendererTest.php
Expand Up @@ -4,6 +4,7 @@

use Knp\Menu\Matcher\Matcher;
use Knp\Menu\Matcher\MatcherInterface;
use Knp\Menu\Matcher\Voter\UriVoter;
use Knp\Menu\MenuFactory;
use Knp\Menu\MenuItem;
use Knp\Menu\Tests\MenuTestCase;
Expand Down Expand Up @@ -223,6 +224,18 @@ public function testRenderWithCurrentItemAsLink(): void
$this->assertEquals($rendered, $this->renderer->render($menu));
}

public function testRenderWithCurrentItemAsLinkUsingMatcherWithVoters(): void
{
$matcher = new Matcher([new UriVoter('/about')]);
$this->renderer = $this->createRenderer($matcher);

$menu = new MenuItem('test', new MenuFactory());
$menu->addChild('About', ['uri' => '/about']);

$rendered = '<ul><li class="current first last"><a href="/about">About</a></li></ul>';
$this->assertEquals($rendered, $this->renderer->render($menu));
}

public function testRenderWithCurrentItemNotAsLink(): void
{
$menu = new MenuItem('test', new MenuFactory());
Expand All @@ -233,6 +246,18 @@ public function testRenderWithCurrentItemNotAsLink(): void
$this->assertEquals($rendered, $this->renderer->render($menu, ['currentAsLink' => false]));
}

public function testRenderWithCurrentItemNotAsLinkUsingMatcherWithVoters(): void
{
$matcher = new Matcher([new UriVoter('/about')]);
$this->renderer = $this->createRenderer($matcher);

$menu = new MenuItem('test', new MenuFactory());
$menu->addChild('About', ['uri' => '/about']);

$rendered = '<ul><li class="current first last"><span>About</span></li></ul>';
$this->assertEquals($rendered, $this->renderer->render($menu, ['currentAsLink' => false]));
}

public function testRenderSubMenuPortionWithClassAndTitle(): void
{
$this->pt2->setChildrenAttribute('class', 'parent2_class')->setChildrenAttribute('title', 'parent2 title');
Expand Down

0 comments on commit 254fb64

Please sign in to comment.