Browse files

Throw exception if requested page number is out of range.

Closes #3459
  • Loading branch information...
1 parent a9293aa commit fd16b8a1e5bfa3591236a6c0c761e618cb750956 @ADmad ADmad committed Dec 27, 2012
View
7 lib/Cake/Controller/Component/PaginatorComponent.php
@@ -122,6 +122,7 @@ public function __construct(ComponentCollection $collection, $settings = array()
* on non-indexed, or undesirable columns.
* @return array Model query results
* @throws MissingModelException
+ * @throws NotFoundException
*/
public function paginate($object = null, $scope = array(), $whitelist = array()) {
if (is_array($object)) {
@@ -206,6 +207,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
$count = $object->find('count', array_merge($parameters, $extra));
}
$pageCount = intval(ceil($count / $limit));
+ $requestedPage = $page;
$page = max(min($page, $pageCount), 1);
$paging = array(
@@ -220,6 +222,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
'options' => Hash::diff($options, $defaults),
'paramType' => $options['paramType']
);
+
if (!isset($this->Controller->request['paging'])) {
$this->Controller->request['paging'] = array();
}
@@ -228,6 +231,10 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
array($object->alias => $paging)
);
+ if ($requestedPage > $page) {
+ throw new NotFoundException();
+ }
+
if (
!in_array('Paginator', $this->Controller->helpers) &&
!array_key_exists('Paginator', $this->Controller->helpers)
View
39 lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php
@@ -872,6 +872,9 @@ public function testValidateSortInvalidDirection() {
/**
* Test that a really large page number gets clamped to the max page size.
+ *
+ * @expectedException NotFoundException
+ * @return void
*/
public function testOutOfRangePageNumberGetsClamped() {
$Controller = new PaginatorTestController($this->request);
@@ -882,21 +885,35 @@ public function testOutOfRangePageNumberGetsClamped() {
$Controller->constructClasses();
$Controller->PaginatorControllerPost->recursive = 0;
$Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(
- 1,
- $Controller->request->params['paging']['PaginatorControllerPost']['page'],
- 'Super big page number should be capped to max number of pages'
- );
+ }
+/**
+ * testOutOfRangePageNumberAndPageCountZero
+ *
+ * @return void
+ */
+ public function testOutOfRangePageNumberAndPageCountZero() {
+ $Controller = new PaginatorTestController($this->request);
+ $Controller->uses = array('PaginatorControllerPost');
+ $Controller->params['named'] = array(
+ 'page' => 3000,
+ );
+ $Controller->constructClasses();
+ $Controller->PaginatorControllerPost->recursive = 0;
$Controller->paginate = array(
'conditions' => array('PaginatorControllerPost.id >' => 100)
);
- $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(
- 1,
- $Controller->request->params['paging']['PaginatorControllerPost']['page'],
- 'Page number should not be 0'
- );
+ try {
+ $Controller->Paginator->paginate('PaginatorControllerPost');
+ } catch (NotFoundException $e) {
+ $this->assertEquals(
+ 1,
+ $Controller->request->params['paging']['PaginatorControllerPost']['page'],
+ 'Page number should not be 0'
+ );
+ return;
+ }
+ $this->fail();
}
/**

0 comments on commit fd16b8a

Please sign in to comment.