Browse Source

Fix page 0 issue.

Refs #2929
mark_story 14 years ago
parent
commit
d1819dcabb

+ 2 - 2
lib/Cake/Controller/Component/PaginatorComponent.php

@@ -184,7 +184,7 @@ class PaginatorComponent extends Component {
 			$count = $object->find('count', array_merge($parameters, $extra));
 		}
 		$pageCount = intval(ceil($count / $limit));
-		$page = min($page, $pageCount);
+		$page = max(min($page, $pageCount), 1);
 
 		$paging = array(
 			'page' => $page,
@@ -375,7 +375,7 @@ class PaginatorComponent extends Component {
 		if (empty($options['limit']) || $options['limit'] < 1) {
 			$options['limit'] = 1;
 		}
-		$options['limit'] = min((int)$options['limit'], $options['maxLimit']);
+		$options['limit'] = min($options['limit'], $options['maxLimit']);
 		return $options;
 	}
 

+ 15 - 1
lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php

@@ -725,7 +725,21 @@ class PaginatorComponentTest extends CakeTestCase {
 		$Controller->constructClasses();
 		$Controller->PaginatorControllerPost->recursive = 0;
 		$Controller->Paginator->paginate('PaginatorControllerPost');
-		$this->assertEquals(1, $Controller->request->params['paging']['PaginatorControllerPost']['page']);
+		$this->assertEquals(
+			1,
+			$Controller->request->params['paging']['PaginatorControllerPost']['page'],
+			'Super big page number should be capped to max number of pages'
+		);
+
+		$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'
+		);
 	}
 
 /**