Throw exception if requested page number is out of range.

Closes #3459
This commit is contained in:
ADmad 2012-12-27 16:40:14 +05:30
parent a9293aa385
commit fd16b8a1e5
2 changed files with 35 additions and 11 deletions

View file

@ -122,6 +122,7 @@ class PaginatorComponent extends Component {
* 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 @@ class PaginatorComponent extends Component {
$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 @@ class PaginatorComponent extends Component {
'options' => Hash::diff($options, $defaults),
'paramType' => $options['paramType']
);
if (!isset($this->Controller->request['paging'])) {
$this->Controller->request['paging'] = array();
}
@ -228,6 +231,10 @@ class PaginatorComponent extends Component {
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 file

@ -872,6 +872,9 @@ class PaginatorComponentTest extends CakeTestCase {
/**
* 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 @@ class PaginatorComponentTest extends CakeTestCase {
$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();
}
/**