3 namespace Drupal\Tests\views_ui\Functional;
5 use Drupal\Core\Menu\MenuTreeParameters;
6 use Drupal\menu_link_content\Entity\MenuLinkContent;
7 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
10 * Tests the UI of generic display path plugin.
13 * @see \Drupal\views\Plugin\views\display\PathPluginBase
15 class DisplayPathTest extends UITestBase {
17 use AssertPageCacheContextsAndTagsTrait;
19 protected function setUp($import_test_views = TRUE) {
20 parent::setUp($import_test_views);
22 $this->placeBlock('page_title_block');
28 public static $modules = ['menu_ui'];
31 * Views used by this test.
35 public static $testViews = ['test_view', 'test_page_display_menu'];
40 public function testPathUI() {
41 $this->doBasicPathUITest();
42 $this->doAdvancedPathsValidationTest();
43 $this->doPathXssFilterTest();
47 * Tests basic functionality in configuring a view.
49 protected function doBasicPathUITest() {
50 $this->drupalGet('admin/structure/views/view/test_view');
52 // Add a new page display and check the appearing text.
53 $this->drupalPostForm(NULL, [], 'Add Page');
54 $this->assertText(t('No path is set'), 'The right text appears if no path was set.');
55 $this->assertNoLink(t('View @display', ['@display' => 'page']), 'No view page link found on the page.');
57 // Save a path and make sure the summary appears as expected.
58 $random_path = $this->randomMachineName();
59 // @todo Once https://www.drupal.org/node/2351379 is resolved, Views will no
60 // longer use Url::fromUri(), and this path will be able to contain ':'.
61 $random_path = str_replace(':', '', $random_path);
63 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_1/path', ['path' => $random_path], t('Apply'));
64 $this->assertText('/' . $random_path, 'The custom path appears in the summary.');
65 $display_link_text = t('View @display', ['@display' => 'Page']);
66 $this->assertLink($display_link_text, 0, 'view page link found on the page.');
67 $this->clickLink($display_link_text);
68 $this->assertUrl($random_path);
72 * Tests that View paths are properly filtered for XSS.
74 public function doPathXssFilterTest() {
75 $this->drupalGet('admin/structure/views/view/test_view');
76 $this->drupalPostForm(NULL, [], 'Add Page');
77 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_2/path', ['path' => '<object>malformed_path</object>'], t('Apply'));
78 $this->drupalPostForm(NULL, [], 'Add Page');
79 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_3/path', ['path' => '<script>alert("hello");</script>'], t('Apply'));
80 $this->drupalPostForm(NULL, [], 'Add Page');
81 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_4/path', ['path' => '<script>alert("hello I have placeholders %");</script>'], t('Apply'));
82 $this->drupalPostForm('admin/structure/views/view/test_view', [], t('Save'));
83 $this->drupalGet('admin/structure/views');
84 // The anchor text should be escaped.
85 $this->assertEscaped('/<object>malformed_path</object>');
86 $this->assertEscaped('/<script>alert("hello");</script>');
87 $this->assertEscaped('/<script>alert("hello I have placeholders %");</script>');
88 // Links should be url-encoded.
89 $this->assertRaw('/%3Cobject%3Emalformed_path%3C/object%3E');
90 $this->assertRaw('/%3Cscript%3Ealert%28%22hello%22%29%3B%3C/script%3E');
94 * Tests a couple of invalid path patterns.
96 protected function doAdvancedPathsValidationTest() {
97 $url = 'admin/structure/views/nojs/display/test_view/page_1/path';
99 $this->drupalPostForm($url, ['path' => '%/magrathea'], t('Apply'));
100 $this->assertUrl($url);
101 $this->assertText('"%" may not be used for the first segment of a path.');
103 $this->drupalPostForm($url, ['path' => 'user/%1/example'], t('Apply'));
104 $this->assertUrl($url);
105 $this->assertText("Numeric placeholders may not be used. Please use plain placeholders (%).");
109 * Tests deleting a page display that has no path.
111 public function testDeleteWithNoPath() {
112 $this->drupalGet('admin/structure/views/view/test_view');
113 $this->drupalPostForm(NULL, [], t('Add Page'));
114 $this->drupalPostForm(NULL, [], t('Delete Page'));
115 $this->drupalPostForm(NULL, [], t('Save'));
116 $this->assertRaw(t('The view %view has been saved.', ['%view' => 'Test view']));
120 * Tests the menu and tab option form.
122 public function testMenuOptions() {
123 $this->container->get('module_installer')->install(['menu_ui']);
124 $this->drupalGet('admin/structure/views/view/test_view');
126 // Add a new page display.
127 $this->drupalPostForm(NULL, [], 'Add Page');
129 // Add an invalid path (only fragment).
130 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_1/path', ['path' => '#foo'], t('Apply'));
131 $this->assertText('Path is empty');
133 // Add an invalid path with a query.
134 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_1/path', ['path' => 'foo?bar'], t('Apply'));
135 $this->assertText('No query allowed.');
137 // Add an invalid path with just a query.
138 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_1/path', ['path' => '?bar'], t('Apply'));
139 $this->assertText('Path is empty');
141 // Provide a random, valid path string.
142 $random_string = $this->randomMachineName();
145 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_1/path', ['path' => $random_string], t('Apply'));
146 $this->drupalGet('admin/structure/views/view/test_view');
148 $this->drupalPostForm('admin/structure/views/nojs/display/test_view/page_1/menu', ['menu[type]' => 'default tab', 'menu[title]' => 'Test tab title'], t('Apply'));
149 $this->assertResponse(200);
150 $this->assertUrl('admin/structure/views/nojs/display/test_view/page_1/tab_options');
152 $this->drupalPostForm(NULL, ['tab_options[type]' => 'tab', 'tab_options[title]' => $this->randomString()], t('Apply'));
153 $this->assertResponse(200);
154 $this->assertUrl('admin/structure/views/view/test_view/edit/page_1');
156 $this->drupalGet('admin/structure/views/view/test_view');
157 $this->assertLink(t('Tab: @title', ['@title' => 'Test tab title']));
158 // If it's a default tab, it should also have an additional settings link.
159 $this->assertLinkByHref('admin/structure/views/nojs/display/test_view/page_1/tab_options');
161 // Ensure that you can select a parent in case the parent does not exist.
162 $this->drupalGet('admin/structure/views/nojs/display/test_page_display_menu/page_5/menu');
163 $this->assertResponse(200);
164 $menu_parent = $this->xpath('//select[@id="edit-menu-parent"]');
165 $menu_options = (array) $menu_parent[0]->findAll('css', 'option');
166 unset($menu_options['@attributes']);
168 // Convert array to make the next assertion possible.
169 $menu_options = array_map(function ($element) {
170 return $element->getText();
174 '<User account menu>',
181 '-- Compose tips (disabled)',
185 // The cache contexts associated with the (in)accessible menu links are
187 $this->assertCacheContext('user.permissions');
191 * Tests the regression in https://www.drupal.org/node/2532490.
193 public function testDefaultMenuTabRegression() {
194 $this->container->get('module_installer')->install(['menu_ui', 'menu_link_content', 'toolbar', 'system']);
195 $admin_user = $this->drupalCreateUser([
198 'bypass node access',
199 'access user profiles',
200 'view all revisions',
201 'administer permissions',
206 $this->drupalLogin($admin_user);
209 'title[0][value]' => 'Menu title',
210 'link[0][uri]' => '/admin/foo',
211 'menu_parent' => 'admin:system.admin'
213 $this->drupalPostForm('admin/structure/menu/manage/admin/add', $edit, t('Save'));
215 $menu_items = \Drupal::entityManager()->getStorage('menu_link_content')->getQuery()
219 $menu_item = end($menu_items);
220 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link_content */
221 $menu_link_content = MenuLinkContent::load($menu_item);
224 $edit['label'] = $this->randomMachineName(16);
225 $view_id = $edit['id'] = strtolower($this->randomMachineName(16));
226 $edit['description'] = $this->randomMachineName(16);
227 $edit['page[create]'] = TRUE;
228 $edit['page[path]'] = 'admin/foo';
230 $this->drupalPostForm('admin/structure/views/add', $edit, t('Save and edit'));
232 $parameters = new MenuTreeParameters();
233 $parameters->addCondition('id', $menu_link_content->getPluginId());
234 $result = \Drupal::menuTree()->load('admin', $parameters);
235 $plugin_definition = end($result)->link->getPluginDefinition();
236 $this->assertEqual('view.' . $view_id . '.page_1', $plugin_definition['route_name']);
238 $this->clickLink(t('No menu'));
240 $this->drupalPostForm(NULL, [
241 'menu[type]' => 'default tab',
242 'menu[title]' => 'Menu title',
245 $this->assertText('Default tab options');
247 $this->drupalPostForm(NULL, [
248 'tab_options[type]' => 'normal',
249 'tab_options[title]' => 'Parent title',
252 $this->drupalPostForm(NULL, [], t('Save'));
253 // Assert that saving the view will not cause an exception.
254 $this->assertResponse(200);