Skip to content

Commit 6b9d9f4

Browse files
committed
Reapplying changes in [33d2f9a] as they got lost when the paginator component was extracted.
1 parent a6cca7c commit 6b9d9f4

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

cake/libs/controller/components/paginator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PaginatorComponent extends Component {
4343
* @param array $settings Array of configuration settings.
4444
*/
4545
public function __construct(ComponentCollection $collection, $settings = array()) {
46-
$settings = array_merge(array('page' => 1, 'limit' => 20), (array)$settings);
46+
$settings = array_merge(array('page' => 1, 'limit' => 20, 'maxLimit' => 100), (array)$settings);
4747
$this->Controller = $collection->getController();
4848
parent::__construct($collection, $settings);
4949
}
@@ -146,6 +146,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
146146
if (empty($options['limit']) || $options['limit'] < 1) {
147147
$options['limit'] = 1;
148148
}
149+
$options['limit'] = min((int)$options['limit'], $options['maxLimit']);
149150

150151
extract($options);
151152

@@ -181,7 +182,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
181182
} elseif (intval($page) < 1) {
182183
$options['page'] = $page = 1;
183184
}
184-
$page = $options['page'] = (integer)$page;
185+
$page = $options['page'] = (int)$page;
185186

186187
if (method_exists($object, 'paginate')) {
187188
$results = $object->paginate(

cake/tests/cases/libs/controller/components/paginator.test.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,43 @@ function testPaginateMissingModel() {
486486
$Controller->constructClasses();
487487
$Controller->Paginator->paginate('MissingModel');
488488
}
489+
490+
/**
491+
* testPaginateMaxLimit
492+
*
493+
* @return void
494+
* @access public
495+
*/
496+
function testPaginateMaxLimit() {
497+
$request = new CakeRequest('controller_posts/index');
498+
$request->params['pass'] = $request->params['named'] = array();
499+
500+
$Controller = new Controller($request);
501+
502+
$Controller->uses = array('ControllerPost', 'ControllerComment');
503+
$Controller->passedArgs[] = '1';
504+
$Controller->params['url'] = array();
505+
$Controller->constructClasses();
506+
507+
$Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000');
508+
$result = $Controller->paginate('ControllerPost');
509+
$this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 100);
510+
511+
$Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000);
512+
$result = $Controller->paginate('ControllerPost');
513+
$this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 100);
514+
515+
$Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '10');
516+
$result = $Controller->paginate('ControllerPost');
517+
$this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 10);
518+
519+
$Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000');
520+
$Controller->paginate = array('maxLimit' => 2000);
521+
$result = $Controller->paginate('ControllerPost');
522+
$this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 1000);
523+
524+
$Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '5000');
525+
$result = $Controller->paginate('ControllerPost');
526+
$this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 2000);
527+
}
489528
}

0 commit comments

Comments
 (0)