04c04aa3e3f49ba1e69db55d5341311153b44a76
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Menu / MenuLinkTreeTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Menu;
4
5 use Drupal\Core\Menu\MenuLinkTreeElement;
6 use Drupal\Core\Menu\MenuTreeParameters;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\Tests\Core\Menu\MenuLinkMock;
9
10 /**
11  * Tests the menu link tree.
12  *
13  * @group Menu
14  *
15  * @see \Drupal\Core\Menu\MenuLinkTree
16  */
17 class MenuLinkTreeTest extends KernelTestBase {
18
19   /**
20    * The tested menu link tree.
21    *
22    * @var \Drupal\Core\Menu\MenuLinkTree
23    */
24   protected $linkTree;
25
26   /**
27    * The menu link plugin manager.
28    *
29    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
30    */
31   protected $menuLinkManager;
32
33   /**
34    * Modules to enable.
35    *
36    * @var array
37    */
38   public static $modules = [
39     'system',
40     'menu_test',
41     'menu_link_content',
42     'field',
43     'link',
44   ];
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51     \Drupal::service('router.builder')->rebuild();
52     $this->installEntitySchema('menu_link_content');
53
54     $this->linkTree = $this->container->get('menu.link_tree');
55     $this->menuLinkManager = $this->container->get('plugin.manager.menu.link');
56   }
57
58   /**
59    * Tests deleting all the links in a menu.
60    */
61   public function testDeleteLinksInMenu() {
62     \Drupal::entityManager()->getStorage('menu')->create(['id' => 'menu1'])->save();
63     \Drupal::entityManager()->getStorage('menu')->create(['id' => 'menu2'])->save();
64
65     \Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
66     \Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
67     \Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu2', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
68
69     $output = $this->linkTree->load('menu1', new MenuTreeParameters());
70     $this->assertEqual(count($output), 2);
71     $output = $this->linkTree->load('menu2', new MenuTreeParameters());
72     $this->assertEqual(count($output), 1);
73
74     $this->menuLinkManager->deleteLinksInMenu('menu1');
75
76     $output = $this->linkTree->load('menu1', new MenuTreeParameters());
77     $this->assertEqual(count($output), 0);
78
79     $output = $this->linkTree->load('menu2', new MenuTreeParameters());
80     $this->assertEqual(count($output), 1);
81   }
82
83   /**
84    * Tests creating links with an expected tree structure.
85    */
86   public function testCreateLinksInMenu() {
87     // This creates a tree with the following structure:
88     // - 1
89     // - 2
90     //   - 3
91     //     - 4
92     // - 5
93     //   - 7
94     // - 6
95     // - 8
96     // With link 6 being the only external link.
97
98     $links = [
99       1 => MenuLinkMock::create(['id' => 'test.example1', 'route_name' => 'example1', 'title' => 'foo', 'parent' => '']),
100       2 => MenuLinkMock::create(['id' => 'test.example2', 'route_name' => 'example2', 'title' => 'bar', 'parent' => 'test.example1', 'route_parameters' => ['foo' => 'bar']]),
101       3 => MenuLinkMock::create(['id' => 'test.example3', 'route_name' => 'example3', 'title' => 'baz', 'parent' => 'test.example2', 'route_parameters' => ['baz' => 'qux']]),
102       4 => MenuLinkMock::create(['id' => 'test.example4', 'route_name' => 'example4', 'title' => 'qux', 'parent' => 'test.example3']),
103       5 => MenuLinkMock::create(['id' => 'test.example5', 'route_name' => 'example5', 'title' => 'foofoo', 'parent' => '']),
104       6 => MenuLinkMock::create(['id' => 'test.example6', 'route_name' => '', 'url' => 'https://www.drupal.org/', 'title' => 'barbar', 'parent' => '']),
105       7 => MenuLinkMock::create(['id' => 'test.example7', 'route_name' => 'example7', 'title' => 'bazbaz', 'parent' => '']),
106       8 => MenuLinkMock::create(['id' => 'test.example8', 'route_name' => 'example8', 'title' => 'quxqux', 'parent' => '']),
107     ];
108     foreach ($links as $instance) {
109       $this->menuLinkManager->addDefinition($instance->getPluginId(), $instance->getPluginDefinition());
110     }
111     $parameters = new MenuTreeParameters();
112     $tree = $this->linkTree->load('mock', $parameters);
113
114     $count = function (array $tree) {
115       $sum = function ($carry, MenuLinkTreeElement $item) {
116         return $carry + $item->count();
117       };
118       return array_reduce($tree, $sum);
119     };
120
121     $this->assertEqual($count($tree), 8);
122     $parameters = new MenuTreeParameters();
123     $parameters->setRoot('test.example2');
124     $tree = $this->linkTree->load($instance->getMenuName(), $parameters);
125     $top_link = reset($tree);
126     $this->assertEqual(count($top_link->subtree), 1);
127     $child = reset($top_link->subtree);
128     $this->assertEqual($child->link->getPluginId(), $links[3]->getPluginId());
129     $height = $this->linkTree->getSubtreeHeight('test.example2');
130     $this->assertEqual($height, 3);
131   }
132
133 }