Updated to Drupal 8.5. Core Media not yet in use.
[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
151     // Create user.
152     $user = User::create(['name' => 'username']);
153     $user->save();
154
155     // Create "canonical" menu link pointing to the user.
156     $menu_link_content = MenuLinkContent::create([
157       'title' => 'username profile',
158       'menu_name' => 'menu_test',
159       'link' => [['uri' => 'entity:user/' . $user->id()]],
160       'bundle' => 'menu_test',
161     ]);
162     $menu_link_content->save();
163
164     // Create "collection" menu link pointing to the user listing page.
165     $menu_link_content_collection = MenuLinkContent::create([
166       'title' => 'users listing',
167       'menu_name' => 'menu_test',
168       'link' => [['uri' => 'internal:/' . $user->toUrl('collection')->getInternalPath()]],
169       'bundle' => 'menu_test',
170     ]);
171     $menu_link_content_collection->save();
172
173     // Check is menu links present in the menu.
174     $menu_tree_condition = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.canonical');
175     $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
176     $menu_tree_condition_collection = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.collection');
177     $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition_collection));
178
179     // Delete the user.
180     $user->delete();
181
182     // The "canonical" menu item has to be deleted.
183     $this->assertCount(0, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
184
185     // The "collection" menu item should still present in the menu.
186     $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition_collection));
187   }
188
189   /**
190    * Test automatic reparenting of menu links.
191    */
192   public function testMenuLinkReparenting($module = 'menu_test') {
193     // Check the initial hierarchy.
194     $links = $this->createLinkHierarchy($module);
195
196     $expected_hierarchy = [
197       'parent' => '',
198       'child-1' => 'parent',
199       'child-1-1' => 'child-1',
200       'child-1-2' => 'child-1',
201       'child-2' => 'parent',
202     ];
203     $this->assertMenuLinkParents($links, $expected_hierarchy);
204
205     // Start over, and move child-1 under child-2, and check that all the
206     // children of child-1 have been moved too.
207     $links = $this->createLinkHierarchy($module);
208     /* @var \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin  */
209     $this->menuLinkManager->updateDefinition($links['child-1'], ['parent' => $links['child-2']]);
210     // Verify that the entity was updated too.
211     $menu_link_plugin = $this->menuLinkManager->createInstance($links['child-1']);
212     $entity = \Drupal::entityManager()->loadEntityByUuid('menu_link_content', $menu_link_plugin->getDerivativeId());
213     $this->assertEqual($entity->getParentId(), $links['child-2']);
214
215     $expected_hierarchy = [
216       'parent' => '',
217       'child-1' => 'child-2',
218       'child-1-1' => 'child-1',
219       'child-1-2' => 'child-1',
220       'child-2' => 'parent',
221     ];
222     $this->assertMenuLinkParents($links, $expected_hierarchy);
223
224     // Start over, and delete child-1, and check that the children of child-1
225     // have been reassigned to the parent.
226     $links = $this->createLinkHierarchy($module);
227     $this->menuLinkManager->removeDefinition($links['child-1']);
228
229     $expected_hierarchy = [
230       'parent' => FALSE,
231       'child-1-1' => 'parent',
232       'child-1-2' => 'parent',
233       'child-2' => 'parent',
234     ];
235     $this->assertMenuLinkParents($links, $expected_hierarchy);
236
237     // Try changing the parent at the entity level.
238     $definition = $this->menuLinkManager->getDefinition($links['child-1-2']);
239     $entity = MenuLinkContent::load($definition['metadata']['entity_id']);
240     $entity->parent->value = '';
241     $entity->save();
242
243     $expected_hierarchy = [
244       'parent' => '',
245       'child-1-1' => 'parent',
246       'child-1-2' => '',
247       'child-2' => 'parent',
248     ];
249     $this->assertMenuLinkParents($links, $expected_hierarchy);
250
251     // @todo Figure out what makes sense to test in terms of automatic
252     //   re-parenting. https://www.drupal.org/node/2309531
253   }
254
255   /**
256    * Tests uninstalling a module providing default links.
257    */
258   public function testModuleUninstalledMenuLinks() {
259     \Drupal::service('module_installer')->install(['menu_test']);
260     \Drupal::service('router.builder')->rebuild();
261     \Drupal::service('plugin.manager.menu.link')->rebuild();
262     $menu_links = $this->menuLinkManager->loadLinksByRoute('menu_test.menu_test');
263     $this->assertEqual(count($menu_links), 1);
264     $menu_link = reset($menu_links);
265     $this->assertEqual($menu_link->getPluginId(), 'menu_test');
266
267     // Uninstall the module and ensure the menu link got removed.
268     \Drupal::service('module_installer')->uninstall(['menu_test']);
269     \Drupal::service('plugin.manager.menu.link')->rebuild();
270     $menu_links = $this->menuLinkManager->loadLinksByRoute('menu_test.menu_test');
271     $this->assertEqual(count($menu_links), 0);
272   }
273
274 }