-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
PaginatorComponentTest.php
722 lines (639 loc) · 18.7 KB
/
PaginatorComponentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Component\PaginatorComponent;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\Error;
use Cake\Network\Request;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\Utility\Hash;
/**
* PaginatorTestController class
*
*/
class PaginatorTestController extends Controller {
/**
* components property
*
* @var array
*/
public $components = array('Paginator');
}
class PaginatorComponentTest extends TestCase {
/**
* fixtures property
*
* @var array
*/
public $fixtures = array('core.post', 'core.author');
/**
* setup
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('App.namespace', 'TestApp');
$this->request = new Request('controller_posts/index');
$this->request->params['pass'] = array();
$controller = new Controller($this->request);
$registry = new ComponentRegistry($controller);
$this->Paginator = new PaginatorComponent($registry, []);
$this->Post = $this->getMock('Cake\ORM\Table', [], [], '', false);
}
/**
* tearDown
*
* @return void
*/
public function tearDown() {
parent::tearDown();
TableRegistry::clear();
}
/**
* Test that non-numeric values are rejected for page, and limit
*
* @return void
*/
public function testPageParamCasting() {
$this->Post->expects($this->any())
->method('alias')
->will($this->returnValue('Posts'));
$query = $this->_getMockFindQuery();
$this->Post->expects($this->any())
->method('find')
->will($this->returnValue($query));
$this->request->query = array('page' => '1 " onclick="alert(\'xss\');">');
$settings = array('limit' => 1, 'maxLimit' => 10);
$this->Paginator->paginate($this->Post, $settings);
$this->assertSame(1, $this->request->params['paging']['Posts']['page'], 'XSS exploit opened');
}
/**
* test that unknown keys in the default settings are
* passed to the find operations.
*
* @return void
*/
public function testPaginateExtraParams() {
$this->request->query = array('page' => '-1');
$settings = array(
'PaginatorPosts' => array(
'contain' => array('PaginatorAuthor'),
'maxLimit' => 10,
'group' => 'PaginatorPosts.published',
'order' => array('PaginatorPosts.id' => 'ASC')
),
);
$table = $this->_getMockPosts(['find']);
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('all', [
'conditions' => [],
'contain' => ['PaginatorAuthor'],
'fields' => null,
'group' => 'PaginatorPosts.published',
'limit' => 10,
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
])
->will($this->returnValue($query));
$this->Paginator->paginate($table, $settings);
}
/**
* Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
*
* @return void
*/
public function testPaginateCustomFinder() {
$settings = array(
'PaginatorPosts' => array(
'findType' => 'popular',
'fields' => array('id', 'title'),
'maxLimit' => 10,
)
);
$table = $this->_getMockPosts(['findPopular']);
$query = $this->_getMockFindQuery();
$table->expects($this->any())
->method('findPopular')
->will($this->returnValue($query));
$this->Paginator->paginate($table, $settings);
$this->assertEquals('popular', $this->request->params['paging']['PaginatorPosts']['findType']);
}
/**
* test that flat default pagination parameters work.
*
* @return void
*/
public function testDefaultPaginateParams() {
$settings = array(
'order' => ['PaginatorPosts.id' => 'DESC'],
'maxLimit' => 10,
);
$table = $this->_getMockPosts(['find']);
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('all', [
'conditions' => [],
'fields' => null,
'limit' => 10,
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC']
])
->will($this->returnValue($query));
$this->Paginator->paginate($table, $settings);
}
/**
* test that option merging prefers specific models
*
* @return void
*/
public function testMergeOptionsModelSpecific() {
$settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'Posts' => array(
'page' => 1,
'limit' => 10,
'maxLimit' => 50,
)
);
$result = $this->Paginator->mergeOptions('Silly', $settings);
$this->assertEquals($settings, $result);
$result = $this->Paginator->mergeOptions('Posts', $settings);
$expected = array('page' => 1, 'limit' => 10, 'maxLimit' => 50);
$this->assertEquals($expected, $result);
}
/**
* test mergeOptions with customFind key
*
* @return void
*/
public function testMergeOptionsCustomFindKey() {
$this->request->query = [
'page' => 10,
'limit' => 10
];
$settings = [
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'findType' => 'myCustomFind'
];
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array(
'page' => 10,
'limit' => 10,
'maxLimit' => 100,
'findType' => 'myCustomFind'
);
$this->assertEquals($expected, $result);
}
/**
* test merging options from the querystring.
*
* @return void
*/
public function testMergeOptionsQueryString() {
$this->request->query = array(
'page' => 99,
'limit' => 75
);
$settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
);
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100);
$this->assertEquals($expected, $result);
}
/**
* test that the default whitelist doesn't let people screw with things they should not be allowed to.
*
* @return void
*/
public function testMergeOptionsDefaultWhiteList() {
$this->request->query = array(
'page' => 10,
'limit' => 10,
'fields' => array('bad.stuff'),
'recursive' => 1000,
'conditions' => array('bad.stuff'),
'contain' => array('bad')
);
$settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
);
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100);
$this->assertEquals($expected, $result);
}
/**
* test that modifying the whitelist works.
*
* @return void
*/
public function testMergeOptionsExtraWhitelist() {
$this->request->query = array(
'page' => 10,
'limit' => 10,
'fields' => array('bad.stuff'),
'recursive' => 1000,
'conditions' => array('bad.stuff'),
'contain' => array('bad')
);
$settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
);
$this->Paginator->whitelist[] = 'fields';
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array(
'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => array('bad.stuff')
);
$this->assertEquals($expected, $result);
}
/**
* test mergeOptions with limit > maxLimit in code.
*
* @return void
*/
public function testMergeOptionsMaxLimit() {
$settings = array(
'limit' => 200,
'paramType' => 'named',
);
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 200, 'paramType' => 'named');
$this->assertEquals($expected, $result);
$settings = array(
'maxLimit' => 10,
'paramType' => 'named',
);
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
$this->assertEquals($expected, $result);
}
/**
* Integration test to ensure that validateSort is being used by paginate()
*
* @return void
*/
public function testValidateSortInvalid() {
$table = $this->_getMockPosts(['find']);
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('all', [
'fields' => null,
'limit' => 20,
'conditions' => [],
'page' => 1,
'order' => ['PaginatorPosts.id' => 'asc'],
])
->will($this->returnValue($query));
$this->request->query = [
'page' => 1,
'sort' => 'id',
'direction' => 'herp'
];
$this->Paginator->paginate($table);
$this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sort']);
$this->assertEquals('asc', $this->request->params['paging']['PaginatorPosts']['direction']);
}
/**
* test that invalid directions are ignored.
*
* @return void
*/
public function testValidateSortInvalidDirection() {
$model = $this->getMock('Cake\ORM\Table');
$model->expects($this->any())
->method('alias')
->will($this->returnValue('model'));
$model->expects($this->any())
->method('hasField')
->will($this->returnValue(true));
$options = array('sort' => 'something', 'direction' => 'boogers');
$result = $this->Paginator->validateSort($model, $options);
$this->assertEquals('asc', $result['order']['model.something']);
}
/**
* Test that a really large page number gets clamped to the max page size.
*
* @return void
*/
public function testOutOfRangePageNumberGetsClamped() {
$this->request->query['page'] = 3000;
$table = TableRegistry::get('PaginatorPosts');
try {
$this->Paginator->paginate($table);
$this->fail('No exception raised');
} catch (\Cake\Error\NotFoundException $e) {
$this->assertEquals(
1,
$this->request->params['paging']['PaginatorPosts']['page'],
'Page number should not be 0'
);
}
}
/**
* Test that a really REALLY large page number gets clamped to the max page size.
*
* @expectedException Cake\Error\NotFoundException
* @return void
*/
public function testOutOfVeryBigPageNumberGetsClamped() {
$this->request->query = [
'page' => '3000000000000000000000000',
];
$table = TableRegistry::get('PaginatorPosts');
$this->Paginator->paginate($table);
}
/**
* test that fields not in whitelist won't be part of order conditions.
*
* @return void
*/
public function testValidateSortWhitelistFailure() {
$model = $this->getMock('Cake\ORM\Table');
$model->expects($this->any())
->method('alias')
->will($this->returnValue('model'));
$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
$options = array(
'sort' => 'body',
'direction' => 'asc',
'sortWhitelist' => ['title', 'id']
);
$result = $this->Paginator->validateSort($model, $options);
$this->assertEquals([], $result['order']);
}
/**
* test that fields in the whitelist are not validated
*
* @return void
*/
public function testValidateSortWhitelistTrusted() {
$model = $this->getMock('Cake\ORM\Table');
$model->expects($this->any())
->method('alias')
->will($this->returnValue('model'));
$model->expects($this->never())->method('hasField');
$options = array(
'sort' => 'body',
'direction' => 'asc',
'sortWhitelist' => ['body']
);
$result = $this->Paginator->validateSort($model, $options);
$expected = array('body' => 'asc');
$this->assertEquals($expected, $result['order']);
}
/**
* test that multiple sort works.
*
* @return void
*/
public function testValidateSortMultiple() {
$model = $this->getMock('Cake\ORM\Table');
$model->expects($this->any())
->method('alias')
->will($this->returnValue('model'));
$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
$options = array(
'order' => array(
'author_id' => 'asc',
'title' => 'asc'
)
);
$result = $this->Paginator->validateSort($model, $options);
$expected = array(
'model.author_id' => 'asc',
'model.title' => 'asc'
);
$this->assertEquals($expected, $result['order']);
}
/**
* Test that no sort doesn't trigger an error.
*
* @return void
*/
public function testValidateSortNoSort() {
$model = $this->getMock('Cake\ORM\Table');
$model->expects($this->any())
->method('alias')
->will($this->returnValue('model'));
$model->expects($this->any())->method('hasField')
->will($this->returnValue(true));
$options = array(
'direction' => 'asc',
'sortWhitelist' => ['title', 'id'],
);
$result = $this->Paginator->validateSort($model, $options);
$this->assertEquals([], $result['order']);
}
/**
* Test sorting with incorrect aliases on valid fields.
*
* @return void
*/
public function testValidateSortInvalidAlias() {
$model = $this->getMock('Cake\ORM\Table');
$model->expects($this->any())
->method('alias')
->will($this->returnValue('model'));
$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
$options = array('sort' => 'Derp.id');
$result = $this->Paginator->validateSort($model, $options);
$this->assertEquals(array(), $result['order']);
}
/**
* test that maxLimit is respected
*
* @return void
*/
public function testCheckLimit() {
$result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
$this->assertEquals(100, $result['limit']);
$result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
$this->assertEquals(1, $result['limit']);
$result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
$this->assertEquals(1, $result['limit']);
$result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
$this->assertEquals(1, $result['limit']);
$result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
$this->assertEquals(1, $result['limit']);
}
/**
* Integration test for checkLimit() being applied inside paginate()
*
* @return void
*/
public function testPaginateMaxLimit() {
$table = TableRegistry::get('PaginatorPosts');
$settings = [
'maxLimit' => 100,
];
$this->request->query = [
'limit' => '1000'
];
$this->Paginator->paginate($table, $settings);
$this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['limit']);
$this->request->query = [
'limit' => '10'
];
$this->Paginator->paginate($table, $settings);
$this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
}
/**
* test paginate() and custom find, to make sure the correct count is returned.
*
* @return void
*/
public function testPaginateCustomFind() {
$idExtractor = function ($result) {
$ids = [];
foreach ($result as $record) {
$ids[] = $record->id;
}
return $ids;
};
$table = TableRegistry::get('PaginatorPosts');
$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
$result = $table->save(new \Cake\ORM\Entity($data));
$this->assertNotEmpty($result);
$result = $this->Paginator->paginate($table);
$this->assertCount(4, $result, '4 rows should come back');
$this->assertEquals(array(1, 2, 3, 4), $idExtractor($result));
$result = $this->request->params['paging']['PaginatorPosts'];
$this->assertEquals(4, $result['current']);
$this->assertEquals(4, $result['count']);
$settings = array('findType' => 'published');
$result = $this->Paginator->paginate($table, $settings);
$this->assertCount(3, $result, '3 rows should come back');
$this->assertEquals(array(1, 2, 3), $idExtractor($result));
$result = $this->request->params['paging']['PaginatorPosts'];
$this->assertEquals(3, $result['current']);
$this->assertEquals(3, $result['count']);
$settings = array('findType' => 'published', 'limit' => 2);
$result = $this->Paginator->paginate($table, $settings);
$this->assertCount(2, $result, '2 rows should come back');
$this->assertEquals(array(1, 2), $idExtractor($result));
$result = $this->request->params['paging']['PaginatorPosts'];
$this->assertEquals(2, $result['current']);
$this->assertEquals(3, $result['count']);
$this->assertEquals(2, $result['pageCount']);
$this->assertTrue($result['nextPage']);
$this->assertFalse($result['prevPage']);
}
/**
* test paginate() and custom find with fields array, to make sure the correct count is returned.
*
* @return void
*/
public function testPaginateCustomFindFieldsArray() {
$table = TableRegistry::get('PaginatorPosts');
$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
$table->save(new \Cake\ORM\Entity($data));
$settings = [
'findType' => 'list',
'conditions' => array('PaginatorPosts.published' => 'Y'),
'limit' => 2
];
$results = $this->Paginator->paginate($table, $settings);
$result = $results->toArray();
$expected = array(
1 => 'First Post',
2 => 'Second Post',
);
$this->assertEquals($expected, $result);
$result = $this->request->params['paging']['PaginatorPosts'];
$this->assertEquals(2, $result['current']);
$this->assertEquals(3, $result['count']);
$this->assertEquals(2, $result['pageCount']);
$this->assertTrue($result['nextPage']);
$this->assertFalse($result['prevPage']);
}
/**
* test paginate() and custom finders to ensure the count + find
* use the custom type.
*
* @return void
*/
public function testPaginateCustomFindCount() {
$settings = array(
'findType' => 'published',
'limit' => 2
);
$table = $this->_getMockPosts(['find']);
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('published', [
'conditions' => [],
'order' => [],
'limit' => 2,
'fields' => null,
'page' => 1,
])
->will($this->returnValue($query));
$this->Paginator->paginate($table, $settings);
}
/**
* Helper method for making mocks.
*
* @return Table
*/
protected function _getMockPosts($methods = []) {
return $this->getMock(
'TestApp\Model\Table\PaginatorPostsTable',
$methods,
[['connection' => ConnectionManager::get('test'), 'alias' => 'PaginatorPosts']]
);
}
/**
* Helper method for mocking queries.
*
* @return Query
*/
protected function _getMockFindQuery() {
$query = $this->getMockBuilder('Cake\ORM\Query')
->setMethods(['total', 'all', 'count'])
->disableOriginalConstructor()
->getMock();
$results = $this->getMock('Cake\ORM\ResultSet', [], [], '', false);
$query->expects($this->any())
->method('count')
->will($this->returnValue(2));
$query->expects($this->any())
->method('all')
->will($this->returnValue($results));
$query->expects($this->any())
->method('count')
->will($this->returnValue(2));
return $query;
}
}