Chris@0: install(['contextual']); Chris@0: $this->resetAll(); Chris@0: Chris@0: $this->drupalGet('admin/structure/views/view/test_preview/edit'); Chris@0: $this->assertResponse(200); Chris@0: $this->drupalPostForm(NULL, $edit = [], t('Update preview')); Chris@0: Chris@0: $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']); Chris@0: $this->assertEqual(count($elements), 1, 'The contextual link to add a new field is shown.'); Chris@0: Chris@0: $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); Chris@0: Chris@0: // Test that area text and exposed filters are present and rendered. Chris@0: $this->assertFieldByName('id', NULL, 'ID exposed filter field found.'); Chris@0: $this->assertText('Test header text', 'Rendered header text found'); Chris@0: $this->assertText('Test footer text', 'Rendered footer text found.'); Chris@0: $this->assertText('Test empty text', 'Rendered empty text found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests arguments in the preview form. Chris@0: */ Chris@0: public function testPreviewUI() { Chris@0: $this->drupalGet('admin/structure/views/view/test_preview/edit'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: $this->drupalPostForm(NULL, $edit = [], t('Update preview')); Chris@0: Chris@0: $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); Chris@0: $this->assertEqual(count($elements), 5); Chris@0: Chris@0: // Filter just the first result. Chris@0: $this->drupalPostForm(NULL, $edit = ['view_args' => '1'], t('Update preview')); Chris@0: Chris@0: $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); Chris@0: $this->assertEqual(count($elements), 1); Chris@0: Chris@0: // Filter for no results. Chris@0: $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); Chris@0: Chris@0: $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); Chris@0: $this->assertEqual(count($elements), 0); Chris@0: Chris@0: // Test that area text and exposed filters are present and rendered. Chris@0: $this->assertFieldByName('id', NULL, 'ID exposed filter field found.'); Chris@0: $this->assertText('Test header text', 'Rendered header text found'); Chris@0: $this->assertText('Test footer text', 'Rendered footer text found.'); Chris@0: $this->assertText('Test empty text', 'Rendered empty text found.'); Chris@0: Chris@0: // Test feed preview. Chris@0: $view = []; Chris@0: $view['label'] = $this->randomMachineName(16); Chris@0: $view['id'] = strtolower($this->randomMachineName(16)); Chris@0: $view['page[create]'] = 1; Chris@0: $view['page[title]'] = $this->randomMachineName(16); Chris@0: $view['page[path]'] = $this->randomMachineName(16); Chris@0: $view['page[feed]'] = 1; Chris@0: $view['page[feed_properties][path]'] = $this->randomMachineName(16); Chris@0: $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit')); Chris@0: $this->clickLink(t('Feed')); Chris@0: $this->drupalPostForm(NULL, [], t('Update preview')); Chris@0: $result = $this->xpath('//div[@id="views-live-preview"]/pre'); Chris@0: $this->assertTrue(strpos($result[0], '' . $view['page[title]'] . ''), 'The Feed RSS preview was rendered.'); Chris@0: Chris@0: // Test the non-default UI display options. Chris@0: // Statistics only, no query. Chris@0: $settings = \Drupal::configFactory()->getEditable('views.settings'); Chris@0: $settings->set('ui.show.performance_statistics', TRUE)->save(); Chris@0: $this->drupalGet('admin/structure/views/view/test_preview/edit'); Chris@0: $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); Chris@0: $this->assertText(t('Query build time')); Chris@0: $this->assertText(t('Query execute time')); Chris@0: $this->assertText(t('View render time')); Chris@0: $this->assertNoRaw('Query'); Chris@0: Chris@0: // Statistics and query. Chris@0: $settings->set('ui.show.sql_query.enabled', TRUE)->save(); Chris@0: $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); Chris@0: $this->assertText(t('Query build time')); Chris@0: $this->assertText(t('Query execute time')); Chris@0: $this->assertText(t('View render time')); Chris@0: $this->assertRaw('Query'); Chris@0: $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' )"); Chris@0: Chris@0: // Test that the statistics and query are rendered above the preview. Chris@0: $this->assertTrue(strpos($this->getRawContent(), 'views-query-info') < strpos($this->getRawContent(), 'view-test-preview'), 'Statistics shown above the preview.'); Chris@0: Chris@0: // Test that statistics and query rendered below the preview. Chris@0: $settings->set('ui.show.sql_query.where', 'below')->save(); Chris@0: $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview')); Chris@0: $this->assertTrue(strpos($this->getRawContent(), 'view-test-preview') < strpos($this->getRawContent(), 'views-query-info'), 'Statistics shown below the preview.'); Chris@0: Chris@0: // Test that the preview title isn't double escaped. Chris@0: $this->drupalPostForm("admin/structure/views/nojs/display/test_preview/default/title", $edit = ['title' => 'Double & escaped'], t('Apply')); Chris@0: $this->drupalPostForm(NULL, [], t('Update preview')); Chris@0: $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => t('Double & escaped')]); Chris@0: $this->assertEqual(1, count($elements)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the taxonomy term preview AJAX. Chris@0: * Chris@0: * This tests a specific regression in the taxonomy term view preview. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2452659 Chris@0: */ Chris@0: public function testTaxonomyAJAX() { Chris@0: \Drupal::service('module_installer')->install(['taxonomy']); Chris@0: $this->getPreviewAJAX('taxonomy_term', 'page_1', 0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests pagers in the preview form. Chris@0: */ Chris@0: public function testPreviewWithPagersUI() { Chris@0: Chris@0: // Create 11 nodes and make sure that everyone is returned. Chris@0: $this->drupalCreateContentType(['type' => 'page']); Chris@0: for ($i = 0; $i < 11; $i++) { Chris@0: $this->drupalCreateNode(); Chris@0: } Chris@0: Chris@0: // Test Full Pager. Chris@0: $this->getPreviewAJAX('test_pager_full', 'default', 5); Chris@0: Chris@0: // Test that the pager is present and rendered. Chris@0: $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); Chris@0: $this->assertTrue(!empty($elements), 'Full pager found.'); Chris@0: Chris@0: // Verify elements and links to pages. Chris@0: // We expect to find 5 elements: current page == 1, links to pages 2 and Chris@0: // and 3, links to 'next >' and 'last >>' pages. Chris@0: $this->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.'); Chris@0: $this->assertTrue($elements[0]->a, 'Element for current page has link.'); Chris@0: Chris@0: $this->assertClass($elements[1], 'pager__item', 'Element for page 2 has .pager__item class.'); Chris@0: $this->assertTrue($elements[1]->a, 'Link to page 2 found.'); Chris@0: Chris@0: $this->assertClass($elements[2], 'pager__item', 'Element for page 3 has .pager__item class.'); Chris@0: $this->assertTrue($elements[2]->a, 'Link to page 3 found.'); Chris@0: Chris@0: $this->assertClass($elements[3], 'pager__item--next', 'Element for next page has .pager__item--next class.'); Chris@0: $this->assertTrue($elements[3]->a, 'Link to next page found.'); Chris@0: Chris@0: $this->assertClass($elements[4], 'pager__item--last', 'Element for last page has .pager__item--last class.'); Chris@0: $this->assertTrue($elements[4]->a, 'Link to last page found.'); Chris@0: Chris@0: // Navigate to next page. Chris@0: $elements = $this->xpath('//li[contains(@class, :class)]/a', [':class' => 'pager__item--next']); Chris@0: $this->clickPreviewLinkAJAX($elements[0]['href'], 5); Chris@0: Chris@0: // Test that the pager is present and rendered. Chris@0: $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); Chris@0: $this->assertTrue(!empty($elements), 'Full pager found.'); Chris@0: Chris@0: // Verify elements and links to pages. Chris@0: // We expect to find 7 elements: links to '<< first' and '< previous' Chris@0: // pages, link to page 1, current page == 2, link to page 3 and links Chris@0: // to 'next >' and 'last >>' pages. Chris@0: $this->assertClass($elements[0], 'pager__item--first', 'Element for first page has .pager__item--first class.'); Chris@0: $this->assertTrue($elements[0]->a, 'Link to first page found.'); Chris@0: Chris@0: $this->assertClass($elements[1], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.'); Chris@0: $this->assertTrue($elements[1]->a, 'Link to previous page found.'); Chris@0: Chris@0: $this->assertClass($elements[2], 'pager__item', 'Element for page 1 has .pager__item class.'); Chris@0: $this->assertTrue($elements[2]->a, 'Link to page 1 found.'); Chris@0: Chris@0: $this->assertClass($elements[3], 'is-active', 'Element for current page has .is-active class.'); Chris@0: $this->assertTrue($elements[3]->a, 'Element for current page has link.'); Chris@0: Chris@0: $this->assertClass($elements[4], 'pager__item', 'Element for page 3 has .pager__item class.'); Chris@0: $this->assertTrue($elements[4]->a, 'Link to page 3 found.'); Chris@0: Chris@0: $this->assertClass($elements[5], 'pager__item--next', 'Element for next page has .pager__item--next class.'); Chris@0: $this->assertTrue($elements[5]->a, 'Link to next page found.'); Chris@0: Chris@0: $this->assertClass($elements[6], 'pager__item--last', 'Element for last page has .pager__item--last class.'); Chris@0: $this->assertTrue($elements[6]->a, 'Link to last page found.'); Chris@0: Chris@0: // Test Mini Pager. Chris@0: $this->getPreviewAJAX('test_mini_pager', 'default', 3); Chris@0: Chris@0: // Test that the pager is present and rendered. Chris@0: $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); Chris@0: $this->assertTrue(!empty($elements), 'Mini pager found.'); Chris@0: Chris@0: // Verify elements and links to pages. Chris@0: // We expect to find current pages element with no link, next page element Chris@0: // with a link, and not to find previous page element. Chris@0: $this->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.'); Chris@0: Chris@0: $this->assertClass($elements[1], 'pager__item--next', 'Element for next page has .pager__item--next class.'); Chris@0: $this->assertTrue($elements[1]->a, 'Link to next page found.'); Chris@0: Chris@0: // Navigate to next page. Chris@0: $elements = $this->xpath('//li[contains(@class, :class)]/a', [':class' => 'pager__item--next']); Chris@0: $this->clickPreviewLinkAJAX($elements[0]['href'], 3); Chris@0: Chris@0: // Test that the pager is present and rendered. Chris@0: $elements = $this->xpath('//ul[contains(@class, :class)]/li', [':class' => 'pager__items']); Chris@0: $this->assertTrue(!empty($elements), 'Mini pager found.'); Chris@0: Chris@0: // Verify elements and links to pages. Chris@0: // We expect to find 3 elements: previous page with a link, current Chris@0: // page with no link, and next page with a link. Chris@0: $this->assertClass($elements[0], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.'); Chris@0: $this->assertTrue($elements[0]->a, 'Link to previous page found.'); Chris@0: Chris@0: $this->assertClass($elements[1], 'is-active', 'Element for current page has .is-active class.'); Chris@0: $this->assertFalse(isset($elements[1]->a), 'Element for current page has no link.'); Chris@0: Chris@0: $this->assertClass($elements[2], 'pager__item--next', 'Element for next page has .pager__item--next class.'); Chris@0: $this->assertTrue($elements[2]->a, 'Link to next page found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the additional information query info area. Chris@0: */ Chris@0: public function testPreviewAdditionalInfo() { Chris@0: \Drupal::service('module_installer')->install(['views_ui_test']); Chris@0: $this->resetAll(); Chris@0: Chris@0: $this->drupalGet('admin/structure/views/view/test_preview/edit'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: $this->drupalPostForm(NULL, $edit = [], t('Update preview')); Chris@0: Chris@0: // Check for implementation of hook_views_preview_info_alter(). Chris@0: // @see views_ui_test.module Chris@0: $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => t('Test row count')]); Chris@0: $this->assertEqual(count($elements), 1, 'Views Query Preview Info area altered.'); Chris@0: // Check that additional assets are attached. Chris@0: $this->assertTrue(strpos($this->getDrupalSettings()['ajaxPageState']['libraries'], 'views_ui_test/views_ui_test.test') !== FALSE, 'Attached library found.'); Chris@0: $this->assertRaw('css/views_ui_test.test.css', 'Attached CSS asset found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests view validation error messages in the preview. Chris@0: */ Chris@0: public function testPreviewError() { Chris@0: $this->drupalGet('admin/structure/views/view/test_preview_error/edit'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: $this->drupalPostForm(NULL, $edit = [], t('Update preview')); Chris@0: Chris@0: $this->assertText('Unable to preview due to validation errors.', 'Preview error text found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the link to sort in the preview form. Chris@0: */ Chris@0: public function testPreviewSortLink() { Chris@0: Chris@0: // Get the preview. Chris@0: $this->getPreviewAJAX('test_click_sort', 'page_1', 0); Chris@0: Chris@0: // Test that the header label is present. Chris@0: $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name']); Chris@0: $this->assertTrue(!empty($elements), 'The header label is present.'); Chris@0: Chris@0: // Verify link. Chris@0: $this->assertLinkByHref('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=desc', 0, 'The output URL is as expected.'); Chris@0: Chris@0: // Click link to sort. Chris@0: $this->clickPreviewLinkAJAX($elements[0]['href'], 0); Chris@0: Chris@0: // Test that the header label is present. Chris@0: $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name is-active']); Chris@0: $this->assertTrue(!empty($elements), 'The header label is present.'); Chris@0: Chris@0: // Verify link. Chris@0: $this->assertLinkByHref('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=asc', 0, 'The output URL is as expected.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the preview form and force an AJAX preview update. Chris@0: * Chris@0: * @param string $view_name Chris@0: * The view to test. Chris@0: * @param string $panel_id Chris@0: * The view panel to test. Chris@0: * @param int $row_count Chris@0: * The expected number of rows in the preview. Chris@0: */ Chris@0: protected function getPreviewAJAX($view_name, $panel_id, $row_count) { Chris@0: $this->drupalGet('admin/structure/views/view/' . $view_name . '/preview/' . $panel_id); Chris@0: $result = $this->drupalPostAjaxForm(NULL, [], ['op' => t('Update preview')]); Chris@0: $this->assertPreviewAJAX($result, $row_count); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Mimic clicking on a preview link. Chris@0: * Chris@0: * @param string $url Chris@0: * The url to navigate to. Chris@0: * @param int $row_count Chris@0: * The expected number of rows in the preview. Chris@0: */ Chris@0: protected function clickPreviewLinkAJAX($url, $row_count) { Chris@0: $content = $this->content; Chris@0: $drupal_settings = $this->drupalSettings; Chris@0: $ajax_settings = [ Chris@0: 'wrapper' => 'views-preview-wrapper', Chris@0: 'method' => 'replaceWith', Chris@0: ]; Chris@0: $url = $this->getAbsoluteUrl($url); Chris@0: $post = ['js' => 'true'] + $this->getAjaxPageStatePostData(); Chris@0: $result = Json::decode($this->drupalPost($url, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']])); Chris@0: if (!empty($result)) { Chris@0: $this->drupalProcessAjaxResponse($content, $result, $ajax_settings, $drupal_settings); Chris@0: } Chris@0: $this->assertPreviewAJAX($result, $row_count); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert that the AJAX response contains expected data. Chris@0: * Chris@0: * @param array $result Chris@0: * An array of AJAX commands. Chris@0: * @param int $row_count Chris@0: * The expected number of rows in the preview. Chris@0: */ Chris@0: protected function assertPreviewAJAX($result, $row_count) { Chris@0: // Has AJAX callback replied with an insert command? If so, we can Chris@0: // assume that the page content was updated with AJAX returned data. Chris@0: $result_commands = []; Chris@0: foreach ($result as $command) { Chris@0: $result_commands[$command['command']] = $command; Chris@0: } Chris@0: $this->assertTrue(isset($result_commands['insert']), 'AJAX insert command received.'); Chris@0: Chris@0: // Test if preview contains the expected number of rows. Chris@0: $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]'); Chris@0: $this->assertEqual(count($elements), $row_count, 'Expected items found on page.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that an element has a given class. Chris@0: * Chris@0: * @param \SimpleXMLElement $element Chris@0: * The element to test. Chris@0: * @param string $class Chris@0: * The class to assert. Chris@0: * @param string $message Chris@0: * (optional) A verbose message to output. Chris@0: */ Chris@0: protected function assertClass(\SimpleXMLElement $element, $class, $message = NULL) { Chris@0: if (!isset($message)) { Chris@0: $message = "Class .$class found."; Chris@0: } Chris@0: $this->assertTrue(strpos($element['class'], $class) !== FALSE, $message); Chris@0: } Chris@0: Chris@0: }