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