function PreviewTest::testPreviewUI

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. 8.9.x core/modules/views_ui/tests/src/Functional/PreviewTest.php \Drupal\Tests\views_ui\Functional\PreviewTest::testPreviewUI()
  3. 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 71

Class

PreviewTest
Tests the UI preview functionality.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testPreviewUI() : void {
    $this->drupalGet('admin/structure/views/view/test_preview/edit');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->submitForm($edit = [], 'Update preview');
    $selector = '//div[@class = "views-row"]';
    $this->assertSession()
        ->elementsCount('xpath', $selector, 5);
    // Filter just the first result.
    $this->submitForm($edit = [
        'view_args' => '1',
    ], 'Update preview');
    $this->assertSession()
        ->elementsCount('xpath', $selector, 1);
    // Filter for no results.
    $this->submitForm($edit = [
        'view_args' => '100',
    ], 'Update preview');
    $this->assertSession()
        ->elementNotExists('xpath', $selector);
    // Test that area text and exposed filters are present and rendered.
    $this->assertSession()
        ->fieldExists('id');
    $this->assertSession()
        ->pageTextContains('Test header text');
    $this->assertSession()
        ->pageTextContains('Test footer text');
    $this->assertSession()
        ->pageTextContains('Test empty text');
    // Test feed preview.
    $view = [];
    $view['label'] = $this->randomMachineName(16);
    $view['id'] = $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->drupalGet('admin/structure/views/add');
    $this->submitForm($view, 'Save and edit');
    $this->clickLink('Feed');
    $this->submitForm([], 'Update preview');
    $this->assertSession()
        ->elementTextContains('xpath', '//div[@id="views-live-preview"]/pre', '<title>' . $view['page[title]'] . '</title>');
    // 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->submitForm($edit = [
        'view_args' => '100',
    ], 'Update preview');
    $this->assertSession()
        ->pageTextContains('Query build time');
    $this->assertSession()
        ->pageTextContains('Query execute time');
    $this->assertSession()
        ->pageTextContains('View render time');
    $this->assertSession()
        ->responseNotContains('<strong>Query</strong>');
    // Statistics and query.
    $settings->set('ui.show.sql_query.enabled', TRUE)
        ->save();
    $this->submitForm($edit = [
        'view_args' => '100',
    ], 'Update preview');
    $this->assertSession()
        ->pageTextContains('Query build time');
    $this->assertSession()
        ->pageTextContains('Query execute time');
    $this->assertSession()
        ->pageTextContains('View render time');
    $this->assertSession()
        ->responseContains('<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->assertSession()
        ->assertEscaped($query_string);
    // Test that the statistics and query are rendered above the preview.
    $this->assertLessThan(strpos($this->getSession()
        ->getPage()
        ->getContent(), 'js-view-dom-id'), strpos($this->getSession()
        ->getPage()
        ->getContent(), 'views-query-info'));
    // Test that statistics and query rendered below the preview.
    $settings->set('ui.show.sql_query.where', 'below')
        ->save();
    $this->submitForm($edit = [
        'view_args' => '100',
    ], 'Update preview');
    $this->assertLessThan(strpos($this->getSession()
        ->getPage()
        ->getContent(), 'views-query-info'), strpos($this->getSession()
        ->getPage()
        ->getContent(), 'js-view-dom-id'), 'Statistics shown below the preview.');
    // Test that the preview title isn't double escaped.
    $this->drupalGet("admin/structure/views/nojs/display/test_preview/default/title");
    $this->submitForm($edit = [
        'title' => 'Double & escaped',
    ], 'Apply');
    $this->submitForm([], 'Update preview');
    $this->assertSession()
        ->elementsCount('xpath', '//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()="Double & escaped"]', 1);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.