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 = 'classy';
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
->drupalPostForm(NULL, $edit = [], t('Update preview'));
$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',
]);
$this
->assertCount(1, $elements, 'The contextual link to add a new field is shown.');
$this
->drupalPostForm(NULL, $edit = [
'view_args' => '100',
], t('Update preview'));
$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.');
}
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);
$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);
$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);
$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.');
$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.');
$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>');
$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);
$this
->assertTrue(strpos($this
->getSession()
->getPage()
->getContent(), 'views-query-info') < strpos($this
->getSession()
->getPage()
->getContent(), 'view-test-preview'), 'Statistics shown above 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.');
$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);
}
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
->drupalPostForm(NULL, $edit = [], t('Update preview'));
$elements = $this
->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [
':text' => 'Test row count',
]);
$this
->assertCount(1, $elements, 'Views Query Preview Info area altered.');
$this
->assertStringContainsString('views_ui_test/views_ui_test.test', $this
->getDrupalSettings()['ajaxPageState']['libraries'], 'Attached library found.');
$this
->assertRaw('css/views_ui_test.test.css', 'Attached CSS asset found.');
}
public function testPreviewError() {
$this
->drupalGet('admin/structure/views/view/test_preview_error/edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalPostForm(NULL, $edit = [], t('Update preview'));
$this
->assertText('Unable to preview due to validation errors.', 'Preview error text found.');
}
}