2010-12-15 02:21:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PaginatorComponentTest file
|
|
|
|
*
|
|
|
|
* Series of tests for paginator component.
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-12-15 02:21:39 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2010-12-15 02:21:39 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-02-28 03:20:25 +00:00
|
|
|
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('PaginatorComponent', 'Controller/Component');
|
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
2010-12-15 02:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PaginatorTestController class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2010-12-15 02:21:39 +00:00
|
|
|
*/
|
|
|
|
class PaginatorTestController extends Controller {
|
2012-03-12 02:20:25 +00:00
|
|
|
|
2010-12-15 02:21:39 +00:00
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'PaginatorTest'
|
|
|
|
*/
|
|
|
|
public $name = 'PaginatorTest';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* components property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $components = array('Paginator');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-15 04:27:12 +00:00
|
|
|
* PaginatorControllerPost class
|
2010-12-15 02:21:39 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2010-12-15 02:21:39 +00:00
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
class PaginatorControllerPost extends CakeTestModel {
|
2010-12-15 02:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
2010-12-15 04:27:12 +00:00
|
|
|
* @var string 'PaginatorControllerPost'
|
2010-12-15 02:21:39 +00:00
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
public $name = 'PaginatorControllerPost';
|
2010-12-15 02:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string 'posts'
|
|
|
|
*/
|
|
|
|
public $useTable = 'posts';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* invalidFields property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $invalidFields = array('name' => 'error_msg');
|
|
|
|
|
|
|
|
/**
|
2011-07-24 01:09:55 +00:00
|
|
|
* lastQueries property
|
2010-12-15 02:21:39 +00:00
|
|
|
*
|
2011-07-24 01:09:55 +00:00
|
|
|
* @var array
|
2010-12-15 02:21:39 +00:00
|
|
|
*/
|
2011-07-24 01:09:55 +00:00
|
|
|
public $lastQueries = array();
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2011-11-14 00:34:49 +00:00
|
|
|
/**
|
|
|
|
* belongsTo property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $belongsTo = array('PaginatorAuthor' => array('foreignKey' => 'author_id'));
|
|
|
|
|
2010-12-15 02:21:39 +00:00
|
|
|
/**
|
|
|
|
* beforeFind method
|
|
|
|
*
|
|
|
|
* @param mixed $query
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function beforeFind($query) {
|
2011-07-24 01:09:55 +00:00
|
|
|
array_unshift($this->lastQueries, $query);
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find method
|
|
|
|
*
|
|
|
|
* @param mixed $type
|
|
|
|
* @param array $options
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
|
2010-12-18 05:03:03 +00:00
|
|
|
if ($conditions == 'popular') {
|
2012-03-12 02:20:25 +00:00
|
|
|
$conditions = array($this->name . '.' . $this->primaryKey . ' > ' => '1');
|
2012-03-11 01:57:18 +00:00
|
|
|
$options = Hash::merge($fields, compact('conditions'));
|
2010-12-15 02:21:39 +00:00
|
|
|
return parent::find('all', $options);
|
|
|
|
}
|
2010-12-18 05:03:03 +00:00
|
|
|
return parent::find($conditions, $fields);
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
2012-03-12 02:20:25 +00:00
|
|
|
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ControllerPaginateModel class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2010-12-15 02:21:39 +00:00
|
|
|
*/
|
|
|
|
class ControllerPaginateModel extends CakeTestModel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'ControllerPaginateModel'
|
|
|
|
*/
|
|
|
|
public $name = 'ControllerPaginateModel';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string 'comments'
|
|
|
|
*/
|
|
|
|
public $useTable = 'comments';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* paginate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
|
|
|
|
$this->extra = $extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* paginateCount
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function paginateCount($conditions, $recursive, $extra) {
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->extraCount = $extra;
|
|
|
|
}
|
2012-03-12 02:20:25 +00:00
|
|
|
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* PaginatorControllerComment class
|
2010-12-15 02:21:39 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2010-12-15 02:21:39 +00:00
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
class PaginatorControllerComment extends CakeTestModel {
|
2010-12-15 02:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'Comment'
|
|
|
|
*/
|
|
|
|
public $name = 'Comment';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string 'comments'
|
|
|
|
*/
|
|
|
|
public $useTable = 'comments';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* alias property
|
|
|
|
*
|
2010-12-15 04:27:12 +00:00
|
|
|
* @var string 'PaginatorControllerComment'
|
2010-12-15 02:21:39 +00:00
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
public $alias = 'PaginatorControllerComment';
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 00:34:49 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* PaginatorAuthor class
|
2011-11-14 00:34:49 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
*/
|
|
|
|
class PaginatorAuthor extends CakeTestModel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'PaginatorAuthor'
|
|
|
|
*/
|
|
|
|
public $name = 'PaginatorAuthor';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string 'authors'
|
|
|
|
*/
|
|
|
|
public $useTable = 'authors';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* alias property
|
|
|
|
*
|
|
|
|
* @var string 'PaginatorAuthor'
|
|
|
|
*/
|
|
|
|
public $alias = 'PaginatorAuthor';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* alias property
|
|
|
|
*
|
|
|
|
* @var string 'PaginatorAuthor'
|
|
|
|
*/
|
|
|
|
public $virtualFields = array(
|
|
|
|
'joined_offset' => 'PaginatorAuthor.id + 1'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-29 19:20:58 +00:00
|
|
|
/**
|
|
|
|
* PaginatorCustomPost class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
*/
|
|
|
|
class PaginatorCustomPost extends CakeTestModel {
|
2012-04-03 03:19:05 +00:00
|
|
|
|
2012-02-29 19:20:58 +00:00
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $useTable = 'posts';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* belongsTo property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $belongsTo = array('Author');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* findMethods property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $findMethods = array(
|
|
|
|
'published' => true,
|
|
|
|
'totals' => true,
|
|
|
|
'totalsOperation' => true
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* _findPublished custom find
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-04-03 03:19:05 +00:00
|
|
|
protected function _findPublished($state, $query, $results = array()) {
|
|
|
|
if ($state === 'before') {
|
|
|
|
$query['conditions']['published'] = 'Y';
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
return $results;
|
2012-02-29 19:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* _findTotals custom find
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-04-03 03:19:05 +00:00
|
|
|
protected function _findTotals($state, $query, $results = array()) {
|
2012-02-29 19:20:58 +00:00
|
|
|
if ($state == 'before') {
|
|
|
|
$query['fields'] = array('author_id');
|
|
|
|
$this->virtualFields['total_posts'] = "COUNT({$this->alias}.id)";
|
|
|
|
$query['fields'][] = 'total_posts';
|
2012-03-13 21:02:59 +00:00
|
|
|
$query['group'] = array('author_id');
|
|
|
|
$query['order'] = array('author_id' => 'ASC');
|
2012-02-29 19:20:58 +00:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
$this->virtualFields = array();
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* _findTotalsOperation custom find
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-04-03 03:19:05 +00:00
|
|
|
protected function _findTotalsOperation($state, $query, $results = array()) {
|
2012-02-29 19:20:58 +00:00
|
|
|
if ($state == 'before') {
|
|
|
|
if (!empty($query['operation']) && $query['operation'] === 'count') {
|
|
|
|
unset($query['limit']);
|
|
|
|
$query['recursive'] = -1;
|
|
|
|
$query['fields'] = array('COUNT(DISTINCT author_id) AS count');
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
$query['recursive'] = 0;
|
|
|
|
$query['callbacks'] = 'before';
|
|
|
|
$query['fields'] = array('author_id', 'Author.user');
|
|
|
|
$this->virtualFields['total_posts'] = "COUNT({$this->alias}.id)";
|
|
|
|
$query['fields'][] = 'total_posts';
|
2012-03-13 23:49:04 +00:00
|
|
|
$query['group'] = array('author_id', 'Author.user');
|
2012-03-13 21:02:59 +00:00
|
|
|
$query['order'] = array('author_id' => 'ASC');
|
2012-02-29 19:20:58 +00:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
$this->virtualFields = array();
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-13 03:00:35 +00:00
|
|
|
class PaginatorComponentTest extends CakeTestCase {
|
2010-12-15 02:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* fixtures property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-11-14 00:34:49 +00:00
|
|
|
public $fixtures = array('core.post', 'core.comment', 'core.author');
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:42:23 +00:00
|
|
|
/**
|
|
|
|
* setup
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-12-19 06:42:23 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->request = new CakeRequest('controller_posts/index');
|
|
|
|
$this->request->params['pass'] = $this->request->params['named'] = array();
|
|
|
|
$this->Controller = new Controller($this->request);
|
|
|
|
$this->Paginator = new PaginatorComponent($this->getMock('ComponentCollection'), array());
|
|
|
|
$this->Paginator->Controller = $this->Controller;
|
2010-12-19 18:53:11 +00:00
|
|
|
$this->Controller->Post = $this->getMock('Model');
|
|
|
|
$this->Controller->Post->alias = 'Post';
|
2010-12-19 06:42:23 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 02:21:39 +00:00
|
|
|
/**
|
|
|
|
* testPaginate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPaginate() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new PaginatorTestController($this->request);
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['pass'] = array('1');
|
|
|
|
$Controller->request->query = array();
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->constructClasses();
|
|
|
|
|
2012-10-25 01:25:44 +00:00
|
|
|
$Controller->Paginator->settings = array(
|
|
|
|
'order' => array('PaginatorControllerComment.id' => 'ASC')
|
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(1, 2, 3, 4, 5, 6), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2012-10-25 01:25:44 +00:00
|
|
|
$Controller->Paginator->settings = array(
|
|
|
|
'order' => array('PaginatorControllerPost.id' => 'ASC')
|
|
|
|
);
|
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
|
|
|
$this->assertEquals(array(1, 2, 3), $results);
|
|
|
|
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->modelClass = null;
|
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->uses[0] = 'Plugin.PaginatorControllerPost';
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(1, 2, 3), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('page' => '-1');
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
|
|
|
$this->assertEquals(array(1, 2, 3), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'asc');
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
|
|
|
$this->assertEquals(array(1, 2, 3), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'desc');
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
|
|
|
$this->assertEquals(array(3, 2, 1), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'id', 'direction' => 'desc');
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
|
|
|
$this->assertEquals(array(3, 2, 1), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc');
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page'], 'Invalid field in query %s');
|
|
|
|
$this->assertEquals(array(1, 2, 3), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array(
|
|
|
|
'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
|
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('PaginatorControllerPost.author_id' => 'asc'), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0]);
|
|
|
|
$this->assertEquals(array(1, 3, 2), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array();
|
2010-12-19 05:38:20 +00:00
|
|
|
$Controller->Paginator->settings = array('limit' => 0, 'maxLimit' => 10, 'paramType' => 'named');
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
|
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
|
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array();
|
2010-12-19 05:38:20 +00:00
|
|
|
$Controller->Paginator->settings = array('limit' => 'garbage!', 'maxLimit' => 10, 'paramType' => 'named');
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
|
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
|
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array();
|
2010-12-19 05:38:20 +00:00
|
|
|
$Controller->Paginator->settings = array('limit' => '-1', 'maxLimit' => 10, 'paramType' => 'named');
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->Paginator->paginate('PaginatorControllerPost');
|
2010-12-19 18:15:04 +00:00
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['limit'], 1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
|
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
|
|
|
|
$this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
|
2012-03-30 17:04:55 +00:00
|
|
|
|
|
|
|
$Controller->Paginator->settings = array('conditions' => array('PaginatorAuthor.user' => 'mariano'));
|
|
|
|
$Controller->Paginator->paginate('PaginatorControllerPost');
|
|
|
|
|
|
|
|
$this->assertSame(2, $Controller->params['paging']['PaginatorControllerPost']['count']);
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 18:53:11 +00:00
|
|
|
/**
|
|
|
|
* Test that non-numeric values are rejected for page, and limit
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPageParamCasting() {
|
2010-12-19 18:53:11 +00:00
|
|
|
$this->Controller->Post->expects($this->at(0))
|
2010-12-26 22:40:34 +00:00
|
|
|
->method('hasMethod')
|
2011-07-24 01:09:55 +00:00
|
|
|
->with('paginate')
|
2010-12-26 22:40:34 +00:00
|
|
|
->will($this->returnValue(false));
|
2011-07-24 01:09:55 +00:00
|
|
|
|
2010-12-26 22:40:34 +00:00
|
|
|
$this->Controller->Post->expects($this->at(1))
|
2010-12-19 18:53:11 +00:00
|
|
|
->method('find')
|
2011-07-24 01:09:55 +00:00
|
|
|
->will($this->returnValue(array('stuff')));
|
|
|
|
|
2010-12-26 22:40:34 +00:00
|
|
|
$this->Controller->Post->expects($this->at(2))
|
|
|
|
->method('hasMethod')
|
2011-07-24 01:09:55 +00:00
|
|
|
->with('paginateCount')
|
2010-12-26 22:40:34 +00:00
|
|
|
->will($this->returnValue(false));
|
2011-07-24 01:09:55 +00:00
|
|
|
|
2010-12-26 22:40:34 +00:00
|
|
|
$this->Controller->Post->expects($this->at(3))
|
2010-12-19 18:53:11 +00:00
|
|
|
->method('find')
|
2011-07-24 01:09:55 +00:00
|
|
|
->will($this->returnValue(2));
|
|
|
|
|
2010-12-19 18:53:11 +00:00
|
|
|
$this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
|
|
|
|
$this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
|
|
|
|
$this->Paginator->paginate('Post');
|
|
|
|
$this->assertSame(1, $this->request->params['paging']['Post']['page'], 'XSS exploit opened');
|
|
|
|
}
|
|
|
|
|
2010-12-15 02:21:39 +00:00
|
|
|
/**
|
|
|
|
* testPaginateExtraParams method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPaginateExtraParams() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new PaginatorTestController($this->request);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['pass'] = array('1');
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->params['url'] = array();
|
|
|
|
$Controller->constructClasses();
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('page' => '-1', 'contain' => array('PaginatorControllerComment'));
|
2012-10-25 01:25:44 +00:00
|
|
|
$Controller->Paginator->settings = array(
|
|
|
|
'order' => array('PaginatorControllerPost.id' => 'ASC')
|
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
2011-07-24 01:09:55 +00:00
|
|
|
$this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('page' => '-1');
|
2010-12-18 22:28:28 +00:00
|
|
|
$Controller->Paginator->settings = array(
|
2010-12-19 05:38:20 +00:00
|
|
|
'PaginatorControllerPost' => array(
|
|
|
|
'contain' => array('PaginatorControllerComment'),
|
|
|
|
'maxLimit' => 10,
|
2012-10-25 02:38:15 +00:00
|
|
|
'paramType' => 'named',
|
|
|
|
'order' => array('PaginatorControllerPost.id' => 'ASC')
|
2010-12-19 05:38:20 +00:00
|
|
|
),
|
2010-12-18 22:28:28 +00:00
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
2011-07-24 01:09:55 +00:00
|
|
|
$this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-18 22:28:28 +00:00
|
|
|
$Controller->Paginator->settings = array(
|
2010-12-19 05:38:20 +00:00
|
|
|
'PaginatorControllerPost' => array(
|
|
|
|
'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
|
|
|
|
),
|
2010-12-18 22:28:28 +00:00
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[1]['conditions']);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('limit' => 12);
|
2010-12-19 05:38:20 +00:00
|
|
|
$Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
|
2010-12-15 04:27:12 +00:00
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
|
|
|
$paging = $Controller->params['paging']['PaginatorControllerPost'];
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[1]['limit']);
|
|
|
|
$this->assertEquals(12, $paging['options']['limit']);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new PaginatorTestController($this->request);
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->uses = array('ControllerPaginateModel');
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->query = array();
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->constructClasses();
|
|
|
|
$Controller->Paginator->settings = array(
|
2010-12-18 22:28:28 +00:00
|
|
|
'ControllerPaginateModel' => array(
|
|
|
|
'contain' => array('ControllerPaginateModel'),
|
|
|
|
'group' => 'Comment.author_id',
|
2010-12-19 05:38:20 +00:00
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
2010-12-18 22:28:28 +00:00
|
|
|
)
|
2010-12-15 02:21:39 +00:00
|
|
|
);
|
|
|
|
$result = $Controller->Paginator->paginate('ControllerPaginateModel');
|
2010-12-19 05:38:20 +00:00
|
|
|
$expected = array(
|
|
|
|
'contain' => array('ControllerPaginateModel'),
|
|
|
|
'group' => 'Comment.author_id',
|
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
|
|
|
|
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
|
2010-12-15 02:21:39 +00:00
|
|
|
|
|
|
|
$Controller->Paginator->settings = array(
|
2010-12-18 22:28:28 +00:00
|
|
|
'ControllerPaginateModel' => array(
|
|
|
|
'foo', 'contain' => array('ControllerPaginateModel'),
|
|
|
|
'group' => 'Comment.author_id',
|
2010-12-19 05:38:20 +00:00
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
2010-12-18 22:28:28 +00:00
|
|
|
)
|
2010-12-15 02:21:39 +00:00
|
|
|
);
|
|
|
|
$Controller->Paginator->paginate('ControllerPaginateModel');
|
2010-12-18 22:28:28 +00:00
|
|
|
$expected = array(
|
|
|
|
'contain' => array('ControllerPaginateModel'),
|
|
|
|
'group' => 'Comment.author_id',
|
|
|
|
'type' => 'foo',
|
2010-12-19 05:38:20 +00:00
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
2010-12-18 22:28:28 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
|
|
|
|
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPaginateSpecialType() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new PaginatorTestController($this->request);
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->passedArgs[] = '1';
|
|
|
|
$Controller->params['url'] = array();
|
|
|
|
$Controller->constructClasses();
|
|
|
|
|
2010-12-18 22:28:28 +00:00
|
|
|
$Controller->Paginator->settings = array(
|
2010-12-19 05:38:20 +00:00
|
|
|
'PaginatorControllerPost' => array(
|
2010-12-19 17:58:07 +00:00
|
|
|
'popular',
|
|
|
|
'fields' => array('id', 'title'),
|
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
2010-12-19 05:38:20 +00:00
|
|
|
)
|
2010-12-18 22:28:28 +00:00
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(
|
2011-07-24 01:09:55 +00:00
|
|
|
$Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
|
2010-12-19 17:58:07 +00:00
|
|
|
array('PaginatorControllerPost.id > ' => '1')
|
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testDefaultPaginateParams method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDefaultPaginateParams() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new PaginatorTestController($this->request);
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->modelClass = 'PaginatorControllerPost';
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->params['url'] = array();
|
|
|
|
$Controller->constructClasses();
|
2010-12-19 05:38:20 +00:00
|
|
|
$Controller->Paginator->settings = array(
|
2011-10-28 05:01:17 +00:00
|
|
|
'order' => 'PaginatorControllerPost.id DESC',
|
2010-12-19 17:58:07 +00:00
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
2010-12-19 05:38:20 +00:00
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('PaginatorControllerPost.id DESC', $Controller->params['paging']['PaginatorControllerPost']['order']);
|
|
|
|
$this->assertEquals(array(3, 2, 1), $results);
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test paginate() and virtualField interactions
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPaginateOrderVirtualField() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new PaginatorTestController($this->request);
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->params['url'] = array();
|
|
|
|
$Controller->constructClasses();
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->PaginatorControllerPost->virtualFields = array(
|
|
|
|
'offset_test' => 'PaginatorControllerPost.id + 1'
|
2010-12-15 02:21:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$Controller->Paginator->settings = array(
|
|
|
|
'fields' => array('id', 'title', 'offset_test'),
|
2010-12-18 22:28:28 +00:00
|
|
|
'order' => array('offset_test' => 'DESC'),
|
2010-12-19 05:38:20 +00:00
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
2010-12-15 02:21:39 +00:00
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(4, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
|
2010-12-15 02:21:39 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
|
2010-12-15 04:27:12 +00:00
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals(array(2, 3, 4), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 00:34:49 +00:00
|
|
|
/**
|
|
|
|
* test paginate() and virtualField on joined model
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPaginateOrderVirtualFieldJoinedModel() {
|
|
|
|
$Controller = new PaginatorTestController($this->request);
|
|
|
|
$Controller->uses = array('PaginatorControllerPost');
|
|
|
|
$Controller->params['url'] = array();
|
|
|
|
$Controller->constructClasses();
|
|
|
|
$Controller->PaginatorControllerPost->recursive = 0;
|
|
|
|
$Controller->Paginator->settings = array(
|
|
|
|
'order' => array('PaginatorAuthor.joined_offset' => 'DESC'),
|
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
|
|
|
);
|
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(4, 2, 2), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
|
2011-11-14 00:34:49 +00:00
|
|
|
|
|
|
|
$Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
|
|
|
|
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(2, 2, 4), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
|
2011-11-14 00:34:49 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 03:00:26 +00:00
|
|
|
/**
|
|
|
|
* Tests for missing models
|
|
|
|
*
|
|
|
|
* @expectedException MissingModelException
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPaginateMissingModel() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new PaginatorTestController($this->request);
|
2010-12-15 02:21:39 +00:00
|
|
|
$Controller->constructClasses();
|
2010-12-18 22:28:28 +00:00
|
|
|
$Controller->Paginator->paginate('MissingModel');
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
2010-12-18 22:17:43 +00:00
|
|
|
|
2010-12-19 06:42:23 +00:00
|
|
|
/**
|
2011-10-28 05:01:17 +00:00
|
|
|
* test that option merging prefers specific models
|
2010-12-19 06:42:23 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMergeOptionsModelSpecific() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 20,
|
|
|
|
'maxLimit' => 100,
|
|
|
|
'paramType' => 'named',
|
|
|
|
'Post' => array(
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 10,
|
|
|
|
'maxLimit' => 50,
|
|
|
|
'paramType' => 'named',
|
|
|
|
)
|
|
|
|
);
|
2010-12-19 07:28:38 +00:00
|
|
|
$result = $this->Paginator->mergeOptions('Silly');
|
2010-12-19 06:42:23 +00:00
|
|
|
$this->assertEquals($this->Paginator->settings, $result);
|
|
|
|
|
2010-12-19 07:28:38 +00:00
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
|
|
|
$expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
|
2010-12-19 06:42:23 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test mergeOptions with named params.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMergeOptionsNamedParams() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$this->request->params['named'] = array(
|
|
|
|
'page' => 10,
|
|
|
|
'limit' => 10
|
|
|
|
);
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 20,
|
|
|
|
'maxLimit' => 100,
|
|
|
|
'paramType' => 'named',
|
|
|
|
);
|
2010-12-19 07:28:38 +00:00
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
2010-12-19 06:42:23 +00:00
|
|
|
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2012-07-19 13:46:06 +00:00
|
|
|
/**
|
|
|
|
* test mergeOptions with customFind key
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testMergeOptionsCustomFindKey() {
|
|
|
|
$this->request->params['named'] = array(
|
|
|
|
'page' => 10,
|
|
|
|
'limit' => 10
|
|
|
|
);
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 20,
|
|
|
|
'maxLimit' => 100,
|
|
|
|
'paramType' => 'named',
|
|
|
|
'findType' => 'myCustomFind'
|
|
|
|
);
|
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
|
|
|
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2010-12-19 06:42:23 +00:00
|
|
|
/**
|
|
|
|
* test merging options from the querystring.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMergeOptionsQueryString() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$this->request->params['named'] = array(
|
|
|
|
'page' => 10,
|
|
|
|
'limit' => 10
|
|
|
|
);
|
|
|
|
$this->request->query = array(
|
|
|
|
'page' => 99,
|
|
|
|
'limit' => 75
|
|
|
|
);
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 20,
|
|
|
|
'maxLimit' => 100,
|
|
|
|
'paramType' => 'querystring',
|
|
|
|
);
|
2010-12-19 07:28:38 +00:00
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
2010-12-19 06:42:23 +00:00
|
|
|
$expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'paramType' => 'querystring');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that the default whitelist doesn't let people screw with things they should not be allowed to.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMergeOptionsDefaultWhiteList() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$this->request->params['named'] = array(
|
|
|
|
'page' => 10,
|
|
|
|
'limit' => 10,
|
|
|
|
'fields' => array('bad.stuff'),
|
|
|
|
'recursive' => 1000,
|
|
|
|
'conditions' => array('bad.stuff'),
|
|
|
|
'contain' => array('bad')
|
|
|
|
);
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 20,
|
|
|
|
'maxLimit' => 100,
|
|
|
|
'paramType' => 'named',
|
|
|
|
);
|
2010-12-19 07:28:38 +00:00
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
2010-12-19 06:42:23 +00:00
|
|
|
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that modifying the whitelist works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMergeOptionsExtraWhitelist() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$this->request->params['named'] = array(
|
|
|
|
'page' => 10,
|
|
|
|
'limit' => 10,
|
|
|
|
'fields' => array('bad.stuff'),
|
|
|
|
'recursive' => 1000,
|
|
|
|
'conditions' => array('bad.stuff'),
|
|
|
|
'contain' => array('bad')
|
|
|
|
);
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 20,
|
|
|
|
'maxLimit' => 100,
|
|
|
|
'paramType' => 'named',
|
|
|
|
);
|
2010-12-27 02:30:43 +00:00
|
|
|
$this->Paginator->whitelist[] = 'fields';
|
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
2010-12-19 06:42:23 +00:00
|
|
|
$expected = array(
|
|
|
|
'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2010-12-19 07:11:39 +00:00
|
|
|
|
2012-11-28 14:01:41 +00:00
|
|
|
/**
|
|
|
|
* test mergeOptions with limit > maxLimit in code.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testMergeOptionsMaxLimit() {
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'limit' => 200,
|
|
|
|
'paramType' => 'named',
|
|
|
|
);
|
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
|
|
|
$expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 200, 'paramType' => 'named');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named',
|
|
|
|
);
|
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
|
|
|
$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->request->params['named'] = array(
|
|
|
|
'limit' => 500
|
|
|
|
);
|
|
|
|
$this->Paginator->settings = array(
|
|
|
|
'limit' => 150,
|
|
|
|
'paramType' => 'named',
|
|
|
|
);
|
|
|
|
$result = $this->Paginator->mergeOptions('Post');
|
|
|
|
$expected = array('page' => 1, 'limit' => 500, 'maxLimit' => 150, 'paramType' => 'named');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2010-12-19 07:11:39 +00:00
|
|
|
/**
|
|
|
|
* test that invalid directions are ignored.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateSortInvalidDirection() {
|
2010-12-19 07:11:39 +00:00
|
|
|
$model = $this->getMock('Model');
|
|
|
|
$model->alias = 'model';
|
|
|
|
$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$options = array('sort' => 'something', 'direction' => 'boogers');
|
|
|
|
$result = $this->Paginator->validateSort($model, $options);
|
|
|
|
|
|
|
|
$this->assertEquals('asc', $result['order']['model.something']);
|
|
|
|
}
|
|
|
|
|
2012-06-03 00:26:09 +00:00
|
|
|
/**
|
|
|
|
* Test that a really large page number gets clamped to the max page size.
|
2012-12-27 11:10:14 +00:00
|
|
|
*
|
|
|
|
* @expectedException NotFoundException
|
|
|
|
* @return void
|
2012-06-03 00:26:09 +00:00
|
|
|
*/
|
|
|
|
public function testOutOfRangePageNumberGetsClamped() {
|
|
|
|
$Controller = new PaginatorTestController($this->request);
|
|
|
|
$Controller->uses = array('PaginatorControllerPost');
|
|
|
|
$Controller->params['named'] = array(
|
|
|
|
'page' => 3000,
|
|
|
|
);
|
|
|
|
$Controller->constructClasses();
|
|
|
|
$Controller->PaginatorControllerPost->recursive = 0;
|
|
|
|
$Controller->Paginator->paginate('PaginatorControllerPost');
|
2012-12-27 11:10:14 +00:00
|
|
|
}
|
2012-06-03 00:56:38 +00:00
|
|
|
|
2012-12-27 11:10:14 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2012-06-03 00:56:38 +00:00
|
|
|
$Controller->paginate = array(
|
|
|
|
'conditions' => array('PaginatorControllerPost.id >' => 100)
|
|
|
|
);
|
2012-12-27 11:10:14 +00:00
|
|
|
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();
|
2012-06-03 00:26:09 +00:00
|
|
|
}
|
|
|
|
|
2010-12-27 02:30:43 +00:00
|
|
|
/**
|
|
|
|
* test that fields not in whitelist won't be part of order conditions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateSortWhitelistFailure() {
|
2010-12-27 02:30:43 +00:00
|
|
|
$model = $this->getMock('Model');
|
|
|
|
$model->alias = 'model';
|
|
|
|
$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$options = array('sort' => 'body', 'direction' => 'asc');
|
|
|
|
$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
|
|
|
|
|
|
|
|
$this->assertNull($result['order']);
|
|
|
|
}
|
|
|
|
|
2010-12-19 07:11:39 +00:00
|
|
|
/**
|
|
|
|
* test that virtual fields work.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateSortVirtualField() {
|
2010-12-19 07:11:39 +00:00
|
|
|
$model = $this->getMock('Model');
|
|
|
|
$model->alias = 'model';
|
|
|
|
|
|
|
|
$model->expects($this->at(0))
|
|
|
|
->method('hasField')
|
|
|
|
->with('something')
|
|
|
|
->will($this->returnValue(false));
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-12-19 07:11:39 +00:00
|
|
|
$model->expects($this->at(1))
|
|
|
|
->method('hasField')
|
|
|
|
->with('something', true)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$options = array('sort' => 'something', 'direction' => 'desc');
|
|
|
|
$result = $this->Paginator->validateSort($model, $options);
|
|
|
|
|
|
|
|
$this->assertEquals('desc', $result['order']['something']);
|
|
|
|
}
|
2010-12-19 07:28:38 +00:00
|
|
|
|
2011-10-12 12:51:46 +00:00
|
|
|
/**
|
|
|
|
* test that multiple sort works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateSortMultiple() {
|
|
|
|
$model = $this->getMock('Model');
|
|
|
|
$model->alias = 'model';
|
|
|
|
$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$options = array('order' => array(
|
|
|
|
'author_id' => 'asc',
|
|
|
|
'title' => 'asc'
|
|
|
|
));
|
|
|
|
$result = $this->Paginator->validateSort($model, $options);
|
|
|
|
$expected = array(
|
|
|
|
'model.author_id' => 'asc',
|
|
|
|
'model.title' => 'asc'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result['order']);
|
|
|
|
}
|
|
|
|
|
2012-01-09 02:55:51 +00:00
|
|
|
/**
|
|
|
|
* Test that no sort doesn't trigger an error.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateSortNoSort() {
|
|
|
|
$model = $this->getMock('Model');
|
|
|
|
$model->alias = 'model';
|
|
|
|
$model->expects($this->any())->method('hasField')->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$options = array('direction' => 'asc');
|
|
|
|
$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
|
|
|
|
$this->assertFalse(isset($result['order']));
|
|
|
|
|
|
|
|
$options = array('order' => 'invalid desc');
|
|
|
|
$result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
|
|
|
|
|
|
|
|
$this->assertEquals($options['order'], $result['order']);
|
|
|
|
}
|
|
|
|
|
2010-12-19 07:28:38 +00:00
|
|
|
/**
|
|
|
|
* test that maxLimit is respected
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCheckLimit() {
|
2010-12-19 07:28:38 +00:00
|
|
|
$result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
|
|
|
|
$this->assertEquals(100, $result['limit']);
|
|
|
|
|
|
|
|
$result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
|
|
|
|
$this->assertEquals(1, $result['limit']);
|
|
|
|
|
|
|
|
$result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
|
|
|
|
$this->assertEquals(1, $result['limit']);
|
|
|
|
|
|
|
|
$result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
|
|
|
|
$this->assertEquals(1, $result['limit']);
|
|
|
|
|
|
|
|
$result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
|
|
|
|
$this->assertEquals(1, $result['limit']);
|
|
|
|
}
|
2010-12-19 18:53:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* testPaginateMaxLimit
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPaginateMaxLimit() {
|
2010-12-19 18:53:11 +00:00
|
|
|
$Controller = new Controller($this->request);
|
|
|
|
|
|
|
|
$Controller->uses = array('PaginatorControllerPost', 'ControllerComment');
|
|
|
|
$Controller->passedArgs[] = '1';
|
|
|
|
$Controller->constructClasses();
|
|
|
|
|
|
|
|
$Controller->request->params['named'] = array(
|
|
|
|
'contain' => array('ControllerComment'), 'limit' => '1000'
|
|
|
|
);
|
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
|
2010-12-19 18:53:11 +00:00
|
|
|
|
|
|
|
$Controller->request->params['named'] = array(
|
|
|
|
'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
|
|
|
|
);
|
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
|
2010-12-19 18:53:11 +00:00
|
|
|
|
|
|
|
$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
|
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(10, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
|
2010-12-19 18:53:11 +00:00
|
|
|
|
|
|
|
$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
|
|
|
|
$Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
|
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
|
2010-12-19 18:53:11 +00:00
|
|
|
|
|
|
|
$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
|
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
|
2012-03-12 02:20:25 +00:00
|
|
|
}
|
2011-10-07 01:06:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test paginate() and virtualField overlapping with real fields.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
public function testPaginateOrderVirtualFieldSharedWithRealField() {
|
2012-02-23 16:27:40 +00:00
|
|
|
$Controller = new Controller($this->request);
|
2011-10-07 01:23:35 +00:00
|
|
|
$Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
|
2011-10-07 01:06:40 +00:00
|
|
|
$Controller->constructClasses();
|
2011-10-07 01:23:35 +00:00
|
|
|
$Controller->PaginatorControllerComment->virtualFields = array(
|
|
|
|
'title' => 'PaginatorControllerComment.comment'
|
2011-10-07 01:06:40 +00:00
|
|
|
);
|
2011-10-07 01:23:35 +00:00
|
|
|
$Controller->PaginatorControllerComment->bindModel(array(
|
2011-10-07 01:06:40 +00:00
|
|
|
'belongsTo' => array(
|
2011-10-07 01:23:35 +00:00
|
|
|
'PaginatorControllerPost' => array(
|
|
|
|
'className' => 'PaginatorControllerPost',
|
2011-10-07 01:06:40 +00:00
|
|
|
'foreignKey' => 'article_id'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), false);
|
|
|
|
|
|
|
|
$Controller->paginate = array(
|
2011-10-07 01:23:35 +00:00
|
|
|
'fields' => array('PaginatorControllerComment.id', 'title', 'PaginatorControllerPost.title'),
|
2011-10-07 01:06:40 +00:00
|
|
|
);
|
2011-10-07 01:23:35 +00:00
|
|
|
$Controller->passedArgs = array('sort' => 'PaginatorControllerPost.title', 'dir' => 'asc');
|
|
|
|
$result = $Controller->paginate('PaginatorControllerComment');
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->assertEquals(array(1, 2, 3, 4, 5, 6), Hash::extract($result, '{n}.PaginatorControllerComment.id'));
|
2010-12-19 18:53:11 +00:00
|
|
|
}
|
2011-10-07 01:06:40 +00:00
|
|
|
|
2012-02-29 19:20:58 +00:00
|
|
|
/**
|
|
|
|
* test paginate() and custom find, to make sure the correct count is returned.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPaginateCustomFind() {
|
2012-07-19 19:54:12 +00:00
|
|
|
$Controller = new Controller($this->request);
|
2012-02-29 19:20:58 +00:00
|
|
|
$Controller->uses = array('PaginatorCustomPost');
|
|
|
|
$Controller->constructClasses();
|
|
|
|
$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
|
|
|
|
$Controller->PaginatorCustomPost->create($data);
|
|
|
|
$result = $Controller->PaginatorCustomPost->save();
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$result = $Controller->paginate();
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals(array(1, 2, 3, 4), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
|
2012-02-29 19:20:58 +00:00
|
|
|
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(4, $result['current']);
|
|
|
|
$this->assertEquals(4, $result['count']);
|
|
|
|
|
|
|
|
$Controller->paginate = array('published');
|
|
|
|
$result = $Controller->paginate();
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
|
2012-02-29 19:20:58 +00:00
|
|
|
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(3, $result['current']);
|
|
|
|
$this->assertEquals(3, $result['count']);
|
|
|
|
|
|
|
|
$Controller->paginate = array('published', 'limit' => 2);
|
|
|
|
$result = $Controller->paginate();
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals(array(1, 2), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
|
2012-02-29 19:20:58 +00:00
|
|
|
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(2, $result['current']);
|
|
|
|
$this->assertEquals(3, $result['count']);
|
|
|
|
$this->assertEquals(2, $result['pageCount']);
|
|
|
|
$this->assertTrue($result['nextPage']);
|
|
|
|
$this->assertFalse($result['prevPage']);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test paginate() and custom find with fields array, to make sure the correct count is returned.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPaginateCustomFindFieldsArray() {
|
2012-07-19 19:54:12 +00:00
|
|
|
$Controller = new Controller($this->request);
|
2012-02-29 19:20:58 +00:00
|
|
|
$Controller->uses = array('PaginatorCustomPost');
|
|
|
|
$Controller->constructClasses();
|
|
|
|
$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
|
|
|
|
$Controller->PaginatorCustomPost->create($data);
|
|
|
|
$result = $Controller->PaginatorCustomPost->save();
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$Controller->paginate = array(
|
|
|
|
'list',
|
|
|
|
'conditions' => array('PaginatorCustomPost.published' => 'Y'),
|
|
|
|
'limit' => 2
|
|
|
|
);
|
|
|
|
$result = $Controller->paginate();
|
|
|
|
$expected = array(
|
|
|
|
1 => 'First Post',
|
|
|
|
2 => 'Second Post',
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(2, $result['current']);
|
|
|
|
$this->assertEquals(3, $result['count']);
|
|
|
|
$this->assertEquals(2, $result['pageCount']);
|
|
|
|
$this->assertTrue($result['nextPage']);
|
|
|
|
$this->assertFalse($result['prevPage']);
|
|
|
|
}
|
2012-07-19 13:46:06 +00:00
|
|
|
/**
|
|
|
|
* test paginate() and custom find with customFind key, to make sure the correct count is returned.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPaginateCustomFindWithCustomFindKey() {
|
2012-07-19 19:54:12 +00:00
|
|
|
$Controller = new Controller($this->request);
|
2012-07-19 13:46:06 +00:00
|
|
|
$Controller->uses = array('PaginatorCustomPost');
|
|
|
|
$Controller->constructClasses();
|
|
|
|
$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
|
|
|
|
$Controller->PaginatorCustomPost->create($data);
|
|
|
|
$result = $Controller->PaginatorCustomPost->save();
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$Controller->paginate = array(
|
|
|
|
'conditions' => array('PaginatorCustomPost.published' => 'Y'),
|
|
|
|
'findType' => 'list',
|
|
|
|
'limit' => 2
|
|
|
|
);
|
|
|
|
$result = $Controller->paginate();
|
|
|
|
$expected = array(
|
|
|
|
1 => 'First Post',
|
|
|
|
2 => 'Second Post',
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(2, $result['current']);
|
|
|
|
$this->assertEquals(3, $result['count']);
|
|
|
|
$this->assertEquals(2, $result['pageCount']);
|
|
|
|
$this->assertTrue($result['nextPage']);
|
|
|
|
$this->assertFalse($result['prevPage']);
|
|
|
|
}
|
2012-02-29 19:20:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test paginate() and custom find with fields array, to make sure the correct count is returned.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPaginateCustomFindGroupBy() {
|
2012-07-19 19:54:12 +00:00
|
|
|
$Controller = new Controller($this->request);
|
2012-02-29 19:20:58 +00:00
|
|
|
$Controller->uses = array('PaginatorCustomPost');
|
|
|
|
$Controller->constructClasses();
|
|
|
|
$data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
|
|
|
|
$Controller->PaginatorCustomPost->create($data);
|
|
|
|
$result = $Controller->PaginatorCustomPost->save();
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$Controller->paginate = array(
|
|
|
|
'totals',
|
|
|
|
'limit' => 2
|
|
|
|
);
|
|
|
|
$result = $Controller->paginate();
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'PaginatorCustomPost' => array(
|
|
|
|
'author_id' => '1',
|
|
|
|
'total_posts' => '2'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'PaginatorCustomPost' => array(
|
|
|
|
'author_id' => '2',
|
|
|
|
'total_posts' => '1'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(2, $result['current']);
|
|
|
|
$this->assertEquals(3, $result['count']);
|
|
|
|
$this->assertEquals(2, $result['pageCount']);
|
|
|
|
$this->assertTrue($result['nextPage']);
|
|
|
|
$this->assertFalse($result['prevPage']);
|
|
|
|
|
|
|
|
$Controller->paginate = array(
|
|
|
|
'totals',
|
|
|
|
'limit' => 2,
|
|
|
|
'page' => 2
|
|
|
|
);
|
|
|
|
$result = $Controller->paginate();
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'PaginatorCustomPost' => array(
|
|
|
|
'author_id' => '3',
|
|
|
|
'total_posts' => '1'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(1, $result['current']);
|
|
|
|
$this->assertEquals(3, $result['count']);
|
|
|
|
$this->assertEquals(2, $result['pageCount']);
|
|
|
|
$this->assertFalse($result['nextPage']);
|
|
|
|
$this->assertTrue($result['prevPage']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-13 21:02:59 +00:00
|
|
|
* test paginate() and custom find with returning other query on count operation,
|
2012-02-29 19:20:58 +00:00
|
|
|
* to make sure the correct count is returned.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPaginateCustomFindCount() {
|
2012-07-19 19:54:12 +00:00
|
|
|
$Controller = new Controller($this->request);
|
2012-02-29 19:20:58 +00:00
|
|
|
$Controller->uses = array('PaginatorCustomPost');
|
|
|
|
$Controller->constructClasses();
|
|
|
|
$data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
|
|
|
|
$Controller->PaginatorCustomPost->create($data);
|
|
|
|
$result = $Controller->PaginatorCustomPost->save();
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$Controller->paginate = array(
|
|
|
|
'totalsOperation',
|
|
|
|
'limit' => 2
|
|
|
|
);
|
|
|
|
$result = $Controller->paginate();
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'PaginatorCustomPost' => array(
|
2012-03-13 21:02:59 +00:00
|
|
|
'author_id' => '1',
|
|
|
|
'total_posts' => '2'
|
2012-02-29 19:20:58 +00:00
|
|
|
),
|
|
|
|
'Author' => array(
|
2012-03-13 21:02:59 +00:00
|
|
|
'user' => 'mariano',
|
2012-02-29 19:20:58 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'PaginatorCustomPost' => array(
|
2012-03-13 21:02:59 +00:00
|
|
|
'author_id' => '2',
|
|
|
|
'total_posts' => '1'
|
2012-02-29 19:20:58 +00:00
|
|
|
),
|
|
|
|
'Author' => array(
|
2012-03-13 21:02:59 +00:00
|
|
|
'user' => 'nate'
|
2012-02-29 19:20:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
|
|
|
$this->assertEquals(2, $result['current']);
|
|
|
|
$this->assertEquals(3, $result['count']);
|
|
|
|
$this->assertEquals(2, $result['pageCount']);
|
|
|
|
$this->assertTrue($result['nextPage']);
|
|
|
|
$this->assertFalse($result['prevPage']);
|
|
|
|
}
|
2011-09-13 03:00:35 +00:00
|
|
|
}
|