-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
PaginatorComponentTest.php
919 lines (822 loc) · 24.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
<?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 2.0.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\Datasource\ConnectionManager;
use Cake\Network\Exception\NotFoundException;
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');
/**
* Don't load data for fixtures for all tests
*
* @var bool
*/
public $autoFixtures = false;
/**
* 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')
->will($this->returnValue($query));
$query->expects($this->once())
->method('applyOptions')
->with([
'contain' => ['PaginatorAuthor'],
'group' => 'PaginatorPosts.published',
'limit' => 10,
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
]);
$this->Paginator->paginate($table, $settings);
}
/**
* Test to make sure options get sent to custom finder methods via paginate
*
* @return void
*/
public function testPaginateCustomFinderOptions() {
$this->loadFixtures('Post');
$settings = [
'PaginatorPosts' => [
'finder' => 'author',
'author_id' => 1
]
];
$table = TableRegistry::get('PaginatorPosts');
$expected = $table->find('author', ['conditions' => ['PaginatorPosts.author_id' => $settings['PaginatorPosts']['author_id']]])
->count();
$result = $this->Paginator->paginate($table, $settings)->count();
$this->assertEquals($expected, $result);
}
/**
* 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(
'finder' => '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']['finder']);
}
/**
* 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')
->will($this->returnValue($query));
$query->expects($this->once())
->method('applyOptions')
->with([
'limit' => 10,
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
]);
$this->Paginator->paginate($table, $settings);
}
/**
* test that default sort and default direction are injected into request
*
* @return void
*/
public function testDefaultPaginateParamsIntoRequest() {
$settings = array(
'order' => ['PaginatorPosts.id' => 'DESC'],
'maxLimit' => 10,
);
$table = $this->_getMockPosts(['find']);
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('all')
->will($this->returnValue($query));
$query->expects($this->once())
->method('applyOptions')
->with([
'limit' => 10,
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
]);
$this->Paginator->paginate($table, $settings);
$this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
$this->assertEquals('DESC', $this->request->params['paging']['PaginatorPosts']['directionDefault']);
}
/**
* 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,
),
'whitelist' => ['limit', 'sort', 'page', 'direction'],
);
$result = $this->Paginator->mergeOptions('Silly', $settings);
$this->assertEquals($settings, $result);
$result = $this->Paginator->mergeOptions('Posts', $settings);
$expected = array('page' => 1, 'limit' => 10, 'maxLimit' => 50, 'whitelist' => ['limit', 'sort', 'page', 'direction']);
$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,
'finder' => 'myCustomFind'
];
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array(
'page' => 10,
'limit' => 10,
'maxLimit' => 100,
'finder' => 'myCustomFind',
'whitelist' => ['limit', 'sort', 'page', 'direction'],
);
$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, 'whitelist' => ['limit', 'sort', 'page', 'direction']);
$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, 'whitelist' => ['limit', 'sort', 'page', 'direction']);
$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->config('whitelist', ['fields']);
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = array(
'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => array('bad.stuff'), 'whitelist' => ['limit', 'sort', 'page', 'direction', 'fields']
);
$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',
'whitelist' => ['limit', 'sort', 'page', 'direction']
);
$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',
'whitelist' => ['limit', 'sort', 'page', 'direction']
);
$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')
->will($this->returnValue($query));
$query->expects($this->once())->method('applyOptions')
->with([
'limit' => 20,
'page' => 1,
'order' => ['PaginatorPosts.id' => 'asc'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
]);
$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->loadFixtures('Post');
$this->request->query['page'] = 3000;
$table = TableRegistry::get('PaginatorPosts');
try {
$this->Paginator->paginate($table);
$this->fail('No exception raised');
} catch (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\Network\Exception\NotFoundException
* @return void
*/
public function testOutOfVeryBigPageNumberGetsClamped() {
$this->loadFixtures('Post');
$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']);
}
/**
* Tests that order strings can used by Paginator
*
* @return void
*/
public function testValidateSortWithString() {
$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' => 'model.author_id DESC'
);
$result = $this->Paginator->validateSort($model, $options);
$expected = 'model.author_id DESC';
$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() {
$this->loadFixtures('Post');
$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->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['perPage']);
$this->request->query = [
'limit' => '10'
];
$this->Paginator->paginate($table, $settings);
$this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
$this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['perPage']);
}
/**
* test paginate() and custom find, to make sure the correct count is returned.
*
* @return void
*/
public function testPaginateCustomFind() {
$this->loadFixtures('Post');
$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('finder' => '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('finder' => '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']);
$this->assertEquals(2, $result['perPage']);
$this->assertNull($result['limit']);
}
/**
* test paginate() and custom find with deprecated option.
*
* @expectedException PHPUnit_Framework_Error_Deprecated
* @return void
*/
public function testPaginateCustomFindOldOption() {
$this->loadFixtures('Post');
$table = TableRegistry::get('PaginatorPosts');
$this->Paginator->paginate($table, ['findType' => 'published']);
}
/**
* test paginate() and custom find with fields array, to make sure the correct count is returned.
*
* @return void
*/
public function testPaginateCustomFindFieldsArray() {
$this->loadFixtures('Post');
$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 = [
'finder' => '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(
'finder' => 'published',
'limit' => 2
);
$table = $this->_getMockPosts(['find']);
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('published')
->will($this->returnValue($query));
$query->expects($this->once())->method('applyOptions')
->with(['limit' => 2, 'page' => 1, 'order' => [], 'whitelist' => ['limit', 'sort', 'page', 'direction']]);
$this->Paginator->paginate($table, $settings);
}
/**
* Tests that it is possible to pass an already made query object to
* paginate()
*
* @return void
*/
public function testPaginateQuery() {
$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);
$table->expects($this->never())->method('find');
$query->expects($this->once())
->method('applyOptions')
->with([
'contain' => ['PaginatorAuthor'],
'group' => 'PaginatorPosts.published',
'limit' => 10,
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
]);
$this->Paginator->paginate($query, $settings);
}
/**
* Tests that passing a query object with a limit clause set will
* overwrite it with the passed defaults.
*
* @return void
*/
public function testPaginateQueryWithLimit() {
$this->request->query = array('page' => '-1');
$settings = array(
'PaginatorPosts' => array(
'contain' => array('PaginatorAuthor'),
'maxLimit' => 10,
'limit' => 5,
'group' => 'PaginatorPosts.published',
'order' => array('PaginatorPosts.id' => 'ASC')
)
);
$table = $this->_getMockPosts(['find']);
$query = $this->_getMockFindQuery($table);
$query->limit(2);
$table->expects($this->never())->method('find');
$query->expects($this->once())
->method('applyOptions')
->with([
'contain' => ['PaginatorAuthor'],
'group' => 'PaginatorPosts.published',
'limit' => 5,
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
]);
$this->Paginator->paginate($query, $settings);
}
/**
* Helper method for making mocks.
*
* @param array $methods
* @return Table
*/
protected function _getMockPosts($methods = []) {
return $this->getMock(
'TestApp\Model\Table\PaginatorPostsTable',
$methods,
[[
'connection' => ConnectionManager::get('test'),
'alias' => 'PaginatorPosts',
'schema' => [
'id' => ['type' => 'integer'],
'author_id' => ['type' => 'integer', 'null' => false],
'title' => ['type' => 'string', 'null' => false],
'body' => 'text',
'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
]
]]
);
}
/**
* Helper method for mocking queries.
*
* @return Query
*/
protected function _getMockFindQuery($table = null) {
$query = $this->getMockBuilder('Cake\ORM\Query')
->setMethods(['total', 'all', 'count', 'applyOptions'])
->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));
$query->repository($table);
return $query;
}
}