af2b26ddda656b5bd8fdf2dca36ab0ac39452fb4
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Widget / Controller / PaginateControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller;
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 */
16 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
17
18 /**
19 * Test case
20 */
21 class PaginateControllerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
22 {
23 /**
24 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Query
25 */
26 protected $query;
27
28 /**
29 * @var \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
30 */
31 protected $controller;
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
35 */
36 protected $querySettings;
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
40 */
41 protected $persistenceManager;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
45 */
46 protected $backend;
47
48 /**
49 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
50 */
51 protected $dataMapper;
52
53 /**
54 * Sets up this test case
55 *
56 * @return void
57 */
58 protected function setUp()
59 {
60 $this->query = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Query::class, ['dummy'], ['someType']);
61 $this->querySettings = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
62 $this->query->_set('querySettings', $this->querySettings);
63 $this->persistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
64 $this->backend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
65 $this->query->_set('persistenceManager', $this->persistenceManager);
66 $this->dataMapper = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
67 $this->query->_set('dataMapper', $this->dataMapper);
68 $this->controller = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
69 ['dummy'], [], '', false);
70 $this->controller->_set('view', $this->createMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class));
71 }
72
73 /**
74 * @test
75 */
76 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinks()
77 {
78 $this->controller->_set('maximumNumberOfLinks', 8);
79 $this->controller->_set('numberOfPages', 100);
80 $this->controller->_set('currentPage', 50);
81 $this->controller->_call('calculateDisplayRange');
82 $this->assertSame(46, $this->controller->_get('displayRangeStart'));
83 $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
84 }
85
86 /**
87 * @test
88 */
89 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinks()
90 {
91 $this->controller->_set('maximumNumberOfLinks', 7);
92 $this->controller->_set('numberOfPages', 100);
93 $this->controller->_set('currentPage', 50);
94 $this->controller->_call('calculateDisplayRange');
95 $this->assertSame(47, $this->controller->_get('displayRangeStart'));
96 $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
97 }
98
99 /**
100 * @test
101 */
102 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnFirstPage()
103 {
104 $this->controller->_set('maximumNumberOfLinks', 8);
105 $this->controller->_set('numberOfPages', 100);
106 $this->controller->_set('currentPage', 1);
107 $this->controller->_call('calculateDisplayRange');
108 $this->assertSame(1, $this->controller->_get('displayRangeStart'));
109 $this->assertSame(8, $this->controller->_get('displayRangeEnd'));
110 }
111
112 /**
113 * @test
114 */
115 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnFirstPage()
116 {
117 $this->controller->_set('maximumNumberOfLinks', 7);
118 $this->controller->_set('numberOfPages', 100);
119 $this->controller->_set('currentPage', 1);
120 $this->controller->_call('calculateDisplayRange');
121 $this->assertSame(1, $this->controller->_get('displayRangeStart'));
122 $this->assertSame(7, $this->controller->_get('displayRangeEnd'));
123 }
124
125 /**
126 * @test
127 */
128 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnLastPage()
129 {
130 $this->controller->_set('maximumNumberOfLinks', 8);
131 $this->controller->_set('numberOfPages', 100);
132 $this->controller->_set('currentPage', 100);
133 $this->controller->_call('calculateDisplayRange');
134 $this->assertSame(93, $this->controller->_get('displayRangeStart'));
135 $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
136 }
137
138 /**
139 * @test
140 */
141 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnLastPage()
142 {
143 $this->controller->_set('maximumNumberOfLinks', 7);
144 $this->controller->_set('numberOfPages', 100);
145 $this->controller->_set('currentPage', 100);
146 $this->controller->_call('calculateDisplayRange');
147 $this->assertSame(94, $this->controller->_get('displayRangeStart'));
148 $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
149 }
150
151 /**
152 * @test
153 */
154 public function acceptQueryResultInterfaceAsObjects()
155 {
156 $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
157 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
158 $mockQueryResult->expects($this->any())->method('getQuery')->will($this->returnValue($mockQuery));
159 $this->controller->_set('objects', $mockQueryResult);
160 $this->controller->indexAction();
161 $this->assertSame($mockQueryResult, $this->controller->_get('objects'));
162 }
163
164 /**
165 * @test
166 */
167 public function acceptArrayAsObjects()
168 {
169 $objects = [];
170 $this->controller->_set('objects', $objects);
171 $this->controller->indexAction();
172 $this->assertSame($objects, $this->controller->_get('objects'));
173 }
174
175 /**
176 * @test
177 */
178 public function acceptObjectStorageAsObjects()
179 {
180 $objects = new ObjectStorage();
181 $this->controller->_set('objects', $objects);
182 $this->controller->indexAction();
183 $this->assertSame($objects, $this->controller->_get('objects'));
184 }
185
186 /**
187 * @test
188 */
189 public function prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndFirstPage()
190 {
191 $objects = new ObjectStorage();
192 for ($i = 0; $i <= 25; $i++) {
193 $item = new \stdClass;
194 $objects->attach($item);
195 }
196 $this->controller->_set('objects', $objects);
197 $expectedPortion = [];
198 for ($j = 0; $j <= 9; $j++) {
199 $expectedPortion[] = $objects->toArray()[$j];
200 }
201 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
202 }
203
204 /**
205 * @test
206 */
207 public function prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndSecondPage()
208 {
209 $this->controller->_set('currentPage', 2);
210 $objects = new ObjectStorage();
211 for ($i = 0; $i <= 55; $i++) {
212 $item = new \stdClass;
213 $objects->attach($item);
214 }
215 $this->controller->_set('objects', $objects);
216 $expectedPortion = [];
217 for ($j = 10; $j <= 19; $j++) {
218 $expectedPortion[] = $objects->toArray()[$j];
219 }
220 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
221 }
222
223 /**
224 * @test
225 */
226 public function prepareObjectsSliceReturnsCorrectPortionForArrayAndFirstPage()
227 {
228 $objects = [];
229 for ($i = 0; $i <= 25; $i++) {
230 $item = new \stdClass;
231 $objects[] = $item;
232 }
233 $this->controller->_set('objects', $objects);
234 $expectedPortion = [];
235 for ($j = 0; $j <= 9; $j++) {
236 $expectedPortion = array_slice($objects, 0, 10);
237 }
238 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
239 }
240
241 /**
242 * @test
243 */
244 public function prepareObjectsSliceReturnsCorrectPortionForArrayAndSecondPage()
245 {
246 $this->controller->_set('currentPage', 2);
247 $objects = [];
248 for ($i = 0; $i <= 55; $i++) {
249 $item = new \stdClass;
250 $objects[] = $item;
251 }
252 $this->controller->_set('objects', $objects);
253 $expectedPortion = [];
254 for ($j = 10; $j <= 19; $j++) {
255 $expectedPortion = array_slice($objects, 10, 10);
256 }
257 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
258 }
259 }