Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / menu_ui / tests / src / FunctionalJavascript / MenuUiJavascriptTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_ui\FunctionalJavascript;
4
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;
9
10 /**
11  * Tests custom menu and menu links operations using the UI.
12  *
13  * @group menu_ui
14  */
15 class MenuUiJavascriptTest extends WebDriverTestBase {
16
17   use ContextualLinkClickTrait;
18   use MenuUiTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   protected static $modules = [
24     'block',
25     'contextual',
26     'menu_link_content',
27     'menu_ui',
28     'test_page_test',
29   ];
30
31   /**
32    * Tests the contextual links on a menu block.
33    */
34   public function testBlockContextualLinks() {
35     $this->drupalLogin($this->drupalCreateUser([
36       'administer menu',
37       'access contextual links',
38       'administer blocks',
39     ]));
40     $menu = $this->addCustomMenu();
41
42     $block = $this->drupalPlaceBlock('system_menu_block:' . $menu->id(), [
43       'label' => 'Custom menu',
44       'provider' => 'system',
45     ]);
46     $this->addMenuLink('', '/', $menu->id());
47
48     $this->drupalGet('test-page');
49
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'));
54
55     $this->drupalGet('test-page');
56
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()}");
61   }
62
63   /**
64    * Creates a custom menu.
65    *
66    * @return \Drupal\system\Entity\Menu
67    *   The custom menu that has been created.
68    */
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));
73
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());
88
89     // Check that the menu was added.
90     $this->drupalGet('admin/structure/menu');
91     $this->assertSession()->pageTextContains($label);
92
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);
100
101     return $menu;
102   }
103
104   /**
105    * Adds a menu link using the UI.
106    *
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
116    *   to FALSE.
117    * @param string $weight
118    *   Menu weight. Defaults to 0.
119    *
120    * @return \Drupal\menu_link_content\Entity\MenuLinkContent
121    *   A menu link entity.
122    */
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");
126
127     $title = '!link_' . $this->randomMachineName(16);
128     $edit = [
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,
136     ];
137
138     // Add menu link.
139     $this->drupalPostForm(NULL, $edit, 'Save');
140     $this->assertSession()->pageTextContains('The menu link has been saved.');
141
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);
145
146     // Check that the stored menu link meeting the expectations.
147     $this->assertNotNull($menu_link);
148     $this->assertMenuLink([
149       'menu_name' => $menu_id,
150       'children' => [],
151       'parent' => $parent,
152     ], $menu_link->getPluginId());
153
154     return $menu_link;
155   }
156
157 }