Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * PaginatorComponentTest file
4
 *
5
 * Series of tests for paginator component.
6
 *
7
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
16
 * @package       Cake.Test.Case.Controller.Component
17
 * @since         CakePHP(tm) v 2.0
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('Controller', 'Controller');
22
App::uses('PaginatorComponent', 'Controller/Component');
23
App::uses('CakeRequest', 'Network');
24
App::uses('CakeResponse', 'Network');
25
 
26
/**
27
 * PaginatorTestController class
28
 *
29
 * @package       Cake.Test.Case.Controller.Component
30
 */
31
class PaginatorTestController extends Controller {
32
 
33
/**
34
 * components property
35
 *
36
 * @var array
37
 */
38
	public $components = array('Paginator');
39
}
40
 
41
/**
42
 * PaginatorControllerPost class
43
 *
44
 * @package       Cake.Test.Case.Controller.Component
45
 */
46
class PaginatorControllerPost extends CakeTestModel {
47
 
48
/**
49
 * useTable property
50
 *
51
 * @var string
52
 */
53
	public $useTable = 'posts';
54
 
55
/**
56
 * invalidFields property
57
 *
58
 * @var array
59
 */
60
	public $invalidFields = array('name' => 'error_msg');
61
 
62
/**
63
 * lastQueries property
64
 *
65
 * @var array
66
 */
67
	public $lastQueries = array();
68
 
69
/**
70
 * belongsTo property
71
 *
72
 * @var array
73
 */
74
	public $belongsTo = array('PaginatorAuthor' => array('foreignKey' => 'author_id'));
75
 
76
/**
77
 * beforeFind method
78
 *
79
 * @param mixed $query
80
 * @return void
81
 */
82
	public function beforeFind($query) {
83
		array_unshift($this->lastQueries, $query);
84
	}
85
 
86
/**
87
 * find method
88
 *
89
 * @param mixed $type
90
 * @param array $options
91
 * @return void
92
 */
93
	public function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
94
		if ($conditions === 'popular') {
95
			$conditions = array($this->name . '.' . $this->primaryKey . ' > ' => '1');
96
			$options = Hash::merge($fields, compact('conditions'));
97
			return parent::find('all', $options);
98
		}
99
		return parent::find($conditions, $fields);
100
	}
101
 
102
}
103
 
104
/**
105
 * ControllerPaginateModel class
106
 *
107
 * @package       Cake.Test.Case.Controller.Component
108
 */
109
class ControllerPaginateModel extends CakeTestModel {
110
 
111
/**
112
 * useTable property
113
 *
114
 * @var string
115
 */
116
	public $useTable = 'comments';
117
 
118
/**
119
 * paginate method
120
 *
121
 * @return boolean
122
 */
123
	public function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
124
		$this->extra = $extra;
125
		return true;
126
	}
127
 
128
/**
129
 * paginateCount
130
 *
131
 * @return void
132
 */
133
	public function paginateCount($conditions, $recursive, $extra) {
134
		$this->extraCount = $extra;
135
	}
136
 
137
}
138
 
139
/**
140
 * PaginatorControllerComment class
141
 *
142
 * @package       Cake.Test.Case.Controller.Component
143
 */
144
class PaginatorControllerComment extends CakeTestModel {
145
 
146
/**
147
 * name property
148
 *
149
 * @var string
150
 */
151
	public $name = 'Comment';
152
 
153
/**
154
 * useTable property
155
 *
156
 * @var string
157
 */
158
	public $useTable = 'comments';
159
 
160
/**
161
 * alias property
162
 *
163
 * @var string
164
 */
165
	public $alias = 'PaginatorControllerComment';
166
}
167
 
168
/**
169
 * PaginatorAuthor class
170
 *
171
 * @package       Cake.Test.Case.Controller.Component
172
 */
173
class PaginatorAuthor extends CakeTestModel {
174
 
175
/**
176
 * useTable property
177
 *
178
 * @var string
179
 */
180
	public $useTable = 'authors';
181
 
182
/**
183
 * alias property
184
 *
185
 * @var string
186
 */
187
	public $virtualFields = array(
188
			'joined_offset' => 'PaginatorAuthor.id + 1'
189
		);
190
 
191
}
192
 
193
/**
194
 * PaginatorCustomPost class
195
 *
196
 * @package       Cake.Test.Case.Controller.Component
197
 */
198
class PaginatorCustomPost extends CakeTestModel {
199
 
200
/**
201
 * useTable property
202
 *
203
 * @var string
204
 */
205
	public $useTable = 'posts';
206
 
207
/**
208
 * belongsTo property
209
 *
210
 * @var string
211
 */
212
	public $belongsTo = array('Author');
213
 
214
/**
215
 * findMethods property
216
 *
217
 * @var array
218
 */
219
	public $findMethods = array(
220
		'published' => true,
221
		'totals' => true,
222
		'totalsOperation' => true
223
	);
224
 
225
/**
226
 * _findPublished custom find
227
 *
228
 * @return array
229
 */
230
	protected function _findPublished($state, $query, $results = array()) {
231
		if ($state === 'before') {
232
			$query['conditions']['published'] = 'Y';
233
			return $query;
234
		}
235
		return $results;
236
	}
237
 
238
/**
239
 * _findTotals custom find
240
 *
241
 * @return array
242
 */
243
	protected function _findTotals($state, $query, $results = array()) {
244
		if ($state === 'before') {
245
			$query['fields'] = array('author_id');
246
			$this->virtualFields['total_posts'] = "COUNT({$this->alias}.id)";
247
			$query['fields'][] = 'total_posts';
248
			$query['group'] = array('author_id');
249
			$query['order'] = array('author_id' => 'ASC');
250
			return $query;
251
		}
252
		$this->virtualFields = array();
253
		return $results;
254
	}
255
 
256
/**
257
 * _findTotalsOperation custom find
258
 *
259
 * @return array
260
 */
261
	protected function _findTotalsOperation($state, $query, $results = array()) {
262
		if ($state === 'before') {
263
			if (!empty($query['operation']) && $query['operation'] === 'count') {
264
				unset($query['limit']);
265
				$query['recursive'] = -1;
266
				$query['fields'] = array('COUNT(DISTINCT author_id) AS count');
267
				return $query;
268
			}
269
			$query['recursive'] = 0;
270
			$query['callbacks'] = 'before';
271
			$query['fields'] = array('author_id', 'Author.user');
272
			$this->virtualFields['total_posts'] = "COUNT({$this->alias}.id)";
273
			$query['fields'][] = 'total_posts';
274
			$query['group'] = array('author_id', 'Author.user');
275
			$query['order'] = array('author_id' => 'ASC');
276
			return $query;
277
		}
278
		$this->virtualFields = array();
279
		return $results;
280
	}
281
 
282
}
283
 
284
class PaginatorComponentTest extends CakeTestCase {
285
 
286
/**
287
 * fixtures property
288
 *
289
 * @var array
290
 */
291
	public $fixtures = array('core.post', 'core.comment', 'core.author');
292
 
293
/**
294
 * setup
295
 *
296
 * @return void
297
 */
298
	public function setUp() {
299
		parent::setUp();
300
		$this->request = new CakeRequest('controller_posts/index');
301
		$this->request->params['pass'] = $this->request->params['named'] = array();
302
		$this->Controller = new Controller($this->request);
303
		$this->Paginator = new PaginatorComponent($this->getMock('ComponentCollection'), array());
304
		$this->Paginator->Controller = $this->Controller;
305
		$this->Controller->Post = $this->getMock('Model');
306
		$this->Controller->Post->alias = 'Post';
307
	}
308
 
309
/**
310
 * testPaginate method
311
 *
312
 * @return void
313
 */
314
	public function testPaginate() {
315
		$Controller = new PaginatorTestController($this->request);
316
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
317
		$Controller->request->params['pass'] = array('1');
318
		$Controller->request->query = array();
319
		$Controller->constructClasses();
320
 
321
		$Controller->PaginatorControllerPost->order = null;
322
 
323
		$Controller->Paginator->settings = array(
324
			'order' => array('PaginatorControllerComment.id' => 'ASC')
325
		);
326
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
327
		$this->assertEquals(array(1, 2, 3, 4, 5, 6), $results);
328
 
329
		$Controller->Paginator->settings = array(
330
			'order' => array('PaginatorControllerPost.id' => 'ASC')
331
		);
332
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
333
		$this->assertEquals(array(1, 2, 3), $results);
334
 
335
		$Controller->modelClass = null;
336
 
337
		$Controller->uses[0] = 'Plugin.PaginatorControllerPost';
338
		$results = Hash::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
339
		$this->assertEquals(array(1, 2, 3), $results);
340
 
341
		$Controller->request->params['named'] = array('page' => '-1');
342
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
343
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
344
		$this->assertEquals(array(1, 2, 3), $results);
345
 
346
		$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'asc');
347
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
348
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
349
		$this->assertEquals(array(1, 2, 3), $results);
350
 
351
		$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'desc');
352
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
353
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
354
		$this->assertEquals(array(3, 2, 1), $results);
355
 
356
		$Controller->request->params['named'] = array('sort' => 'id', 'direction' => 'desc');
357
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
358
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
359
		$this->assertEquals(array(3, 2, 1), $results);
360
 
361
		$Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc');
362
		$Controller->Paginator->paginate('PaginatorControllerPost');
363
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
364
		$this->assertEquals(array(), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0], 'no order should be set.');
365
 
366
		$Controller->request->params['named'] = array(
367
			'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
368
		);
369
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
370
		$this->assertEquals(array('PaginatorControllerPost.author_id' => 'asc'), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0]);
371
		$this->assertEquals(array(1, 3, 2), $results);
372
 
373
		$Controller->request->params['named'] = array();
374
		$Controller->Paginator->settings = array('limit' => 0, 'maxLimit' => 10, 'paramType' => 'named');
375
		$Controller->Paginator->paginate('PaginatorControllerPost');
376
		$this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
377
		$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
378
		$this->assertFalse($Controller->params['paging']['PaginatorControllerPost']['prevPage']);
379
		$this->assertTrue($Controller->params['paging']['PaginatorControllerPost']['nextPage']);
380
 
381
		$Controller->request->params['named'] = array();
382
		$Controller->Paginator->settings = array('limit' => 'garbage!', 'maxLimit' => 10, 'paramType' => 'named');
383
		$Controller->Paginator->paginate('PaginatorControllerPost');
384
		$this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
385
		$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
386
		$this->assertFalse($Controller->params['paging']['PaginatorControllerPost']['prevPage']);
387
		$this->assertTrue($Controller->params['paging']['PaginatorControllerPost']['nextPage']);
388
 
389
		$Controller->request->params['named'] = array();
390
		$Controller->Paginator->settings = array('limit' => '-1', 'maxLimit' => 10, 'paramType' => 'named');
391
		$Controller->Paginator->paginate('PaginatorControllerPost');
392
 
393
		$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['limit'], 1);
394
		$this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
395
		$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
396
		$this->assertFalse($Controller->params['paging']['PaginatorControllerPost']['prevPage']);
397
		$this->assertTrue($Controller->params['paging']['PaginatorControllerPost']['nextPage']);
398
 
399
		$Controller->Paginator->settings = array('conditions' => array('PaginatorAuthor.user' => 'mariano'));
400
		$Controller->Paginator->paginate('PaginatorControllerPost');
401
 
402
		$this->assertSame(2, $Controller->params['paging']['PaginatorControllerPost']['count']);
403
	}
404
 
405
/**
406
 * Test that non-numeric values are rejected for page, and limit
407
 *
408
 * @return void
409
 */
410
	public function testPageParamCasting() {
411
		$this->Controller->Post->expects($this->at(0))
412
			->method('hasMethod')
413
			->with('paginate')
414
			->will($this->returnValue(false));
415
 
416
		$this->Controller->Post->expects($this->at(1))
417
			->method('find')
418
			->will($this->returnValue(array('stuff')));
419
 
420
		$this->Controller->Post->expects($this->at(2))
421
			->method('hasMethod')
422
			->with('paginateCount')
423
			->will($this->returnValue(false));
424
 
425
		$this->Controller->Post->expects($this->at(3))
426
			->method('find')
427
			->will($this->returnValue(2));
428
 
429
		$this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
430
		$this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
431
		$this->Paginator->paginate('Post');
432
		$this->assertSame(1, $this->request->params['paging']['Post']['page'], 'XSS exploit opened');
433
	}
434
 
435
/**
436
 * testPaginateExtraParams method
437
 *
438
 * @return void
439
 */
440
	public function testPaginateExtraParams() {
441
		$Controller = new PaginatorTestController($this->request);
442
 
443
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
444
		$Controller->request->params['pass'] = array('1');
445
		$Controller->params['url'] = array();
446
		$Controller->constructClasses();
447
 
448
		$Controller->request->params['named'] = array('page' => '-1', 'contain' => array('PaginatorControllerComment'));
449
		$Controller->Paginator->settings = array(
450
			'order' => array('PaginatorControllerPost.id' => 'ASC')
451
		);
452
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
453
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
454
		$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
455
		$this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
456
 
457
		$Controller->request->params['named'] = array('page' => '-1');
458
		$Controller->Paginator->settings = array(
459
			'PaginatorControllerPost' => array(
460
				'contain' => array('PaginatorControllerComment'),
461
				'maxLimit' => 10,
462
				'paramType' => 'named',
463
				'order' => array('PaginatorControllerPost.id' => 'ASC')
464
			),
465
		);
466
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
467
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
468
		$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
469
		$this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
470
 
471
		$Controller->Paginator->settings = array(
472
			'PaginatorControllerPost' => array(
473
				'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
474
			),
475
		);
476
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
477
		$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
478
		$this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[1]['conditions']);
479
 
480
		$Controller->request->params['named'] = array('limit' => 12);
481
		$Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
482
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
483
		$paging = $Controller->params['paging']['PaginatorControllerPost'];
484
 
485
		$this->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[1]['limit']);
486
		$this->assertEquals(12, $paging['options']['limit']);
487
 
488
		$Controller = new PaginatorTestController($this->request);
489
		$Controller->uses = array('ControllerPaginateModel');
490
		$Controller->request->query = array();
491
		$Controller->constructClasses();
492
		$Controller->Paginator->settings = array(
493
			'ControllerPaginateModel' => array(
494
				'contain' => array('ControllerPaginateModel'),
495
				'group' => 'Comment.author_id',
496
				'maxLimit' => 10,
497
				'paramType' => 'named'
498
			)
499
		);
500
		$result = $Controller->Paginator->paginate('ControllerPaginateModel');
501
		$expected = array(
502
			'contain' => array('ControllerPaginateModel'),
503
			'group' => 'Comment.author_id',
504
			'maxLimit' => 10,
505
			'paramType' => 'named'
506
		);
507
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
508
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
509
 
510
		$Controller->Paginator->settings = array(
511
			'ControllerPaginateModel' => array(
512
				'foo', 'contain' => array('ControllerPaginateModel'),
513
				'group' => 'Comment.author_id',
514
				'maxLimit' => 10,
515
				'paramType' => 'named'
516
			)
517
		);
518
		$Controller->Paginator->paginate('ControllerPaginateModel');
519
		$expected = array(
520
			'contain' => array('ControllerPaginateModel'),
521
			'group' => 'Comment.author_id',
522
			'type' => 'foo',
523
			'maxLimit' => 10,
524
			'paramType' => 'named'
525
		);
526
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
527
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
528
	}
529
 
530
/**
531
 * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
532
 *
533
 * @return void
534
 */
535
	public function testPaginateSpecialType() {
536
		$Controller = new PaginatorTestController($this->request);
537
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
538
		$Controller->request->params['pass'][] = '1';
539
		$Controller->params['url'] = array();
540
		$Controller->constructClasses();
541
 
542
		$Controller->Paginator->settings = array(
543
			'PaginatorControllerPost' => array(
544
				'popular',
545
				'fields' => array('id', 'title'),
546
				'maxLimit' => 10,
547
				'paramType' => 'named'
548
			)
549
		);
550
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
551
 
552
		$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
553
		$this->assertEquals(
554
			$Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
555
			array('PaginatorControllerPost.id > ' => '1')
556
		);
557
		$this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
558
	}
559
 
560
/**
561
 * testDefaultPaginateParams method
562
 *
563
 * @return void
564
 */
565
	public function testDefaultPaginateParams() {
566
		$Controller = new PaginatorTestController($this->request);
567
		$Controller->modelClass = 'PaginatorControllerPost';
568
		$Controller->params['url'] = array();
569
		$Controller->constructClasses();
570
		$Controller->Paginator->settings = array(
571
			'order' => 'PaginatorControllerPost.id DESC',
572
			'maxLimit' => 10,
573
			'paramType' => 'named'
574
		);
575
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
576
		$this->assertEquals('PaginatorControllerPost.id DESC', $Controller->params['paging']['PaginatorControllerPost']['order']);
577
		$this->assertEquals(array(3, 2, 1), $results);
578
	}
579
 
580
/**
581
 * test paginate() and model default order
582
 *
583
 * @return void
584
 */
585
	public function testPaginateOrderModelDefault() {
586
		$Controller = new PaginatorTestController($this->request);
587
		$Controller->uses = array('PaginatorControllerPost');
588
		$Controller->params['url'] = array();
589
		$Controller->constructClasses();
590
		$Controller->PaginatorControllerPost->order = array(
591
			$Controller->PaginatorControllerPost->alias . '.created' => 'desc'
592
		);
593
 
594
		$Controller->Paginator->settings = array(
595
			'fields' => array('id', 'title', 'created'),
596
			'maxLimit' => 10,
597
			'paramType' => 'named'
598
		);
599
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
600
		$expected = array('2007-03-18 10:43:23', '2007-03-18 10:41:23', '2007-03-18 10:39:23');
601
		$this->assertEquals($expected, Hash::extract($result, '{n}.PaginatorControllerPost.created'));
602
		$this->assertEquals(
603
			$Controller->PaginatorControllerPost->order,
604
			$Controller->request->paging['PaginatorControllerPost']['options']['order']
605
		);
606
 
607
		$Controller->PaginatorControllerPost->order = array('PaginatorControllerPost.id');
608
		$result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array());
609
		$this->assertEmpty($result['order']);
610
 
611
		$Controller->PaginatorControllerPost->order = 'PaginatorControllerPost.id';
612
		$results = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array());
613
		$this->assertEmpty($result['order']);
614
 
615
		$Controller->PaginatorControllerPost->order = array(
616
			'PaginatorControllerPost.id',
617
			'PaginatorControllerPost.created' => 'asc'
618
		);
619
		$result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array());
620
		$expected = array('PaginatorControllerPost.created' => 'asc');
621
		$this->assertEquals($expected, $result['order']);
622
	}
623
 
624
/**
625
 * test paginate() and virtualField interactions
626
 *
627
 * @return void
628
 */
629
	public function testPaginateOrderVirtualField() {
630
		$Controller = new PaginatorTestController($this->request);
631
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
632
		$Controller->params['url'] = array();
633
		$Controller->constructClasses();
634
		$Controller->PaginatorControllerPost->virtualFields = array(
635
			'offset_test' => 'PaginatorControllerPost.id + 1'
636
		);
637
 
638
		$Controller->Paginator->settings = array(
639
			'fields' => array('id', 'title', 'offset_test'),
640
			'order' => array('offset_test' => 'DESC'),
641
			'maxLimit' => 10,
642
			'paramType' => 'named'
643
		);
644
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
645
		$this->assertEquals(array(4, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
646
 
647
		$Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
648
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
649
		$this->assertEquals(array(2, 3, 4), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
650
	}
651
 
652
/**
653
 * test paginate() and virtualField on joined model
654
 *
655
 * @return void
656
 */
657
	public function testPaginateOrderVirtualFieldJoinedModel() {
658
		$Controller = new PaginatorTestController($this->request);
659
		$Controller->uses = array('PaginatorControllerPost');
660
		$Controller->params['url'] = array();
661
		$Controller->constructClasses();
662
		$Controller->PaginatorControllerPost->recursive = 0;
663
		$Controller->Paginator->settings = array(
664
			'order' => array('PaginatorAuthor.joined_offset' => 'DESC'),
665
			'maxLimit' => 10,
666
			'paramType' => 'named'
667
		);
668
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
669
		$this->assertEquals(array(4, 2, 2), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
670
 
671
		$Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
672
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
673
		$this->assertEquals(array(2, 2, 4), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
674
	}
675
 
676
/**
677
 * Tests for missing models
678
 *
679
 * @expectedException MissingModelException
680
 */
681
	public function testPaginateMissingModel() {
682
		$Controller = new PaginatorTestController($this->request);
683
		$Controller->constructClasses();
684
		$Controller->Paginator->paginate('MissingModel');
685
	}
686
 
687
/**
688
 * test that option merging prefers specific models
689
 *
690
 * @return void
691
 */
692
	public function testMergeOptionsModelSpecific() {
693
		$this->Paginator->settings = array(
694
			'page' => 1,
695
			'limit' => 20,
696
			'maxLimit' => 100,
697
			'paramType' => 'named',
698
			'Post' => array(
699
				'page' => 1,
700
				'limit' => 10,
701
				'maxLimit' => 50,
702
				'paramType' => 'named',
703
			)
704
		);
705
		$result = $this->Paginator->mergeOptions('Silly');
706
		$this->assertEquals($this->Paginator->settings, $result);
707
 
708
		$result = $this->Paginator->mergeOptions('Post');
709
		$expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
710
		$this->assertEquals($expected, $result);
711
	}
712
 
713
/**
714
 * test mergeOptions with named params.
715
 *
716
 * @return void
717
 */
718
	public function testMergeOptionsNamedParams() {
719
		$this->request->params['named'] = array(
720
			'page' => 10,
721
			'limit' => 10
722
		);
723
		$this->Paginator->settings = array(
724
			'page' => 1,
725
			'limit' => 20,
726
			'maxLimit' => 100,
727
			'paramType' => 'named',
728
		);
729
		$result = $this->Paginator->mergeOptions('Post');
730
		$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
731
		$this->assertEquals($expected, $result);
732
	}
733
 
734
/**
735
 * test mergeOptions with customFind key
736
 *
737
 * @return void
738
 */
739
	public function testMergeOptionsCustomFindKey() {
740
		$this->request->params['named'] = array(
741
			'page' => 10,
742
			'limit' => 10
743
		);
744
		$this->Paginator->settings = array(
745
			'page' => 1,
746
			'limit' => 20,
747
			'maxLimit' => 100,
748
			'paramType' => 'named',
749
			'findType' => 'myCustomFind'
750
		);
751
		$result = $this->Paginator->mergeOptions('Post');
752
		$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
753
		$this->assertEquals($expected, $result);
754
	}
755
 
756
/**
757
 * test merging options from the querystring.
758
 *
759
 * @return void
760
 */
761
	public function testMergeOptionsQueryString() {
762
		$this->request->params['named'] = array(
763
			'page' => 10,
764
			'limit' => 10
765
		);
766
		$this->request->query = array(
767
			'page' => 99,
768
			'limit' => 75
769
		);
770
		$this->Paginator->settings = array(
771
			'page' => 1,
772
			'limit' => 20,
773
			'maxLimit' => 100,
774
			'paramType' => 'querystring',
775
		);
776
		$result = $this->Paginator->mergeOptions('Post');
777
		$expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'paramType' => 'querystring');
778
		$this->assertEquals($expected, $result);
779
	}
780
 
781
/**
782
 * test that the default whitelist doesn't let people screw with things they should not be allowed to.
783
 *
784
 * @return void
785
 */
786
	public function testMergeOptionsDefaultWhiteList() {
787
		$this->request->params['named'] = array(
788
			'page' => 10,
789
			'limit' => 10,
790
			'fields' => array('bad.stuff'),
791
			'recursive' => 1000,
792
			'conditions' => array('bad.stuff'),
793
			'contain' => array('bad')
794
		);
795
		$this->Paginator->settings = array(
796
			'page' => 1,
797
			'limit' => 20,
798
			'maxLimit' => 100,
799
			'paramType' => 'named',
800
		);
801
		$result = $this->Paginator->mergeOptions('Post');
802
		$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
803
		$this->assertEquals($expected, $result);
804
	}
805
 
806
/**
807
 * test that modifying the whitelist works.
808
 *
809
 * @return void
810
 */
811
	public function testMergeOptionsExtraWhitelist() {
812
		$this->request->params['named'] = array(
813
			'page' => 10,
814
			'limit' => 10,
815
			'fields' => array('bad.stuff'),
816
			'recursive' => 1000,
817
			'conditions' => array('bad.stuff'),
818
			'contain' => array('bad')
819
		);
820
		$this->Paginator->settings = array(
821
			'page' => 1,
822
			'limit' => 20,
823
			'maxLimit' => 100,
824
			'paramType' => 'named',
825
		);
826
		$this->Paginator->whitelist[] = 'fields';
827
		$result = $this->Paginator->mergeOptions('Post');
828
		$expected = array(
829
			'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
830
		);
831
		$this->assertEquals($expected, $result);
832
	}
833
 
834
/**
835
 * test mergeOptions with limit > maxLimit in code.
836
 *
837
 * @return void
838
 */
839
	public function testMergeOptionsMaxLimit() {
840
		$this->Paginator->settings = array(
841
			'limit' => 200,
842
			'paramType' => 'named',
843
		);
844
		$result = $this->Paginator->mergeOptions('Post');
845
		$expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 200, 'paramType' => 'named');
846
		$this->assertEquals($expected, $result);
847
 
848
		$this->Paginator->settings = array(
849
			'maxLimit' => 10,
850
			'paramType' => 'named',
851
		);
852
		$result = $this->Paginator->mergeOptions('Post');
853
		$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
854
		$this->assertEquals($expected, $result);
855
 
856
		$this->request->params['named'] = array(
857
			'limit' => 500
858
		);
859
		$this->Paginator->settings = array(
860
			'limit' => 150,
861
			'paramType' => 'named',
862
		);
863
		$result = $this->Paginator->mergeOptions('Post');
864
		$expected = array('page' => 1, 'limit' => 500, 'maxLimit' => 150, 'paramType' => 'named');
865
		$this->assertEquals($expected, $result);
866
	}
867
 
868
/**
869
 * test that invalid directions are ignored.
870
 *
871
 * @return void
872
 */
873
	public function testValidateSortInvalidDirection() {
874
		$model = $this->getMock('Model');
875
		$model->alias = 'model';
876
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
877
 
878
		$options = array('sort' => 'something', 'direction' => 'boogers');
879
		$result = $this->Paginator->validateSort($model, $options);
880
 
881
		$this->assertEquals('asc', $result['order']['model.something']);
882
	}
883
 
884
/**
885
 * Test that a really large page number gets clamped to the max page size.
886
 *
887
 * @expectedException NotFoundException
888
 * @return void
889
 */
890
	public function testOutOfRangePageNumberGetsClamped() {
891
		$Controller = new PaginatorTestController($this->request);
892
		$Controller->uses = array('PaginatorControllerPost');
893
		$Controller->params['named'] = array(
894
			'page' => 3000,
895
		);
896
		$Controller->constructClasses();
897
		$Controller->PaginatorControllerPost->recursive = 0;
898
		$Controller->Paginator->paginate('PaginatorControllerPost');
899
	}
900
 
901
/**
902
 * Test that a really REALLY large page number gets clamped to the max page size.
903
 *
904
 *
905
 * @expectedException NotFoundException
906
 * @return void
907
 */
908
	public function testOutOfVeryBigPageNumberGetsClamped() {
909
		$Controller = new PaginatorTestController($this->request);
910
		$Controller->uses = array('PaginatorControllerPost');
911
		$Controller->params['named'] = array(
912
			'page' => '3000000000000000000000000',
913
		);
914
		$Controller->constructClasses();
915
		$Controller->PaginatorControllerPost->recursive = 0;
916
		$Controller->Paginator->paginate('PaginatorControllerPost');
917
	}
918
 
919
/**
920
 * testOutOfRangePageNumberAndPageCountZero
921
 *
922
 * @return void
923
 */
924
	public function testOutOfRangePageNumberAndPageCountZero() {
925
		$Controller = new PaginatorTestController($this->request);
926
		$Controller->uses = array('PaginatorControllerPost');
927
		$Controller->params['named'] = array(
928
			'page' => '3000',
929
		);
930
		$Controller->constructClasses();
931
		$Controller->PaginatorControllerPost->recursive = 0;
932
		$Controller->paginate = array(
933
			'conditions' => array('PaginatorControllerPost.id >' => 100)
934
		);
935
 
936
		try {
937
			$Controller->Paginator->paginate('PaginatorControllerPost');
938
			$this->fail();
939
		} catch (NotFoundException $e) {
940
			$this->assertEquals(
941
				1,
942
				$Controller->request->params['paging']['PaginatorControllerPost']['page'],
943
				'Page number should not be 0'
944
			);
945
		}
946
	}
947
 
948
/**
949
 * test that fields not in whitelist won't be part of order conditions.
950
 *
951
 * @return void
952
 */
953
	public function testValidateSortWhitelistFailure() {
954
		$model = $this->getMock('Model');
955
		$model->alias = 'model';
956
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
957
 
958
		$options = array('sort' => 'body', 'direction' => 'asc');
959
		$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
960
 
961
		$this->assertNull($result['order']);
962
	}
963
 
964
/**
965
 * test that fields in the whitelist are not validated
966
 *
967
 * @return void
968
 */
969
	public function testValidateSortWhitelistTrusted() {
970
		$model = $this->getMock('Model');
971
		$model->alias = 'model';
972
		$model->expects($this->never())->method('hasField');
973
 
974
		$options = array('sort' => 'body', 'direction' => 'asc');
975
		$result = $this->Paginator->validateSort($model, $options, array('body'));
976
 
977
		$expected = array('body' => 'asc');
978
		$this->assertEquals($expected, $result['order']);
979
	}
980
 
981
/**
982
 * test that virtual fields work.
983
 *
984
 * @return void
985
 */
986
	public function testValidateSortVirtualField() {
987
		$model = $this->getMock('Model');
988
		$model->alias = 'model';
989
 
990
		$model->expects($this->at(0))
991
			->method('hasField')
992
			->with('something')
993
			->will($this->returnValue(false));
994
 
995
		$model->expects($this->at(1))
996
			->method('hasField')
997
			->with('something', true)
998
			->will($this->returnValue(true));
999
 
1000
		$options = array('sort' => 'something', 'direction' => 'desc');
1001
		$result = $this->Paginator->validateSort($model, $options);
1002
 
1003
		$this->assertEquals('desc', $result['order']['something']);
1004
	}
1005
 
1006
/**
1007
 * test that sorting fields is alias specific
1008
 *
1009
 * @return void
1010
 */
1011
	public function testValidateSortSharedFields() {
1012
		$model = $this->getMock('Model');
1013
		$model->alias = 'Parent';
1014
		$model->Child = $this->getMock('Model');
1015
		$model->Child->alias = 'Child';
1016
 
1017
		$model->expects($this->never())
1018
			->method('hasField');
1019
 
1020
		$model->Child->expects($this->at(0))
1021
			->method('hasField')
1022
			->with('something')
1023
			->will($this->returnValue(true));
1024
 
1025
		$options = array('sort' => 'Child.something', 'direction' => 'desc');
1026
		$result = $this->Paginator->validateSort($model, $options);
1027
 
1028
		$this->assertEquals('desc', $result['order']['Child.something']);
1029
	}
1030
/**
1031
 * test that multiple sort works.
1032
 *
1033
 * @return void
1034
 */
1035
	public function testValidateSortMultiple() {
1036
		$model = $this->getMock('Model');
1037
		$model->alias = 'model';
1038
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
1039
 
1040
		$options = array(
1041
			'order' => array(
1042
				'author_id' => 'asc',
1043
				'title' => 'asc'
1044
			)
1045
		);
1046
		$result = $this->Paginator->validateSort($model, $options);
1047
		$expected = array(
1048
			'model.author_id' => 'asc',
1049
			'model.title' => 'asc'
1050
		);
1051
 
1052
		$this->assertEquals($expected, $result['order']);
1053
	}
1054
 
1055
/**
1056
 * Test that no sort doesn't trigger an error.
1057
 *
1058
 * @return void
1059
 */
1060
	public function testValidateSortNoSort() {
1061
		$model = $this->getMock('Model');
1062
		$model->alias = 'model';
1063
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
1064
 
1065
		$options = array('direction' => 'asc');
1066
		$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
1067
		$this->assertFalse(isset($result['order']));
1068
 
1069
		$options = array('order' => 'invalid desc');
1070
		$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
1071
 
1072
		$this->assertEquals($options['order'], $result['order']);
1073
	}
1074
 
1075
/**
1076
 * Test sorting with incorrect aliases on valid fields.
1077
 *
1078
 * @return void
1079
 */
1080
	public function testValidateSortInvalidAlias() {
1081
		$model = $this->getMock('Model');
1082
		$model->alias = 'Model';
1083
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
1084
 
1085
		$options = array('sort' => 'Derp.id');
1086
		$result = $this->Paginator->validateSort($model, $options);
1087
		$this->assertEquals(array(), $result['order']);
1088
	}
1089
 
1090
/**
1091
 * test that maxLimit is respected
1092
 *
1093
 * @return void
1094
 */
1095
	public function testCheckLimit() {
1096
		$result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
1097
		$this->assertEquals(100, $result['limit']);
1098
 
1099
		$result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
1100
		$this->assertEquals(1, $result['limit']);
1101
 
1102
		$result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
1103
		$this->assertEquals(1, $result['limit']);
1104
 
1105
		$result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
1106
		$this->assertEquals(1, $result['limit']);
1107
 
1108
		$result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
1109
		$this->assertEquals(1, $result['limit']);
1110
	}
1111
 
1112
/**
1113
 * testPaginateMaxLimit
1114
 *
1115
 * @return void
1116
 */
1117
	public function testPaginateMaxLimit() {
1118
		$Controller = new Controller($this->request);
1119
 
1120
		$Controller->uses = array('PaginatorControllerPost', 'ControllerComment');
1121
		$Controller->request->params['pass'][] = '1';
1122
		$Controller->constructClasses();
1123
 
1124
		$Controller->request->params['named'] = array(
1125
			'contain' => array('ControllerComment'), 'limit' => '1000'
1126
		);
1127
		$result = $Controller->paginate('PaginatorControllerPost');
1128
		$this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1129
 
1130
		$Controller->request->params['named'] = array(
1131
			'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
1132
		);
1133
		$result = $Controller->paginate('PaginatorControllerPost');
1134
		$this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1135
 
1136
		$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
1137
		$result = $Controller->paginate('PaginatorControllerPost');
1138
		$this->assertEquals(10, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1139
 
1140
		$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
1141
		$Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
1142
		$result = $Controller->paginate('PaginatorControllerPost');
1143
		$this->assertEquals(1000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1144
 
1145
		$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
1146
		$result = $Controller->paginate('PaginatorControllerPost');
1147
		$this->assertEquals(2000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1148
	}
1149
 
1150
/**
1151
 * test paginate() and virtualField overlapping with real fields.
1152
 *
1153
 * @return void
1154
 */
1155
	public function testPaginateOrderVirtualFieldSharedWithRealField() {
1156
		$Controller = new Controller($this->request);
1157
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
1158
		$Controller->constructClasses();
1159
		$Controller->PaginatorControllerComment->virtualFields = array(
1160
			'title' => 'PaginatorControllerComment.comment'
1161
		);
1162
		$Controller->PaginatorControllerComment->bindModel(array(
1163
			'belongsTo' => array(
1164
				'PaginatorControllerPost' => array(
1165
					'className' => 'PaginatorControllerPost',
1166
					'foreignKey' => 'article_id'
1167
				)
1168
			)
1169
		), false);
1170
 
1171
		$Controller->paginate = array(
1172
			'fields' => array(
1173
				'PaginatorControllerComment.id',
1174
				'title',
1175
				'PaginatorControllerPost.title'
1176
			),
1177
		);
1178
		$Controller->request->params['named'] = array(
1179
			'sort' => 'PaginatorControllerPost.title',
1180
			'direction' => 'desc'
1181
		);
1182
		$result = Hash::extract(
1183
			$Controller->paginate('PaginatorControllerComment'),
1184
			'{n}.PaginatorControllerComment.id'
1185
		);
1186
		$result1 = array_splice($result, 0, 2);
1187
		sort($result1);
1188
		$this->assertEquals(array(5, 6), $result1);
1189
 
1190
		sort($result);
1191
		$this->assertEquals(array(1, 2, 3, 4), $result);
1192
	}
1193
 
1194
/**
1195
 * test paginate() and custom find, to make sure the correct count is returned.
1196
 *
1197
 * @return void
1198
 */
1199
	public function testPaginateCustomFind() {
1200
		$Controller = new Controller($this->request);
1201
		$Controller->uses = array('PaginatorCustomPost');
1202
		$Controller->constructClasses();
1203
		$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1204
		$Controller->PaginatorCustomPost->create($data);
1205
		$result = $Controller->PaginatorCustomPost->save();
1206
		$this->assertTrue(!empty($result));
1207
 
1208
		$result = $Controller->paginate();
1209
		$this->assertEquals(array(1, 2, 3, 4), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
1210
 
1211
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1212
		$this->assertEquals(4, $result['current']);
1213
		$this->assertEquals(4, $result['count']);
1214
 
1215
		$Controller->paginate = array('published');
1216
		$result = $Controller->paginate();
1217
		$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
1218
 
1219
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1220
		$this->assertEquals(3, $result['current']);
1221
		$this->assertEquals(3, $result['count']);
1222
 
1223
		$Controller->paginate = array('published', 'limit' => 2);
1224
		$result = $Controller->paginate();
1225
		$this->assertEquals(array(1, 2), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
1226
 
1227
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1228
		$this->assertEquals(2, $result['current']);
1229
		$this->assertEquals(3, $result['count']);
1230
		$this->assertEquals(2, $result['pageCount']);
1231
		$this->assertTrue($result['nextPage']);
1232
		$this->assertFalse($result['prevPage']);
1233
	}
1234
/**
1235
 * test paginate() and custom find with fields array, to make sure the correct count is returned.
1236
 *
1237
 * @return void
1238
 */
1239
	public function testPaginateCustomFindFieldsArray() {
1240
		$Controller = new Controller($this->request);
1241
		$Controller->uses = array('PaginatorCustomPost');
1242
		$Controller->constructClasses();
1243
		$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1244
		$Controller->PaginatorCustomPost->create($data);
1245
		$result = $Controller->PaginatorCustomPost->save();
1246
		$this->assertTrue(!empty($result));
1247
 
1248
		$Controller->paginate = array(
1249
			'list',
1250
			'conditions' => array('PaginatorCustomPost.published' => 'Y'),
1251
			'limit' => 2
1252
		);
1253
		$result = $Controller->paginate();
1254
		$expected = array(
1255
			1 => 'First Post',
1256
			2 => 'Second Post',
1257
		);
1258
		$this->assertEquals($expected, $result);
1259
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1260
		$this->assertEquals(2, $result['current']);
1261
		$this->assertEquals(3, $result['count']);
1262
		$this->assertEquals(2, $result['pageCount']);
1263
		$this->assertTrue($result['nextPage']);
1264
		$this->assertFalse($result['prevPage']);
1265
	}
1266
/**
1267
 * test paginate() and custom find with customFind key, to make sure the correct count is returned.
1268
 *
1269
 * @return void
1270
 */
1271
	public function testPaginateCustomFindWithCustomFindKey() {
1272
		$Controller = new Controller($this->request);
1273
		$Controller->uses = array('PaginatorCustomPost');
1274
		$Controller->constructClasses();
1275
		$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1276
		$Controller->PaginatorCustomPost->create($data);
1277
		$result = $Controller->PaginatorCustomPost->save();
1278
		$this->assertTrue(!empty($result));
1279
 
1280
		$Controller->paginate = array(
1281
			'conditions' => array('PaginatorCustomPost.published' => 'Y'),
1282
			'findType' => 'list',
1283
			'limit' => 2
1284
		);
1285
		$result = $Controller->paginate();
1286
		$expected = array(
1287
			1 => 'First Post',
1288
			2 => 'Second Post',
1289
		);
1290
		$this->assertEquals($expected, $result);
1291
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1292
		$this->assertEquals(2, $result['current']);
1293
		$this->assertEquals(3, $result['count']);
1294
		$this->assertEquals(2, $result['pageCount']);
1295
		$this->assertTrue($result['nextPage']);
1296
		$this->assertFalse($result['prevPage']);
1297
	}
1298
 
1299
/**
1300
 * test paginate() and custom find with fields array, to make sure the correct count is returned.
1301
 *
1302
 * @return void
1303
 */
1304
	public function testPaginateCustomFindGroupBy() {
1305
		$Controller = new Controller($this->request);
1306
		$Controller->uses = array('PaginatorCustomPost');
1307
		$Controller->constructClasses();
1308
		$data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1309
		$Controller->PaginatorCustomPost->create($data);
1310
		$result = $Controller->PaginatorCustomPost->save();
1311
		$this->assertTrue(!empty($result));
1312
 
1313
		$Controller->paginate = array(
1314
			'totals',
1315
			'limit' => 2
1316
		);
1317
		$result = $Controller->paginate();
1318
		$expected = array(
1319
			array(
1320
				'PaginatorCustomPost' => array(
1321
					'author_id' => '1',
1322
					'total_posts' => '2'
1323
				)
1324
			),
1325
			array(
1326
				'PaginatorCustomPost' => array(
1327
					'author_id' => '2',
1328
					'total_posts' => '1'
1329
				)
1330
			)
1331
		);
1332
		$this->assertEquals($expected, $result);
1333
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1334
		$this->assertEquals(2, $result['current']);
1335
		$this->assertEquals(3, $result['count']);
1336
		$this->assertEquals(2, $result['pageCount']);
1337
		$this->assertTrue($result['nextPage']);
1338
		$this->assertFalse($result['prevPage']);
1339
 
1340
		$Controller->paginate = array(
1341
			'totals',
1342
			'limit' => 2,
1343
			'page' => 2
1344
		);
1345
		$result = $Controller->paginate();
1346
		$expected = array(
1347
			array(
1348
				'PaginatorCustomPost' => array(
1349
					'author_id' => '3',
1350
					'total_posts' => '1'
1351
				)
1352
			),
1353
		);
1354
		$this->assertEquals($expected, $result);
1355
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1356
		$this->assertEquals(1, $result['current']);
1357
		$this->assertEquals(3, $result['count']);
1358
		$this->assertEquals(2, $result['pageCount']);
1359
		$this->assertFalse($result['nextPage']);
1360
		$this->assertTrue($result['prevPage']);
1361
	}
1362
 
1363
/**
1364
 * test paginate() and custom find with returning other query on count operation,
1365
 * to make sure the correct count is returned.
1366
 *
1367
 * @return void
1368
 */
1369
	public function testPaginateCustomFindCount() {
1370
		$Controller = new Controller($this->request);
1371
		$Controller->uses = array('PaginatorCustomPost');
1372
		$Controller->constructClasses();
1373
		$data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1374
		$Controller->PaginatorCustomPost->create($data);
1375
		$result = $Controller->PaginatorCustomPost->save();
1376
		$this->assertTrue(!empty($result));
1377
 
1378
		$Controller->paginate = array(
1379
			'totalsOperation',
1380
			'limit' => 2
1381
		);
1382
		$result = $Controller->paginate();
1383
		$expected = array(
1384
			array(
1385
				'PaginatorCustomPost' => array(
1386
					'author_id' => '1',
1387
					'total_posts' => '2'
1388
				),
1389
				'Author' => array(
1390
					'user' => 'mariano',
1391
				)
1392
			),
1393
			array(
1394
				'PaginatorCustomPost' => array(
1395
					'author_id' => '2',
1396
					'total_posts' => '1'
1397
				),
1398
				'Author' => array(
1399
					'user' => 'nate'
1400
				)
1401
			)
1402
		);
1403
		$this->assertEquals($expected, $result);
1404
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1405
		$this->assertEquals(2, $result['current']);
1406
		$this->assertEquals(3, $result['count']);
1407
		$this->assertEquals(2, $result['pageCount']);
1408
		$this->assertTrue($result['nextPage']);
1409
		$this->assertFalse($result['prevPage']);
1410
	}
1411
}