3 namespace Drupal\views_ui\Tests;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
9 * Tests the UI preview functionality.
13 class PreviewTest extends UITestBase {
16 * Views used by this test.
20 public static $testViews = ['test_preview', 'test_preview_error', 'test_pager_full', 'test_mini_pager', 'test_click_sort'];
23 * Tests contextual links in the preview form.
25 public function testPreviewContextual() {
26 \Drupal::service('module_installer')->install(['contextual']);
29 $this->drupalGet('admin/structure/views/view/test_preview/edit');
30 $this->assertResponse(200);
31 $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
33 $elements = $this->xpath('//div[@id="views-live-preview"]//ul[contains(@class, :ul-class)]/li[contains(@class, :li-class)]', [':ul-class' => 'contextual-links', ':li-class' => 'filter-add']);
34 $this->assertEqual(count($elements), 1, 'The contextual link to add a new field is shown.');
36 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
38 // Test that area text and exposed filters are present and rendered.
39 $this->assertFieldByName('id', NULL, 'ID exposed filter field found.');
40 $this->assertText('Test header text', 'Rendered header text found');
41 $this->assertText('Test footer text', 'Rendered footer text found.');
42 $this->assertText('Test empty text', 'Rendered empty text found.');
46 * Tests arguments in the preview form.
48 public function testPreviewUI() {
49 $this->drupalGet('admin/structure/views/view/test_preview/edit');
50 $this->assertResponse(200);
52 $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
54 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
55 $this->assertEqual(count($elements), 5);
57 // Filter just the first result.
58 $this->drupalPostForm(NULL, $edit = ['view_args' => '1'], t('Update preview'));
60 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
61 $this->assertEqual(count($elements), 1);
63 // Filter for no results.
64 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
66 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
67 $this->assertEqual(count($elements), 0);
69 // Test that area text and exposed filters are present and rendered.
70 $this->assertFieldByName('id', NULL, 'ID exposed filter field found.');
71 $this->assertText('Test header text', 'Rendered header text found');
72 $this->assertText('Test footer text', 'Rendered footer text found.');
73 $this->assertText('Test empty text', 'Rendered empty text found.');
77 $view['label'] = $this->randomMachineName(16);
78 $view['id'] = strtolower($this->randomMachineName(16));
79 $view['page[create]'] = 1;
80 $view['page[title]'] = $this->randomMachineName(16);
81 $view['page[path]'] = $this->randomMachineName(16);
82 $view['page[feed]'] = 1;
83 $view['page[feed_properties][path]'] = $this->randomMachineName(16);
84 $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
85 $this->clickLink(t('Feed'));
86 $this->drupalPostForm(NULL, [], t('Update preview'));
87 $result = $this->xpath('//div[@id="views-live-preview"]/pre');
88 $this->assertTrue(strpos($result[0], '<title>' . $view['page[title]'] . '</title>'), 'The Feed RSS preview was rendered.');
90 // Test the non-default UI display options.
91 // Statistics only, no query.
92 $settings = \Drupal::configFactory()->getEditable('views.settings');
93 $settings->set('ui.show.performance_statistics', TRUE)->save();
94 $this->drupalGet('admin/structure/views/view/test_preview/edit');
95 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
96 $this->assertText(t('Query build time'));
97 $this->assertText(t('Query execute time'));
98 $this->assertText(t('View render time'));
99 $this->assertNoRaw('<strong>Query</strong>');
101 // Statistics and query.
102 $settings->set('ui.show.sql_query.enabled', TRUE)->save();
103 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
104 $this->assertText(t('Query build time'));
105 $this->assertText(t('Query execute time'));
106 $this->assertText(t('View render time'));
107 $this->assertRaw('<strong>Query</strong>');
108 $query_string = <<<SQL
109 SELECT views_test_data.name AS views_test_data_name
111 {views_test_data} views_test_data
112 WHERE (views_test_data.id = '100')
114 $this->assertEscaped($query_string);
116 // Test that the statistics and query are rendered above the preview.
117 $this->assertTrue(strpos($this->getRawContent(), 'views-query-info') < strpos($this->getRawContent(), 'view-test-preview'), 'Statistics shown above the preview.');
119 // Test that statistics and query rendered below the preview.
120 $settings->set('ui.show.sql_query.where', 'below')->save();
121 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
122 $this->assertTrue(strpos($this->getRawContent(), 'view-test-preview') < strpos($this->getRawContent(), 'views-query-info'), 'Statistics shown below the preview.');
124 // Test that the preview title isn't double escaped.
125 $this->drupalPostForm("admin/structure/views/nojs/display/test_preview/default/title", $edit = ['title' => 'Double & escaped'], t('Apply'));
126 $this->drupalPostForm(NULL, [], t('Update preview'));
127 $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => t('Double & escaped')]);
128 $this->assertEqual(1, count($elements));
132 * Tests the taxonomy term preview AJAX.
134 * This tests a specific regression in the taxonomy term view preview.
136 * @see https://www.drupal.org/node/2452659
138 public function testTaxonomyAJAX() {
139 \Drupal::service('module_installer')->install(['taxonomy']);
140 $this->getPreviewAJAX('taxonomy_term', 'page_1', 0);
144 * Tests pagers in the preview form.
146 public function testPreviewWithPagersUI() {
148 // Create 11 nodes and make sure that everyone is returned.
149 $this->drupalCreateContentType(['type' => 'page']);
150 for ($i = 0; $i < 11; $i++) {
151 $this->drupalCreateNode();
155 $this->getPreviewAJAX('test_pager_full', 'default', 5);
157 // Test that the pager is present and rendered.
158 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']);
159 $this->assertTrue(!empty($elements), 'Full pager found.');
161 // Verify elements and links to pages.
162 // We expect to find 5 elements: current page == 1, links to pages 2 and
163 // and 3, links to 'next >' and 'last >>' pages.
164 $this->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.');
165 $this->assertTrue($elements[0]->a, 'Element for current page has link.');
167 $this->assertClass($elements[1], 'pager__item', 'Element for page 2 has .pager__item class.');
168 $this->assertTrue($elements[1]->a, 'Link to page 2 found.');
170 $this->assertClass($elements[2], 'pager__item', 'Element for page 3 has .pager__item class.');
171 $this->assertTrue($elements[2]->a, 'Link to page 3 found.');
173 $this->assertClass($elements[3], 'pager__item--next', 'Element for next page has .pager__item--next class.');
174 $this->assertTrue($elements[3]->a, 'Link to next page found.');
176 $this->assertClass($elements[4], 'pager__item--last', 'Element for last page has .pager__item--last class.');
177 $this->assertTrue($elements[4]->a, 'Link to last page found.');
179 // Navigate to next page.
180 $elements = $this->xpath('//li[contains(@class, :class)]/a', [':class' => 'pager__item--next']);
181 $this->clickPreviewLinkAJAX($elements[0]['href'], 5);
183 // Test that the pager is present and rendered.
184 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']);
185 $this->assertTrue(!empty($elements), 'Full pager found.');
187 // Verify elements and links to pages.
188 // We expect to find 7 elements: links to '<< first' and '< previous'
189 // pages, link to page 1, current page == 2, link to page 3 and links
190 // to 'next >' and 'last >>' pages.
191 $this->assertClass($elements[0], 'pager__item--first', 'Element for first page has .pager__item--first class.');
192 $this->assertTrue($elements[0]->a, 'Link to first page found.');
194 $this->assertClass($elements[1], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.');
195 $this->assertTrue($elements[1]->a, 'Link to previous page found.');
197 $this->assertClass($elements[2], 'pager__item', 'Element for page 1 has .pager__item class.');
198 $this->assertTrue($elements[2]->a, 'Link to page 1 found.');
200 $this->assertClass($elements[3], 'is-active', 'Element for current page has .is-active class.');
201 $this->assertTrue($elements[3]->a, 'Element for current page has link.');
203 $this->assertClass($elements[4], 'pager__item', 'Element for page 3 has .pager__item class.');
204 $this->assertTrue($elements[4]->a, 'Link to page 3 found.');
206 $this->assertClass($elements[5], 'pager__item--next', 'Element for next page has .pager__item--next class.');
207 $this->assertTrue($elements[5]->a, 'Link to next page found.');
209 $this->assertClass($elements[6], 'pager__item--last', 'Element for last page has .pager__item--last class.');
210 $this->assertTrue($elements[6]->a, 'Link to last page found.');
213 $this->getPreviewAJAX('test_mini_pager', 'default', 3);
215 // Test that the pager is present and rendered.
216 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']);
217 $this->assertTrue(!empty($elements), 'Mini pager found.');
219 // Verify elements and links to pages.
220 // We expect to find current pages element with no link, next page element
221 // with a link, and not to find previous page element.
222 $this->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.');
224 $this->assertClass($elements[1], 'pager__item--next', 'Element for next page has .pager__item--next class.');
225 $this->assertTrue($elements[1]->a, 'Link to next page found.');
227 // Navigate to next page.
228 $elements = $this->xpath('//li[contains(@class, :class)]/a', [':class' => 'pager__item--next']);
229 $this->clickPreviewLinkAJAX($elements[0]['href'], 3);
231 // Test that the pager is present and rendered.
232 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']);
233 $this->assertTrue(!empty($elements), 'Mini pager found.');
235 // Verify elements and links to pages.
236 // We expect to find 3 elements: previous page with a link, current
237 // page with no link, and next page with a link.
238 $this->assertClass($elements[0], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.');
239 $this->assertTrue($elements[0]->a, 'Link to previous page found.');
241 $this->assertClass($elements[1], 'is-active', 'Element for current page has .is-active class.');
242 $this->assertFalse(isset($elements[1]->a), 'Element for current page has no link.');
244 $this->assertClass($elements[2], 'pager__item--next', 'Element for next page has .pager__item--next class.');
245 $this->assertTrue($elements[2]->a, 'Link to next page found.');
249 * Tests the additional information query info area.
251 public function testPreviewAdditionalInfo() {
252 \Drupal::service('module_installer')->install(['views_ui_test']);
255 $this->drupalGet('admin/structure/views/view/test_preview/edit');
256 $this->assertResponse(200);
258 $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
260 // Check for implementation of hook_views_preview_info_alter().
261 // @see views_ui_test.module
262 $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => t('Test row count')]);
263 $this->assertEqual(count($elements), 1, 'Views Query Preview Info area altered.');
264 // Check that additional assets are attached.
265 $this->assertTrue(strpos($this->getDrupalSettings()['ajaxPageState']['libraries'], 'views_ui_test/views_ui_test.test') !== FALSE, 'Attached library found.');
266 $this->assertRaw('css/views_ui_test.test.css', 'Attached CSS asset found.');
270 * Tests view validation error messages in the preview.
272 public function testPreviewError() {
273 $this->drupalGet('admin/structure/views/view/test_preview_error/edit');
274 $this->assertResponse(200);
276 $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
278 $this->assertText('Unable to preview due to validation errors.', 'Preview error text found.');
282 * Tests the link to sort in the preview form.
284 public function testPreviewSortLink() {
287 $this->getPreviewAJAX('test_click_sort', 'page_1', 0);
289 // Test that the header label is present.
290 $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name']);
291 $this->assertTrue(!empty($elements), 'The header label is present.');
294 $this->assertLinkByHref('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=desc', 0, 'The output URL is as expected.');
296 // Click link to sort.
297 $this->clickPreviewLinkAJAX($elements[0]['href'], 0);
299 // Test that the header label is present.
300 $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name is-active']);
301 $this->assertTrue(!empty($elements), 'The header label is present.');
304 $this->assertLinkByHref('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=asc', 0, 'The output URL is as expected.');
308 * Get the preview form and force an AJAX preview update.
310 * @param string $view_name
312 * @param string $panel_id
313 * The view panel to test.
314 * @param int $row_count
315 * The expected number of rows in the preview.
317 protected function getPreviewAJAX($view_name, $panel_id, $row_count) {
318 $this->drupalGet('admin/structure/views/view/' . $view_name . '/preview/' . $panel_id);
319 $result = $this->drupalPostAjaxForm(NULL, [], ['op' => t('Update preview')]);
320 $this->assertPreviewAJAX($result, $row_count);
324 * Mimic clicking on a preview link.
327 * The url to navigate to.
328 * @param int $row_count
329 * The expected number of rows in the preview.
331 protected function clickPreviewLinkAJAX($url, $row_count) {
332 $content = $this->content;
333 $drupal_settings = $this->drupalSettings;
335 'wrapper' => 'views-preview-wrapper',
336 'method' => 'replaceWith',
338 $url = $this->getAbsoluteUrl($url);
339 $post = ['js' => 'true'] + $this->getAjaxPageStatePostData();
340 $result = Json::decode($this->drupalPost($url, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]));
341 if (!empty($result)) {
342 $this->drupalProcessAjaxResponse($content, $result, $ajax_settings, $drupal_settings);
344 $this->assertPreviewAJAX($result, $row_count);
348 * Assert that the AJAX response contains expected data.
350 * @param array $result
351 * An array of AJAX commands.
352 * @param int $row_count
353 * The expected number of rows in the preview.
355 protected function assertPreviewAJAX($result, $row_count) {
356 // Has AJAX callback replied with an insert command? If so, we can
357 // assume that the page content was updated with AJAX returned data.
358 $result_commands = [];
359 foreach ($result as $command) {
360 $result_commands[$command['command']] = $command;
362 $this->assertTrue(isset($result_commands['insert']), 'AJAX insert command received.');
364 // Test if preview contains the expected number of rows.
365 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
366 $this->assertEqual(count($elements), $row_count, 'Expected items found on page.');
370 * Asserts that an element has a given class.
372 * @param \SimpleXMLElement $element
373 * The element to test.
374 * @param string $class
375 * The class to assert.
376 * @param string $message
377 * (optional) A verbose message to output.
379 protected function assertClass(\SimpleXMLElement $element, $class, $message = NULL) {
380 if (!isset($message)) {
381 $message = "Class .$class found.";
383 $this->assertTrue(strpos($element['class'], $class) !== FALSE, $message);