PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/menu_ui/src/Tests/MenuNodeTest.php

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