3 namespace Drupal\Tests\menu_ui\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6 use Drupal\system\Entity\Menu;
7 use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
8 use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
11 * Tests custom menu and menu links operations using the UI.
15 class MenuUiJavascriptTest extends WebDriverTestBase {
17 use ContextualLinkClickTrait;
23 protected static $modules = [
32 * Tests the contextual links on a menu block.
34 public function testBlockContextualLinks() {
35 $this->drupalLogin($this->drupalCreateUser([
37 'access contextual links',
40 $menu = $this->addCustomMenu();
42 $block = $this->drupalPlaceBlock('system_menu_block:' . $menu->id(), [
43 'label' => 'Custom menu',
44 'provider' => 'system',
46 $this->addMenuLink('', '/', $menu->id());
48 $this->drupalGet('test-page');
50 // Click on 'Configure block' contextual link.
51 $this->clickContextualLink("#block-{$block->id()}", 'Configure block');
52 // Check that we're on block configuration form.
53 $this->assertNotEmpty($this->getSession()->getPage()->findLink('Remove block'));
55 $this->drupalGet('test-page');
57 // Click on 'Edit menu' contextual link.
58 $this->clickContextualLink("#block-{$block->id()}", 'Edit menu');
59 // Check that we're on block configuration form.
60 $this->assertSession()->pageTextContains("Machine name: {$menu->id()}");
64 * Creates a custom menu.
66 * @return \Drupal\system\Entity\Menu
67 * The custom menu that has been created.
69 protected function addCustomMenu() {
70 // Try adding a menu using a menu_name that is too long.
71 $label = $this->randomMachineName(16);
72 $menu_id = strtolower($this->randomMachineName(MENU_MAX_MENU_NAME_LENGTH_UI + 1));
74 $this->drupalGet('admin/structure/menu/add');
75 $page = $this->getSession()->getPage();
76 // Type the label to activate the machine name field and the edit button.
77 $page->fillField('Title', $label);
78 // Wait for the machine name widget to be activated.
79 $this->assertSession()->waitForElementVisible('css', 'button[type=button].link:contains(Edit)');
80 // Activate the machine name text field.
81 $page->pressButton('Edit');
82 // Try to fill a text longer than the allowed limit.
83 $page->fillField('Menu name', $menu_id);
84 $page->pressButton('Save');
85 // Check that the menu was saved with the ID truncated to the max length.
86 $menu = Menu::load(substr($menu_id, 0, MENU_MAX_MENU_NAME_LENGTH_UI));
87 $this->assertEquals($label, $menu->label());
89 // Check that the menu was added.
90 $this->drupalGet('admin/structure/menu');
91 $this->assertSession()->pageTextContains($label);
93 // Confirm that the custom menu block is available.
94 $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
95 $this->clickLink('Place block');
96 // Wait for the modal dialog to be loaded.
97 $this->assertSession()->waitForElement('css', "div[aria-describedby=drupal-modal]");
98 // Check that the block is available to be placed.
99 $this->assertSession()->pageTextContains($label);
105 * Adds a menu link using the UI.
107 * @param string $parent
108 * Optional parent menu link id.
109 * @param string $path
110 * The path to enter on the form. Defaults to the front page.
111 * @param string $menu_id
112 * Menu ID. Defaults to 'tools'.
113 * @param bool $expanded
114 * Whether or not this menu link is expanded. Setting this to TRUE should
115 * test whether it works when we do the authenticatedUser tests. Defaults
117 * @param string $weight
118 * Menu weight. Defaults to 0.
120 * @return \Drupal\menu_link_content\Entity\MenuLinkContent
121 * A menu link entity.
123 protected function addMenuLink($parent = '', $path = '/', $menu_id = 'tools', $expanded = FALSE, $weight = '0') {
124 // View add menu link page.
125 $this->drupalGet("admin/structure/menu/manage/$menu_id/add");
127 $title = '!link_' . $this->randomMachineName(16);
129 'link[0][uri]' => $path,
130 'title[0][value]' => $title,
131 'description[0][value]' => '',
132 'enabled[value]' => 1,
133 'expanded[value]' => $expanded,
134 'menu_parent' => $menu_id . ':' . $parent,
135 'weight[0][value]' => $weight,
139 $this->drupalPostForm(NULL, $edit, 'Save');
140 $this->assertSession()->pageTextContains('The menu link has been saved.');
142 $storage = $this->container->get('entity_type.manager')->getStorage('menu_link_content');
143 $menu_links = $storage->loadByProperties(['title' => $title]);
144 $menu_link = reset($menu_links);
146 // Check that the stored menu link meeting the expectations.
147 $this->assertNotNull($menu_link);
148 $this->assertMenuLink([
149 'menu_name' => $menu_id,
152 ], $menu_link->getPluginId());