function MenuLinkTreeTest::testCreateLinksInMenu
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php \Drupal\KernelTests\Core\Menu\MenuLinkTreeTest::testCreateLinksInMenu()
- 8.9.x core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php \Drupal\KernelTests\Core\Menu\MenuLinkTreeTest::testCreateLinksInMenu()
- 10 core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php \Drupal\KernelTests\Core\Menu\MenuLinkTreeTest::testCreateLinksInMenu()
Tests creating links with an expected tree structure.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Menu/ MenuLinkTreeTest.php, line 92
Class
- MenuLinkTreeTest
- Tests the menu link tree.
Namespace
Drupal\KernelTests\Core\MenuCode
public function testCreateLinksInMenu() : void {
// This creates a tree with the following structure:
// - 1
// - 2
// - 3
// - 4
// - 5
// - 7
// - 6
// - 8
// With link 6 being the only external link.
$links = [
1 => MenuLinkMock::create([
'id' => 'test.example1',
'route_name' => 'example1',
'title' => 'foo',
'parent' => '',
]),
2 => MenuLinkMock::create([
'id' => 'test.example2',
'route_name' => 'example2',
'title' => 'bar',
'parent' => 'test.example1',
'route_parameters' => [
'foo' => 'bar',
],
]),
3 => MenuLinkMock::create([
'id' => 'test.example3',
'route_name' => 'example3',
'title' => 'baz',
'parent' => 'test.example2',
'route_parameters' => [
'baz' => 'qux',
],
]),
4 => MenuLinkMock::create([
'id' => 'test.example4',
'route_name' => 'example4',
'title' => 'qux',
'parent' => 'test.example3',
]),
5 => MenuLinkMock::create([
'id' => 'test.example5',
'route_name' => 'example5',
'title' => 'title5',
'parent' => '',
]),
6 => MenuLinkMock::create([
'id' => 'test.example6',
'route_name' => '',
'url' => 'https://www.drupal.org/',
'title' => 'bar_bar',
'parent' => '',
]),
7 => MenuLinkMock::create([
'id' => 'test.example7',
'route_name' => 'example7',
'title' => 'title7',
'parent' => '',
]),
8 => MenuLinkMock::create([
'id' => 'test.example8',
'route_name' => 'example8',
'title' => 'title8',
'parent' => '',
]),
];
foreach ($links as $instance) {
$this->menuLinkManager
->addDefinition($instance->getPluginId(), $instance->getPluginDefinition());
}
$parameters = new MenuTreeParameters();
$tree = $this->linkTree
->load('mock', $parameters);
$count = function (array $tree) {
$sum = function ($carry, MenuLinkTreeElement $item) {
return $carry + $item->count();
};
return array_reduce($tree, $sum);
};
$this->assertEquals(8, $count($tree));
$parameters = new MenuTreeParameters();
$parameters->setRoot('test.example2');
$tree = $this->linkTree
->load($instance->getMenuName(), $parameters);
$top_link = reset($tree);
$this->assertCount(1, $top_link->subtree);
$child = reset($top_link->subtree);
$this->assertEquals($links[3]->getPluginId(), $child->link
->getPluginId());
$height = $this->linkTree
->getSubtreeHeight('test.example2');
$this->assertEquals(3, $height);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.