Skip to content

Commit fd16b8a

Browse files
committed
Throw exception if requested page number is out of range.
Closes #3459
1 parent a9293aa commit fd16b8a

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

lib/Cake/Controller/Component/PaginatorComponent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function __construct(ComponentCollection $collection, $settings = array()
122122
* on non-indexed, or undesirable columns.
123123
* @return array Model query results
124124
* @throws MissingModelException
125+
* @throws NotFoundException
125126
*/
126127
public function paginate($object = null, $scope = array(), $whitelist = array()) {
127128
if (is_array($object)) {
@@ -206,6 +207,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
206207
$count = $object->find('count', array_merge($parameters, $extra));
207208
}
208209
$pageCount = intval(ceil($count / $limit));
210+
$requestedPage = $page;
209211
$page = max(min($page, $pageCount), 1);
210212

211213
$paging = array(
@@ -220,6 +222,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
220222
'options' => Hash::diff($options, $defaults),
221223
'paramType' => $options['paramType']
222224
);
225+
223226
if (!isset($this->Controller->request['paging'])) {
224227
$this->Controller->request['paging'] = array();
225228
}
@@ -228,6 +231,10 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
228231
array($object->alias => $paging)
229232
);
230233

234+
if ($requestedPage > $page) {
235+
throw new NotFoundException();
236+
}
237+
231238
if (
232239
!in_array('Paginator', $this->Controller->helpers) &&
233240
!array_key_exists('Paginator', $this->Controller->helpers)

lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,9 @@ public function testValidateSortInvalidDirection() {
872872

873873
/**
874874
* Test that a really large page number gets clamped to the max page size.
875+
*
876+
* @expectedException NotFoundException
877+
* @return void
875878
*/
876879
public function testOutOfRangePageNumberGetsClamped() {
877880
$Controller = new PaginatorTestController($this->request);
@@ -882,21 +885,35 @@ public function testOutOfRangePageNumberGetsClamped() {
882885
$Controller->constructClasses();
883886
$Controller->PaginatorControllerPost->recursive = 0;
884887
$Controller->Paginator->paginate('PaginatorControllerPost');
885-
$this->assertEquals(
886-
1,
887-
$Controller->request->params['paging']['PaginatorControllerPost']['page'],
888-
'Super big page number should be capped to max number of pages'
889-
);
888+
}
890889

890+
/**
891+
* testOutOfRangePageNumberAndPageCountZero
892+
*
893+
* @return void
894+
*/
895+
public function testOutOfRangePageNumberAndPageCountZero() {
896+
$Controller = new PaginatorTestController($this->request);
897+
$Controller->uses = array('PaginatorControllerPost');
898+
$Controller->params['named'] = array(
899+
'page' => 3000,
900+
);
901+
$Controller->constructClasses();
902+
$Controller->PaginatorControllerPost->recursive = 0;
891903
$Controller->paginate = array(
892904
'conditions' => array('PaginatorControllerPost.id >' => 100)
893905
);
894-
$Controller->Paginator->paginate('PaginatorControllerPost');
895-
$this->assertEquals(
896-
1,
897-
$Controller->request->params['paging']['PaginatorControllerPost']['page'],
898-
'Page number should not be 0'
899-
);
906+
try {
907+
$Controller->Paginator->paginate('PaginatorControllerPost');
908+
} catch (NotFoundException $e) {
909+
$this->assertEquals(
910+
1,
911+
$Controller->request->params['paging']['PaginatorControllerPost']['page'],
912+
'Page number should not be 0'
913+
);
914+
return;
915+
}
916+
$this->fail();
900917
}
901918

902919
/**

0 commit comments

Comments
 (0)