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

Fix: active state in side menu for nested child admins #8173

Merged
merged 3 commits into from
Apr 10, 2024
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
43 changes: 27 additions & 16 deletions src/Menu/Matcher/Voter/AdminVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,43 @@ public function __construct(

public function matchItem(ItemInterface $item): ?bool
{
$admin = $item->getExtra('admin');

$request = $this->requestStack->getMainRequest();
if (null === $request) {
return null;
}

if ($admin instanceof AdminInterface
$admin = $item->getExtra('admin');
if (
$admin instanceof AdminInterface
&& $admin->hasRoute('list') && $admin->hasAccess('list')
&& null !== $request
&& $this->match($admin, $request->get('_sonata_admin'))
) {
$requestCode = $request->get('_sonata_admin');

if ($admin->getCode() === $requestCode) {
return true;
}

foreach ($admin->getChildren() as $child) {
if ($child->getBaseCodeRoute() === $requestCode) {
return true;
}
}
return true;
}

$route = $item->getExtra('route');
if (null !== $route && null !== $request && $route === $request->get('_route')) {
if (null !== $route && $route === $request->get('_route')) {
return true;
}

return null;
}

/**
* @param AdminInterface<object> $admin
*/
private function match(AdminInterface $admin, mixed $requestCode): bool
{
if ($admin->getBaseCodeRoute() === $requestCode) {
return true;
}

foreach ($admin->getChildren() as $child) {
if ($this->match($child, $requestCode)) {
return true;
}
}

return false;
}
}
50 changes: 48 additions & 2 deletions tests/Menu/Matcher/Voter/AdminVoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function provideMatchingCases(): iterable
yield 'has admin' => [$this->getAdmin('_sonata_admin', true, true), '_sonata_admin', null, true];
yield 'has child admin' => [$this->getChildAdmin('_sonata_admin', '_sonata_child_admin', true, true), '_sonata_admin|_sonata_child_admin', null, true];
yield 'has bad child admin' => [$this->getChildAdmin('_sonata_admin', '_sonata_child_admin', true, true), '_sonata_admin|_sonata_child_admin_unexpected', null, null];
yield 'has nested child admin' => [$this->getNestedChildAdmin('_sonata_admin', '_sonata_child_admin', '_sonata_nested_child_admin', true, true), '_sonata_admin|_sonata_child_admin|_sonata_nested_child_admin', null, true];
yield 'has bad nested child admin' => [$this->getNestedChildAdmin('_sonata_admin', '_sonata_child_admin', '_sonata_nested_child_admin', true, true), '_sonata_admin|_sonata_child_admin|_sonata_nested_child_admin_unexpected', null, null];
yield 'direct link' => ['admin_post', null, 'admin_post', true];
yield 'no direct link' => ['admin_post', null, 'admin_blog', null];
}
Expand All @@ -81,7 +83,7 @@ private function getAdmin(string $code, bool $list = false, bool $granted = fals
->with('list')
->willReturn($granted);
$admin
->method('getCode')
->method('getBaseCodeRoute')
->willReturn($code);
$admin
->method('getChildren')
Expand Down Expand Up @@ -109,7 +111,7 @@ private function getChildAdmin(
->with('list')
->willReturn($granted);
$parentAdmin
->method('getCode')
->method('getBaseCodeRoute')
->willReturn($parentCode);

$childAdmin = $this->createMock(AdminInterface::class);
Expand All @@ -123,4 +125,48 @@ private function getChildAdmin(

return $parentAdmin;
}

/**
* @return AdminInterface<object>
*/
private function getNestedChildAdmin(
string $grandParentCode,
string $parentCode,
string $childCode,
bool $list = false,
bool $granted = false
): AdminInterface {
$grandParentAdmin = $this->createMock(AdminInterface::class);
$grandParentAdmin
->method('hasRoute')
->with('list')
->willReturn($list);
$grandParentAdmin
->method('hasAccess')
->with('list')
->willReturn($granted);
$grandParentAdmin
->method('getBaseCodeRoute')
->willReturn($grandParentCode);

$parentAdmin = $this->createMock(AdminInterface::class);
$parentAdmin
->method('getBaseCodeRoute')
->willReturn(sprintf('%s|%s', $grandParentCode, $parentCode));

$grandParentAdmin
->method('getChildren')
->willReturn([$parentAdmin]);

$childAdmin = $this->createMock(AdminInterface::class);
$childAdmin
->method('getBaseCodeRoute')
->willReturn(sprintf('%s|%s|%s', $grandParentCode, $parentCode, $childCode));

$parentAdmin
->method('getChildren')
->willReturn([$childAdmin]);

return $grandParentAdmin;
}
}
Loading