You are here

public function PreviewTest::testPreviewUI in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views_ui/tests/src/Functional/PreviewTest.php \Drupal\Tests\views_ui\Functional\PreviewTest::testPreviewUI()
  2. 10 core/modules/views_ui/tests/src/Functional/PreviewTest.php \Drupal\Tests\views_ui\Functional\PreviewTest::testPreviewUI()

Tests arguments in the preview form.

File

core/modules/views_ui/tests/src/Functional/PreviewTest.php, line 50

Class

PreviewTest
Tests the UI preview functionality.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testPreviewUI() {
  $this
    ->drupalGet('admin/structure/views/view/test_preview/edit');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, $edit = [], t('Update preview'));
  $elements = $this
    ->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
  $this
    ->assertCount(5, $elements);

  // Filter just the first result.
  $this
    ->drupalPostForm(NULL, $edit = [
    'view_args' => '1',
  ], t('Update preview'));
  $elements = $this
    ->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
  $this
    ->assertCount(1, $elements);

  // Filter for no results.
  $this
    ->drupalPostForm(NULL, $edit = [
    'view_args' => '100',
  ], t('Update preview'));
  $elements = $this
    ->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
  $this
    ->assertCount(0, $elements);

  // Test that area text and exposed filters are present and rendered.
  $this
    ->assertFieldByName('id', NULL, 'ID exposed filter field found.');
  $this
    ->assertText('Test header text', 'Rendered header text found');
  $this
    ->assertText('Test footer text', 'Rendered footer text found.');
  $this
    ->assertText('Test empty text', 'Rendered empty text found.');

  // Test feed preview.
  $view = [];
  $view['label'] = $this
    ->randomMachineName(16);
  $view['id'] = strtolower($this
    ->randomMachineName(16));
  $view['page[create]'] = 1;
  $view['page[title]'] = $this
    ->randomMachineName(16);
  $view['page[path]'] = $this
    ->randomMachineName(16);
  $view['page[feed]'] = 1;
  $view['page[feed_properties][path]'] = $this
    ->randomMachineName(16);
  $this
    ->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
  $this
    ->clickLink(t('Feed'));
  $this
    ->drupalPostForm(NULL, [], t('Update preview'));
  $result = $this
    ->xpath('//div[@id="views-live-preview"]/pre');
  $this
    ->assertStringContainsString('<title>' . $view['page[title]'] . '</title>', $result[0]
    ->getText(), 'The Feed RSS preview was rendered.');

  // Test the non-default UI display options.
  // Statistics only, no query.
  $settings = \Drupal::configFactory()
    ->getEditable('views.settings');
  $settings
    ->set('ui.show.performance_statistics', TRUE)
    ->save();
  $this
    ->drupalGet('admin/structure/views/view/test_preview/edit');
  $this
    ->drupalPostForm(NULL, $edit = [
    'view_args' => '100',
  ], t('Update preview'));
  $this
    ->assertText(t('Query build time'));
  $this
    ->assertText(t('Query execute time'));
  $this
    ->assertText(t('View render time'));
  $this
    ->assertNoRaw('<strong>Query</strong>');

  // Statistics and query.
  $settings
    ->set('ui.show.sql_query.enabled', TRUE)
    ->save();
  $this
    ->drupalPostForm(NULL, $edit = [
    'view_args' => '100',
  ], t('Update preview'));
  $this
    ->assertText(t('Query build time'));
  $this
    ->assertText(t('Query execute time'));
  $this
    ->assertText(t('View render time'));
  $this
    ->assertRaw('<strong>Query</strong>');
  $query_string = <<<SQL
SELECT views_test_data.name AS views_test_data_name
FROM
{views_test_data} views_test_data
WHERE (views_test_data.id = '100')
SQL;
  $this
    ->assertEscaped($query_string);

  // Test that the statistics and query are rendered above the preview.
  $this
    ->assertTrue(strpos($this
    ->getSession()
    ->getPage()
    ->getContent(), 'views-query-info') < strpos($this
    ->getSession()
    ->getPage()
    ->getContent(), 'view-test-preview'), 'Statistics shown above the preview.');

  // Test that statistics and query rendered below the preview.
  $settings
    ->set('ui.show.sql_query.where', 'below')
    ->save();
  $this
    ->drupalPostForm(NULL, $edit = [
    'view_args' => '100',
  ], t('Update preview'));
  $this
    ->assertTrue(strpos($this
    ->getSession()
    ->getPage()
    ->getContent(), 'view-test-preview') < strpos($this
    ->getSession()
    ->getPage()
    ->getContent(), 'views-query-info'), 'Statistics shown below the preview.');

  // Test that the preview title isn't double escaped.
  $this
    ->drupalPostForm("admin/structure/views/nojs/display/test_preview/default/title", $edit = [
    'title' => 'Double & escaped',
  ], t('Apply'));
  $this
    ->drupalPostForm(NULL, [], t('Update preview'));
  $elements = $this
    ->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [
    ':text' => 'Double & escaped',
  ]);
  $this
    ->assertCount(1, $elements);
}