3 namespace Drupal\Tests\views\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6 use Drupal\simpletest\ContentTypeCreationTrait;
7 use Drupal\simpletest\NodeCreationTrait;
8 use Drupal\views\Tests\ViewTestData;
11 * Tests the click sorting AJAX functionality of Views exposed forms.
15 class PaginationAJAXTest extends WebDriverTestBase {
17 use ContentTypeCreationTrait;
18 use NodeCreationTrait;
23 public static $modules = ['node', 'views', 'views_test_config'];
27 * Test Views to enable.
29 public static $testViews = ['test_content_ajax'];
36 protected function setUp() {
39 ViewTestData::createTestViews(self::class, ['views_test_config']);
41 // Create a Content type and eleven test nodes.
42 $this->createContentType(['type' => 'page']);
43 for ($i = 1; $i <= 11; $i++) {
44 $this->createNode(['title' => 'Node ' . $i . ' content', 'changed' => $i * 1000]);
47 // Create a user privileged enough to view content.
48 $user = $this->drupalCreateUser([
49 'administer site configuration',
51 'access content overview',
53 $this->drupalLogin($user);
57 * Tests if pagination via AJAX works for the "Content" View.
59 public function testBasicPagination() {
60 // Visit the content page.
61 $this->drupalGet('test-content-ajax');
63 $session_assert = $this->assertSession();
65 $page = $this->getSession()->getPage();
67 // Set the number of items displayed per page to 5 using the exposed pager.
68 $page->selectFieldOption('edit-items-per-page', 5);
69 $page->pressButton('Filter');
70 $session_assert->assertWaitOnAjaxRequest();
72 // Change 'Updated' sorting from descending to ascending.
73 $page->clickLink('Updated');
74 $session_assert->assertWaitOnAjaxRequest();
76 // Use the pager by clicking on the links and test if we see the expected
77 // number of rows on each page. For easy targeting the titles of the pager
79 /** @var \Behat\Mink\Element\NodeElement[] $rows */
80 $rows = $page->findAll('css', 'tbody tr');
81 $this->assertCount(5, $rows);
82 $this->assertContains('Node 1 content', $rows[0]->getHtml());
84 $this->clickLink('Go to page 2');
85 $session_assert->assertWaitOnAjaxRequest();
86 $rows = $page->findAll('css', 'tbody tr');
87 $this->assertCount(5, $rows);
88 $this->assertContains('Node 6 content', $rows[0]->getHtml());
89 $link = $page->findLink('Go to page 3');
90 // Test that no unwanted parameters are added to the URL.
91 $this->assertEquals('?status=All&type=All&langcode=All&items_per_page=5&order=changed&sort=asc&title=&page=2', $link->getAttribute('href'));
92 $this->assertNoDuplicateAssetsOnPage();
94 $this->clickLink('Go to page 3');
95 $session_assert->assertWaitOnAjaxRequest();
96 $rows = $page->findAll('css', 'tbody tr');
97 $this->assertCount(1, $rows);
98 $this->assertContains('Node 11 content', $rows[0]->getHtml());
100 // Navigate back to the first page.
101 $this->clickLink('Go to first page');
102 $session_assert->assertWaitOnAjaxRequest();
103 $rows = $page->findAll('css', 'tbody tr');
104 $this->assertCount(5, $rows);
105 $this->assertContains('Node 1 content', $rows[0]->getHtml());
107 // Navigate using the 'next' link.
108 $this->clickLink('Go to next page');
109 $session_assert->assertWaitOnAjaxRequest();
110 $rows = $page->findAll('css', 'tbody tr');
111 $this->assertCount(5, $rows);
112 $this->assertContains('Node 6 content', $rows[0]->getHtml());
114 // Navigate using the 'last' link.
115 $this->clickLink('Go to last page');
116 $session_assert->assertWaitOnAjaxRequest();
117 $rows = $page->findAll('css', 'tbody tr');
118 $this->assertCount(1, $rows);
119 $this->assertContains('Node 11 content', $rows[0]->getHtml());
123 * Assert that assets are not loaded twice on a page.
125 protected function assertNoDuplicateAssetsOnPage() {
126 /** @var \Behat\Mink\Element\NodeElement[] $scripts */
127 $scripts = $this->getSession()->getPage()->findAll('xpath', '//script');
129 foreach ($scripts as $script) {
130 $this->assertFalse(in_array($script->getAttribute('src'), $script_src));
131 $script_src[] = $script->getAttribute('src');