Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Throw exception if requested page number is out of range.
Closes #3459
  • Loading branch information
ADmad committed Dec 27, 2012
1 parent a9293aa commit fd16b8a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/Cake/Controller/Component/PaginatorComponent.php
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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(
Expand All @@ -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();
}
Expand All @@ -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)
Expand Down
39 changes: 28 additions & 11 deletions lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -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);
Expand All @@ -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();
}

/**
Expand Down

0 comments on commit fd16b8a

Please sign in to comment.