diff --git a/src/Helpers/MenuItemHelper.php b/src/Helpers/MenuItemHelper.php index edc62729..5df95a43 100644 --- a/src/Helpers/MenuItemHelper.php +++ b/src/Helpers/MenuItemHelper.php @@ -64,6 +64,14 @@ public static function isLegacySearch($item) */ public static function isAllowed($item) { + // We won't allow empty submenu items on the menu. + + if (self::isSubmenu($item) && ! count($item['submenu'])) { + return false; + } + + // In any other case, fallback to the restricted property. + return $item && empty($item['restricted']); } } diff --git a/src/Menu/Filters/GateFilter.php b/src/Menu/Filters/GateFilter.php index b68fd0f9..9fbd57da 100644 --- a/src/Menu/Filters/GateFilter.php +++ b/src/Menu/Filters/GateFilter.php @@ -36,10 +36,7 @@ public function transform($item) // Set a special attribute when item is not allowed. Items with this // attribute will be filtered out of the menu. - $isWholeRestrictedSubmenu = MenuItemHelper::isSubmenu($item) - && $this->allItemsRestricted($item['submenu']); - - if (! $this->isAllowed($item) || $isWholeRestrictedSubmenu) { + if (! $this->isAllowed($item)) { $item['restricted'] = true; } @@ -72,23 +69,4 @@ protected function isAllowed($item) return true; } - - /** - * Check if a set of items are all restricted (or unallowed). - * - * @param array $items An array with the menu items to check - * @return bool - */ - protected function allItemsRestricted($items) - { - // Check if every provided item is restricted. - - foreach ($items as $item) { - if ($this->isAllowed($item)) { - return false; - } - } - - return true; - } } diff --git a/tests/AdminLteTest.php b/tests/AdminLteTest.php index 9acaa0a2..df1da26a 100644 --- a/tests/AdminLteTest.php +++ b/tests/AdminLteTest.php @@ -43,9 +43,16 @@ public function addMenuItems(BuildingMenu $event) $event->menu->add(['text' => 'searchLT', 'type' => 'navbar-search', 'topnav' => true]); - // Add (1) submenu to the sidebar menu. + // Add (1) empty submenu to the sidebar menu. This item should be + // filtered out of the menu. - $event->menu->add(['text' => 'submenu', 'submenu' => []]); + $event->menu->add(['text' => 'submenu1', 'submenu' => []]); + + // Add (1) non empty submenu to the sidebar menu. + + $event->menu->add(['text' => 'submenu2', 'submenu' => [ + ['text' => 'subitem', 'url' => 'url'] + ]]); // Add (1) invalid item. @@ -70,7 +77,7 @@ public function testMenuWithoutFilters() $this->assertEquals('topnavRT', $menu[6]['text']); $this->assertEquals('topnavUT', $menu[7]['text']); $this->assertEquals('searchLT', $menu[8]['text']); - $this->assertEquals('submenu', $menu[9]['text']); + $this->assertEquals('submenu2', $menu[9]['text']); $this->assertEquals('invalid', $menu[10]['text']); $this->assertEquals('search', $menu[11]['text']); } @@ -90,7 +97,7 @@ public function testMenuSidebarFilter() $this->assertEquals('topnavLF', $menu[2]['text']); $this->assertEquals('topnavRF', $menu[3]['text']); $this->assertEquals('topnavUF', $menu[4]['text']); - $this->assertEquals('submenu', $menu[9]['text']); + $this->assertEquals('submenu2', $menu[9]['text']); $this->assertEquals('search', $menu[11]['text']); } @@ -131,7 +138,7 @@ public function testMenuNavbarLeftFilter() $this->assertEquals('topnavUF', $menu[4]['text']); $this->assertEquals('topnavLT', $menu[5]['text']); $this->assertEquals('searchLT', $menu[8]['text']); - $this->assertEquals('submenu', $menu[9]['text']); + $this->assertEquals('submenu2', $menu[9]['text']); $this->assertEquals('search', $menu[11]['text']); } diff --git a/tests/Menu/BuilderTest.php b/tests/Menu/BuilderTest.php index 1dd849dc..ef7c5b97 100644 --- a/tests/Menu/BuilderTest.php +++ b/tests/Menu/BuilderTest.php @@ -851,7 +851,7 @@ public function testCanOnWholeRestrictedSubmenu() $builder->add( [ - 'text' => 'Submenu', + 'text' => 'Submenu1', 'submenu' => [ [ 'text' => 'About', @@ -863,6 +863,21 @@ public function testCanOnWholeRestrictedSubmenu() 'url' => '/', 'can' => 'show-home', ], + [ + 'text' => 'Submenu2', + 'submenu' => [ + [ + 'text' => 'Home1', + 'url' => '/home1', + 'can' => 'show-home', + ], + [ + 'text' => 'Home2', + 'url' => '/home2', + 'can' => 'show-home', + ], + ], + ], ], ] );