Skip to content
Snippets Groups Projects
Unverified Commit 7b23a99b authored by Pablo López's avatar Pablo López
Browse files

Basic test coverage

parent 9ef494eb
No related branches found
No related tags found
1 merge request!8026Issue #3415572: Adjust how Help link and Content links are removed from the Administration menu for the Navigation bar
......@@ -202,12 +202,6 @@ public function navigationMenuLinkTreeAlter(array &$tree): void {
if ($plugin_id == 'help.main' || $plugin_id == 'system.admin_content') {
unset($tree[$key]);
}
// Remove child items of content menu, if any.
$parent = $item->link->getParent();
if ($parent == 'system.admin_content') {
unset($tree[$key]);
}
}
}
......
......@@ -34,4 +34,24 @@ public function blockAlter(&$definitions): void {
}
}
/**
* Implements hook_navigation_menu_link_tree_alter().
*/
#[Hook('navigation_menu_link_tree_alter')]
public function navigationMenuLinkTreeAlter(array &$tree): void {
foreach ($tree as $key => $item) {
// Skip elements where menu is not the 'admin' one.
$menu_name = $item->link->getMenuName();
// Removes all items from menu1.
if ($menu_name == 'menu1') {
unset($tree[$key]);
}
// Updates title for items in menu2
if ($menu_name == 'menu2') {
$item->link->updateLink(['title' => 'New Link Title'], FALSE);
}
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\navigation\Kernel;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\KernelTests\KernelTestBase;
use Drupal\navigation\Menu\NavigationMenuLinkTree;
/**
* Tests \Drupal\navigation\Menu\NavigationMenuLinkTree.
*
* @group navigation
*
* @see \Drupal\navigation\Menu\NavigationMenuLinkTree
*/
class NavigationMenuLinkTreeTest extends KernelTestBase {
/**
* The tested navigation menu link tree.
*
* @var \Drupal\navigation\Menu\NavigationMenuLinkTree
*/
protected NavigationMenuLinkTree $linkTree;
/**
* {@inheritdoc}
*/
protected static $modules = [
'field',
'layout_builder',
'layout_discovery',
'link',
'menu_link_content',
'menu_test',
'navigation',
'navigation_test',
'system',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('menu_link_content');
$this->linkTree = $this->container->get('navigation.menu_tree');
}
/**
* Tests the hook_navigation_menu_link_tree_alter logic.
*/
public function testNavigationMenuLinkTreeAlter(): void {
/** @var \Drupal\system\MenuStorage $storage */
$storage = \Drupal::entityTypeManager()->getStorage('menu');
$storage->create(['id' => 'menu1', 'label' => 'Menu 1'])->save();
$storage->create(['id' => 'menu2', 'label' => 'Menu 2'])->save();
\Drupal::entityTypeManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
\Drupal::entityTypeManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
\Drupal::entityTypeManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu2', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
$output = $this->linkTree->load('menu1', new MenuTreeParameters());
$this->assertCount(2, $output);
$output = $this->linkTree->transform($output, []);
$this->assertCount(0, $output);
$output = $this->linkTree->load('menu2', new MenuTreeParameters());
$this->assertCount(1, $output);
$output = $this->linkTree->transform($output, []);
$this->assertCount(1, $output);
$item = reset($output);
$this->assertSame('New Link Title', $item->link->getTitle());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment