d94840746b39520b0168dc4da4406a86e4127df2
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / pager / PagerPluginBaseTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\views\Unit\Plugin\pager\PagerPluginBaseTest.
6  */
7
8 namespace Drupal\Tests\views\Unit\Plugin\pager;
9
10 use Drupal\Tests\UnitTestCase;
11 use Drupal\Core\Database\StatementInterface;
12 use Drupal\Core\Database\Query\Select;
13
14 /**
15  * @coversDefaultClass \Drupal\views\Plugin\views\pager\PagerPluginBase
16  * @group views
17  */
18 class PagerPluginBaseTest extends UnitTestCase {
19
20   /**
21    * The mock pager plugin instance.
22    *
23    * @var \Drupal\views\Plugin\views\pager\PagerPluginBase|\PHPUnit_Framework_MockObject_MockObject
24    */
25   protected $pager;
26
27   protected function setUp() {
28     $this->pager = $this->getMockBuilder('Drupal\views\Plugin\views\pager\PagerPluginBase')
29       ->disableOriginalConstructor()
30       ->getMockForAbstractClass();
31
32     $view = $this->getMockBuilder('Drupal\views\ViewExecutable')
33       ->disableOriginalConstructor()
34       ->getMock();
35     $display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
36       ->disableOriginalConstructor()
37       ->getMock();
38
39     $options = [
40       'items_per_page' => 5,
41       'offset' => 1,
42     ];
43
44     $this->pager->init($view, $display, $options);
45
46     $this->pager->current_page = 1;
47   }
48
49   /**
50    * Tests the getItemsPerPage() method.
51    *
52    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getItemsPerPage()
53    */
54   public function testGetItemsPerPage() {
55     $this->assertEquals(5, $this->pager->getItemsPerPage());
56   }
57
58   /**
59    * Tests the setItemsPerPage() method.
60    *
61    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setItemsPerPage()
62    */
63   public function testSetItemsPerPage() {
64     $this->pager->setItemsPerPage(6);
65     $this->assertEquals(6, $this->pager->getItemsPerPage());
66   }
67
68   /**
69    * Tests the getOffset() method.
70    *
71    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getOffset()
72    */
73   public function testGetOffset() {
74     $this->assertEquals(1, $this->pager->getOffset());
75   }
76
77   /**
78    * Tests the setOffset() method.
79    *
80    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setOffset()
81    */
82   public function testSetOffset() {
83     $this->pager->setOffset(2);
84     $this->assertEquals(2, $this->pager->getOffset());
85   }
86
87   /**
88    * Tests the getCurrentPage() method.
89    *
90    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getCurrentPage()
91    */
92   public function testGetCurrentPage() {
93     $this->assertEquals(1, $this->pager->getCurrentPage());
94   }
95
96   /**
97    * Tests the setCurrentPage() method.
98    *
99    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setCurrentPage()
100    */
101   public function testSetCurrentPage() {
102     $this->pager->setCurrentPage(2);
103     $this->assertEquals(2, $this->pager->getCurrentPage());
104
105     // A non numeric number or number below 0 should return 0.
106     $this->pager->setCurrentPage('two');
107     $this->assertEquals(0, $this->pager->getCurrentPage());
108
109     $this->pager->setCurrentPage(-2);
110     $this->assertEquals(0, $this->pager->getCurrentPage());
111   }
112
113   /**
114    * Tests the getTotalItems() method.
115    *
116    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getTotalItems()
117    */
118   public function testGetTotalItems() {
119     // Should return 0 by default.
120     $this->assertEquals(0, $this->pager->getTotalItems());
121
122     $this->pager->total_items = 10;
123     $this->assertEquals(10, $this->pager->getTotalItems());
124   }
125
126   /**
127    * Tests the getPagerId() method.
128    *
129    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getPagerId()
130    */
131   public function testGetPagerId() {
132     // Should return 0 if 'id' is not set.
133     $this->assertEquals(0, $this->pager->getPagerId());
134
135     $this->pager->options['id'] = 1;
136
137     $this->assertEquals(1, $this->pager->getPagerId());
138   }
139
140   /**
141    * Tests the usePager() method.
142    *
143    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::usePager()
144    */
145   public function testUsePager() {
146     $this->assertTrue($this->pager->usePager());
147   }
148
149   /**
150    * Tests the useCountQuery() method.
151    *
152    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::useCountQuery()
153    */
154   public function testUseCountQuery() {
155     $this->assertTrue($this->pager->useCountQuery());
156   }
157
158   /**
159    * Tests the usesExposed() method.
160    *
161    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::usedExposed()
162    */
163   public function testUsesExposed() {
164     $this->assertFalse($this->pager->usesExposed());
165   }
166
167   /**
168    * Tests the hasMoreRecords() method.
169    *
170    * @dataProvider providerTestHasMoreRecords
171    *
172    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::hasMoreRecords()
173    */
174   public function testHasMoreRecords($items_per_page, $total_items, $current_page, $has_more_records) {
175     $this->pager->setItemsPerPage($items_per_page);
176     $this->pager->total_items = $total_items;
177     $this->pager->setCurrentPage($current_page);
178     $this->assertEquals($has_more_records, $this->pager->hasMoreRecords());
179   }
180
181   /**
182    * Provides test data for the hasMoreRecord method test.
183    *
184    * @see self::testHasMoreRecords
185    */
186   public function providerTestHasMoreRecords() {
187     return [
188       // No items per page, so there can't be more available records.
189       [0, 0, 0, FALSE],
190       [0, 10, 0, FALSE],
191       // The amount of total items equals the items per page, so there is no
192       // next page available.
193       [5, 5, 0, FALSE],
194       // There is one more item, and we are at the first page.
195       [5, 6, 0, TRUE],
196       // Now we are on the second page, which has just a single one left.
197       [5, 6, 1, FALSE],
198       // Increase the total items, so we have some available on the third page.
199       [5, 12, 1, TRUE],
200     ];
201   }
202
203   /**
204    * Tests the executeCountQuery method without a set offset.
205    *
206    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::executeCountQuery()
207    */
208   public function testExecuteCountQueryWithoutOffset() {
209     $statement = $this->getMock('\Drupal\Tests\views\Unit\Plugin\pager\TestStatementInterface');
210
211     $statement->expects($this->once())
212       ->method('fetchField')
213       ->will($this->returnValue(3));
214
215     $query = $this->getMockBuilder('\Drupal\Core\Database\Query\Select')
216       ->disableOriginalConstructor()
217       ->getMock();
218
219     $query->expects($this->once())
220       ->method('execute')
221       ->will($this->returnValue($statement));
222
223     $this->pager->setOffset(0);
224     $this->assertEquals(3, $this->pager->executeCountQuery($query));
225   }
226
227   /**
228    * Tests the executeCountQuery method with a set offset.
229    *
230    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::executeCountQuery()
231    */
232   public function testExecuteCountQueryWithOffset() {
233     $statement = $this->getMock('\Drupal\Tests\views\Unit\Plugin\pager\TestStatementInterface');
234
235     $statement->expects($this->once())
236       ->method('fetchField')
237       ->will($this->returnValue(3));
238
239     $query = $this->getMockBuilder('\Drupal\Core\Database\Query\Select')
240       ->disableOriginalConstructor()
241       ->getMock();
242
243     $query->expects($this->once())
244       ->method('execute')
245       ->will($this->returnValue($statement));
246
247     $this->pager->setOffset(2);
248     $this->assertEquals(1, $this->pager->executeCountQuery($query));
249   }
250
251   /**
252    * Tests the executeCountQuery method with an offset larger than result count.
253    *
254    * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::executeCountQuery()
255    */
256   public function testExecuteCountQueryWithOffsetLargerThanResult() {
257     $statement = $this->getMock(TestStatementInterface::class);
258
259     $statement->expects($this->once())
260       ->method('fetchField')
261       ->will($this->returnValue(2));
262
263     $query = $this->getMockBuilder(Select::class)
264       ->disableOriginalConstructor()
265       ->getMock();
266
267     $query->expects($this->once())
268       ->method('execute')
269       ->will($this->returnValue($statement));
270
271     $this->pager->setOffset(3);
272     $this->assertEquals(0, $this->pager->executeCountQuery($query));
273   }
274
275 }
276
277 /**
278  * As StatementInterface extends \Traversable, which though always needs
279  * an additional interface. The Statement class itself can't be mocked because
280  * of its __wakeup function.
281  */
282 interface TestStatementInterface extends StatementInterface, \Iterator {}