3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Views;
9 * Tests the mini pager plugin.
12 * @see \Drupal\views\Plugin\views\pager\Mini
14 class MiniPagerTest extends ViewTestBase {
17 * Views used by this test.
21 public static $testViews = ['test_mini_pager'];
28 public static $modules = ['node'];
31 * Nodes used by the test.
37 protected function setUp($import_test_views = TRUE) {
38 parent::setUp($import_test_views);
40 $this->drupalCreateContentType(['type' => 'page']);
41 // Create a bunch of test nodes.
42 for ($i = 0; $i < 20; $i++) {
43 $this->nodes[] = $this->drupalCreateNode();
48 * Tests the rendering of mini pagers.
50 public function testMiniPagerRender() {
51 $this->drupalGet('test_mini_pager');
52 $this->assertText('›› test', 'The next link appears on the first page.');
53 $this->assertText('Page 1', 'The current page info shows the first page.');
54 $this->assertNoText('‹‹ test', 'The previous link does not appear on the first page.');
55 $this->assertText($this->nodes[0]->label());
56 $this->assertText($this->nodes[1]->label());
57 $this->assertText($this->nodes[2]->label());
59 $this->drupalGet('test_mini_pager', ['query' => ['page' => 1]]);
60 $this->assertText('‹‹ test', 'The previous link appears.');
61 $this->assertText('Page 2', 'The current page info shows the second page.');
62 $this->assertText('›› test', 'The next link appears.');
63 $this->assertText($this->nodes[3]->label());
64 $this->assertText($this->nodes[4]->label());
65 $this->assertText($this->nodes[5]->label());
67 $this->drupalGet('test_mini_pager', ['query' => ['page' => 6]]);
68 $this->assertNoText('›› test', 'The next link appears on the last page.');
69 $this->assertText('Page 7', 'The current page info shows the last page.');
70 $this->assertText('‹‹ test', 'The previous link does not appear on the last page.');
71 $this->assertText($this->nodes[18]->label());
72 $this->assertText($this->nodes[19]->label());
74 // Test @total value in result summary
75 $view = Views::getView('test_mini_pager');
76 $view->setDisplay('page_4');
77 $this->executeView($view);
78 $this->assertIdentical($view->get_total_rows, TRUE, 'The query was set to calculate the total number of rows.');
79 $this->assertEqual(count($this->nodes), $view->total_rows, 'The total row count is equal to the number of nodes.');
81 $this->drupalGet('test_mini_pager_total', ['query' => ['page' => 1]]);
82 $this->assertText('of ' . count($this->nodes), 'The first page shows the total row count.');
83 $this->drupalGet('test_mini_pager_total', ['query' => ['page' => 6]]);
84 $this->assertText('of ' . count($this->nodes), 'The last page shows the total row count.');
86 // Test a mini pager with just one item per page.
87 $this->drupalGet('test_mini_pager_one');
88 $this->assertText('››');
89 $this->assertText('Page 1');
90 $this->assertText($this->nodes[0]->label());
92 $this->drupalGet('test_mini_pager_one', ['query' => ['page' => 1]]);
93 $this->assertText('‹‹');
94 $this->assertText('Page 2');
95 $this->assertText('››');
96 $this->assertText($this->nodes[1]->label());
98 $this->drupalGet('test_mini_pager_one', ['query' => ['page' => 19]]);
99 $this->assertNoText('››');
100 $this->assertText('Page 20');
101 $this->assertText('‹‹');
102 $this->assertText($this->nodes[19]->label());
104 $this->drupalGet('test_mini_pager_all');
105 $this->assertNoText('‹‹ test', 'The previous link does not appear on the page.');
106 $this->assertNoText('Page 1', 'The current page info shows the only page.');
107 $this->assertNoText('test ››', 'The next link does not appear on the page.');
108 $result = $this->xpath('//div[contains(@class, "views-row")]');
109 $this->assertEqual(count($result), count($this->nodes), 'All rows appear on the page.');
111 // Remove all items beside 1, so there should be no links shown.
112 for ($i = 0; $i < 19; $i++) {
113 $this->nodes[$i]->delete();
116 $this->drupalGet('test_mini_pager');
117 $this->assertNoText('‹‹ test', 'The previous link does not appear on the page.');
118 $this->assertNoText('Page 1', 'The current page info shows the only page.');
119 $this->assertNoText('‹‹ test', 'The previous link does not appear on the page.');
120 $this->assertText($this->nodes[19]->label());
122 $view = Views::getView('test_mini_pager');
123 $this->executeView($view);
124 $this->assertIdentical($view->get_total_rows, NULL, 'The query was not forced to calculate the total number of results.');
125 $this->assertIdentical($view->total_rows, 1, 'The pager calculated the total number of rows.');
127 // Remove the last node as well and ensure that no "Page 1" is shown.
128 $this->nodes[19]->delete();
129 $this->drupalGet('test_mini_pager');
130 $this->assertNoText('‹‹ test', 'The previous link does not appear on the page.');
131 $this->assertNoText('Page 1', 'The current page info shows the only page.');
132 $this->assertNoText('‹‹ test', 'The previous link does not appear on the page.');