Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\menu_ui\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
6 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
7 use Drupal\menu_link_content\Entity\MenuLinkContent;
|
Chris@0
|
8 use Drupal\node\Entity\Node;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Add, edit, and delete a node with menu link.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group menu_ui
|
Chris@0
|
14 */
|
Chris@0
|
15 class MenuNodeTest extends WebTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * An editor user.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\user\UserInterface
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $editor;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Modules to enable.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var array
|
Chris@0
|
28 */
|
Chris@0
|
29 public static $modules = ['menu_ui', 'test_page_test', 'node', 'block', 'locale', 'language', 'content_translation'];
|
Chris@0
|
30
|
Chris@0
|
31 protected function setUp() {
|
Chris@0
|
32 parent::setUp();
|
Chris@0
|
33
|
Chris@0
|
34 $this->drupalPlaceBlock('system_menu_block:main');
|
Chris@0
|
35 $this->drupalPlaceBlock('page_title_block');
|
Chris@0
|
36
|
Chris@0
|
37 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
Chris@0
|
38
|
Chris@0
|
39 $this->editor = $this->drupalCreateUser([
|
Chris@0
|
40 'access administration pages',
|
Chris@0
|
41 'administer content types',
|
Chris@0
|
42 'administer menu',
|
Chris@0
|
43 'create page content',
|
Chris@0
|
44 'edit any page content',
|
Chris@0
|
45 'delete any page content',
|
Chris@0
|
46 'create content translations',
|
Chris@0
|
47 'update content translations',
|
Chris@0
|
48 'delete content translations',
|
Chris@0
|
49 'translate any entity',
|
Chris@0
|
50 ]);
|
Chris@0
|
51 $this->drupalLogin($this->editor);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Test creating, editing, deleting menu links via node form widget.
|
Chris@0
|
56 */
|
Chris@0
|
57 public function testMenuNodeFormWidget() {
|
Chris@0
|
58 // Verify that cacheability metadata is bubbled from the menu link tree
|
Chris@0
|
59 // access checking that is performed when determining the "default parent
|
Chris@0
|
60 // item" options in menu_ui_form_node_type_form_alter(). The "log out" link
|
Chris@0
|
61 // adds the "user.roles:authenticated" cache context.
|
Chris@0
|
62 $this->drupalGet('admin/structure/types/manage/page');
|
Chris@0
|
63 $this->assertCacheContext('user.roles:authenticated');
|
Chris@0
|
64
|
Chris@0
|
65 // Verify that the menu link title has the correct maxlength.
|
Chris@0
|
66 $max_length = \Drupal::entityManager()->getBaseFieldDefinitions('menu_link_content')['title']->getSetting('max_length');
|
Chris@0
|
67 $this->drupalGet('node/add/page');
|
Chris@0
|
68 $this->assertPattern('/<input .* id="edit-menu-title" .* maxlength="' . $max_length . '" .* \/>/', 'Menu link title field has correct maxlength in node add form.');
|
Chris@0
|
69
|
Chris@0
|
70 // Disable the default main menu, so that no menus are enabled.
|
Chris@0
|
71 $edit = [
|
Chris@0
|
72 'menu_options[main]' => FALSE,
|
Chris@0
|
73 ];
|
Chris@0
|
74 $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
|
Chris@0
|
75
|
Chris@0
|
76 // Verify that no menu settings are displayed and nodes can be created.
|
Chris@0
|
77 $this->drupalGet('node/add/page');
|
Chris@0
|
78 $this->assertText(t('Create Basic page'));
|
Chris@0
|
79 $this->assertNoText(t('Menu settings'));
|
Chris@0
|
80 $node_title = $this->randomMachineName();
|
Chris@0
|
81 $edit = [
|
Chris@0
|
82 'title[0][value]' => $node_title,
|
Chris@0
|
83 'body[0][value]' => $this->randomString(),
|
Chris@0
|
84 ];
|
Chris@0
|
85 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
86 $node = $this->drupalGetNodeByTitle($node_title);
|
Chris@0
|
87 $this->assertEqual($node->getTitle(), $edit['title[0][value]']);
|
Chris@0
|
88
|
Chris@0
|
89 // Test that we cannot set a menu item from a menu that is not set as
|
Chris@0
|
90 // available.
|
Chris@0
|
91 $edit = [
|
Chris@0
|
92 'menu_options[tools]' => 1,
|
Chris@0
|
93 'menu_parent' => 'main:',
|
Chris@0
|
94 ];
|
Chris@0
|
95 $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
|
Chris@0
|
96 $this->assertText(t('The selected menu item is not under one of the selected menus.'));
|
Chris@0
|
97 $this->assertNoRaw(t('The content type %name has been updated.', ['%name' => 'Basic page']));
|
Chris@0
|
98
|
Chris@0
|
99 // Enable Tools menu as available menu.
|
Chris@0
|
100 $edit = [
|
Chris@0
|
101 'menu_options[main]' => 1,
|
Chris@0
|
102 'menu_options[tools]' => 1,
|
Chris@0
|
103 'menu_parent' => 'main:',
|
Chris@0
|
104 ];
|
Chris@0
|
105 $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
|
Chris@0
|
106 $this->assertRaw(t('The content type %name has been updated.', ['%name' => 'Basic page']));
|
Chris@0
|
107
|
Chris@0
|
108 // Test that we can preview a node that will create a menu item.
|
Chris@0
|
109 $edit = [
|
Chris@0
|
110 'title[0][value]' => $node_title,
|
Chris@0
|
111 'menu[enabled]' => 1,
|
Chris@0
|
112 'menu[title]' => 'Test preview',
|
Chris@0
|
113 ];
|
Chris@0
|
114 $this->drupalPostForm('node/add/page', $edit, t('Preview'));
|
Chris@0
|
115
|
Chris@0
|
116 // Create a node.
|
Chris@0
|
117 $node_title = $this->randomMachineName();
|
Chris@0
|
118 $edit = [
|
Chris@0
|
119 'title[0][value]' => $node_title,
|
Chris@0
|
120 'body[0][value]' => $this->randomString(),
|
Chris@0
|
121 ];
|
Chris@0
|
122 $this->drupalPostForm('node/add/page', $edit, t('Save'));
|
Chris@0
|
123 $node = $this->drupalGetNodeByTitle($node_title);
|
Chris@0
|
124 // Assert that there is no link for the node.
|
Chris@0
|
125 $this->drupalGet('test-page');
|
Chris@0
|
126 $this->assertNoLink($node_title);
|
Chris@0
|
127
|
Chris@0
|
128 // Edit the node, enable the menu link setting, but skip the link title.
|
Chris@0
|
129 $edit = [
|
Chris@0
|
130 'menu[enabled]' => 1,
|
Chris@0
|
131 ];
|
Chris@0
|
132 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
133 // Assert that there is no link for the node.
|
Chris@0
|
134 $this->drupalGet('test-page');
|
Chris@0
|
135 $this->assertNoLink($node_title);
|
Chris@0
|
136
|
Chris@0
|
137 // Make sure the menu links only appear when the node is published.
|
Chris@0
|
138 // These buttons just appear for 'administer nodes' users.
|
Chris@0
|
139 $admin_user = $this->drupalCreateUser([
|
Chris@0
|
140 'access administration pages',
|
Chris@0
|
141 'administer content types',
|
Chris@0
|
142 'administer nodes',
|
Chris@0
|
143 'administer menu',
|
Chris@0
|
144 'create page content',
|
Chris@0
|
145 'edit any page content',
|
Chris@0
|
146 ]);
|
Chris@0
|
147 $this->drupalLogin($admin_user);
|
Chris@0
|
148 // Assert that the link does not exist if unpublished.
|
Chris@0
|
149 $edit = [
|
Chris@0
|
150 'menu[enabled]' => 1,
|
Chris@0
|
151 'menu[title]' => $node_title,
|
Chris@0
|
152 'status[value]' => FALSE,
|
Chris@0
|
153 ];
|
Chris@0
|
154 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save');
|
Chris@0
|
155 $this->drupalGet('test-page');
|
Chris@0
|
156 $this->assertNoLink($node_title, 'Found no menu link with the node unpublished');
|
Chris@0
|
157 // Assert that the link exists if published.
|
Chris@0
|
158 $edit['status[value]'] = TRUE;
|
Chris@0
|
159 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save');
|
Chris@0
|
160 $this->drupalGet('test-page');
|
Chris@0
|
161 $this->assertLink($node_title, 0, 'Found a menu link with the node published');
|
Chris@0
|
162
|
Chris@0
|
163 // Log back in as normal user.
|
Chris@0
|
164 $this->drupalLogin($this->editor);
|
Chris@0
|
165 // Edit the node and create a menu link.
|
Chris@0
|
166 $edit = [
|
Chris@0
|
167 'menu[enabled]' => 1,
|
Chris@0
|
168 'menu[title]' => $node_title,
|
Chris@0
|
169 'menu[weight]' => 17,
|
Chris@0
|
170 ];
|
Chris@0
|
171 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
172 // Assert that the link exists.
|
Chris@0
|
173 $this->drupalGet('test-page');
|
Chris@0
|
174 $this->assertLink($node_title);
|
Chris@0
|
175
|
Chris@0
|
176 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@0
|
177 $this->assertFieldById('edit-menu-weight', 17, 'Menu weight correct in edit form');
|
Chris@0
|
178 $this->assertPattern('/<input .* id="edit-menu-title" .* maxlength="' . $max_length . '" .* \/>/', 'Menu link title field has correct maxlength in node edit form.');
|
Chris@0
|
179
|
Chris@0
|
180 // Disable the menu link, then edit the node--the link should stay disabled.
|
Chris@0
|
181 $link_id = menu_ui_get_menu_link_defaults($node)['entity_id'];
|
Chris@0
|
182 /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $link */
|
Chris@0
|
183 $link = MenuLinkContent::load($link_id);
|
Chris@0
|
184 $link->set('enabled', FALSE);
|
Chris@0
|
185 $link->save();
|
Chris@0
|
186 $this->drupalPostForm($node->urlInfo('edit-form'), $edit, t('Save'));
|
Chris@0
|
187 $link = MenuLinkContent::load($link_id);
|
Chris@0
|
188 $this->assertFalse($link->isEnabled(), 'Saving a node with a disabled menu link keeps the menu link disabled.');
|
Chris@0
|
189
|
Chris@0
|
190 // Edit the node and remove the menu link.
|
Chris@0
|
191 $edit = [
|
Chris@0
|
192 'menu[enabled]' => FALSE,
|
Chris@0
|
193 ];
|
Chris@0
|
194 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
195 // Assert that there is no link for the node.
|
Chris@0
|
196 $this->drupalGet('test-page');
|
Chris@0
|
197 $this->assertNoLink($node_title);
|
Chris@0
|
198
|
Chris@0
|
199 // Add a menu link to the Administration menu.
|
Chris@0
|
200 $item = MenuLinkContent::create([
|
Chris@0
|
201 'link' => [['uri' => 'entity:node/' . $node->id()]],
|
Chris@0
|
202 'title' => $this->randomMachineName(16),
|
Chris@0
|
203 'menu_name' => 'admin',
|
Chris@0
|
204 ]);
|
Chris@0
|
205 $item->save();
|
Chris@0
|
206
|
Chris@0
|
207 // Assert that disabled Administration menu is not shown on the
|
Chris@0
|
208 // node/$nid/edit page.
|
Chris@0
|
209 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@0
|
210 $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form');
|
Chris@0
|
211 // Assert that the link is still in the Administration menu after save.
|
Chris@0
|
212 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
213 $link = MenuLinkContent::load($item->id());
|
Chris@0
|
214 $this->assertTrue($link, 'Link in not allowed menu still exists after saving node');
|
Chris@0
|
215
|
Chris@0
|
216 // Move the menu link back to the Tools menu.
|
Chris@0
|
217 $item->menu_name->value = 'tools';
|
Chris@0
|
218 $item->save();
|
Chris@0
|
219 // Create a second node.
|
Chris@0
|
220 $child_node = $this->drupalCreateNode(['type' => 'article']);
|
Chris@0
|
221 // Assign a menu link to the second node, being a child of the first one.
|
Chris@0
|
222 $child_item = MenuLinkContent::create([
|
Chris@0
|
223 'link' => [['uri' => 'entity:node/' . $child_node->id()]],
|
Chris@0
|
224 'title' => $this->randomMachineName(16),
|
Chris@0
|
225 'parent' => $item->getPluginId(),
|
Chris@0
|
226 'menu_name' => $item->getMenuName(),
|
Chris@0
|
227 ]);
|
Chris@0
|
228 $child_item->save();
|
Chris@0
|
229 // Edit the first node.
|
Chris@0
|
230 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@0
|
231 // Assert that it is not possible to set the parent of the first node to itself or the second node.
|
Chris@0
|
232 $this->assertNoOption('edit-menu-menu-parent', 'tools:' . $item->getPluginId());
|
Chris@0
|
233 $this->assertNoOption('edit-menu-menu-parent', 'tools:' . $child_item->getPluginId());
|
Chris@0
|
234 // Assert that unallowed Administration menu is not available in options.
|
Chris@0
|
235 $this->assertNoOption('edit-menu-menu-parent', 'admin:');
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 /**
|
Chris@0
|
239 * Testing correct loading and saving of menu links via node form widget in a multilingual environment.
|
Chris@0
|
240 */
|
Chris@0
|
241 public function testMultilingualMenuNodeFormWidget() {
|
Chris@0
|
242 // Setup languages.
|
Chris@0
|
243 $langcodes = ['de'];
|
Chris@0
|
244 foreach ($langcodes as $langcode) {
|
Chris@0
|
245 ConfigurableLanguage::createFromLangcode($langcode)->save();
|
Chris@0
|
246 }
|
Chris@0
|
247 array_unshift($langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
|
Chris@0
|
248
|
Chris@0
|
249 $config = \Drupal::service('config.factory')->getEditable('language.negotiation');
|
Chris@0
|
250 // Ensure path prefix is used to determine the language.
|
Chris@0
|
251 $config->set('url.source', 'path_prefix');
|
Chris@0
|
252 // Ensure that there's a path prefix set for english as well.
|
Chris@0
|
253 $config->set('url.prefixes.' . $langcodes[0], $langcodes[0]);
|
Chris@0
|
254 $config->save();
|
Chris@0
|
255
|
Chris@0
|
256 $this->rebuildContainer();
|
Chris@0
|
257
|
Chris@0
|
258 $languages = [];
|
Chris@0
|
259 foreach ($langcodes as $langcode) {
|
Chris@0
|
260 $languages[$langcode] = ConfigurableLanguage::load($langcode);
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 // Use a UI form submission to make the node type and menu link content entity translatable.
|
Chris@0
|
264 $this->drupalLogout();
|
Chris@0
|
265 $this->drupalLogin($this->rootUser);
|
Chris@0
|
266 $edit = [
|
Chris@0
|
267 'entity_types[node]' => TRUE,
|
Chris@0
|
268 'entity_types[menu_link_content]' => TRUE,
|
Chris@0
|
269 'settings[node][page][settings][language][language_alterable]' => TRUE,
|
Chris@0
|
270 'settings[node][page][translatable]' => TRUE,
|
Chris@0
|
271 'settings[node][page][fields][title]' => TRUE,
|
Chris@0
|
272 'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
|
Chris@0
|
273 ];
|
Chris@0
|
274 $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
|
Chris@0
|
275
|
Chris@0
|
276 // Log out and back in as normal user.
|
Chris@0
|
277 $this->drupalLogout();
|
Chris@0
|
278 $this->drupalLogin($this->editor);
|
Chris@0
|
279
|
Chris@0
|
280 // Create a node.
|
Chris@0
|
281 $node_title = $this->randomMachineName(8);
|
Chris@0
|
282 $node = Node::create([
|
Chris@0
|
283 'type' => 'page',
|
Chris@0
|
284 'title' => $node_title,
|
Chris@0
|
285 'body' => $this->randomMachineName(16),
|
Chris@0
|
286 'uid' => $this->editor->id(),
|
Chris@0
|
287 'status' => 1,
|
Chris@0
|
288 'langcode' => $langcodes[0],
|
Chris@0
|
289 ]);
|
Chris@0
|
290 $node->save();
|
Chris@0
|
291
|
Chris@0
|
292 // Create translation.
|
Chris@0
|
293 $translated_node_title = $this->randomMachineName(8);
|
Chris@0
|
294 $node->addTranslation($langcodes[1], ['title' => $translated_node_title, 'body' => $this->randomMachineName(16), 'status' => 1]);
|
Chris@0
|
295 $node->save();
|
Chris@0
|
296
|
Chris@0
|
297 // Edit the node and create a menu link.
|
Chris@0
|
298 $edit = [
|
Chris@0
|
299 'menu[enabled]' => 1,
|
Chris@0
|
300 'menu[title]' => $node_title,
|
Chris@0
|
301 'menu[weight]' => 17,
|
Chris@0
|
302 ];
|
Chris@0
|
303 $options = ['language' => $languages[$langcodes[0]]];
|
Chris@0
|
304 $url = $node->toUrl('edit-form', $options);
|
Chris@0
|
305 $this->drupalPostForm($url, $edit, t('Save') . ' ' . t('(this translation)'));
|
Chris@0
|
306
|
Chris@0
|
307 // Edit the node in a different language and translate the menu link.
|
Chris@0
|
308 $edit = [
|
Chris@0
|
309 'menu[enabled]' => 1,
|
Chris@0
|
310 'menu[title]' => $translated_node_title,
|
Chris@0
|
311 'menu[weight]' => 17,
|
Chris@0
|
312 ];
|
Chris@0
|
313 $options = ['language' => $languages[$langcodes[1]]];
|
Chris@0
|
314 $url = $node->toUrl('edit-form', $options);
|
Chris@0
|
315 $this->drupalPostForm($url, $edit, t('Save') . ' ' . t('(this translation)'));
|
Chris@0
|
316
|
Chris@0
|
317 // Assert that the original link exists in the frontend.
|
Chris@0
|
318 $this->drupalGet('node/' . $node->id(), ['language' => $languages[$langcodes[0]]]);
|
Chris@0
|
319 $this->assertLink($node_title);
|
Chris@0
|
320
|
Chris@0
|
321 // Assert that the translated link exists in the frontend.
|
Chris@0
|
322 $this->drupalGet('node/' . $node->id(), ['language' => $languages[$langcodes[1]]]);
|
Chris@0
|
323 $this->assertLink($translated_node_title);
|
Chris@0
|
324
|
Chris@0
|
325 // Revisit the edit page in original language, check the loaded menu item title and save.
|
Chris@0
|
326 $options = ['language' => $languages[$langcodes[0]]];
|
Chris@0
|
327 $url = $node->toUrl('edit-form', $options);
|
Chris@0
|
328 $this->drupalGet($url);
|
Chris@0
|
329 $this->assertFieldById('edit-menu-title', $node_title);
|
Chris@0
|
330 $this->drupalPostForm(NULL, [], t('Save') . ' ' . t('(this translation)'));
|
Chris@0
|
331
|
Chris@0
|
332 // Revisit the edit page of the translation and check the loaded menu item title.
|
Chris@0
|
333 $options = ['language' => $languages[$langcodes[1]]];
|
Chris@0
|
334 $url = $node->toUrl('edit-form', $options);
|
Chris@0
|
335 $this->drupalGet($url);
|
Chris@0
|
336 $this->assertFieldById('edit-menu-title', $translated_node_title);
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@0
|
339 }
|