850c5536f5f222164eb98c6f95299ea071e29a52
[yaffs-website] / web / core / modules / views / tests / src / FunctionalJavascript / PaginationAJAXTest.php
1 <?php
2
3 namespace Drupal\Tests\views\FunctionalJavascript;
4
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
6 use Drupal\simpletest\ContentTypeCreationTrait;
7 use Drupal\simpletest\NodeCreationTrait;
8 use Drupal\views\Tests\ViewTestData;
9
10 /**
11  * Tests the click sorting AJAX functionality of Views exposed forms.
12  *
13  * @group views
14  */
15 class PaginationAJAXTest extends JavascriptTestBase {
16
17   use ContentTypeCreationTrait;
18   use NodeCreationTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['node', 'views', 'views_test_config'];
24
25   /**
26    * @var array
27    * Test Views to enable.
28    */
29   public static $testViews = ['test_content_ajax'];
30
31   protected $user;
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38
39     ViewTestData::createTestViews(self::class, ['views_test_config']);
40
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]);
45     }
46
47     // Create a user privileged enough to view content.
48     $user = $this->drupalCreateUser([
49       'administer site configuration',
50       'access content',
51       'access content overview',
52     ]);
53     $this->drupalLogin($user);
54   }
55
56   /**
57    * Tests if pagination via AJAX works for the "Content" View.
58    */
59   public function testBasicPagination() {
60     // Visit the content page.
61     $this->drupalGet('test-content-ajax');
62
63     $session_assert = $this->assertSession();
64
65     $page = $this->getSession()->getPage();
66
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();
71
72     // Change 'Updated' sorting from descending to ascending.
73     $page->clickLink('Updated');
74     $session_assert->assertWaitOnAjaxRequest();
75
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
78     // links are used.
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());
83
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
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());
95
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());
102
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());
109
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());
116   }
117
118 }