View source
<?php
namespace Drupal\Tests\views_ui\Functional;
class PreviewTest extends UITestBase {
public static $testViews = [
'test_preview',
'test_preview_error',
'test_pager_full',
'test_mini_pager',
'test_click_sort',
];
protected $defaultTheme = 'stark';
public function testPreviewContextual() {
\Drupal::service('module_installer')
->install([
'contextual',
]);
$this
->resetAll();
$this
->drupalGet('admin/structure/views/view/test_preview/edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->submitForm($edit = [], 'Update preview');
$selector = $this
->assertSession()
->buildXPathQuery('//div[@id="views-live-preview"]//ul[contains(@class, :ul-class)]/li/a[contains(@href, :href)]', [
':ul-class' => 'contextual-links',
':href' => '/admin/structure/views/nojs/add-handler/test_preview/default/filter',
]);
$this
->assertSession()
->elementsCount('xpath', $selector, 1);
$this
->submitForm([
'view_args' => '100',
], 'Update preview');
$this
->assertSession()
->fieldExists('id');
$this
->assertSession()
->pageTextContains('Test header text');
$this
->assertSession()
->pageTextContains('Test footer text');
$this
->assertSession()
->pageTextContains('Test empty text');
$this
->submitForm([
'view_args' => '0',
], 'Update preview');
$this
->assertSession()
->fieldExists('id');
$this
->assertSession()
->pageTextContains('Test header text');
$this
->assertSession()
->pageTextContains('Test footer text');
$this
->assertSession()
->pageTextContains('Test empty text');
}
public function testPreviewUI() {
$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);
$this
->submitForm($edit = [
'view_args' => '1',
], 'Update preview');
$this
->assertSession()
->elementsCount('xpath', $selector, 1);
$this
->submitForm($edit = [
'view_args' => '100',
], 'Update preview');
$this
->assertSession()
->elementNotExists('xpath', $selector);
$this
->assertSession()
->fieldExists('id');
$this
->assertSession()
->pageTextContains('Test header text');
$this
->assertSession()
->pageTextContains('Test footer text');
$this
->assertSession()
->pageTextContains('Test empty text');
$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
->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>');
$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>');
$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);
$this
->assertLessThan(strpos($this
->getSession()
->getPage()
->getContent(), 'js-view-dom-id'), strpos($this
->getSession()
->getPage()
->getContent(), 'views-query-info'));
$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.');
$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);
}
public function testPreviewAdditionalInfo() {
\Drupal::service('module_installer')
->install([
'views_ui_test',
]);
$this
->resetAll();
$this
->drupalGet('admin/structure/views/view/test_preview/edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->submitForm($edit = [], 'Update preview');
$this
->assertSession()
->elementsCount('xpath', '//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()="Test row count"]', 1);
$this
->assertStringContainsString('views_ui_test/views_ui_test.test', $this
->getDrupalSettings()['ajaxPageState']['libraries'], 'Attached library found.');
$this
->assertSession()
->responseContains('css/views_ui_test.test.css');
}
public function testPreviewError() {
$this
->drupalGet('admin/structure/views/view/test_preview_error/edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->submitForm($edit = [], 'Update preview');
$this
->assertSession()
->pageTextContains('Unable to preview due to validation errors.');
}
}