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

/web/core/modules/menu_ui/tests/src/FunctionalJavascript/MenuUiJavascriptTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 163 lines | 83 code | 24 blank | 56 comment | 0 complexity | 3f3eec83ad8d7fa0752a6f1dddd303d8 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\menu_ui\FunctionalJavascript;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. use Drupal\system\Entity\Menu;
  5. use Drupal\system\MenuStorage;
  6. use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
  7. use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
  8. /**
  9. * Tests custom menu and menu links operations using the UI.
  10. *
  11. * @group menu_ui
  12. */
  13. class MenuUiJavascriptTest extends WebDriverTestBase {
  14. use ContextualLinkClickTrait;
  15. use MenuUiTrait;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected static $modules = [
  20. 'block',
  21. 'contextual',
  22. 'menu_link_content',
  23. 'menu_ui',
  24. 'test_page_test',
  25. ];
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected $defaultTheme = 'stark';
  30. /**
  31. * Tests the contextual links on a menu block.
  32. */
  33. public function testBlockContextualLinks() {
  34. $this->drupalLogin($this->drupalCreateUser([
  35. 'administer menu',
  36. 'access contextual links',
  37. 'administer blocks',
  38. ]));
  39. $menu = $this->addCustomMenu();
  40. $block = $this->drupalPlaceBlock('system_menu_block:' . $menu->id(), [
  41. 'label' => 'Custom menu',
  42. 'provider' => 'system',
  43. ]);
  44. $this->addMenuLink('', '/', $menu->id());
  45. $this->drupalGet('test-page');
  46. // Click on 'Configure block' contextual link.
  47. $this->clickContextualLink("#block-{$block->id()}", 'Configure block');
  48. // Check that we're on block configuration form.
  49. $this->assertNotEmpty($this->getSession()->getPage()->findLink('Remove block'));
  50. $this->drupalGet('test-page');
  51. // Click on 'Edit menu' contextual link.
  52. $this->clickContextualLink("#block-{$block->id()}", 'Edit menu');
  53. // Check that we're on block configuration form.
  54. $this->assertSession()->pageTextContains("Machine name: {$menu->id()}");
  55. }
  56. /**
  57. * Creates a custom menu.
  58. *
  59. * @return \Drupal\system\Entity\Menu
  60. * The custom menu that has been created.
  61. */
  62. protected function addCustomMenu() {
  63. // Try adding a menu using a menu_name that is too long.
  64. $label = $this->randomMachineName(16);
  65. $menu_id = strtolower($this->randomMachineName(MenuStorage::MAX_ID_LENGTH + 1));
  66. $this->drupalGet('admin/structure/menu/add');
  67. $page = $this->getSession()->getPage();
  68. // Type the label to activate the machine name field and the edit button.
  69. $page->fillField('Title', $label);
  70. // Wait for the machine name widget to be activated.
  71. $this->assertSession()->waitForElementVisible('css', 'button[type=button].link:contains(Edit)');
  72. // Activate the machine name text field.
  73. $page->pressButton('Edit');
  74. // Try to fill a text longer than the allowed limit.
  75. $page->fillField('Menu name', $menu_id);
  76. $page->pressButton('Save');
  77. // Check that the menu was saved with the ID truncated to the max length.
  78. $menu = Menu::load(substr($menu_id, 0, MenuStorage::MAX_ID_LENGTH));
  79. $this->assertEquals($label, $menu->label());
  80. // Check that the menu was added.
  81. $this->drupalGet('admin/structure/menu');
  82. $this->assertSession()->pageTextContains($label);
  83. // Confirm that the custom menu block is available.
  84. $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
  85. $this->clickLink('Place block');
  86. // Wait for the modal dialog to be loaded.
  87. $this->assertSession()->waitForElement('css', "div[aria-describedby=drupal-modal]");
  88. // Check that the block is available to be placed.
  89. $this->assertSession()->pageTextContains($label);
  90. return $menu;
  91. }
  92. /**
  93. * Adds a menu link using the UI.
  94. *
  95. * @param string $parent
  96. * Optional parent menu link id.
  97. * @param string $path
  98. * The path to enter on the form. Defaults to the front page.
  99. * @param string $menu_id
  100. * Menu ID. Defaults to 'tools'.
  101. * @param bool $expanded
  102. * Whether or not this menu link is expanded. Setting this to TRUE should
  103. * test whether it works when we do the authenticatedUser tests. Defaults
  104. * to FALSE.
  105. * @param string $weight
  106. * Menu weight. Defaults to 0.
  107. *
  108. * @return \Drupal\menu_link_content\Entity\MenuLinkContent
  109. * A menu link entity.
  110. */
  111. protected function addMenuLink($parent = '', $path = '/', $menu_id = 'tools', $expanded = FALSE, $weight = '0') {
  112. // View add menu link page.
  113. $this->drupalGet("admin/structure/menu/manage/$menu_id/add");
  114. $title = '!link_' . $this->randomMachineName(16);
  115. $edit = [
  116. 'link[0][uri]' => $path,
  117. 'title[0][value]' => $title,
  118. 'description[0][value]' => '',
  119. 'enabled[value]' => 1,
  120. 'expanded[value]' => $expanded,
  121. 'menu_parent' => $menu_id . ':' . $parent,
  122. 'weight[0][value]' => $weight,
  123. ];
  124. // Add menu link.
  125. $this->submitForm($edit, 'Save');
  126. $this->assertSession()->pageTextContains('The menu link has been saved.');
  127. $storage = $this->container->get('entity_type.manager')->getStorage('menu_link_content');
  128. $menu_links = $storage->loadByProperties(['title' => $title]);
  129. $menu_link = reset($menu_links);
  130. // Check that the stored menu link meeting the expectations.
  131. $this->assertNotNull($menu_link);
  132. $this->assertMenuLink([
  133. 'menu_name' => $menu_id,
  134. 'children' => [],
  135. 'parent' => $parent,
  136. ], $menu_link->getPluginId());
  137. return $menu_link;
  138. }
  139. }