[CLEANUP] Make use of possible imports and remove unused ones
[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 script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
14
15 /**
16 * Test case
17 */
18 class PaginateControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
19
20 /**
21 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Query
22 */
23 protected $query;
24
25 /**
26 * @var \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
27 */
28 protected $controller;
29
30 /**
31 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
32 */
33 protected $querySettings;
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
37 */
38 protected $persistenceManager;
39
40 /**
41 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
42 */
43 protected $backend;
44
45 /**
46 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
47 */
48 protected $dataMapper;
49
50 /**
51 * Sets up this test case
52 *
53 * @return void
54 */
55 protected function setUp() {
56 $this->query = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Query::class, array('dummy'), array('someType'));
57 $this->querySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
58 $this->query->_set('querySettings', $this->querySettings);
59 $this->persistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
60 $this->backend = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
61 $this->backend->expects($this->any())->method('getQomFactory')->will($this->returnValue(NULL));
62 $this->persistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
63 $this->query->_set('persistenceManager', $this->persistenceManager);
64 $this->dataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
65 $this->query->_set('dataMapper', $this->dataMapper);
66 $this->controller = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
67 array('dummy'), array(), '', FALSE);
68 $this->controller->_set('view', $this->getMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class));
69 }
70
71 /**
72 * @test
73 */
74 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinks() {
75 $this->controller->_set('maximumNumberOfLinks', 8);
76 $this->controller->_set('numberOfPages', 100);
77 $this->controller->_set('currentPage', 50);
78 $this->controller->_call('calculateDisplayRange');
79 $this->assertSame(46, $this->controller->_get('displayRangeStart'));
80 $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
81 }
82
83 /**
84 * @test
85 */
86 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinks() {
87 $this->controller->_set('maximumNumberOfLinks', 7);
88 $this->controller->_set('numberOfPages', 100);
89 $this->controller->_set('currentPage', 50);
90 $this->controller->_call('calculateDisplayRange');
91 $this->assertSame(47, $this->controller->_get('displayRangeStart'));
92 $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
93 }
94
95 /**
96 * @test
97 */
98 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnFirstPage() {
99 $this->controller->_set('maximumNumberOfLinks', 8);
100 $this->controller->_set('numberOfPages', 100);
101 $this->controller->_set('currentPage', 1);
102 $this->controller->_call('calculateDisplayRange');
103 $this->assertSame(1, $this->controller->_get('displayRangeStart'));
104 $this->assertSame(8, $this->controller->_get('displayRangeEnd'));
105 }
106
107 /**
108 * @test
109 */
110 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnFirstPage() {
111 $this->controller->_set('maximumNumberOfLinks', 7);
112 $this->controller->_set('numberOfPages', 100);
113 $this->controller->_set('currentPage', 1);
114 $this->controller->_call('calculateDisplayRange');
115 $this->assertSame(1, $this->controller->_get('displayRangeStart'));
116 $this->assertSame(7, $this->controller->_get('displayRangeEnd'));
117 }
118
119 /**
120 * @test
121 */
122 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnLastPage() {
123 $this->controller->_set('maximumNumberOfLinks', 8);
124 $this->controller->_set('numberOfPages', 100);
125 $this->controller->_set('currentPage', 100);
126 $this->controller->_call('calculateDisplayRange');
127 $this->assertSame(93, $this->controller->_get('displayRangeStart'));
128 $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
129 }
130
131 /**
132 * @test
133 */
134 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnLastPage() {
135 $this->controller->_set('maximumNumberOfLinks', 7);
136 $this->controller->_set('numberOfPages', 100);
137 $this->controller->_set('currentPage', 100);
138 $this->controller->_call('calculateDisplayRange');
139 $this->assertSame(94, $this->controller->_get('displayRangeStart'));
140 $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
141 }
142
143 /**
144 * @test
145 */
146 public function acceptQueryResultInterfaceAsObjects() {
147 $mockQueryResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
148 $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
149 $mockQueryResult->expects($this->any())->method('getQuery')->will($this->returnValue($mockQuery));
150 $this->controller->_set('objects', $mockQueryResult);
151 $this->controller->indexAction();
152 $this->assertSame($mockQueryResult, $this->controller->_get('objects'));
153 }
154
155 /**
156 * @test
157 */
158 public function acceptArrayAsObjects() {
159 $objects = array();
160 $this->controller->_set('objects', $objects);
161 $this->controller->indexAction();
162 $this->assertSame($objects, $this->controller->_get('objects'));
163 }
164
165 /**
166 * @test
167 */
168 public function acceptObjectStorageAsObjects() {
169 $objects = new ObjectStorage();
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 prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndFirstPage() {
179 $objects = new ObjectStorage();
180 for ($i = 0; $i <= 25; $i++) {
181 $item = new \StdClass;
182 $objects->attach($item);
183 }
184 $this->controller->_set('objects', $objects);
185 $expectedPortion = array();
186 for ($j = 0; $j <= 9; $j++) {
187 $expectedPortion[] = $objects->toArray()[$j];
188 }
189 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice',10, 0));
190 }
191
192 /**
193 * @test
194 */
195 public function prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndSecondPage() {
196 $this->controller->_set('currentPage', 2);
197 $objects = new ObjectStorage();
198 for ($i = 0; $i <= 55; $i++) {
199 $item = new \StdClass;
200 $objects->attach($item);
201 }
202 $this->controller->_set('objects', $objects);
203 $expectedPortion = array();
204 for ($j = 10; $j <= 19; $j++) {
205 $expectedPortion[] = $objects->toArray()[$j];
206 }
207 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
208
209 }
210
211 /**
212 * @test
213 */
214 public function prepareObjectsSliceReturnsCorrectPortionForArrayAndFirstPage() {
215 $objects = array();
216 for ($i = 0; $i <= 25; $i++) {
217 $item = new \StdClass;
218 $objects[] = $item;
219 }
220 $this->controller->_set('objects', $objects);
221 $expectedPortion = array();
222 for ($j = 0; $j <= 9; $j++) {
223 $expectedPortion = array_slice($objects, 0, 10);
224 }
225 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
226 }
227
228 /**
229 * @test
230 */
231 public function prepareObjectsSliceReturnsCorrectPortionForArrayAndSecondPage() {
232 $this->controller->_set('currentPage', 2);
233 $objects = array();
234 for ($i = 0; $i <= 55; $i++) {
235 $item = new \StdClass;
236 $objects[] = $item;
237 }
238 $this->controller->_set('objects', $objects);
239 $expectedPortion = array();
240 for ($j = 10; $j <= 19; $j++) {
241 $expectedPortion = array_slice($objects, 10, 10);
242 }
243 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
244 }
245 }