TYPO3 CMS  TYPO3_8-7
PaginateControllerTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
17 
21 class PaginateControllerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
22 {
26  protected $query;
27 
31  protected $controller;
32 
36  protected $querySettings;
37 
42 
46  protected $backend;
47 
51  protected $dataMapper;
52 
56  protected function setUp()
57  {
58  $this->query = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Query::class, ['dummy'], ['someType']);
59  $this->querySettings = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
60  $this->query->_set('querySettings', $this->querySettings);
61  $this->persistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
62  $this->backend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
63  $this->query->_set('persistenceManager', $this->persistenceManager);
64  $this->dataMapper = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
65  $this->query->_set('dataMapper', $this->dataMapper);
66  $this->controller = $this->getAccessibleMock(
67  \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
68  ['dummy'],
69  [],
70  '',
71  false
72  );
73  $this->controller->_set('view', $this->createMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class));
74  }
75 
80  {
81  $this->controller->_set('maximumNumberOfLinks', 8);
82  $this->controller->_set('numberOfPages', 100);
83  $this->controller->_set('currentPage', 50);
84  $this->controller->_call('calculateDisplayRange');
85  $this->assertSame(46, $this->controller->_get('displayRangeStart'));
86  $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
87  }
88 
93  {
94  $this->controller->_set('maximumNumberOfLinks', 7);
95  $this->controller->_set('numberOfPages', 100);
96  $this->controller->_set('currentPage', 50);
97  $this->controller->_call('calculateDisplayRange');
98  $this->assertSame(47, $this->controller->_get('displayRangeStart'));
99  $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
100  }
101 
106  {
107  $this->controller->_set('maximumNumberOfLinks', 8);
108  $this->controller->_set('numberOfPages', 100);
109  $this->controller->_set('currentPage', 1);
110  $this->controller->_call('calculateDisplayRange');
111  $this->assertSame(1, $this->controller->_get('displayRangeStart'));
112  $this->assertSame(8, $this->controller->_get('displayRangeEnd'));
113  }
114 
119  {
120  $this->controller->_set('maximumNumberOfLinks', 7);
121  $this->controller->_set('numberOfPages', 100);
122  $this->controller->_set('currentPage', 1);
123  $this->controller->_call('calculateDisplayRange');
124  $this->assertSame(1, $this->controller->_get('displayRangeStart'));
125  $this->assertSame(7, $this->controller->_get('displayRangeEnd'));
126  }
127 
132  {
133  $this->controller->_set('maximumNumberOfLinks', 8);
134  $this->controller->_set('numberOfPages', 100);
135  $this->controller->_set('currentPage', 100);
136  $this->controller->_call('calculateDisplayRange');
137  $this->assertSame(93, $this->controller->_get('displayRangeStart'));
138  $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
139  }
140 
145  {
146  $this->controller->_set('maximumNumberOfLinks', 7);
147  $this->controller->_set('numberOfPages', 100);
148  $this->controller->_set('currentPage', 100);
149  $this->controller->_call('calculateDisplayRange');
150  $this->assertSame(94, $this->controller->_get('displayRangeStart'));
151  $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
152  }
153 
158  {
159  $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
160  $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
161  $mockQueryResult->expects($this->any())->method('getQuery')->will($this->returnValue($mockQuery));
162  $this->controller->_set('objects', $mockQueryResult);
163  $this->controller->_set('widgetConfiguration', ['as' => 'paginatedObjects']);
164  $this->controller->indexAction();
165  $this->assertSame($mockQueryResult, $this->controller->_get('objects'));
166  }
167 
171  public function acceptArrayAsObjects()
172  {
173  $objects = [];
174  $this->controller->_set('objects', $objects);
175  $this->controller->_set('widgetConfiguration', ['as' => 'paginatedObjects']);
176  $this->controller->indexAction();
177  $this->assertSame($objects, $this->controller->_get('objects'));
178  }
179 
184  {
185  $objects = new ObjectStorage();
186  $this->controller->_set('objects', $objects);
187  $this->controller->_set('widgetConfiguration', ['as' => 'paginatedObjects']);
188  $this->controller->indexAction();
189  $this->assertSame($objects, $this->controller->_get('objects'));
190  }
191 
196  {
197  $objects = new ObjectStorage();
198  for ($i = 0; $i <= 25; $i++) {
199  $item = new \stdClass;
200  $objects->attach($item);
201  }
202  $this->controller->_set('objects', $objects);
203  $expectedPortion = [];
204  for ($j = 0; $j <= 9; $j++) {
205  $expectedPortion[] = $objects->toArray()[$j];
206  }
207  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
208  }
209 
214  {
215  $this->controller->_set('currentPage', 2);
216  $objects = new ObjectStorage();
217  for ($i = 0; $i <= 55; $i++) {
218  $item = new \stdClass;
219  $objects->attach($item);
220  }
221  $this->controller->_set('objects', $objects);
222  $expectedPortion = [];
223  for ($j = 10; $j <= 19; $j++) {
224  $expectedPortion[] = $objects->toArray()[$j];
225  }
226  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
227  }
228 
233  {
234  $this->controller->_set('currentPage', 3);
235  $objects = new ObjectStorage();
236  for ($i = 0; $i <= 25; $i++) {
237  $item = new \stdClass;
238  $objects->attach($item);
239  }
240  $this->controller->_set('objects', $objects);
241  $expectedPortion = [];
242  for ($j = 20; $j <= 25; $j++) {
243  $expectedPortion[] = $objects->toArray()[$j];
244  }
245  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 20));
246  }
247 
252  {
253  $objects = [];
254  for ($i = 0; $i <= 25; $i++) {
255  $item = new \stdClass;
256  $objects[] = $item;
257  }
258  $this->controller->_set('objects', $objects);
259  $expectedPortion = [];
260  for ($j = 0; $j <= 9; $j++) {
261  $expectedPortion = array_slice($objects, 0, 10);
262  }
263  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
264  }
265 
270  {
271  $this->controller->_set('currentPage', 2);
272  $objects = [];
273  for ($i = 0; $i <= 55; $i++) {
274  $item = new \stdClass;
275  $objects[] = $item;
276  }
277  $this->controller->_set('objects', $objects);
278  $expectedPortion = [];
279  for ($j = 10; $j <= 19; $j++) {
280  $expectedPortion = array_slice($objects, 10, 10);
281  }
282  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
283  }
284 }