View source
<?php
namespace Drupal\Tests\views_ui\FunctionalJavascript;
use Behat\Mink\Element\NodeElement;
use Drupal\Core\Database\Database;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\views\Tests\ViewTestData;
class PreviewTest extends WebDriverTestBase {
public static $testViews = [
'test_preview',
'test_pager_full_ajax',
'test_mini_pager_ajax',
'test_click_sort_ajax',
];
protected static $modules = [
'node',
'views',
'views_ui',
'views_test_config',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
ViewTestData::createTestViews(self::class, [
'views_test_config',
]);
$this
->enableViewsTestModule();
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
'administer views',
'administer nodes',
'access content overview',
]);
\Drupal::configFactory()
->getEditable('views.settings')
->set('ui.always_live_preview', FALSE)
->save();
$this
->drupalLogin($admin_user);
}
protected function enableViewsTestModule() {
\Drupal::state()
->set('views_test_data_schema', $this
->schemaDefinition());
\Drupal::state()
->set('views_test_data_views_data', $this
->viewsData());
\Drupal::service('module_installer')
->install([
'views_test_data',
]);
$this
->resetAll();
$this
->rebuildContainer();
$this->container
->get('module_handler')
->reload();
$data_set = $this
->dataSet();
$query = Database::getConnection()
->insert('views_test_data')
->fields(array_keys($data_set[0]));
foreach ($data_set as $record) {
$query
->values($record);
}
$query
->execute();
}
protected function schemaDefinition() {
return ViewTestData::schemaDefinition();
}
protected function viewsData() {
return ViewTestData::viewsData();
}
protected function dataSet() {
return ViewTestData::dataSet();
}
public function testTaxonomyAJAX() {
\Drupal::service('module_installer')
->install([
'taxonomy',
]);
$this
->getPreviewAJAX('taxonomy_term', 'page_1', 0);
}
public function testPreviewWithPagersUI() {
$this
->drupalCreateContentType([
'type' => 'page',
]);
for ($i = 0; $i < 11; $i++) {
$this
->drupalCreateNode();
}
$this
->getPreviewAJAX('test_pager_full_ajax', 'default', 5);
$elements = $this
->xpath('//ul[contains(@class, :class)]/li', [
':class' => 'pager__items',
]);
$this
->assertNotEmpty($elements, 'Full pager found.');
$this
->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.');
$this
->assertNotEmpty($elements[0]
->find('css', 'a'), 'Element for current page has link.');
$this
->assertClass($elements[1], 'pager__item', 'Element for page 2 has .pager__item class.');
$this
->assertNotEmpty($elements[1]
->find('css', 'a'), 'Link to page 2 found.');
$this
->assertClass($elements[2], 'pager__item', 'Element for page 3 has .pager__item class.');
$this
->assertNotEmpty($elements[2]
->find('css', 'a'), 'Link to page 3 found.');
$this
->assertClass($elements[3], 'pager__item--next', 'Element for next page has .pager__item--next class.');
$this
->assertNotEmpty($elements[3]
->find('css', 'a'), 'Link to next page found.');
$this
->assertClass($elements[4], 'pager__item--last', 'Element for last page has .pager__item--last class.');
$this
->assertNotEmpty($elements[4]
->find('css', 'a'), 'Link to last page found.');
$element = $this
->assertSession()
->elementExists('xpath', '//li[contains(@class, "pager__item--next")]/a');
$this
->clickPreviewLinkAJAX($element, 5);
$elements = $this
->xpath('//ul[contains(@class, :class)]/li', [
':class' => 'pager__items',
]);
$this
->assertNotEmpty($elements, 'Full pager found.');
$this
->assertClass($elements[0], 'pager__item--first', 'Element for first page has .pager__item--first class.');
$this
->assertNotEmpty($elements[0]
->find('css', 'a'), 'Link to first page found.');
$this
->assertClass($elements[1], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.');
$this
->assertNotEmpty($elements[1]
->find('css', 'a'), 'Link to previous page found.');
$this
->assertClass($elements[2], 'pager__item', 'Element for page 1 has .pager__item class.');
$this
->assertNotEmpty($elements[2]
->find('css', 'a'), 'Link to page 1 found.');
$this
->assertClass($elements[3], 'is-active', 'Element for current page has .is-active class.');
$this
->assertNotEmpty($elements[3]
->find('css', 'a'), 'Element for current page has link.');
$this
->assertClass($elements[4], 'pager__item', 'Element for page 3 has .pager__item class.');
$this
->assertNotEmpty($elements[4]
->find('css', 'a'), 'Link to page 3 found.');
$this
->assertClass($elements[5], 'pager__item--next', 'Element for next page has .pager__item--next class.');
$this
->assertNotEmpty($elements[5]
->find('css', 'a'), 'Link to next page found.');
$this
->assertClass($elements[6], 'pager__item--last', 'Element for last page has .pager__item--last class.');
$this
->assertNotEmpty($elements[6]
->find('css', 'a'), 'Link to last page found.');
$this
->getPreviewAJAX('test_mini_pager_ajax', 'default', 3);
$elements = $this
->xpath('//ul[contains(@class, :class)]/li', [
':class' => 'pager__items',
]);
$this
->assertNotEmpty($elements, 'Mini pager found.');
$this
->assertEquals('Page 1', trim($elements[0]
->getHtml()), 'Element for current page is not a link.');
$next_page_link = $elements[1]
->find('css', 'a');
$this
->assertNotEmpty($next_page_link, 'Link to next page found.');
$this
->assertEquals('Go to next page', $next_page_link
->getAttribute('title'));
$this
->clickPreviewLinkAJAX($next_page_link, 3);
$elements = $this
->xpath('//ul[contains(@class, :class)]/li', [
':class' => 'pager__items',
]);
$this
->assertNotEmpty($elements, 'Mini pager found.');
$previous_page_link = $elements[0]
->find('css', 'a');
$this
->assertNotEmpty($previous_page_link, 'Link to previous page found.');
$this
->assertEquals('Go to previous page', $previous_page_link
->getAttribute('title'));
$this
->assertEquals('Page 2', trim($elements[1]
->getHtml()), 'Element for current page is not a link.');
$next_page_link = $elements[2]
->find('css', 'a');
$this
->assertNotEmpty($next_page_link, 'Link to next page found.');
$this
->assertEquals('Go to next page', $next_page_link
->getAttribute('title'));
}
public function testPreviewSortLink() {
$this
->getPreviewAJAX('test_click_sort_ajax', 'page_1', 0);
$element = $this
->assertSession()
->elementExists('xpath', '//th[contains(@class, "views-field views-field-name")]/a');
$this
->assertSession()
->linkByHrefExists('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=desc', 0, 'The output URL is as expected.');
$element
->click();
$sort_link = $this
->assertSession()
->waitForElement('xpath', '//th[contains(@class, \'views-field views-field-name is-active\')]/a');
$this
->assertNotEmpty($sort_link);
$this
->assertSession()
->linkByHrefExists('preview/page_1?_wrapper_format=drupal_ajax&order=name&sort=asc', 0, 'The output URL is as expected.');
}
protected function getPreviewAJAX($view_name, $panel_id, $row_count) {
$this
->drupalGet('admin/structure/views/view/' . $view_name . '/edit/' . $panel_id);
$this
->getSession()
->getPage()
->pressButton('Update preview');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertPreviewAJAX($row_count);
}
protected function clickPreviewLinkAJAX(NodeElement $element, $row_count) {
$element
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertPreviewAJAX($row_count);
}
protected function assertPreviewAJAX(int $row_count) : void {
$elements = $this
->getSession()
->getPage()
->findAll('css', '#views-live-preview .views-row');
$this
->assertCount($row_count, $elements, 'Expected items found on page.');
}
protected function assertClass(NodeElement $element, string $class, string $message = '') : void {
if (!isset($message)) {
$message = "Class .{$class} found.";
}
$this
->assertStringContainsString($class, $element
->getAttribute('class'), $message);
}
}