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