PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/drupal/drupal
PHP | 961 lines | 522 code | 128 blank | 311 comment | 13 complexity | de0fff65efef669da82e04514d1eed6d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\menu_ui\Tests;
  3. use Drupal\block\Entity\Block;
  4. use Drupal\Component\Serialization\Json;
  5. use Drupal\Component\Utility\Unicode;
  6. use Drupal\Core\Menu\MenuLinkInterface;
  7. use Drupal\Core\Url;
  8. use Drupal\menu_link_content\Entity\MenuLinkContent;
  9. use Drupal\system\Entity\Menu;
  10. use Drupal\node\Entity\Node;
  11. /**
  12. * Add a custom menu, add menu links to the custom menu and Tools menu, check
  13. * their data, and delete them using the UI.
  14. *
  15. * @group menu_ui
  16. */
  17. class MenuTest extends MenuWebTestBase {
  18. /**
  19. * Modules to enable.
  20. *
  21. * @var array
  22. */
  23. public static $modules = array('node', 'block', 'contextual', 'help', 'path', 'test_page_test');
  24. /**
  25. * A user with administration rights.
  26. *
  27. * @var \Drupal\user\UserInterface
  28. */
  29. protected $adminUser;
  30. /**
  31. * An authenticated user.
  32. *
  33. * @var \Drupal\user\UserInterface
  34. */
  35. protected $authenticatedUser;
  36. /**
  37. * Array of placed menu blocks keyed by block ID.
  38. *
  39. * @var array
  40. */
  41. protected $blockPlacements;
  42. /**
  43. * A test menu.
  44. *
  45. * @var \Drupal\system\Entity\Menu
  46. */
  47. protected $menu;
  48. /**
  49. * An array of test menu links.
  50. *
  51. * @var \Drupal\menu_link_content\MenuLinkContentInterface[]
  52. */
  53. protected $items;
  54. protected function setUp() {
  55. parent::setUp();
  56. $this->drupalPlaceBlock('page_title_block');
  57. $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
  58. // Create users.
  59. $this->adminUser = $this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer menu', 'create article content'));
  60. $this->authenticatedUser = $this->drupalCreateUser(array());
  61. }
  62. /**
  63. * Tests menu functionality using the admin and user interfaces.
  64. */
  65. function testMenu() {
  66. // Log in the user.
  67. $this->drupalLogin($this->adminUser);
  68. $this->items = array();
  69. $this->menu = $this->addCustomMenu();
  70. $this->doMenuTests();
  71. $this->doTestMenuBlock();
  72. $this->addInvalidMenuLink();
  73. $this->addCustomMenuCRUD();
  74. // Verify that the menu links rebuild is idempotent and leaves the same
  75. // number of links in the table.
  76. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  77. $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  78. $before_count = $menu_link_manager->countMenuLinks(NULL);
  79. $menu_link_manager->rebuild();
  80. $after_count = $menu_link_manager->countMenuLinks(NULL);
  81. $this->assertIdentical($before_count, $after_count, 'MenuLinkManager::rebuild() does not add more links');
  82. // Do standard user tests.
  83. // Log in the user.
  84. $this->drupalLogin($this->authenticatedUser);
  85. $this->verifyAccess(403);
  86. foreach ($this->items as $item) {
  87. // Menu link URIs are stored as 'internal:/node/$nid'.
  88. $node = Node::load(str_replace('internal:/node/', '', $item->link->uri));
  89. $this->verifyMenuLink($item, $node);
  90. }
  91. // Log in the administrator.
  92. $this->drupalLogin($this->adminUser);
  93. // Verify delete link exists and reset link does not exist.
  94. $this->drupalGet('admin/structure/menu/manage/' . $this->menu->id());
  95. $this->assertLinkByHref(Url::fromRoute('entity.menu_link_content.delete_form', ['menu_link_content' => $this->items[0]->id()])->toString());
  96. $this->assertNoLinkByHref(Url::fromRoute('menu_ui.link_reset', ['menu_link_plugin' => $this->items[0]->getPluginId()])->toString());
  97. // Check delete and reset access.
  98. $this->drupalGet('admin/structure/menu/item/' . $this->items[0]->id() . '/delete');
  99. $this->assertResponse(200);
  100. $this->drupalGet('admin/structure/menu/link/' . $this->items[0]->getPluginId() . '/reset');
  101. $this->assertResponse(403);
  102. // Delete menu links.
  103. foreach ($this->items as $item) {
  104. $this->deleteMenuLink($item);
  105. }
  106. // Delete custom menu.
  107. $this->deleteCustomMenu();
  108. // Modify and reset a standard menu link.
  109. $instance = $this->getStandardMenuLink();
  110. $old_weight = $instance->getWeight();
  111. // Edit the static menu link.
  112. $edit = array();
  113. $edit['weight'] = 10;
  114. $id = $instance->getPluginId();
  115. $this->drupalPostForm("admin/structure/menu/link/$id/edit", $edit, t('Save'));
  116. $this->assertResponse(200);
  117. $this->assertText('The menu link has been saved.');
  118. $menu_link_manager->resetDefinitions();
  119. $instance = $menu_link_manager->createInstance($instance->getPluginId());
  120. $this->assertEqual($edit['weight'], $instance->getWeight(), 'Saving an existing link updates the weight.');
  121. $this->resetMenuLink($instance, $old_weight);
  122. }
  123. /**
  124. * Adds a custom menu using CRUD functions.
  125. */
  126. function addCustomMenuCRUD() {
  127. // Add a new custom menu.
  128. $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
  129. $label = $this->randomMachineName(16);
  130. $menu = Menu::create(array(
  131. 'id' => $menu_name,
  132. 'label' => $label,
  133. 'description' => 'Description text',
  134. ));
  135. $menu->save();
  136. // Assert the new menu.
  137. $this->drupalGet('admin/structure/menu/manage/' . $menu_name);
  138. $this->assertRaw($label, 'Custom menu was added.');
  139. // Edit the menu.
  140. $new_label = $this->randomMachineName(16);
  141. $menu->set('label', $new_label);
  142. $menu->save();
  143. $this->drupalGet('admin/structure/menu/manage/' . $menu_name);
  144. $this->assertRaw($new_label, 'Custom menu was edited.');
  145. }
  146. /**
  147. * Creates a custom menu.
  148. *
  149. * @return \Drupal\system\Entity\Menu
  150. * The custom menu that has been created.
  151. */
  152. function addCustomMenu() {
  153. // Try adding a menu using a menu_name that is too long.
  154. $this->drupalGet('admin/structure/menu/add');
  155. $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
  156. $label = $this->randomMachineName(16);
  157. $edit = array(
  158. 'id' => $menu_name,
  159. 'description' => '',
  160. 'label' => $label,
  161. );
  162. $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
  163. // Verify that using a menu_name that is too long results in a validation
  164. // message.
  165. $this->assertRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', array(
  166. '@name' => t('Menu name'),
  167. '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
  168. '%length' => Unicode::strlen($menu_name),
  169. )));
  170. // Change the menu_name so it no longer exceeds the maximum length.
  171. $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
  172. $edit['id'] = $menu_name;
  173. $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
  174. // Verify that no validation error is given for menu_name length.
  175. $this->assertNoRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', array(
  176. '@name' => t('Menu name'),
  177. '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
  178. '%length' => Unicode::strlen($menu_name),
  179. )));
  180. // Verify that the confirmation message is displayed.
  181. $this->assertRaw(t('Menu %label has been added.', array('%label' => $label)));
  182. $this->drupalGet('admin/structure/menu');
  183. $this->assertText($label, 'Menu created');
  184. // Confirm that the custom menu block is available.
  185. $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
  186. $this->clickLinkPartialName('Place block');
  187. $this->assertText($label);
  188. // Enable the block.
  189. $block = $this->drupalPlaceBlock('system_menu_block:' . $menu_name);
  190. $this->blockPlacements[$menu_name] = $block->id();
  191. return Menu::load($menu_name);
  192. }
  193. /**
  194. * Deletes the locally stored custom menu.
  195. *
  196. * This deletes the custom menu that is stored in $this->menu and performs
  197. * tests on the menu delete user interface.
  198. */
  199. function deleteCustomMenu() {
  200. $menu_name = $this->menu->id();
  201. $label = $this->menu->label();
  202. // Delete custom menu.
  203. $this->drupalPostForm("admin/structure/menu/manage/$menu_name/delete", array(), t('Delete'));
  204. $this->assertResponse(200);
  205. $this->assertRaw(t('The menu %title has been deleted.', array('%title' => $label)), 'Custom menu was deleted');
  206. $this->assertNull(Menu::load($menu_name), 'Custom menu was deleted');
  207. // Test if all menu links associated with the menu were removed from
  208. // database.
  209. $result = entity_load_multiple_by_properties('menu_link_content', array('menu_name' => $menu_name));
  210. $this->assertFalse($result, 'All menu links associated with the custom menu were deleted.');
  211. // Make sure there's no delete button on system menus.
  212. $this->drupalGet('admin/structure/menu/manage/main');
  213. $this->assertNoRaw('edit-delete', 'The delete button was not found');
  214. // Try to delete the main menu.
  215. $this->drupalGet('admin/structure/menu/manage/main/delete');
  216. $this->assertText(t('You are not authorized to access this page.'));
  217. }
  218. /**
  219. * Tests menu functionality.
  220. */
  221. function doMenuTests() {
  222. $menu_name = $this->menu->id();
  223. // Test the 'Add link' local action.
  224. $this->drupalGet(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
  225. $this->clickLink(t('Add link'));
  226. $link_title = $this->randomString();
  227. $this->drupalPostForm(NULL, array('link[0][uri]' => '/', 'title[0][value]' => $link_title), t('Save'));
  228. $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
  229. // Test the 'Edit' operation.
  230. $this->clickLink(t('Edit'));
  231. $this->assertFieldByName('title[0][value]', $link_title);
  232. $link_title = $this->randomString();
  233. $this->drupalPostForm(NULL, array('title[0][value]' => $link_title), t('Save'));
  234. $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
  235. // Test the 'Delete' operation.
  236. $this->clickLink(t('Delete'));
  237. $this->assertRaw(t('Are you sure you want to delete the custom menu link %item?', array('%item' => $link_title)));
  238. $this->drupalPostForm(NULL, array(), t('Delete'));
  239. $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
  240. // Add nodes to use as links for menu links.
  241. $node1 = $this->drupalCreateNode(array('type' => 'article'));
  242. $node2 = $this->drupalCreateNode(array('type' => 'article'));
  243. $node3 = $this->drupalCreateNode(array('type' => 'article'));
  244. $node4 = $this->drupalCreateNode(array('type' => 'article'));
  245. // Create a node with an alias.
  246. $node5 = $this->drupalCreateNode(array(
  247. 'type' => 'article',
  248. 'path' => array(
  249. 'alias' => '/node5',
  250. ),
  251. ));
  252. // Verify add link button.
  253. $this->drupalGet('admin/structure/menu');
  254. $this->assertLinkByHref('admin/structure/menu/manage/' . $menu_name . '/add', 0, "The add menu link button URL is correct");
  255. // Verify form defaults.
  256. $this->doMenuLinkFormDefaultsTest();
  257. // Add menu links.
  258. $item1 = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE);
  259. $item2 = $this->addMenuLink($item1->getPluginId(), '/node/' . $node2->id(), $menu_name, FALSE);
  260. $item3 = $this->addMenuLink($item2->getPluginId(), '/node/' . $node3->id(), $menu_name);
  261. // Hierarchy
  262. // <$menu_name>
  263. // - item1
  264. // -- item2
  265. // --- item3
  266. $this->assertMenuLink($item1->getPluginId(), array(
  267. 'children' => array($item2->getPluginId(), $item3->getPluginId()),
  268. 'parents' => array($item1->getPluginId()),
  269. // We assert the language code here to make sure that the language
  270. // selection element degrades gracefully without the Language module.
  271. 'langcode' => 'en',
  272. ));
  273. $this->assertMenuLink($item2->getPluginId(), array(
  274. 'children' => array($item3->getPluginId()),
  275. 'parents' => array($item2->getPluginId(), $item1->getPluginId()),
  276. // See above.
  277. 'langcode' => 'en',
  278. ));
  279. $this->assertMenuLink($item3->getPluginId(), array(
  280. 'children' => array(),
  281. 'parents' => array($item3->getPluginId(), $item2->getPluginId(), $item1->getPluginId()),
  282. // See above.
  283. 'langcode' => 'en',
  284. ));
  285. // Verify menu links.
  286. $this->verifyMenuLink($item1, $node1);
  287. $this->verifyMenuLink($item2, $node2, $item1, $node1);
  288. $this->verifyMenuLink($item3, $node3, $item2, $node2);
  289. // Add more menu links.
  290. $item4 = $this->addMenuLink('', '/node/' . $node4->id(), $menu_name);
  291. $item5 = $this->addMenuLink($item4->getPluginId(), '/node/' . $node5->id(), $menu_name);
  292. // Create a menu link pointing to an alias.
  293. $item6 = $this->addMenuLink($item4->getPluginId(), '/node5', $menu_name, TRUE, '0');
  294. // Hierarchy
  295. // <$menu_name>
  296. // - item1
  297. // -- item2
  298. // --- item3
  299. // - item4
  300. // -- item5
  301. // -- item6
  302. $this->assertMenuLink($item4->getPluginId(), array(
  303. 'children' => array($item5->getPluginId(), $item6->getPluginId()),
  304. 'parents' => array($item4->getPluginId()),
  305. // See above.
  306. 'langcode' => 'en',
  307. ));
  308. $this->assertMenuLink($item5->getPluginId(), array(
  309. 'children' => array(),
  310. 'parents' => array($item5->getPluginId(), $item4->getPluginId()),
  311. 'langcode' => 'en',
  312. ));
  313. $this->assertMenuLink($item6->getPluginId(), array(
  314. 'children' => array(),
  315. 'parents' => array($item6->getPluginId(), $item4->getPluginId()),
  316. 'route_name' => 'entity.node.canonical',
  317. 'route_parameters' => array('node' => $node5->id()),
  318. 'url' => '',
  319. // See above.
  320. 'langcode' => 'en',
  321. ));
  322. // Modify menu links.
  323. $this->modifyMenuLink($item1);
  324. $this->modifyMenuLink($item2);
  325. // Toggle menu links.
  326. $this->toggleMenuLink($item1);
  327. $this->toggleMenuLink($item2);
  328. // Move link and verify that descendants are updated.
  329. $this->moveMenuLink($item2, $item5->getPluginId(), $menu_name);
  330. // Hierarchy
  331. // <$menu_name>
  332. // - item1
  333. // - item4
  334. // -- item5
  335. // --- item2
  336. // ---- item3
  337. // -- item6
  338. $this->assertMenuLink($item1->getPluginId(), array(
  339. 'children' => array(),
  340. 'parents' => array($item1->getPluginId()),
  341. // See above.
  342. 'langcode' => 'en',
  343. ));
  344. $this->assertMenuLink($item4->getPluginId(), array(
  345. 'children' => array($item5->getPluginId(), $item6->getPluginId(), $item2->getPluginId(), $item3->getPluginId()),
  346. 'parents' => array($item4->getPluginId()),
  347. // See above.
  348. 'langcode' => 'en',
  349. ));
  350. $this->assertMenuLink($item5->getPluginId(), array(
  351. 'children' => array($item2->getPluginId(), $item3->getPluginId()),
  352. 'parents' => array($item5->getPluginId(), $item4->getPluginId()),
  353. // See above.
  354. 'langcode' => 'en',
  355. ));
  356. $this->assertMenuLink($item2->getPluginId(), array(
  357. 'children' => array($item3->getPluginId()),
  358. 'parents' => array($item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()),
  359. // See above.
  360. 'langcode' => 'en',
  361. ));
  362. $this->assertMenuLink($item3->getPluginId(), array(
  363. 'children' => array(),
  364. 'parents' => array($item3->getPluginId(), $item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()),
  365. // See above.
  366. 'langcode' => 'en',
  367. ));
  368. // Add 102 menu links with increasing weights, then make sure the last-added
  369. // item's weight doesn't get changed because of the old hardcoded delta=50.
  370. $items = array();
  371. for ($i = -50; $i <= 51; $i++) {
  372. $items[$i] = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE, strval($i));
  373. }
  374. $this->assertMenuLink($items[51]->getPluginId(), array('weight' => '51'));
  375. // Disable a link and then re-enable the link via the overview form.
  376. $this->disableMenuLink($item1);
  377. $edit = array();
  378. $edit['links[menu_plugin_id:' . $item1->getPluginId() . '][enabled]'] = TRUE;
  379. $this->drupalPostForm('admin/structure/menu/manage/' . $item1->getMenuName(), $edit, t('Save'));
  380. // Mark item2, item4 and item5 as expanded.
  381. // This is done in order to show them on the frontpage.
  382. $item2->expanded->value = 1;
  383. $item2->save();
  384. $item4->expanded->value = 1;
  385. $item4->save();
  386. $item5->expanded->value = 1;
  387. $item5->save();
  388. // Verify in the database.
  389. $this->assertMenuLink($item1->getPluginId(), array('enabled' => 1));
  390. // Add an external link.
  391. $item7 = $this->addMenuLink('', 'https://www.drupal.org', $menu_name);
  392. $this->assertMenuLink($item7->getPluginId(), array('url' => 'https://www.drupal.org'));
  393. // Add <front> menu item.
  394. $item8 = $this->addMenuLink('', '/', $menu_name);
  395. $this->assertMenuLink($item8->getPluginId(), array('route_name' => '<front>'));
  396. $this->drupalGet('');
  397. $this->assertResponse(200);
  398. // Make sure we get routed correctly.
  399. $this->clickLink($item8->getTitle());
  400. $this->assertResponse(200);
  401. // Check invalid menu link parents.
  402. $this->checkInvalidParentMenuLinks();
  403. // Save menu links for later tests.
  404. $this->items[] = $item1;
  405. $this->items[] = $item2;
  406. }
  407. /**
  408. * Ensures that the proper default values are set when adding a menu link
  409. */
  410. protected function doMenuLinkFormDefaultsTest() {
  411. $this->drupalGet("admin/structure/menu/manage/tools/add");
  412. $this->assertResponse(200);
  413. $this->assertFieldByName('title[0][value]', '');
  414. $this->assertFieldByName('link[0][uri]', '');
  415. $this->assertNoFieldChecked('edit-expanded-value');
  416. $this->assertFieldChecked('edit-enabled-value');
  417. $this->assertFieldByName('description[0][value]', '');
  418. $this->assertFieldByName('weight[0][value]', 0);
  419. }
  420. /**
  421. * Adds and removes a menu link with a query string and fragment.
  422. */
  423. function testMenuQueryAndFragment() {
  424. $this->drupalLogin($this->adminUser);
  425. // Make a path with query and fragment on.
  426. $path = '/test-page?arg1=value1&arg2=value2';
  427. $item = $this->addMenuLink('', $path);
  428. $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
  429. $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
  430. // Now change the path to something without query and fragment.
  431. $path = '/test-page';
  432. $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array('link[0][uri]' => $path), t('Save'));
  433. $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
  434. $this->assertFieldByName('link[0][uri]', $path, 'Path no longer has query or fragment.');
  435. // Use <front>#fragment and ensure that saving it does not lose its content.
  436. $path = '<front>?arg1=value#fragment';
  437. $item = $this->addMenuLink('', $path);
  438. $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
  439. $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
  440. $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array(), t('Save'));
  441. $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
  442. $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
  443. }
  444. /**
  445. * Tests renaming the built-in menu.
  446. */
  447. function testSystemMenuRename() {
  448. $this->drupalLogin($this->adminUser);
  449. $edit = array(
  450. 'label' => $this->randomMachineName(16),
  451. );
  452. $this->drupalPostForm('admin/structure/menu/manage/main', $edit, t('Save'));
  453. // Make sure menu shows up with new name in block addition.
  454. $default_theme = $this->config('system.theme')->get('default');
  455. $this->drupalget('admin/structure/block/list/' . $default_theme);
  456. $this->clickLinkPartialName('Place block');
  457. $this->assertText($edit['label']);
  458. }
  459. /**
  460. * Tests that menu items pointing to unpublished nodes are editable.
  461. */
  462. function testUnpublishedNodeMenuItem() {
  463. $this->drupalLogin($this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer menu', 'create article content', 'bypass node access')));
  464. // Create an unpublished node.
  465. $node = $this->drupalCreateNode(array(
  466. 'type' => 'article',
  467. 'status' => NODE_NOT_PUBLISHED,
  468. ));
  469. $item = $this->addMenuLink('', '/node/' . $node->id());
  470. $this->modifyMenuLink($item);
  471. // Test that a user with 'administer menu' but without 'bypass node access'
  472. // cannot see the menu item.
  473. $this->drupalLogout();
  474. $this->drupalLogin($this->adminUser);
  475. $this->drupalGet('admin/structure/menu/manage/' . $item->getMenuName());
  476. $this->assertNoText($item->getTitle(), "Menu link pointing to unpublished node is only visible to users with 'bypass node access' permission");
  477. // The cache contexts associated with the (in)accessible menu links are
  478. // bubbled. See DefaultMenuLinkTreeManipulators::menuLinkCheckAccess().
  479. $this->assertCacheContext('user.permissions');
  480. }
  481. /**
  482. * Tests the contextual links on a menu block.
  483. */
  484. public function testBlockContextualLinks() {
  485. $this->drupalLogin($this->drupalCreateUser(array('administer menu', 'access contextual links', 'administer blocks')));
  486. $custom_menu = $this->addCustomMenu();
  487. $this->addMenuLink('', '/', $custom_menu->id());
  488. $block = $this->drupalPlaceBlock('system_menu_block:' . $custom_menu->id(), array('label' => 'Custom menu', 'provider' => 'system'));
  489. $this->drupalGet('test-page');
  490. $id = 'block:block=' . $block->id() . ':langcode=en|menu:menu=' . $custom_menu->id() . ':langcode=en';
  491. // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
  492. $this->assertRaw('<div data-contextual-id="' . $id . '"></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id)));
  493. // Get server-rendered contextual links.
  494. // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks()
  495. $post = array('ids[0]' => $id);
  496. $response = $this->drupalPost('contextual/render', 'application/json', $post, array('query' => array('destination' => 'test-page')));
  497. $this->assertResponse(200);
  498. $json = Json::decode($response);
  499. $this->assertIdentical($json[$id], '<ul class="contextual-links"><li class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '">Configure block</a></li><li class="entitymenuedit-form"><a href="' . base_path() . 'admin/structure/menu/manage/' . $custom_menu->id() . '">Edit menu</a></li></ul>');
  500. }
  501. /**
  502. * Adds a menu link using the UI.
  503. *
  504. * @param string $parent
  505. * Optional parent menu link id.
  506. * @param string $path
  507. * The path to enter on the form. Defaults to the front page.
  508. * @param string $menu_name
  509. * Menu name. Defaults to 'tools'.
  510. * @param bool $expanded
  511. * Whether or not this menu link is expanded. Setting this to TRUE should
  512. * test whether it works when we do the authenticatedUser tests. Defaults
  513. * to FALSE.
  514. * @param string $weight
  515. * Menu weight. Defaults to 0.
  516. *
  517. * @return \Drupal\menu_link_content\Entity\MenuLinkContent
  518. * A menu link entity.
  519. */
  520. function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $expanded = FALSE, $weight = '0') {
  521. // View add menu link page.
  522. $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
  523. $this->assertResponse(200);
  524. $title = '!link_' . $this->randomMachineName(16);
  525. $edit = array(
  526. 'link[0][uri]' => $path,
  527. 'title[0][value]' => $title,
  528. 'description[0][value]' => '',
  529. 'enabled[value]' => 1,
  530. 'expanded[value]' => $expanded,
  531. 'menu_parent' => $menu_name . ':' . $parent,
  532. 'weight[0][value]' => $weight,
  533. );
  534. // Add menu link.
  535. $this->drupalPostForm(NULL, $edit, t('Save'));
  536. $this->assertResponse(200);
  537. $this->assertText('The menu link has been saved.');
  538. $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $title));
  539. $menu_link = reset($menu_links);
  540. $this->assertTrue($menu_link, 'Menu link was found in database.');
  541. $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'children' => array(), 'parent' => $parent));
  542. return $menu_link;
  543. }
  544. /**
  545. * Attempts to add menu link with invalid path or no access permission.
  546. */
  547. function addInvalidMenuLink() {
  548. foreach (array('access' => '/admin/people/permissions') as $type => $link_path) {
  549. $edit = array(
  550. 'link[0][uri]' => $link_path,
  551. 'title[0][value]' => 'title',
  552. );
  553. $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
  554. $this->assertRaw(t("The path '@link_path' is inaccessible.", array('@link_path' => $link_path)), 'Menu link was not created');
  555. }
  556. }
  557. /**
  558. * Tests that parent options are limited by depth when adding menu links.
  559. */
  560. function checkInvalidParentMenuLinks() {
  561. $last_link = NULL;
  562. $created_links = array();
  563. // Get the max depth of the tree.
  564. $menu_link_tree = \Drupal::service('menu.link_tree');
  565. $max_depth = $menu_link_tree->maxDepth();
  566. // Create a maximum number of menu links, each a child of the previous.
  567. for ($i = 0; $i <= $max_depth - 1; $i++) {
  568. $parent = $last_link ? 'tools:' . $last_link->getPluginId() : 'tools:';
  569. $title = 'title' . $i;
  570. $edit = array(
  571. 'link[0][uri]' => '/',
  572. 'title[0][value]' => $title,
  573. 'menu_parent' => $parent,
  574. 'description[0][value]' => '',
  575. 'enabled[value]' => 1,
  576. 'expanded[value]' => FALSE,
  577. 'weight[0][value]' => '0',
  578. );
  579. $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
  580. $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $title));
  581. $last_link = reset($menu_links);
  582. $created_links[] = 'tools:' . $last_link->getPluginId();
  583. }
  584. // The last link cannot be a parent in the new menu link form.
  585. $this->drupalGet('admin/structure/menu/manage/admin/add');
  586. $value = 'tools:' . $last_link->getPluginId();
  587. $this->assertNoOption('edit-menu-parent', $value, 'The invalid option is not there.');
  588. // All but the last link can be parents in the new menu link form.
  589. array_pop($created_links);
  590. foreach ($created_links as $key => $link) {
  591. $this->assertOption('edit-menu-parent', $link, 'The valid option number ' . ($key + 1) . ' is there.');
  592. }
  593. }
  594. /**
  595. * Verifies a menu link using the UI.
  596. *
  597. * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
  598. * Menu link.
  599. * @param object $item_node
  600. * Menu link content node.
  601. * @param \Drupal\menu_link_content\Entity\MenuLinkContent $parent
  602. * Parent menu link.
  603. * @param object $parent_node
  604. * Parent menu link content node.
  605. */
  606. function verifyMenuLink(MenuLinkContent $item, $item_node, MenuLinkContent $parent = NULL, $parent_node = NULL) {
  607. // View home page.
  608. $this->drupalGet('');
  609. $this->assertResponse(200);
  610. // Verify parent menu link.
  611. if (isset($parent)) {
  612. // Verify menu link.
  613. $title = $parent->getTitle();
  614. $this->assertLink($title, 0, 'Parent menu link was displayed');
  615. // Verify menu link link.
  616. $this->clickLink($title);
  617. $title = $parent_node->label();
  618. $this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Parent menu link link target was correct');
  619. }
  620. // Verify menu link.
  621. $title = $item->getTitle();
  622. $this->assertLink($title, 0, 'Menu link was displayed');
  623. // Verify menu link link.
  624. $this->clickLink($title);
  625. $title = $item_node->label();
  626. $this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Menu link link target was correct');
  627. }
  628. /**
  629. * Changes the parent of a menu link using the UI.
  630. *
  631. * @param \Drupal\menu_link_content\MenuLinkContentInterface $item
  632. * The menu link item to move.
  633. * @param int $parent
  634. * The id of the new parent.
  635. * @param string $menu_name
  636. * The menu the menu link will be moved to.
  637. */
  638. function moveMenuLink(MenuLinkContent $item, $parent, $menu_name) {
  639. $mlid = $item->id();
  640. $edit = array(
  641. 'menu_parent' => $menu_name . ':' . $parent,
  642. );
  643. $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
  644. $this->assertResponse(200);
  645. }
  646. /**
  647. * Modifies a menu link using the UI.
  648. *
  649. * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
  650. * Menu link entity.
  651. */
  652. function modifyMenuLink(MenuLinkContent $item) {
  653. $item->title->value = $this->randomMachineName(16);
  654. $mlid = $item->id();
  655. $title = $item->getTitle();
  656. // Edit menu link.
  657. $edit = array();
  658. $edit['title[0][value]'] = $title;
  659. $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
  660. $this->assertResponse(200);
  661. $this->assertText('The menu link has been saved.');
  662. // Verify menu link.
  663. $this->drupalGet('admin/structure/menu/manage/' . $item->getMenuName());
  664. $this->assertText($title, 'Menu link was edited');
  665. }
  666. /**
  667. * Resets a standard menu link using the UI.
  668. *
  669. * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link
  670. * The Menu link.
  671. * @param int $old_weight
  672. * Original title for menu link.
  673. */
  674. function resetMenuLink(MenuLinkInterface $menu_link, $old_weight) {
  675. // Reset menu link.
  676. $this->drupalPostForm("admin/structure/menu/link/{$menu_link->getPluginId()}/reset", array(), t('Reset'));
  677. $this->assertResponse(200);
  678. $this->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset');
  679. // Verify menu link.
  680. $instance = \Drupal::service('plugin.manager.menu.link')->createInstance($menu_link->getPluginId());
  681. $this->assertEqual($old_weight, $instance->getWeight(), 'Resets to the old weight.');
  682. }
  683. /**
  684. * Deletes a menu link using the UI.
  685. *
  686. * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
  687. * Menu link.
  688. */
  689. function deleteMenuLink(MenuLinkContent $item) {
  690. $mlid = $item->id();
  691. $title = $item->getTitle();
  692. // Delete menu link.
  693. $this->drupalPostForm("admin/structure/menu/item/$mlid/delete", array(), t('Delete'));
  694. $this->assertResponse(200);
  695. $this->assertRaw(t('The menu link %title has been deleted.', array('%title' => $title)), 'Menu link was deleted');
  696. // Verify deletion.
  697. $this->drupalGet('');
  698. $this->assertNoText($title, 'Menu link was deleted');
  699. }
  700. /**
  701. * Alternately disables and enables a menu link.
  702. *
  703. * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
  704. * Menu link.
  705. */
  706. function toggleMenuLink(MenuLinkContent $item) {
  707. $this->disableMenuLink($item);
  708. // Verify menu link is absent.
  709. $this->drupalGet('');
  710. $this->assertNoText($item->getTitle(), 'Menu link was not displayed');
  711. $this->enableMenuLink($item);
  712. // Verify menu link is displayed.
  713. $this->drupalGet('');
  714. $this->assertText($item->getTitle(), 'Menu link was displayed');
  715. }
  716. /**
  717. * Disables a menu link.
  718. *
  719. * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
  720. * Menu link.
  721. */
  722. function disableMenuLink(MenuLinkContent $item) {
  723. $mlid = $item->id();
  724. $edit['enabled[value]'] = FALSE;
  725. $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
  726. // Unlike most other modules, there is no confirmation message displayed.
  727. // Verify in the database.
  728. $this->assertMenuLink($item->getPluginId(), array('enabled' => 0));
  729. }
  730. /**
  731. * Enables a menu link.
  732. *
  733. * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
  734. * Menu link.
  735. */
  736. function enableMenuLink(MenuLinkContent $item) {
  737. $mlid = $item->id();
  738. $edit['enabled[value]'] = TRUE;
  739. $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
  740. // Verify in the database.
  741. $this->assertMenuLink($item->getPluginId(), array('enabled' => 1));
  742. }
  743. /**
  744. * Tests if administrative users other than user 1 can access the menu parents
  745. * AJAX callback.
  746. */
  747. public function testMenuParentsJsAccess() {
  748. $admin = $this->drupalCreateUser(array('administer menu'));
  749. $this->drupalLogin($admin);
  750. // Just check access to the callback overall, the POST data is irrelevant.
  751. $this->drupalGetAjax('admin/structure/menu/parents');
  752. $this->assertResponse(200);
  753. // Do standard user tests.
  754. // Log in the user.
  755. $this->drupalLogin($this->authenticatedUser);
  756. $this->drupalGetAjax('admin/structure/menu/parents');
  757. $this->assertResponse(403);
  758. }
  759. /**
  760. * Returns standard menu link.
  761. *
  762. * @return \Drupal\Core\Menu\MenuLinkInterface
  763. * A menu link plugin.
  764. */
  765. private function getStandardMenuLink() {
  766. // Retrieve menu link id of the Log out menu link, which will always be on
  767. // the front page.
  768. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  769. $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  770. $instance = $menu_link_manager->getInstance(['id' => 'user.logout']);
  771. $this->assertTrue((bool) $instance, 'Standard menu link was loaded');
  772. return $instance;
  773. }
  774. /**
  775. * Verifies the logged in user has the desired access to various menu pages.
  776. *
  777. * @param int $response
  778. * (optional) The expected HTTP response code. Defaults to 200.
  779. */
  780. private function verifyAccess($response = 200) {
  781. // View menu help page.
  782. $this->drupalGet('admin/help/menu');
  783. $this->assertResponse($response);
  784. if ($response == 200) {
  785. $this->assertText(t('Menu'), 'Menu help was displayed');
  786. }
  787. // View menu build overview page.
  788. $this->drupalGet('admin/structure/menu');
  789. $this->assertResponse($response);
  790. if ($response == 200) {
  791. $this->assertText(t('Menus'), 'Menu build overview page was displayed');
  792. }
  793. // View tools menu customization page.
  794. $this->drupalGet('admin/structure/menu/manage/' . $this->menu->id());
  795. $this->assertResponse($response);
  796. if ($response == 200) {
  797. $this->assertText(t('Tools'), 'Tools menu page was displayed');
  798. }
  799. // View menu edit page for a static link.
  800. $item = $this->getStandardMenuLink();
  801. $this->drupalGet('admin/structure/menu/link/' . $item->getPluginId() . '/edit');
  802. $this->assertResponse($response);
  803. if ($response == 200) {
  804. $this->assertText(t('Edit menu item'), 'Menu edit page was displayed');
  805. }
  806. // View add menu page.
  807. $this->drupalGet('admin/structure/menu/add');
  808. $this->assertResponse($response);
  809. if ($response == 200) {
  810. $this->assertText(t('Menus'), 'Add menu page was displayed');
  811. }
  812. }
  813. /**
  814. * Tests menu block settings.
  815. */
  816. protected function doTestMenuBlock() {
  817. $menu_id = $this->menu->id();
  818. $block_id = $this->blockPlacements[$menu_id];
  819. $this->drupalGet('admin/structure/block/manage/' . $block_id);
  820. $this->drupalPostForm(NULL, [
  821. 'settings[depth]' => 3,
  822. 'settings[level]' => 2,
  823. ], t('Save block'));
  824. $block = Block::load($block_id);
  825. $settings = $block->getPlugin()->getConfiguration();
  826. $this->assertEqual($settings['depth'], 3);
  827. $this->assertEqual($settings['level'], 2);
  828. // Reset settings.
  829. $block->getPlugin()->setConfigurationValue('depth', 0);
  830. $block->getPlugin()->setConfigurationValue('level', 1);
  831. $block->save();
  832. }
  833. }