From 6b9d9f4aea1f542eda766ea6bb68520739bfdaaf Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 18 Dec 2010 17:17:43 -0500 Subject: [PATCH] Reapplying changes in [33d2f9a6ed7adfee0aedd0fd20d9f31368ef1836] as they got lost when the paginator component was extracted. --- cake/libs/controller/components/paginator.php | 5 ++- .../controller/components/paginator.test.php | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cake/libs/controller/components/paginator.php b/cake/libs/controller/components/paginator.php index 4a0bd2ffc48..26e36b36758 100644 --- a/cake/libs/controller/components/paginator.php +++ b/cake/libs/controller/components/paginator.php @@ -43,7 +43,7 @@ class PaginatorComponent extends Component { * @param array $settings Array of configuration settings. */ public function __construct(ComponentCollection $collection, $settings = array()) { - $settings = array_merge(array('page' => 1, 'limit' => 20), (array)$settings); + $settings = array_merge(array('page' => 1, 'limit' => 20, 'maxLimit' => 100), (array)$settings); $this->Controller = $collection->getController(); parent::__construct($collection, $settings); } @@ -146,6 +146,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array()) if (empty($options['limit']) || $options['limit'] < 1) { $options['limit'] = 1; } + $options['limit'] = min((int)$options['limit'], $options['maxLimit']); extract($options); @@ -181,7 +182,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array()) } elseif (intval($page) < 1) { $options['page'] = $page = 1; } - $page = $options['page'] = (integer)$page; + $page = $options['page'] = (int)$page; if (method_exists($object, 'paginate')) { $results = $object->paginate( diff --git a/cake/tests/cases/libs/controller/components/paginator.test.php b/cake/tests/cases/libs/controller/components/paginator.test.php index 20abfd8da45..82efa4b02af 100644 --- a/cake/tests/cases/libs/controller/components/paginator.test.php +++ b/cake/tests/cases/libs/controller/components/paginator.test.php @@ -486,4 +486,43 @@ function testPaginateMissingModel() { $Controller->constructClasses(); $Controller->Paginator->paginate('MissingModel'); } + +/** + * testPaginateMaxLimit + * + * @return void + * @access public + */ + function testPaginateMaxLimit() { + $request = new CakeRequest('controller_posts/index'); + $request->params['pass'] = $request->params['named'] = array(); + + $Controller = new Controller($request); + + $Controller->uses = array('ControllerPost', 'ControllerComment'); + $Controller->passedArgs[] = '1'; + $Controller->params['url'] = array(); + $Controller->constructClasses(); + + $Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000'); + $result = $Controller->paginate('ControllerPost'); + $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 100); + + $Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000); + $result = $Controller->paginate('ControllerPost'); + $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 100); + + $Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '10'); + $result = $Controller->paginate('ControllerPost'); + $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 10); + + $Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000'); + $Controller->paginate = array('maxLimit' => 2000); + $result = $Controller->paginate('ControllerPost'); + $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 1000); + + $Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '5000'); + $result = $Controller->paginate('ControllerPost'); + $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['limit'], 2000); + } } \ No newline at end of file