Permalink
Browse files

Changed the paginator to do a count after the find.

  • Loading branch information...
1 parent 6fb3c72 commit b8c00d5f56d4eb2b848cb761fe4a4061a1807134 @jrbasso jrbasso committed Jul 24, 2011
@@ -153,21 +153,8 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
$extra['type'] = $type;
}
- if ($object->hasMethod('paginateCount')) {
- $count = $object->paginateCount($conditions, $recursive, $extra);
- } else {
- $parameters = compact('conditions');
- if ($recursive != $object->recursive) {
- $parameters['recursive'] = $recursive;
- }
- $count = $object->find('count', array_merge($parameters, $extra));
- }
- $pageCount = intval(ceil($count / $limit));
-
- if ($page === 'last' || $page >= $pageCount) {
- $options['page'] = $page = $pageCount;
- } elseif (intval($page) < 1) {
- $options['page'] = $page = 1;
+ if (intval($page) < 1) {
+ $page = 1;
}
$page = $options['page'] = (int)$page;
@@ -185,6 +172,17 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
$defaults = $this->getDefaults($object->alias);
unset($defaults[0]);
+ if ($object->hasMethod('paginateCount')) {
+ $count = $object->paginateCount($conditions, $recursive, $extra);
+ } else {
+ $parameters = compact('conditions');
+ if ($recursive != $object->recursive) {
+ $parameters['recursive'] = $recursive;
+ }
+ $count = $object->find('count', array_merge($parameters, $extra));
+ }
+ $pageCount = intval(ceil($count / $limit));
+
$paging = array(
'page' => $page,
'current' => count($results),
@@ -87,12 +87,11 @@ class PaginatorControllerPost extends CakeTestModel {
public $invalidFields = array('name' => 'error_msg');
/**
- * lastQuery property
+ * lastQueries property
*
- * @var mixed null
- * @access public
+ * @var array
*/
- public $lastQuery = null;
+ public $lastQueries = array();
/**
* beforeFind method
@@ -102,7 +101,7 @@ class PaginatorControllerPost extends CakeTestModel {
* @return void
*/
public function beforeFind($query) {
- $this->lastQuery = $query;
+ array_unshift($this->lastQueries, $query);
}
/**
@@ -278,7 +277,7 @@ public function testPaginate() {
'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
);
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
- $this->assertEqual($Controller->PaginatorControllerPost->lastQuery['order'][0], array('PaginatorControllerPost.author_id' => 'asc'));
+ $this->assertEqual($Controller->PaginatorControllerPost->lastQueries[1]['order'][0], array('PaginatorControllerPost.author_id' => 'asc'));
$this->assertEqual($results, array(1, 3, 2));
$Controller->request->params['named'] = array();
@@ -316,22 +315,22 @@ public function testPaginate() {
public function testPageParamCasting() {
$this->Controller->Post->expects($this->at(0))
->method('hasMethod')
- ->with('paginateCount')
+ ->with('paginate')
->will($this->returnValue(false));
-
+
$this->Controller->Post->expects($this->at(1))
->method('find')
- ->will($this->returnValue(2));
-
+ ->will($this->returnValue(array('stuff')));
+
$this->Controller->Post->expects($this->at(2))
->method('hasMethod')
- ->with('paginate')
+ ->with('paginateCount')
->will($this->returnValue(false));
-
+
$this->Controller->Post->expects($this->at(3))
->method('find')
- ->will($this->returnValue(array('stuff')));
-
+ ->will($this->returnValue(2));
+
$this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
$this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
$this->Paginator->paginate('Post');
@@ -356,7 +355,7 @@ public function testPaginateExtraParams() {
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3));
- $this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQuery['contain']));
+ $this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
$Controller->request->params['named'] = array('page' => '-1');
$Controller->Paginator->settings = array(
@@ -369,7 +368,7 @@ public function testPaginateExtraParams() {
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3));
- $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQuery['contain']));
+ $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
$Controller->Paginator->settings = array(
'PaginatorControllerPost' => array(
@@ -378,14 +377,14 @@ public function testPaginateExtraParams() {
);
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
- $this->assertEqual($Controller->PaginatorControllerPost->lastQuery['conditions'], array('PaginatorControllerPost.id > ' => '1'));
+ $this->assertEqual($Controller->PaginatorControllerPost->lastQueries[1]['conditions'], array('PaginatorControllerPost.id > ' => '1'));
$Controller->request->params['named'] = array('limit' => 12);
$Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
$paging = $Controller->params['paging']['PaginatorControllerPost'];
- $this->assertEqual($Controller->PaginatorControllerPost->lastQuery['limit'], 12);
+ $this->assertEqual($Controller->PaginatorControllerPost->lastQueries[1]['limit'], 12);
$this->assertEqual($paging['options']['limit'], 12);
$Controller = new PaginatorTestController($this->request);
@@ -454,7 +453,7 @@ public function testPaginateSpecialType() {
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
$this->assertEqual(
- $Controller->PaginatorControllerPost->lastQuery['conditions'],
+ $Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
array('PaginatorControllerPost.id > ' => '1')
);
$this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));

0 comments on commit b8c00d5

Please sign in to comment.