298af6fae3ecd6f5d78fea887e25126c4af2b091
[yaffs-website] / web / core / modules / menu_link_content / tests / src / Functional / LinksTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_link_content\Functional;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Menu\MenuTreeParameters;
7 use Drupal\menu_link_content\Entity\MenuLinkContent;
8 use Drupal\system\Entity\Menu;
9 use Drupal\Tests\BrowserTestBase;
10 use Drupal\user\Entity\User;
11
12 /**
13  * Tests handling of menu links hierarchies.
14  *
15  * @group Menu
16  */
17 class LinksTest extends BrowserTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['router_test', 'menu_link_content'];
25
26   /**
27    * The menu link plugin manager.
28    *
29    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
30    */
31   protected $menuLinkManager;
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38
39     $this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
40
41     Menu::create([
42       'id' => 'menu_test',
43       'label' => 'Test menu',
44       'description' => 'Description text',
45     ])->save();
46   }
47
48   /**
49    * Create a simple hierarchy of links.
50    */
51   public function createLinkHierarchy($module = 'menu_test') {
52     // First remove all the menu links in the menu.
53     $this->menuLinkManager->deleteLinksInMenu('menu_test');
54
55     // Then create a simple link hierarchy:
56     // - parent
57     //   - child-1
58     //      - child-1-1
59     //      - child-1-2
60     //   - child-2
61     $base_options = [
62       'title' => 'Menu link test',
63       'provider' => $module,
64       'menu_name' => 'menu_test',
65     ];
66
67     $parent = $base_options + [
68       'link' => ['uri' => 'internal:/menu-test/hierarchy/parent'],
69     ];
70     $link = MenuLinkContent::create($parent);
71     $link->save();
72     $links['parent'] = $link->getPluginId();
73
74     $child_1 = $base_options + [
75       'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child'],
76       'parent' => $links['parent'],
77     ];
78     $link = MenuLinkContent::create($child_1);
79     $link->save();
80     $links['child-1'] = $link->getPluginId();
81
82     $child_1_1 = $base_options + [
83       'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child2/child'],
84       'parent' => $links['child-1'],
85     ];
86     $link = MenuLinkContent::create($child_1_1);
87     $link->save();
88     $links['child-1-1'] = $link->getPluginId();
89
90     $child_1_2 = $base_options + [
91       'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child2/child'],
92       'parent' => $links['child-1'],
93     ];
94     $link = MenuLinkContent::create($child_1_2);
95     $link->save();
96     $links['child-1-2'] = $link->getPluginId();
97
98     $child_2 = $base_options + [
99       'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child'],
100       'parent' => $links['parent'],
101     ];
102     $link = MenuLinkContent::create($child_2);
103     $link->save();
104     $links['child-2'] = $link->getPluginId();
105
106     return $links;
107   }
108
109   /**
110    * Assert that at set of links is properly parented.
111    */
112   public function assertMenuLinkParents($links, $expected_hierarchy) {
113     foreach ($expected_hierarchy as $id => $parent) {
114       /* @var \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin  */
115       $menu_link_plugin = $this->menuLinkManager->createInstance($links[$id]);
116       $expected_parent = isset($links[$parent]) ? $links[$parent] : '';
117
118       $this->assertEqual($menu_link_plugin->getParent(), $expected_parent, SafeMarkup::format('Menu link %id has parent of %parent, expected %expected_parent.', ['%id' => $id, '%parent' => $menu_link_plugin->getParent(), '%expected_parent' => $expected_parent]));
119     }
120   }
121
122   /**
123    * Assert that a link entity's created timestamp is set.
124    */
125   public function testCreateLink() {
126     $options = [
127       'menu_name' => 'menu_test',
128       'bundle' => 'menu_link_content',
129       'link' => [['uri' => 'internal:/']],
130       'title' => 'Link test',
131     ];
132     $link = MenuLinkContent::create($options);
133     $link->save();
134     // Make sure the changed timestamp is set.
135     $this->assertEqual($link->getChangedTime(), REQUEST_TIME, 'Creating a menu link sets the "changed" timestamp.');
136     $options = [
137       'title' => 'Test Link',
138     ];
139     $link->link->options = $options;
140     $link->changed->value = 0;
141     $link->save();
142     // Make sure the changed timestamp is updated.
143     $this->assertEqual($link->getChangedTime(), REQUEST_TIME, 'Changing a menu link sets "changed" timestamp.');
144   }
145
146   /**
147    * Tests that menu link pointing to entities get removed on entity remove.
148    */
149   public function testMenuLinkOnEntityDelete() {
150     $user = User::create(['name' => 'username']);
151     $user->save();
152     $menu_link_content = MenuLinkContent::create([
153       'title' => 'username profile',
154       'menu_name' => 'menu_test',
155       'link' => [['uri' => 'entity:user/' . $user->id()]],
156       'bundle' => 'menu_test',
157     ]);
158     $menu_link_content->save();
159     $menu_tree_condition = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.canonical');
160     $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
161
162     $user->delete();
163     $this->assertCount(0, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
164   }
165
166   /**
167    * Test automatic reparenting of menu links.
168    */
169   public function testMenuLinkReparenting($module = 'menu_test') {
170     // Check the initial hierarchy.
171     $links = $this->createLinkHierarchy($module);
172
173     $expected_hierarchy = [
174       'parent' => '',
175       'child-1' => 'parent',
176       'child-1-1' => 'child-1',
177       'child-1-2' => 'child-1',
178       'child-2' => 'parent',
179     ];
180     $this->assertMenuLinkParents($links, $expected_hierarchy);
181
182     // Start over, and move child-1 under child-2, and check that all the
183     // children of child-1 have been moved too.
184     $links = $this->createLinkHierarchy($module);
185     /* @var \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin  */
186     $this->menuLinkManager->updateDefinition($links['child-1'], ['parent' => $links['child-2']]);
187     // Verify that the entity was updated too.
188     $menu_link_plugin = $this->menuLinkManager->createInstance($links['child-1']);
189     $entity = \Drupal::entityManager()->loadEntityByUuid('menu_link_content', $menu_link_plugin->getDerivativeId());
190     $this->assertEqual($entity->getParentId(), $links['child-2']);
191
192     $expected_hierarchy = [
193       'parent' => '',
194       'child-1' => 'child-2',
195       'child-1-1' => 'child-1',
196       'child-1-2' => 'child-1',
197       'child-2' => 'parent',
198     ];
199     $this->assertMenuLinkParents($links, $expected_hierarchy);
200
201     // Start over, and delete child-1, and check that the children of child-1
202     // have been reassigned to the parent.
203     $links = $this->createLinkHierarchy($module);
204     $this->menuLinkManager->removeDefinition($links['child-1']);
205
206     $expected_hierarchy = [
207       'parent' => FALSE,
208       'child-1-1' => 'parent',
209       'child-1-2' => 'parent',
210       'child-2' => 'parent',
211     ];
212     $this->assertMenuLinkParents($links, $expected_hierarchy);
213
214     // Try changing the parent at the entity level.
215     $definition = $this->menuLinkManager->getDefinition($links['child-1-2']);
216     $entity = MenuLinkContent::load($definition['metadata']['entity_id']);
217     $entity->parent->value = '';
218     $entity->save();
219
220     $expected_hierarchy = [
221       'parent' => '',
222       'child-1-1' => 'parent',
223       'child-1-2' => '',
224       'child-2' => 'parent',
225     ];
226     $this->assertMenuLinkParents($links, $expected_hierarchy);
227
228     // @todo Figure out what makes sense to test in terms of automatic
229     //   re-parenting. https://www.drupal.org/node/2309531
230   }
231
232   /**
233    * Tests uninstalling a module providing default links.
234    */
235   public function testModuleUninstalledMenuLinks() {
236     \Drupal::service('module_installer')->install(['menu_test']);
237     \Drupal::service('router.builder')->rebuild();
238     \Drupal::service('plugin.manager.menu.link')->rebuild();
239     $menu_links = $this->menuLinkManager->loadLinksByRoute('menu_test.menu_test');
240     $this->assertEqual(count($menu_links), 1);
241     $menu_link = reset($menu_links);
242     $this->assertEqual($menu_link->getPluginId(), 'menu_test');
243
244     // Uninstall the module and ensure the menu link got removed.
245     \Drupal::service('module_installer')->uninstall(['menu_test']);
246     \Drupal::service('plugin.manager.menu.link')->rebuild();
247     $menu_links = $this->menuLinkManager->loadLinksByRoute('menu_test.menu_test');
248     $this->assertEqual(count($menu_links), 0);
249   }
250
251 }