Skip to content

Commit

Permalink
Merge pull request #22 from dmaicher/remove_if_no_childs
Browse files Browse the repository at this point in the history
add Node::setRemoveIfNoChildren
  • Loading branch information
dmaicher authored May 20, 2021
2 parents bd2c7ad + 648096d commit 7b7dec0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Node
*/
protected $ifTrue = null;

/**
* @var bool
*/
protected $removeIfNoChildren = false;

/**
* @param null $label
*/
Expand Down Expand Up @@ -476,4 +481,19 @@ public function hasUrl()
{
return $this->url !== null;
}

public function isRemoveIfNoChildren(): bool
{
return $this->removeIfNoChildren;
}

/**
* @return $this
*/
public function setRemoveIfNoChildren(bool $removeIfNoChildren): self
{
$this->removeIfNoChildren = $removeIfNoChildren;

return $this;
}
}
15 changes: 14 additions & 1 deletion src/NodeVisitor/NodeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,23 @@ public function visit(Node $node)
{
foreach ($node->getRequiredPermissions() as $permission) {
if (!$this->tokenStorage->getToken() || !$this->authChecker->isGranted($permission)) {
$node->getParent()->removeChild($node);
$this->removeNode($node);

return MenuTreeTraverserInterface::STOP_TRAVERSAL;
}
}
}

private function removeNode(Node $node): void
{
$parentNode = $node->getParent();
$parentNode->removeChild($node);

if (!$parentNode->isRootNode()
&& $parentNode->isRemoveIfNoChildren()
&& count($parentNode->getChildren()) === 0
) {
$parentNode->getParent()->removeChild($parentNode);
}
}
}
38 changes: 36 additions & 2 deletions tests/NodeVisitor/NodeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DAMA\MenuBundle\MenuTree\MenuTreeTraverserInterface;
use DAMA\MenuBundle\Node\Node;
use DAMA\MenuBundle\Node\NodeFactory;
use DAMA\MenuBundle\NodeVisitor\NodeFilter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -19,12 +20,12 @@ class NodeFilterTest extends TestCase
private $filter;

/**
* @var TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject
* @var TokenStorageInterface|MockObject
*/
private $tokenStorage;

/**
* @var AuthorizationCheckerInterface|\PHPUnit_Framework_MockObject_MockObject
* @var AuthorizationCheckerInterface|MockObject
*/
private $authChecker;

Expand Down Expand Up @@ -82,6 +83,39 @@ public function testVisit(array $permissions, $getTokenReturn, $isGrantedReturn,
}
}

/**
* @testWith [true]
* [false]
*/
public function testRemoveParentIfNoActiveChildren(bool $remove): void
{
$tree = (new NodeFactory())->create();
$tree
->child('foo')
->setRemoveIfNoChildren($remove)
->child('bar')
->setRequiredPermissions(['foo'])
->end()
->end()
;

$this->tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue(null))
;

$children = $tree->getChildren();
$foo = reset($children);
$this->filter->visit($foo);
$this->assertCount(1, $tree->getChildren());

$children = $foo->getChildren();
$bar = reset($children);
$this->filter->visit($bar);
$this->assertCount($remove ? 0 : 1, $tree->getChildren());
}

public function getTestData()
{
return [
Expand Down

0 comments on commit 7b7dec0

Please sign in to comment.