Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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 bool
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', 'limit' => 2);
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[0]['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->Paginator->settings = array(
458
			'order' => array('PaginatorControllerPost.author_id')
459
		);
460
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
461
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
462
		$this->assertEquals(array(1, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
463
 
464
		$Controller->request->params['named'] = array('page' => '-1');
465
		$Controller->Paginator->settings = array(
466
			'PaginatorControllerPost' => array(
467
				'contain' => array('PaginatorControllerComment'),
468
				'maxLimit' => 10,
469
				'paramType' => 'named',
470
				'order' => array('PaginatorControllerPost.id' => 'ASC')
471
			),
472
		);
473
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
474
		$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
475
		$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
476
		$this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[0]['contain']));
477
 
478
		$Controller->Paginator->settings = array(
479
			'PaginatorControllerPost' => array(
480
				'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
481
			),
482
		);
483
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
484
		$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
485
		$this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[0]['conditions']);
486
 
487
		$Controller->request->params['named'] = array('limit' => 12);
488
		$Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
489
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
490
		$paging = $Controller->params['paging']['PaginatorControllerPost'];
491
 
492
		$this->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[0]['limit']);
493
		$this->assertEquals(12, $paging['options']['limit']);
494
 
495
		$Controller = new PaginatorTestController($this->request);
496
		$Controller->uses = array('ControllerPaginateModel');
497
		$Controller->request->query = array();
498
		$Controller->constructClasses();
499
		$Controller->Paginator->settings = array(
500
			'ControllerPaginateModel' => array(
501
				'contain' => array('ControllerPaginateModel'),
502
				'group' => 'Comment.author_id',
503
				'maxLimit' => 10,
504
				'paramType' => 'named'
505
			)
506
		);
507
		$result = $Controller->Paginator->paginate('ControllerPaginateModel');
508
		$expected = array(
509
			'contain' => array('ControllerPaginateModel'),
510
			'group' => 'Comment.author_id',
511
			'maxLimit' => 10,
512
			'paramType' => 'named'
513
		);
514
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
515
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
516
 
517
		$Controller->Paginator->settings = array(
518
			'ControllerPaginateModel' => array(
519
				'foo', 'contain' => array('ControllerPaginateModel'),
520
				'group' => 'Comment.author_id',
521
				'maxLimit' => 10,
522
				'paramType' => 'named'
523
			)
524
		);
525
		$Controller->Paginator->paginate('ControllerPaginateModel');
526
		$expected = array(
527
			'contain' => array('ControllerPaginateModel'),
528
			'group' => 'Comment.author_id',
529
			'type' => 'foo',
530
			'maxLimit' => 10,
531
			'paramType' => 'named'
532
		);
533
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
534
		$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
535
	}
536
 
537
/**
538
 * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
539
 *
540
 * @return void
541
 */
542
	public function testPaginateSpecialType() {
543
		$Controller = new PaginatorTestController($this->request);
544
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
545
		$Controller->request->params['pass'][] = '1';
546
		$Controller->params['url'] = array();
547
		$Controller->constructClasses();
548
 
549
		$Controller->Paginator->settings = array(
550
			'PaginatorControllerPost' => array(
551
				'popular',
552
				'fields' => array('id', 'title'),
553
				'maxLimit' => 10,
554
				'paramType' => 'named'
555
			)
556
		);
557
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
558
 
559
		$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
560
		$this->assertEquals(
561
			$Controller->PaginatorControllerPost->lastQueries[0]['conditions'],
562
			array('PaginatorControllerPost.id > ' => '1')
563
		);
564
		$this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
565
	}
566
 
567
/**
568
 * testDefaultPaginateParams method
569
 *
570
 * @return void
571
 */
572
	public function testDefaultPaginateParams() {
573
		$Controller = new PaginatorTestController($this->request);
574
		$Controller->modelClass = 'PaginatorControllerPost';
575
		$Controller->params['url'] = array();
576
		$Controller->constructClasses();
577
		$Controller->Paginator->settings = array(
578
			'order' => 'PaginatorControllerPost.id DESC',
579
			'maxLimit' => 10,
580
			'paramType' => 'named'
581
		);
582
		$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
583
		$this->assertEquals('PaginatorControllerPost.id DESC', $Controller->params['paging']['PaginatorControllerPost']['order']);
584
		$this->assertEquals(array(3, 2, 1), $results);
585
	}
586
 
587
/**
588
 * test paginate() and model default order
589
 *
590
 * @return void
591
 */
592
	public function testPaginateOrderModelDefault() {
593
		$Controller = new PaginatorTestController($this->request);
594
		$Controller->uses = array('PaginatorControllerPost');
595
		$Controller->params['url'] = array();
596
		$Controller->constructClasses();
597
		$Controller->PaginatorControllerPost->order = array(
598
			$Controller->PaginatorControllerPost->alias . '.created' => 'desc'
599
		);
600
 
601
		$Controller->Paginator->settings = array(
602
			'fields' => array('id', 'title', 'created'),
603
			'maxLimit' => 10,
604
			'paramType' => 'named'
605
		);
606
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
607
		$expected = array('2007-03-18 10:43:23', '2007-03-18 10:41:23', '2007-03-18 10:39:23');
608
		$this->assertEquals($expected, Hash::extract($result, '{n}.PaginatorControllerPost.created'));
609
		$this->assertEquals(
610
			$Controller->PaginatorControllerPost->order,
611
			$Controller->request->paging['PaginatorControllerPost']['options']['order']
612
		);
613
 
614
		$Controller->PaginatorControllerPost->order = array('PaginatorControllerPost.id');
615
		$result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array());
616
		$this->assertEquals(array('PaginatorControllerPost.id' => 'asc'), $result['order']);
617
 
618
		$Controller->PaginatorControllerPost->order = 'PaginatorControllerPost.id';
619
		$result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array());
620
		$this->assertArrayNotHasKey('order', $result);
621
 
622
		$Controller->PaginatorControllerPost->order = array(
623
			'PaginatorControllerPost.id',
624
			'PaginatorControllerPost.created' => 'asc'
625
		);
626
		$result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array());
627
		$expected = array(
628
			'PaginatorControllerPost.id' => 'asc',
629
			'PaginatorControllerPost.created' => 'asc'
630
		);
631
		$this->assertEquals($expected, $result['order']);
632
	}
633
 
634
/**
635
 * test paginate() and virtualField interactions
636
 *
637
 * @return void
638
 */
639
	public function testPaginateOrderVirtualField() {
640
		$Controller = new PaginatorTestController($this->request);
641
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
642
		$Controller->params['url'] = array();
643
		$Controller->constructClasses();
644
		$Controller->PaginatorControllerPost->virtualFields = array(
645
			'offset_test' => 'PaginatorControllerPost.id + 1'
646
		);
647
 
648
		$Controller->Paginator->settings = array(
649
			'fields' => array('id', 'title', 'offset_test'),
650
			'order' => array('offset_test' => 'DESC'),
651
			'maxLimit' => 10,
652
			'paramType' => 'named'
653
		);
654
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
655
		$this->assertEquals(array(4, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
656
 
657
		$Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
658
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
659
		$this->assertEquals(array(2, 3, 4), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
660
	}
661
 
662
/**
663
 * test paginate() and virtualField on joined model
664
 *
665
 * @return void
666
 */
667
	public function testPaginateOrderVirtualFieldJoinedModel() {
668
		$Controller = new PaginatorTestController($this->request);
669
		$Controller->uses = array('PaginatorControllerPost');
670
		$Controller->params['url'] = array();
671
		$Controller->constructClasses();
672
		$Controller->PaginatorControllerPost->recursive = 0;
673
		$Controller->Paginator->settings = array(
674
			'order' => array('PaginatorAuthor.joined_offset' => 'DESC'),
675
			'maxLimit' => 10,
676
			'paramType' => 'named'
677
		);
678
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
679
		$this->assertEquals(array(4, 2, 2), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
680
 
681
		$Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
682
		$result = $Controller->Paginator->paginate('PaginatorControllerPost');
683
		$this->assertEquals(array(2, 2, 4), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
684
	}
685
 
686
/**
687
 * Tests for missing models
688
 *
689
 * @expectedException MissingModelException
690
 * @return void
691
 */
692
	public function testPaginateMissingModel() {
693
		$Controller = new PaginatorTestController($this->request);
694
		$Controller->constructClasses();
695
		$Controller->Paginator->paginate('MissingModel');
696
	}
697
 
698
/**
699
 * test that option merging prefers specific models
700
 *
701
 * @return void
702
 */
703
	public function testMergeOptionsModelSpecific() {
704
		$this->Paginator->settings = array(
705
			'page' => 1,
706
			'limit' => 20,
707
			'maxLimit' => 100,
708
			'paramType' => 'named',
709
			'Post' => array(
710
				'page' => 1,
711
				'limit' => 10,
712
				'maxLimit' => 50,
713
				'paramType' => 'named',
714
			)
715
		);
716
		$result = $this->Paginator->mergeOptions('Silly');
717
		$this->assertEquals($this->Paginator->settings, $result);
718
 
719
		$result = $this->Paginator->mergeOptions('Post');
720
		$expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
721
		$this->assertEquals($expected, $result);
722
	}
723
 
724
/**
725
 * test mergeOptions with named params.
726
 *
727
 * @return void
728
 */
729
	public function testMergeOptionsNamedParams() {
730
		$this->request->params['named'] = array(
731
			'page' => 10,
732
			'limit' => 10
733
		);
734
		$this->Paginator->settings = array(
735
			'page' => 1,
736
			'limit' => 20,
737
			'maxLimit' => 100,
738
			'paramType' => 'named',
739
		);
740
		$result = $this->Paginator->mergeOptions('Post');
741
		$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
742
		$this->assertEquals($expected, $result);
743
	}
744
 
745
/**
746
 * test mergeOptions with customFind key
747
 *
748
 * @return void
749
 */
750
	public function testMergeOptionsCustomFindKey() {
751
		$this->request->params['named'] = array(
752
			'page' => 10,
753
			'limit' => 10
754
		);
755
		$this->Paginator->settings = array(
756
			'page' => 1,
757
			'limit' => 20,
758
			'maxLimit' => 100,
759
			'paramType' => 'named',
760
			'findType' => 'myCustomFind'
761
		);
762
		$result = $this->Paginator->mergeOptions('Post');
763
		$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
764
		$this->assertEquals($expected, $result);
765
	}
766
 
767
/**
768
 * test merging options from the querystring.
769
 *
770
 * @return void
771
 */
772
	public function testMergeOptionsQueryString() {
773
		$this->request->params['named'] = array(
774
			'page' => 10,
775
			'limit' => 10
776
		);
777
		$this->request->query = array(
778
			'page' => 99,
779
			'limit' => 75
780
		);
781
		$this->Paginator->settings = array(
782
			'page' => 1,
783
			'limit' => 20,
784
			'maxLimit' => 100,
785
			'paramType' => 'querystring',
786
		);
787
		$result = $this->Paginator->mergeOptions('Post');
788
		$expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'paramType' => 'querystring');
789
		$this->assertEquals($expected, $result);
790
	}
791
 
792
/**
793
 * test that the default whitelist doesn't let people screw with things they should not be allowed to.
794
 *
795
 * @return void
796
 */
797
	public function testMergeOptionsDefaultWhiteList() {
798
		$this->request->params['named'] = array(
799
			'page' => 10,
800
			'limit' => 10,
801
			'fields' => array('bad.stuff'),
802
			'recursive' => 1000,
803
			'conditions' => array('bad.stuff'),
804
			'contain' => array('bad')
805
		);
806
		$this->Paginator->settings = array(
807
			'page' => 1,
808
			'limit' => 20,
809
			'maxLimit' => 100,
810
			'paramType' => 'named',
811
		);
812
		$result = $this->Paginator->mergeOptions('Post');
813
		$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
814
		$this->assertEquals($expected, $result);
815
	}
816
 
817
/**
818
 * test that modifying the whitelist works.
819
 *
820
 * @return void
821
 */
822
	public function testMergeOptionsExtraWhitelist() {
823
		$this->request->params['named'] = array(
824
			'page' => 10,
825
			'limit' => 10,
826
			'fields' => array('bad.stuff'),
827
			'recursive' => 1000,
828
			'conditions' => array('bad.stuff'),
829
			'contain' => array('bad')
830
		);
831
		$this->Paginator->settings = array(
832
			'page' => 1,
833
			'limit' => 20,
834
			'maxLimit' => 100,
835
			'paramType' => 'named',
836
		);
837
		$this->Paginator->whitelist[] = 'fields';
838
		$result = $this->Paginator->mergeOptions('Post');
839
		$expected = array(
840
			'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
841
		);
842
		$this->assertEquals($expected, $result);
843
	}
844
 
845
/**
846
 * test mergeOptions with limit > maxLimit in code.
847
 *
848
 * @return void
849
 */
850
	public function testMergeOptionsMaxLimit() {
851
		$this->Paginator->settings = array(
852
			'limit' => 200,
853
			'paramType' => 'named',
854
		);
855
		$result = $this->Paginator->mergeOptions('Post');
856
		$expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 100, 'paramType' => 'named');
857
		$this->assertEquals($expected, $result);
858
 
859
		$this->Paginator->settings = array(
860
			'maxLimit' => 10,
861
			'paramType' => 'named',
862
		);
863
		$result = $this->Paginator->mergeOptions('Post');
864
		$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
865
		$this->assertEquals($expected, $result);
866
 
867
		$this->request->params['named'] = array(
868
			'limit' => 500
869
		);
870
		$this->Paginator->settings = array(
871
			'limit' => 150,
872
			'paramType' => 'named',
873
		);
874
		$result = $this->Paginator->mergeOptions('Post');
875
		$expected = array('page' => 1, 'limit' => 500, 'maxLimit' => 100, 'paramType' => 'named');
876
		$this->assertEquals($expected, $result);
877
	}
878
 
879
/**
880
 * test that invalid directions are ignored.
881
 *
882
 * @return void
883
 */
884
	public function testValidateSortInvalidDirection() {
885
		$model = $this->getMock('Model');
886
		$model->alias = 'model';
887
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
888
 
889
		$options = array('sort' => 'something', 'direction' => 'boogers');
890
		$result = $this->Paginator->validateSort($model, $options);
891
 
892
		$this->assertEquals('asc', $result['order']['model.something']);
893
	}
894
 
895
/**
896
 * Test that a really large page number gets clamped to the max page size.
897
 *
898
 * @expectedException NotFoundException
899
 * @return void
900
 */
901
	public function testOutOfRangePageNumberGetsClamped() {
902
		$Controller = new PaginatorTestController($this->request);
903
		$Controller->uses = array('PaginatorControllerPost');
904
		$Controller->params['named'] = array(
905
			'page' => 3000,
906
		);
907
		$Controller->constructClasses();
908
		$Controller->PaginatorControllerPost->recursive = 0;
909
		$Controller->Paginator->paginate('PaginatorControllerPost');
910
	}
911
 
912
/**
913
 * Test that a really REALLY large page number gets clamped to the max page size.
914
 *
915
 * @expectedException NotFoundException
916
 * @return void
917
 */
918
	public function testOutOfVeryBigPageNumberGetsClamped() {
919
		$Controller = new PaginatorTestController($this->request);
920
		$Controller->uses = array('PaginatorControllerPost');
921
		$Controller->params['named'] = array(
922
			'page' => '3000000000000000000000000',
923
		);
924
		$Controller->constructClasses();
925
		$Controller->PaginatorControllerPost->recursive = 0;
926
		$Controller->Paginator->paginate('PaginatorControllerPost');
927
	}
928
 
929
/**
930
 * testOutOfRangePageNumberAndPageCountZero
931
 *
932
 * @return void
933
 */
934
	public function testOutOfRangePageNumberAndPageCountZero() {
935
		$Controller = new PaginatorTestController($this->request);
936
		$Controller->uses = array('PaginatorControllerPost');
937
		$Controller->params['named'] = array(
938
			'page' => '3000',
939
		);
940
		$Controller->constructClasses();
941
		$Controller->PaginatorControllerPost->recursive = 0;
942
		$Controller->paginate = array(
943
			'conditions' => array('PaginatorControllerPost.id >' => 100)
944
		);
945
 
946
		try {
947
			$Controller->Paginator->paginate('PaginatorControllerPost');
948
			$this->fail();
949
		} catch (NotFoundException $e) {
950
			$this->assertEquals(
951
				1,
952
				$Controller->request->params['paging']['PaginatorControllerPost']['page'],
953
				'Page number should not be 0'
954
			);
955
		}
956
	}
957
 
958
/**
959
 * test that fields not in whitelist won't be part of order conditions.
960
 *
961
 * @return void
962
 */
963
	public function testValidateSortWhitelistFailure() {
964
		$model = $this->getMock('Model');
965
		$model->alias = 'model';
966
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
967
 
968
		$options = array('sort' => 'body', 'direction' => 'asc');
969
		$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
970
 
971
		$this->assertNull($result['order']);
972
	}
973
 
974
/**
975
 * test that fields in the whitelist are not validated
976
 *
977
 * @return void
978
 */
979
	public function testValidateSortWhitelistTrusted() {
980
		$model = $this->getMock('Model');
981
		$model->alias = 'model';
982
		$model->expects($this->never())->method('hasField');
983
 
984
		$options = array('sort' => 'body', 'direction' => 'asc');
985
		$result = $this->Paginator->validateSort($model, $options, array('body'));
986
 
987
		$expected = array('body' => 'asc');
988
		$this->assertEquals($expected, $result['order']);
989
	}
990
 
991
/**
992
 * test that virtual fields work.
993
 *
994
 * @return void
995
 */
996
	public function testValidateSortVirtualField() {
997
		$model = $this->getMock('Model');
998
		$model->alias = 'model';
999
 
1000
		$model->expects($this->at(0))
1001
			->method('hasField')
1002
			->with('something')
1003
			->will($this->returnValue(false));
1004
 
1005
		$model->expects($this->at(1))
1006
			->method('hasField')
1007
			->with('something', true)
1008
			->will($this->returnValue(true));
1009
 
1010
		$options = array('sort' => 'something', 'direction' => 'desc');
1011
		$result = $this->Paginator->validateSort($model, $options);
1012
 
1013
		$this->assertEquals('desc', $result['order']['something']);
1014
	}
1015
 
1016
/**
1017
 * test that sorting fields is alias specific
1018
 *
1019
 * @return void
1020
 */
1021
	public function testValidateSortSharedFields() {
1022
		$model = $this->getMock('Model');
1023
		$model->alias = 'Parent';
1024
		$model->Child = $this->getMock('Model');
1025
		$model->Child->alias = 'Child';
1026
 
1027
		$model->expects($this->never())
1028
			->method('hasField');
1029
 
1030
		$model->Child->expects($this->at(0))
1031
			->method('hasField')
1032
			->with('something')
1033
			->will($this->returnValue(true));
1034
 
1035
		$options = array('sort' => 'Child.something', 'direction' => 'desc');
1036
		$result = $this->Paginator->validateSort($model, $options);
1037
 
1038
		$this->assertEquals('desc', $result['order']['Child.something']);
1039
	}
1040
/**
1041
 * test that multiple sort works.
1042
 *
1043
 * @return void
1044
 */
1045
	public function testValidateSortMultiple() {
1046
		$model = $this->getMock('Model');
1047
		$model->alias = 'model';
1048
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
1049
 
1050
		$options = array(
1051
			'order' => array(
1052
				'author_id' => 'asc',
1053
				'title' => 'asc'
1054
			)
1055
		);
1056
		$result = $this->Paginator->validateSort($model, $options);
1057
		$expected = array(
1058
			'model.author_id' => 'asc',
1059
			'model.title' => 'asc'
1060
		);
1061
 
1062
		$this->assertEquals($expected, $result['order']);
1063
	}
1064
 
1065
/**
1066
 * Test that no sort doesn't trigger an error.
1067
 *
1068
 * @return void
1069
 */
1070
	public function testValidateSortNoSort() {
1071
		$model = $this->getMock('Model');
1072
		$model->alias = 'model';
1073
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
1074
 
1075
		$options = array('direction' => 'asc');
1076
		$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
1077
		$this->assertFalse(isset($result['order']));
1078
 
1079
		$options = array('order' => 'invalid desc');
1080
		$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
1081
 
1082
		$this->assertEquals($options['order'], $result['order']);
1083
	}
1084
 
1085
/**
1086
 * Test sorting with incorrect aliases on valid fields.
1087
 *
1088
 * @return void
1089
 */
1090
	public function testValidateSortInvalidAlias() {
1091
		$model = $this->getMock('Model');
1092
		$model->alias = 'Model';
1093
		$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
1094
 
1095
		$options = array('sort' => 'Derp.id');
1096
		$result = $this->Paginator->validateSort($model, $options);
1097
		$this->assertEquals(array(), $result['order']);
1098
	}
1099
 
1100
/**
1101
 * test that maxLimit is respected
1102
 *
1103
 * @return void
1104
 */
1105
	public function testCheckLimit() {
1106
		$result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
1107
		$this->assertEquals(100, $result['limit']);
1108
 
1109
		$result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
1110
		$this->assertEquals(1, $result['limit']);
1111
 
1112
		$result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
1113
		$this->assertEquals(1, $result['limit']);
1114
 
1115
		$result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
1116
		$this->assertEquals(1, $result['limit']);
1117
 
1118
		$result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
1119
		$this->assertEquals(1, $result['limit']);
1120
	}
1121
 
1122
/**
1123
 * testPaginateMaxLimit
1124
 *
1125
 * @return void
1126
 */
1127
	public function testPaginateMaxLimit() {
1128
		$Controller = new Controller($this->request);
1129
 
1130
		$Controller->uses = array('PaginatorControllerPost', 'ControllerComment');
1131
		$Controller->request->params['pass'][] = '1';
1132
		$Controller->constructClasses();
1133
 
1134
		$Controller->request->params['named'] = array(
1135
			'contain' => array('ControllerComment'), 'limit' => '1000'
1136
		);
1137
		$Controller->paginate('PaginatorControllerPost');
1138
		$this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1139
 
1140
		$Controller->request->params['named'] = array(
1141
			'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
1142
		);
1143
		$Controller->paginate('PaginatorControllerPost');
1144
		$this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1145
 
1146
		$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
1147
		$Controller->paginate('PaginatorControllerPost');
1148
		$this->assertEquals(10, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1149
 
1150
		$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
1151
		$Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
1152
		$Controller->paginate('PaginatorControllerPost');
1153
		$this->assertEquals(1000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1154
 
1155
		$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
1156
		$Controller->paginate('PaginatorControllerPost');
1157
		$this->assertEquals(2000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
1158
	}
1159
 
1160
/**
1161
 * test paginate() and virtualField overlapping with real fields.
1162
 *
1163
 * @return void
1164
 */
1165
	public function testPaginateOrderVirtualFieldSharedWithRealField() {
1166
		$Controller = new Controller($this->request);
1167
		$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
1168
		$Controller->constructClasses();
1169
		$Controller->PaginatorControllerComment->virtualFields = array(
1170
			'title' => 'PaginatorControllerComment.comment'
1171
		);
1172
		$Controller->PaginatorControllerComment->bindModel(array(
1173
			'belongsTo' => array(
1174
				'PaginatorControllerPost' => array(
1175
					'className' => 'PaginatorControllerPost',
1176
					'foreignKey' => 'article_id'
1177
				)
1178
			)
1179
		), false);
1180
 
1181
		$Controller->paginate = array(
1182
			'fields' => array(
1183
				'PaginatorControllerComment.id',
1184
				'title',
1185
				'PaginatorControllerPost.title'
1186
			),
1187
		);
1188
		$Controller->request->params['named'] = array(
1189
			'sort' => 'PaginatorControllerPost.title',
1190
			'direction' => 'desc'
1191
		);
1192
		$result = Hash::extract(
1193
			$Controller->paginate('PaginatorControllerComment'),
1194
			'{n}.PaginatorControllerComment.id'
1195
		);
1196
		$result1 = array_splice($result, 0, 2);
1197
		sort($result1);
1198
		$this->assertEquals(array(5, 6), $result1);
1199
 
1200
		sort($result);
1201
		$this->assertEquals(array(1, 2, 3, 4), $result);
1202
	}
1203
 
1204
/**
1205
 * test paginate() and custom find, to make sure the correct count is returned.
1206
 *
1207
 * @return void
1208
 */
1209
	public function testPaginateCustomFind() {
1210
		$Controller = new Controller($this->request);
1211
		$Controller->uses = array('PaginatorCustomPost');
1212
		$Controller->constructClasses();
1213
		$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1214
		$Controller->PaginatorCustomPost->create($data);
1215
		$result = $Controller->PaginatorCustomPost->save();
1216
		$this->assertTrue(!empty($result));
1217
 
1218
		$result = $Controller->paginate();
1219
		$this->assertEquals(array(1, 2, 3, 4), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
1220
 
1221
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1222
		$this->assertEquals(4, $result['current']);
1223
		$this->assertEquals(4, $result['count']);
1224
 
1225
		$Controller->paginate = array('published');
1226
		$result = $Controller->paginate();
1227
		$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
1228
 
1229
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1230
		$this->assertEquals(3, $result['current']);
1231
		$this->assertEquals(3, $result['count']);
1232
 
1233
		$Controller->paginate = array('published', 'limit' => 2);
1234
		$result = $Controller->paginate();
1235
		$this->assertEquals(array(1, 2), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
1236
 
1237
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1238
		$this->assertEquals(2, $result['current']);
1239
		$this->assertEquals(3, $result['count']);
1240
		$this->assertEquals(2, $result['pageCount']);
1241
		$this->assertTrue($result['nextPage']);
1242
		$this->assertFalse($result['prevPage']);
1243
	}
1244
/**
1245
 * test paginate() and custom find with fields array, to make sure the correct count is returned.
1246
 *
1247
 * @return void
1248
 */
1249
	public function testPaginateCustomFindFieldsArray() {
1250
		$Controller = new Controller($this->request);
1251
		$Controller->uses = array('PaginatorCustomPost');
1252
		$Controller->constructClasses();
1253
		$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1254
		$Controller->PaginatorCustomPost->create($data);
1255
		$result = $Controller->PaginatorCustomPost->save();
1256
		$this->assertTrue(!empty($result));
1257
 
1258
		$Controller->paginate = array(
1259
			'list',
1260
			'conditions' => array('PaginatorCustomPost.published' => 'Y'),
1261
			'limit' => 2
1262
		);
1263
		$result = $Controller->paginate();
1264
		$expected = array(
1265
			1 => 'First Post',
1266
			2 => 'Second Post',
1267
		);
1268
		$this->assertEquals($expected, $result);
1269
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1270
		$this->assertEquals(2, $result['current']);
1271
		$this->assertEquals(3, $result['count']);
1272
		$this->assertEquals(2, $result['pageCount']);
1273
		$this->assertTrue($result['nextPage']);
1274
		$this->assertFalse($result['prevPage']);
1275
	}
1276
/**
1277
 * test paginate() and custom find with customFind key, to make sure the correct count is returned.
1278
 *
1279
 * @return void
1280
 */
1281
	public function testPaginateCustomFindWithCustomFindKey() {
1282
		$Controller = new Controller($this->request);
1283
		$Controller->uses = array('PaginatorCustomPost');
1284
		$Controller->constructClasses();
1285
		$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1286
		$Controller->PaginatorCustomPost->create($data);
1287
		$result = $Controller->PaginatorCustomPost->save();
1288
		$this->assertTrue(!empty($result));
1289
 
1290
		$Controller->paginate = array(
1291
			'conditions' => array('PaginatorCustomPost.published' => 'Y'),
1292
			'findType' => 'list',
1293
			'limit' => 2
1294
		);
1295
		$result = $Controller->paginate();
1296
		$expected = array(
1297
			1 => 'First Post',
1298
			2 => 'Second Post',
1299
		);
1300
		$this->assertEquals($expected, $result);
1301
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1302
		$this->assertEquals(2, $result['current']);
1303
		$this->assertEquals(3, $result['count']);
1304
		$this->assertEquals(2, $result['pageCount']);
1305
		$this->assertTrue($result['nextPage']);
1306
		$this->assertFalse($result['prevPage']);
1307
	}
1308
 
1309
/**
1310
 * test paginate() and custom find with fields array, to make sure the correct count is returned.
1311
 *
1312
 * @return void
1313
 */
1314
	public function testPaginateCustomFindGroupBy() {
1315
		$Controller = new Controller($this->request);
1316
		$Controller->uses = array('PaginatorCustomPost');
1317
		$Controller->constructClasses();
1318
		$data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1319
		$Controller->PaginatorCustomPost->create($data);
1320
		$result = $Controller->PaginatorCustomPost->save();
1321
		$this->assertTrue(!empty($result));
1322
 
1323
		$Controller->paginate = array(
1324
			'totals',
1325
			'limit' => 2
1326
		);
1327
		$result = $Controller->paginate();
1328
		$expected = array(
1329
			array(
1330
				'PaginatorCustomPost' => array(
1331
					'author_id' => '1',
1332
					'total_posts' => '2'
1333
				)
1334
			),
1335
			array(
1336
				'PaginatorCustomPost' => array(
1337
					'author_id' => '2',
1338
					'total_posts' => '1'
1339
				)
1340
			)
1341
		);
1342
		$this->assertEquals($expected, $result);
1343
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1344
		$this->assertEquals(2, $result['current']);
1345
		$this->assertEquals(3, $result['count']);
1346
		$this->assertEquals(2, $result['pageCount']);
1347
		$this->assertTrue($result['nextPage']);
1348
		$this->assertFalse($result['prevPage']);
1349
 
1350
		$Controller->paginate = array(
1351
			'totals',
1352
			'limit' => 2,
1353
			'page' => 2
1354
		);
1355
		$result = $Controller->paginate();
1356
		$expected = array(
1357
			array(
1358
				'PaginatorCustomPost' => array(
1359
					'author_id' => '3',
1360
					'total_posts' => '1'
1361
				)
1362
			),
1363
		);
1364
		$this->assertEquals($expected, $result);
1365
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1366
		$this->assertEquals(1, $result['current']);
1367
		$this->assertEquals(3, $result['count']);
1368
		$this->assertEquals(2, $result['pageCount']);
1369
		$this->assertFalse($result['nextPage']);
1370
		$this->assertTrue($result['prevPage']);
1371
	}
1372
 
1373
/**
1374
 * test paginate() and custom find with returning other query on count operation,
1375
 * to make sure the correct count is returned.
1376
 *
1377
 * @return void
1378
 */
1379
	public function testPaginateCustomFindCount() {
1380
		$Controller = new Controller($this->request);
1381
		$Controller->uses = array('PaginatorCustomPost');
1382
		$Controller->constructClasses();
1383
		$data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
1384
		$Controller->PaginatorCustomPost->create($data);
1385
		$result = $Controller->PaginatorCustomPost->save();
1386
		$this->assertTrue(!empty($result));
1387
 
1388
		$Controller->paginate = array(
1389
			'totalsOperation',
1390
			'limit' => 2
1391
		);
1392
		$result = $Controller->paginate();
1393
		$expected = array(
1394
			array(
1395
				'PaginatorCustomPost' => array(
1396
					'author_id' => '1',
1397
					'total_posts' => '2'
1398
				),
1399
				'Author' => array(
1400
					'user' => 'mariano',
1401
				)
1402
			),
1403
			array(
1404
				'PaginatorCustomPost' => array(
1405
					'author_id' => '2',
1406
					'total_posts' => '1'
1407
				),
1408
				'Author' => array(
1409
					'user' => 'nate'
1410
				)
1411
			)
1412
		);
1413
		$this->assertEquals($expected, $result);
1414
		$result = $Controller->params['paging']['PaginatorCustomPost'];
1415
		$this->assertEquals(2, $result['current']);
1416
		$this->assertEquals(3, $result['count']);
1417
		$this->assertEquals(2, $result['pageCount']);
1418
		$this->assertTrue($result['nextPage']);
1419
		$this->assertFalse($result['prevPage']);
1420
	}
1421
}