3 namespace Drupal\Tests\views\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
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 JavascriptTestBase {
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());
90 $this->clickLink('Go to page 3');
91 $session_assert->assertWaitOnAjaxRequest();
92 $rows = $page->findAll('css', 'tbody tr');
93 $this->assertCount(1, $rows);
94 $this->assertContains('Node 11 content', $rows[0]->getHtml());
96 // Navigate back to the first page.
97 $this->clickLink('Go to first page');
98 $session_assert->assertWaitOnAjaxRequest();
99 $rows = $page->findAll('css', 'tbody tr');
100 $this->assertCount(5, $rows);
101 $this->assertContains('Node 1 content', $rows[0]->getHtml());
103 // Navigate using the 'next' link.
104 $this->clickLink('Go to next page');
105 $session_assert->assertWaitOnAjaxRequest();
106 $rows = $page->findAll('css', 'tbody tr');
107 $this->assertCount(5, $rows);
108 $this->assertContains('Node 6 content', $rows[0]->getHtml());
110 // Navigate using the 'last' link.
111 $this->clickLink('Go to last page');
112 $session_assert->assertWaitOnAjaxRequest();
113 $rows = $page->findAll('css', 'tbody tr');
114 $this->assertCount(1, $rows);
115 $this->assertContains('Node 11 content', $rows[0]->getHtml());