1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\ArrayList; |
6
|
|
|
use SilverStripe\ORM\DataObject; |
7
|
|
|
use SilverStripe\ORM\PaginatedList; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
10
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Player; |
11
|
|
|
use SilverStripe\View\ArrayData; |
12
|
|
|
use SilverStripe\Control\HTTPRequest; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Tests for the {@link SilverStripe\ORM\PaginatedList} class. |
16
|
|
|
*/ |
17
|
|
|
class PaginatedListTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected static $fixture_file = 'DataObjectTest.yml'; |
21
|
|
|
|
22
|
|
|
public static function getExtraDataObjects() |
23
|
|
|
{ |
24
|
|
|
return array_merge( |
25
|
|
|
DataObjectTest::$extra_data_objects, |
26
|
|
|
ManyManyListTest::$extra_data_objects |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testPageStart() |
31
|
|
|
{ |
32
|
|
|
$list = new PaginatedList(new ArrayList()); |
33
|
|
|
$this->assertEquals(0, $list->getPageStart(), 'The start defaults to 0.'); |
34
|
|
|
|
35
|
|
|
$list->setPageStart(10); |
36
|
|
|
$this->assertEquals(10, $list->getPageStart(), 'You can set the page start.'); |
37
|
|
|
|
38
|
|
|
$list = new PaginatedList(new ArrayList(), ['start' => 50]); |
39
|
|
|
$this->assertEquals(50, $list->getPageStart(), 'The page start can be read from the request.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testGetTotalItems() |
43
|
|
|
{ |
44
|
|
|
$list = new PaginatedList(new ArrayList()); |
45
|
|
|
$this->assertEquals(0, $list->getTotalItems()); |
46
|
|
|
|
47
|
|
|
$list->setTotalItems(10); |
48
|
|
|
$this->assertEquals(10, $list->getTotalItems()); |
49
|
|
|
|
50
|
|
|
$list = new PaginatedList( |
51
|
|
|
new ArrayList( |
52
|
|
|
[ |
53
|
|
|
new ArrayData([]), |
54
|
|
|
new ArrayData([]) |
55
|
|
|
] |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
$this->assertEquals(2, $list->getTotalItems()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testSetPaginationFromQuery() |
62
|
|
|
{ |
63
|
|
|
$query = $this->getMockBuilder(SQLSelect::class)->getMock(); |
64
|
|
|
$query->expects($this->once()) |
65
|
|
|
->method('getLimit') |
66
|
|
|
->will($this->returnValue(['limit' => 15, 'start' => 30])); |
67
|
|
|
$query->expects($this->once()) |
68
|
|
|
->method('unlimitedRowCount') |
69
|
|
|
->will($this->returnValue(100)); |
70
|
|
|
|
71
|
|
|
$list = new PaginatedList(new ArrayList()); |
72
|
|
|
$list->setPaginationFromQuery($query); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(15, $list->getPageLength()); |
75
|
|
|
$this->assertEquals(30, $list->getPageStart()); |
76
|
|
|
$this->assertEquals(100, $list->getTotalItems()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSetCurrentPage() |
80
|
|
|
{ |
81
|
|
|
$list = new PaginatedList(new ArrayList()); |
82
|
|
|
$list->setPageLength(10); |
83
|
|
|
$list->setCurrentPage(10); |
84
|
|
|
|
85
|
|
|
$this->assertEquals(10, $list->CurrentPage()); |
86
|
|
|
$this->assertEquals(90, $list->getPageStart()); |
87
|
|
|
|
88
|
|
|
// Test disabled paging |
89
|
|
|
$list->setPageLength(0); |
90
|
|
|
$this->assertEquals(1, $list->CurrentPage()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGetIterator() |
94
|
|
|
{ |
95
|
|
|
$list = new PaginatedList( |
96
|
|
|
new ArrayList([ |
97
|
|
|
new DataObject(['Num' => 1]), |
98
|
|
|
new DataObject(['Num' => 2]), |
99
|
|
|
new DataObject(['Num' => 3]), |
100
|
|
|
new DataObject(['Num' => 4]), |
101
|
|
|
new DataObject(['Num' => 5]), |
102
|
|
|
]) |
103
|
|
|
); |
104
|
|
|
$list->setPageLength(2); |
105
|
|
|
|
106
|
|
|
$this->assertListEquals( |
107
|
|
|
[['Num' => 1], ['Num' => 2]], |
108
|
|
|
ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy()) |
|
|
|
|
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$list->setCurrentPage(2); |
112
|
|
|
$this->assertListEquals( |
113
|
|
|
[['Num' => 3], ['Num' => 4]], |
114
|
|
|
ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy()) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$list->setCurrentPage(3); |
118
|
|
|
$this->assertListEquals( |
119
|
|
|
[['Num' => 5]], |
120
|
|
|
ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy()) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$list->setCurrentPage(999); |
124
|
|
|
$this->assertListEquals( |
125
|
|
|
[], |
126
|
|
|
ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy()) |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
// Test disabled paging |
130
|
|
|
$list->setPageLength(0); |
131
|
|
|
$list->setCurrentPage(1); |
132
|
|
|
$this->assertListEquals( |
133
|
|
|
[ |
134
|
|
|
['Num' => 1], |
135
|
|
|
['Num' => 2], |
136
|
|
|
['Num' => 3], |
137
|
|
|
['Num' => 4], |
138
|
|
|
['Num' => 5], |
139
|
|
|
], |
140
|
|
|
ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy()) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
// Test with dataobjectset |
144
|
|
|
$players = Player::get(); |
145
|
|
|
$list = new PaginatedList($players); |
146
|
|
|
$list->setPageLength(1); |
147
|
|
|
$list->getIterator(); |
148
|
|
|
$this->assertEquals( |
149
|
|
|
4, |
150
|
|
|
$list->getTotalItems(), |
151
|
|
|
'Getting an iterator should not trim the list to the page length.' |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testPages() |
156
|
|
|
{ |
157
|
|
|
$list = new PaginatedList(new ArrayList()); |
158
|
|
|
$list->setPageLength(10); |
159
|
|
|
$list->setTotalItems(50); |
160
|
|
|
|
161
|
|
|
$this->assertCount(5, $list->Pages()); |
162
|
|
|
$this->assertCount(3, $list->Pages(3)); |
163
|
|
|
$this->assertCount(5, $list->Pages(15)); |
164
|
|
|
|
165
|
|
|
$list->setCurrentPage(3); |
166
|
|
|
|
167
|
|
|
$expectAll = [ |
168
|
|
|
['PageNum' => 1], |
169
|
|
|
['PageNum' => 2], |
170
|
|
|
['PageNum' => 3, 'CurrentBool' => true], |
171
|
|
|
['PageNum' => 4], |
172
|
|
|
['PageNum' => 5], |
173
|
|
|
]; |
174
|
|
|
$this->assertListEquals($expectAll, $list->Pages()); |
175
|
|
|
|
176
|
|
|
$expectLimited = [ |
177
|
|
|
['PageNum' => 2], |
178
|
|
|
['PageNum' => 3, 'CurrentBool' => true], |
179
|
|
|
['PageNum' => 4], |
180
|
|
|
]; |
181
|
|
|
$this->assertListEquals($expectLimited, $list->Pages(3)); |
182
|
|
|
|
183
|
|
|
// Disable paging |
184
|
|
|
$list->setPageLength(0); |
185
|
|
|
$expectAll = [ |
186
|
|
|
['PageNum' => 1, 'CurrentBool' => true], |
187
|
|
|
]; |
188
|
|
|
$this->assertListEquals($expectAll, $list->Pages()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testPaginationSummary() |
192
|
|
|
{ |
193
|
|
|
$list = new PaginatedList(new ArrayList()); |
194
|
|
|
|
195
|
|
|
$list->setPageLength(10); |
196
|
|
|
$list->setTotalItems(250); |
197
|
|
|
$list->setCurrentPage(6); |
198
|
|
|
|
199
|
|
|
$expect = [ |
200
|
|
|
['PageNum' => 1], |
201
|
|
|
['PageNum' => null], |
202
|
|
|
['PageNum' => 4], |
203
|
|
|
['PageNum' => 5], |
204
|
|
|
['PageNum' => 6, 'CurrentBool' => true], |
205
|
|
|
['PageNum' => 7], |
206
|
|
|
['PageNum' => 8], |
207
|
|
|
['PageNum' => null], |
208
|
|
|
['PageNum' => 25], |
209
|
|
|
]; |
210
|
|
|
$this->assertListEquals($expect, $list->PaginationSummary(4)); |
211
|
|
|
|
212
|
|
|
// Disable paging |
213
|
|
|
$list->setPageLength(0); |
214
|
|
|
$expect = [ |
215
|
|
|
['PageNum' => 1, 'CurrentBool' => true] |
216
|
|
|
]; |
217
|
|
|
$this->assertListEquals($expect, $list->PaginationSummary(4)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testLimitItems() |
221
|
|
|
{ |
222
|
|
|
$list = new ArrayList(range(1, 50)); |
223
|
|
|
$list = new PaginatedList($list); |
224
|
|
|
|
225
|
|
|
$list->setCurrentPage(3); |
226
|
|
|
$this->assertCount(10, $list->getIterator()->getInnerIterator()); |
227
|
|
|
|
228
|
|
|
$list->setLimitItems(false); |
229
|
|
|
$this->assertCount(50, $list->getIterator()->getInnerIterator()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testCurrentPage() |
233
|
|
|
{ |
234
|
|
|
$list = new PaginatedList(new ArrayList()); |
235
|
|
|
$list->setTotalItems(50); |
236
|
|
|
|
237
|
|
|
$this->assertEquals(1, $list->CurrentPage()); |
238
|
|
|
$list->setPageStart(10); |
239
|
|
|
$this->assertEquals(2, $list->CurrentPage()); |
240
|
|
|
$list->setPageStart(40); |
241
|
|
|
$this->assertEquals(5, $list->CurrentPage()); |
242
|
|
|
|
243
|
|
|
// Disable paging |
244
|
|
|
$list->setPageLength(0); |
245
|
|
|
$this->assertEquals(1, $list->CurrentPage()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testTotalPages() |
249
|
|
|
{ |
250
|
|
|
$list = new PaginatedList(new ArrayList()); |
251
|
|
|
|
252
|
|
|
$list->setPageLength(1); |
253
|
|
|
$this->assertEquals(0, $list->TotalPages()); |
254
|
|
|
|
255
|
|
|
$list->setTotalItems(1); |
256
|
|
|
$this->assertEquals(1, $list->TotalPages()); |
257
|
|
|
|
258
|
|
|
$list->setTotalItems(5); |
259
|
|
|
$this->assertEquals(5, $list->TotalPages()); |
260
|
|
|
|
261
|
|
|
// Disable paging |
262
|
|
|
$list->setPageLength(0); |
263
|
|
|
$this->assertEquals(1, $list->TotalPages()); |
264
|
|
|
|
265
|
|
|
$list->setTotalItems(0); |
266
|
|
|
$this->assertEquals(0, $list->TotalPages()); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testMoreThanOnePage() |
270
|
|
|
{ |
271
|
|
|
$list = new PaginatedList(new ArrayList()); |
272
|
|
|
|
273
|
|
|
$list->setPageLength(1); |
274
|
|
|
$list->setTotalItems(1); |
275
|
|
|
$this->assertFalse($list->MoreThanOnePage()); |
276
|
|
|
|
277
|
|
|
$list->setTotalItems(2); |
278
|
|
|
$this->assertTrue($list->MoreThanOnePage()); |
279
|
|
|
|
280
|
|
|
// Disable paging |
281
|
|
|
$list->setPageLength(0); |
282
|
|
|
$this->assertFalse($list->MoreThanOnePage()); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function testFirstPage() |
286
|
|
|
{ |
287
|
|
|
$list = new PaginatedList(new ArrayList()); |
288
|
|
|
$this->assertTrue($list->FirstPage()); |
289
|
|
|
$list->setCurrentPage(2); |
290
|
|
|
$this->assertFalse($list->FirstPage()); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function testNotFirstPage() |
294
|
|
|
{ |
295
|
|
|
$list = new PaginatedList(new ArrayList()); |
296
|
|
|
$this->assertFalse($list->NotFirstPage()); |
297
|
|
|
$list->setCurrentPage(2); |
298
|
|
|
$this->assertTrue($list->NotFirstPage()); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function testLastPage() |
302
|
|
|
{ |
303
|
|
|
$list = new PaginatedList(new ArrayList()); |
304
|
|
|
$list->setTotalItems(50); |
305
|
|
|
|
306
|
|
|
$this->assertFalse($list->LastPage()); |
307
|
|
|
$list->setCurrentPage(5); |
308
|
|
|
$this->assertTrue($list->LastPage()); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function testNotLastPage() |
312
|
|
|
{ |
313
|
|
|
$list = new PaginatedList(new ArrayList()); |
314
|
|
|
$list->setTotalItems(50); |
315
|
|
|
|
316
|
|
|
$this->assertTrue($list->NotLastPage()); |
317
|
|
|
$list->setCurrentPage(5); |
318
|
|
|
$this->assertFalse($list->NotLastPage()); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function testFirstItem() |
322
|
|
|
{ |
323
|
|
|
$list = new PaginatedList(new ArrayList()); |
324
|
|
|
$this->assertEquals(1, $list->FirstItem()); |
325
|
|
|
$list->setPageStart(10); |
326
|
|
|
$this->assertEquals(11, $list->FirstItem()); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function testLastItem() |
330
|
|
|
{ |
331
|
|
|
$list = new PaginatedList(new ArrayList()); |
332
|
|
|
$list->setPageLength(10); |
333
|
|
|
$list->setTotalItems(25); |
334
|
|
|
|
335
|
|
|
$list->setCurrentPage(1); |
336
|
|
|
$this->assertEquals(10, $list->LastItem()); |
337
|
|
|
$list->setCurrentPage(2); |
338
|
|
|
$this->assertEquals(20, $list->LastItem()); |
339
|
|
|
$list->setCurrentPage(3); |
340
|
|
|
$this->assertEquals(25, $list->LastItem()); |
341
|
|
|
|
342
|
|
|
// Disable paging |
343
|
|
|
$list->setPageLength(0); |
344
|
|
|
$this->assertEquals(25, $list->LastItem()); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public function testFirstLink() |
348
|
|
|
{ |
349
|
|
|
$list = new PaginatedList(new ArrayList()); |
350
|
|
|
$this->assertStringContainsString('start=0', $list->FirstLink()); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function testFirstLinkContainsCurrentGetParameters() |
354
|
|
|
{ |
355
|
|
|
$request = new HTTPRequest( |
356
|
|
|
'GET', |
357
|
|
|
'http://example.com/my-cool-page', |
358
|
|
|
['awesomeness' => 'nextLevel', 'start' => 20] |
359
|
|
|
); |
360
|
|
|
$list = new PaginatedList(new ArrayList(), $request); |
361
|
|
|
$list->setTotalItems(50); |
362
|
|
|
$list->setPageLength(10); |
363
|
|
|
|
364
|
|
|
// check the query string has correct parameters |
365
|
|
|
$queryString = parse_url($list->FirstLink(), PHP_URL_QUERY); |
366
|
|
|
parse_str($queryString, $queryParams); |
367
|
|
|
|
368
|
|
|
$this->assertArrayHasKey('awesomeness', $queryParams); |
369
|
|
|
$this->assertequals('nextLevel', $queryParams['awesomeness']); |
370
|
|
|
$this->assertArrayHasKey('start', $queryParams); |
371
|
|
|
$this->assertequals(0, $queryParams['start']); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function testLastLink() |
375
|
|
|
{ |
376
|
|
|
$list = new PaginatedList(new ArrayList()); |
377
|
|
|
$list->setPageLength(10); |
378
|
|
|
$list->setTotalItems(100); |
379
|
|
|
$this->assertStringContainsString('start=90', $list->LastLink()); |
380
|
|
|
|
381
|
|
|
// Disable paging |
382
|
|
|
$list->setPageLength(0); |
383
|
|
|
$this->assertStringContainsString('start=0', $list->LastLink()); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function testLastLinkContainsCurrentGetParameters() |
387
|
|
|
{ |
388
|
|
|
$request = new HTTPRequest( |
389
|
|
|
'GET', |
390
|
|
|
'http://example.com/my-cool-page', |
391
|
|
|
['awesomeness' => 'nextLevel'] |
392
|
|
|
); |
393
|
|
|
$list = new PaginatedList(new ArrayList(), $request); |
394
|
|
|
$list->setTotalItems(50); |
395
|
|
|
$list->setPageLength(10); |
396
|
|
|
|
397
|
|
|
// check the query string has correct parameters |
398
|
|
|
$queryString = parse_url($list->LastLink(), PHP_URL_QUERY); |
399
|
|
|
parse_str($queryString, $queryParams); |
400
|
|
|
|
401
|
|
|
$this->assertArrayHasKey('awesomeness', $queryParams); |
402
|
|
|
$this->assertequals('nextLevel', $queryParams['awesomeness']); |
403
|
|
|
$this->assertArrayHasKey('start', $queryParams); |
404
|
|
|
$this->assertequals(40, $queryParams['start']); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function testNextLink() |
408
|
|
|
{ |
409
|
|
|
$list = new PaginatedList(new ArrayList()); |
410
|
|
|
$list->setTotalItems(50); |
411
|
|
|
|
412
|
|
|
$this->assertStringContainsString('start=10', $list->NextLink()); |
413
|
|
|
$list->setCurrentPage(2); |
414
|
|
|
$this->assertStringContainsString('start=20', $list->NextLink()); |
415
|
|
|
$list->setCurrentPage(3); |
416
|
|
|
$this->assertStringContainsString('start=30', $list->NextLink()); |
417
|
|
|
$list->setCurrentPage(4); |
418
|
|
|
$this->assertStringContainsString('start=40', $list->NextLink()); |
419
|
|
|
$list->setCurrentPage(5); |
420
|
|
|
$this->assertNull($list->NextLink()); |
421
|
|
|
|
422
|
|
|
// Disable paging |
423
|
|
|
$list->setCurrentPage(1); |
424
|
|
|
$list->setPageLength(0); |
425
|
|
|
$this->assertNull($list->NextLink()); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
public function testNextLinkContainsCurrentGetParameters() |
429
|
|
|
{ |
430
|
|
|
$request = new HTTPRequest( |
431
|
|
|
'GET', |
432
|
|
|
'http://example.com/my-cool-page', |
433
|
|
|
['awesomeness' => 'nextLevel'] |
434
|
|
|
); |
435
|
|
|
$list = new PaginatedList(new ArrayList(), $request); |
436
|
|
|
$list->setTotalItems(50); |
437
|
|
|
$list->setPageLength(10); |
438
|
|
|
|
439
|
|
|
// check the query string has correct parameters |
440
|
|
|
$queryString = parse_url($list->NextLink(), PHP_URL_QUERY); |
441
|
|
|
parse_str($queryString, $queryParams); |
442
|
|
|
|
443
|
|
|
$this->assertArrayHasKey('awesomeness', $queryParams); |
444
|
|
|
$this->assertequals('nextLevel', $queryParams['awesomeness']); |
445
|
|
|
$this->assertArrayHasKey('start', $queryParams); |
446
|
|
|
$this->assertequals(10, $queryParams['start']); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
public function testPrevLink() |
450
|
|
|
{ |
451
|
|
|
$list = new PaginatedList(new ArrayList()); |
452
|
|
|
$list->setTotalItems(50); |
453
|
|
|
|
454
|
|
|
$this->assertNull($list->PrevLink()); |
455
|
|
|
$list->setCurrentPage(2); |
456
|
|
|
$this->assertStringContainsString('start=0', $list->PrevLink()); |
457
|
|
|
$list->setCurrentPage(3); |
458
|
|
|
$this->assertStringContainsString('start=10', $list->PrevLink()); |
459
|
|
|
$list->setCurrentPage(5); |
460
|
|
|
$this->assertStringContainsString('start=30', $list->PrevLink()); |
461
|
|
|
|
462
|
|
|
// Disable paging |
463
|
|
|
$list->setPageLength(0); |
464
|
|
|
$this->assertNull($list->PrevLink()); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
public function testPrevLinkContainsCurrentGetParameters() |
468
|
|
|
{ |
469
|
|
|
$request = new HTTPRequest( |
470
|
|
|
'GET', |
471
|
|
|
'http://example.com/my-cool-page', |
472
|
|
|
['awesomeness' => 'nextLevel', 'start' => '30'] |
473
|
|
|
); |
474
|
|
|
$list = new PaginatedList(new ArrayList(), $request); |
475
|
|
|
$list->setTotalItems(50); |
476
|
|
|
$list->setPageLength(10); |
477
|
|
|
|
478
|
|
|
// check the query string has correct parameters |
479
|
|
|
$queryString = parse_url($list->PrevLink(), PHP_URL_QUERY); |
480
|
|
|
parse_str($queryString, $queryParams); |
481
|
|
|
|
482
|
|
|
$this->assertArrayHasKey('awesomeness', $queryParams); |
483
|
|
|
$this->assertequals('nextLevel', $queryParams['awesomeness']); |
484
|
|
|
$this->assertArrayHasKey('start', $queryParams); |
485
|
|
|
$this->assertequals(20, $queryParams['start']); |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
|