Mercurial > hg > isophonics-drupal-site
comparison core/modules/views_ui/src/Tests/PreviewTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\views_ui\Tests; | |
4 | |
5 use Drupal\Component\Serialization\Json; | |
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber; | |
7 | |
8 /** | |
9 * Tests the UI preview functionality. | |
10 * | |
11 * @group views_ui | |
12 */ | |
13 class PreviewTest extends UITestBase { | |
14 | |
15 /** | |
16 * Views used by this test. | |
17 * | |
18 * @var array | |
19 */ | |
20 public static $testViews = ['test_preview', 'test_preview_error', 'test_pager_full', 'test_mini_pager', 'test_click_sort']; | |
21 | |
22 /** | |
23 * Tests contextual links in the preview form. | |
24 */ | |
25 public function testPreviewContextual() { | |
26 \Drupal::service('module_installer')->install(['contextual']); | |
27 $this->resetAll(); | |
28 | |
29 $this->drupalGet('admin/structure/views/view/test_preview/edit'); | |
30 $this->assertResponse(200); | |
31 $this->drupalPostForm(NULL, $edit = [], t('Update preview')); | |
32 | |
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.'); | |
35 | |
36 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); | |
37 | |
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.'); | |
43 } | |
44 | |
45 /** | |
46 * Tests arguments in the preview form. | |
47 */ | |
48 public function testPreviewUI() { | |
49 $this->drupalGet('admin/structure/views/view/test_preview/edit'); | |
50 $this->assertResponse(200); | |
51 | |
52 $this->drupalPostForm(NULL, $edit = [], t('Update preview')); | |
53 | |
54 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); | |
55 $this->assertEqual(count($elements), 5); | |
56 | |
57 // Filter just the first result. | |
58 $this->drupalPostForm(NULL, $edit = ['view_args' => '1'], t('Update preview')); | |
59 | |
60 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); | |
61 $this->assertEqual(count($elements), 1); | |
62 | |
63 // Filter for no results. | |
64 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); | |
65 | |
66 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); | |
67 $this->assertEqual(count($elements), 0); | |
68 | |
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.'); | |
74 | |
75 // Test feed preview. | |
76 $view = []; | |
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.'); | |
89 | |
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>'); | |
100 | |
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 $this->assertText("SELECT views_test_data.name AS views_test_data_name\nFROM \n{views_test_data} views_test_data\nWHERE (views_test_data.id = '100' )"); | |
109 | |
110 // Test that the statistics and query are rendered above the preview. | |
111 $this->assertTrue(strpos($this->getRawContent(), 'views-query-info') < strpos($this->getRawContent(), 'view-test-preview'), 'Statistics shown above the preview.'); | |
112 | |
113 // Test that statistics and query rendered below the preview. | |
114 $settings->set('ui.show.sql_query.where', 'below')->save(); | |
115 $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); | |
116 $this->assertTrue(strpos($this->getRawContent(), 'view-test-preview') < strpos($this->getRawContent(), 'views-query-info'), 'Statistics shown below the preview.'); | |
117 | |
118 // Test that the preview title isn't double escaped. | |
119 $this->drupalPostForm("admin/structure/views/nojs/display/test_preview/default/title", $edit = ['title' => 'Double & escaped'], t('Apply')); | |
120 $this->drupalPostForm(NULL, [], t('Update preview')); | |
121 $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => t('Double & escaped')]); | |
122 $this->assertEqual(1, count($elements)); | |
123 } | |
124 | |
125 /** | |
126 * Tests the taxonomy term preview AJAX. | |
127 * | |
128 * This tests a specific regression in the taxonomy term view preview. | |
129 * | |
130 * @see https://www.drupal.org/node/2452659 | |
131 */ | |
132 public function testTaxonomyAJAX() { | |
133 \Drupal::service('module_installer')->install(['taxonomy']); | |
134 $this->getPreviewAJAX('taxonomy_term', 'page_1', 0); | |
135 } | |
136 | |
137 /** | |
138 * Tests pagers in the preview form. | |
139 */ | |
140 public function testPreviewWithPagersUI() { | |
141 | |
142 // Create 11 nodes and make sure that everyone is returned. | |
143 $this->drupalCreateContentType(['type' => 'page']); | |
144 for ($i = 0; $i < 11; $i++) { | |
145 $this->drupalCreateNode(); | |
146 } | |
147 | |
148 // Test Full Pager. | |
149 $this->getPreviewAJAX('test_pager_full', 'default', 5); | |
150 | |
151 // Test that the pager is present and rendered. | |
152 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); | |
153 $this->assertTrue(!empty($elements), 'Full pager found.'); | |
154 | |
155 // Verify elements and links to pages. | |
156 // We expect to find 5 elements: current page == 1, links to pages 2 and | |
157 // and 3, links to 'next >' and 'last >>' pages. | |
158 $this->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.'); | |
159 $this->assertTrue($elements[0]->a, 'Element for current page has link.'); | |
160 | |
161 $this->assertClass($elements[1], 'pager__item', 'Element for page 2 has .pager__item class.'); | |
162 $this->assertTrue($elements[1]->a, 'Link to page 2 found.'); | |
163 | |
164 $this->assertClass($elements[2], 'pager__item', 'Element for page 3 has .pager__item class.'); | |
165 $this->assertTrue($elements[2]->a, 'Link to page 3 found.'); | |
166 | |
167 $this->assertClass($elements[3], 'pager__item--next', 'Element for next page has .pager__item--next class.'); | |
168 $this->assertTrue($elements[3]->a, 'Link to next page found.'); | |
169 | |
170 $this->assertClass($elements[4], 'pager__item--last', 'Element for last page has .pager__item--last class.'); | |
171 $this->assertTrue($elements[4]->a, 'Link to last page found.'); | |
172 | |
173 // Navigate to next page. | |
174 $elements = $this->xpath('//li[contains(@class, :class)]/a', [':class' => 'pager__item--next']); | |
175 $this->clickPreviewLinkAJAX($elements[0]['href'], 5); | |
176 | |
177 // Test that the pager is present and rendered. | |
178 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); | |
179 $this->assertTrue(!empty($elements), 'Full pager found.'); | |
180 | |
181 // Verify elements and links to pages. | |
182 // We expect to find 7 elements: links to '<< first' and '< previous' | |
183 // pages, link to page 1, current page == 2, link to page 3 and links | |
184 // to 'next >' and 'last >>' pages. | |
185 $this->assertClass($elements[0], 'pager__item--first', 'Element for first page has .pager__item--first class.'); | |
186 $this->assertTrue($elements[0]->a, 'Link to first page found.'); | |
187 | |
188 $this->assertClass($elements[1], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.'); | |
189 $this->assertTrue($elements[1]->a, 'Link to previous page found.'); | |
190 | |
191 $this->assertClass($elements[2], 'pager__item', 'Element for page 1 has .pager__item class.'); | |
192 $this->assertTrue($elements[2]->a, 'Link to page 1 found.'); | |
193 | |
194 $this->assertClass($elements[3], 'is-active', 'Element for current page has .is-active class.'); | |
195 $this->assertTrue($elements[3]->a, 'Element for current page has link.'); | |
196 | |
197 $this->assertClass($elements[4], 'pager__item', 'Element for page 3 has .pager__item class.'); | |
198 $this->assertTrue($elements[4]->a, 'Link to page 3 found.'); | |
199 | |
200 $this->assertClass($elements[5], 'pager__item--next', 'Element for next page has .pager__item--next class.'); | |
201 $this->assertTrue($elements[5]->a, 'Link to next page found.'); | |
202 | |
203 $this->assertClass($elements[6], 'pager__item--last', 'Element for last page has .pager__item--last class.'); | |
204 $this->assertTrue($elements[6]->a, 'Link to last page found.'); | |
205 | |
206 // Test Mini Pager. | |
207 $this->getPreviewAJAX('test_mini_pager', 'default', 3); | |
208 | |
209 // Test that the pager is present and rendered. | |
210 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); | |
211 $this->assertTrue(!empty($elements), 'Mini pager found.'); | |
212 | |
213 // Verify elements and links to pages. | |
214 // We expect to find current pages element with no link, next page element | |
215 // with a link, and not to find previous page element. | |
216 $this->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.'); | |
217 | |
218 $this->assertClass($elements[1], 'pager__item--next', 'Element for next page has .pager__item--next class.'); | |
219 $this->assertTrue($elements[1]->a, 'Link to next page found.'); | |
220 | |
221 // Navigate to next page. | |
222 $elements = $this->xpath('//li[contains(@class, :class)]/a', [':class' => 'pager__item--next']); | |
223 $this->clickPreviewLinkAJAX($elements[0]['href'], 3); | |
224 | |
225 // Test that the pager is present and rendered. | |
226 $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); | |
227 $this->assertTrue(!empty($elements), 'Mini pager found.'); | |
228 | |
229 // Verify elements and links to pages. | |
230 // We expect to find 3 elements: previous page with a link, current | |
231 // page with no link, and next page with a link. | |
232 $this->assertClass($elements[0], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.'); | |
233 $this->assertTrue($elements[0]->a, 'Link to previous page found.'); | |
234 | |
235 $this->assertClass($elements[1], 'is-active', 'Element for current page has .is-active class.'); | |
236 $this->assertFalse(isset($elements[1]->a), 'Element for current page has no link.'); | |
237 | |
238 $this->assertClass($elements[2], 'pager__item--next', 'Element for next page has .pager__item--next class.'); | |
239 $this->assertTrue($elements[2]->a, 'Link to next page found.'); | |
240 } | |
241 | |
242 /** | |
243 * Tests the additional information query info area. | |
244 */ | |
245 public function testPreviewAdditionalInfo() { | |
246 \Drupal::service('module_installer')->install(['views_ui_test']); | |
247 $this->resetAll(); | |
248 | |
249 $this->drupalGet('admin/structure/views/view/test_preview/edit'); | |
250 $this->assertResponse(200); | |
251 | |
252 $this->drupalPostForm(NULL, $edit = [], t('Update preview')); | |
253 | |
254 // Check for implementation of hook_views_preview_info_alter(). | |
255 // @see views_ui_test.module | |
256 $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => t('Test row count')]); | |
257 $this->assertEqual(count($elements), 1, 'Views Query Preview Info area altered.'); | |
258 // Check that additional assets are attached. | |
259 $this->assertTrue(strpos($this->getDrupalSettings()['ajaxPageState']['libraries'], 'views_ui_test/views_ui_test.test') !== FALSE, 'Attached library found.'); | |
260 $this->assertRaw('css/views_ui_test.test.css', 'Attached CSS asset found.'); | |
261 } | |
262 | |
263 /** | |
264 * Tests view validation error messages in the preview. | |
265 */ | |
266 public function testPreviewError() { | |
267 $this->drupalGet('admin/structure/views/view/test_preview_error/edit'); | |
268 $this->assertResponse(200); | |
269 | |
270 $this->drupalPostForm(NULL, $edit = [], t('Update preview')); | |
271 | |
272 $this->assertText('Unable to preview due to validation errors.', 'Preview error text found.'); | |
273 } | |
274 | |
275 /** | |
276 * Tests the link to sort in the preview form. | |
277 */ | |
278 public function testPreviewSortLink() { | |
279 | |
280 // Get the preview. | |
281 $this->getPreviewAJAX('test_click_sort', 'page_1', 0); | |
282 | |
283 // Test that the header label is present. | |
284 $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name']); | |
285 $this->assertTrue(!empty($elements), 'The header label is present.'); | |
286 | |
287 // Verify link. | |
288 $this->assertLinkByHref('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=desc', 0, 'The output URL is as expected.'); | |
289 | |
290 // Click link to sort. | |
291 $this->clickPreviewLinkAJAX($elements[0]['href'], 0); | |
292 | |
293 // Test that the header label is present. | |
294 $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name is-active']); | |
295 $this->assertTrue(!empty($elements), 'The header label is present.'); | |
296 | |
297 // Verify link. | |
298 $this->assertLinkByHref('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=asc', 0, 'The output URL is as expected.'); | |
299 } | |
300 | |
301 /** | |
302 * Get the preview form and force an AJAX preview update. | |
303 * | |
304 * @param string $view_name | |
305 * The view to test. | |
306 * @param string $panel_id | |
307 * The view panel to test. | |
308 * @param int $row_count | |
309 * The expected number of rows in the preview. | |
310 */ | |
311 protected function getPreviewAJAX($view_name, $panel_id, $row_count) { | |
312 $this->drupalGet('admin/structure/views/view/' . $view_name . '/preview/' . $panel_id); | |
313 $result = $this->drupalPostAjaxForm(NULL, [], ['op' => t('Update preview')]); | |
314 $this->assertPreviewAJAX($result, $row_count); | |
315 } | |
316 | |
317 /** | |
318 * Mimic clicking on a preview link. | |
319 * | |
320 * @param string $url | |
321 * The url to navigate to. | |
322 * @param int $row_count | |
323 * The expected number of rows in the preview. | |
324 */ | |
325 protected function clickPreviewLinkAJAX($url, $row_count) { | |
326 $content = $this->content; | |
327 $drupal_settings = $this->drupalSettings; | |
328 $ajax_settings = [ | |
329 'wrapper' => 'views-preview-wrapper', | |
330 'method' => 'replaceWith', | |
331 ]; | |
332 $url = $this->getAbsoluteUrl($url); | |
333 $post = ['js' => 'true'] + $this->getAjaxPageStatePostData(); | |
334 $result = Json::decode($this->drupalPost($url, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']])); | |
335 if (!empty($result)) { | |
336 $this->drupalProcessAjaxResponse($content, $result, $ajax_settings, $drupal_settings); | |
337 } | |
338 $this->assertPreviewAJAX($result, $row_count); | |
339 } | |
340 | |
341 /** | |
342 * Assert that the AJAX response contains expected data. | |
343 * | |
344 * @param array $result | |
345 * An array of AJAX commands. | |
346 * @param int $row_count | |
347 * The expected number of rows in the preview. | |
348 */ | |
349 protected function assertPreviewAJAX($result, $row_count) { | |
350 // Has AJAX callback replied with an insert command? If so, we can | |
351 // assume that the page content was updated with AJAX returned data. | |
352 $result_commands = []; | |
353 foreach ($result as $command) { | |
354 $result_commands[$command['command']] = $command; | |
355 } | |
356 $this->assertTrue(isset($result_commands['insert']), 'AJAX insert command received.'); | |
357 | |
358 // Test if preview contains the expected number of rows. | |
359 $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); | |
360 $this->assertEqual(count($elements), $row_count, 'Expected items found on page.'); | |
361 } | |
362 | |
363 /** | |
364 * Asserts that an element has a given class. | |
365 * | |
366 * @param \SimpleXMLElement $element | |
367 * The element to test. | |
368 * @param string $class | |
369 * The class to assert. | |
370 * @param string $message | |
371 * (optional) A verbose message to output. | |
372 */ | |
373 protected function assertClass(\SimpleXMLElement $element, $class, $message = NULL) { | |
374 if (!isset($message)) { | |
375 $message = "Class .$class found."; | |
376 } | |
377 $this->assertTrue(strpos($element['class'], $class) !== FALSE, $message); | |
378 } | |
379 | |
380 } |