2010-12-15 02:21:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PaginatorComponentTest file
|
|
|
|
*
|
|
|
|
* Series of tests for paginator component.
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.cases.libs.controller.components
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
App::import('Controller', 'Controller', false);
|
2010-12-19 06:42:23 +00:00
|
|
|
App::import('Component', 'Paginator');
|
2010-12-15 02:21:39 +00:00
|
|
|
App::import('Core', array('CakeRequest', 'CakeResponse'));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PaginatorTestController class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
|
|
|
*/
|
|
|
|
class PaginatorTestController extends Controller {
|
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'PaginatorTest'
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $name = 'PaginatorTest';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* uses property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
//public $uses = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* components property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $components = array('Paginator');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-15 04:27:12 +00:00
|
|
|
* PaginatorControllerPost class
|
2010-12-15 02:21:39 +00:00
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
|
|
|
*/
|
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
|
|
|
* @access public
|
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
public $name = 'PaginatorControllerPost';
|
2010-12-15 02:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string 'posts'
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $useTable = 'posts';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* invalidFields property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $invalidFields = array('name' => 'error_msg');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lastQuery property
|
|
|
|
*
|
|
|
|
* @var mixed null
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $lastQuery = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* beforeFind method
|
|
|
|
*
|
|
|
|
* @param mixed $query
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function beforeFind($query) {
|
|
|
|
$this->lastQuery = $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find method
|
|
|
|
*
|
|
|
|
* @param mixed $type
|
|
|
|
* @param array $options
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
|
|
|
|
if ($conditions == 'popular') {
|
2010-12-15 02:21:39 +00:00
|
|
|
$conditions = array($this->name . '.' . $this->primaryKey .' > ' => '1');
|
2010-12-18 05:03:03 +00:00
|
|
|
$options = Set::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ControllerPaginateModel class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
|
|
|
*/
|
|
|
|
class ControllerPaginateModel extends CakeTestModel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'ControllerPaginateModel'
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $name = 'ControllerPaginateModel';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string 'comments'
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $useTable = 'comments';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* paginate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
|
|
|
|
$this->extra = $extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* paginateCount
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function paginateCount($conditions, $recursive, $extra) {
|
|
|
|
$this->extraCount = $extra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-15 04:27:12 +00:00
|
|
|
* PaginatorControllerCommentclass
|
2010-12-15 02:21:39 +00:00
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
|
|
|
*/
|
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'
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $name = 'Comment';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string 'comments'
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $useTable = 'comments';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* alias property
|
|
|
|
*
|
2010-12-15 04:27:12 +00:00
|
|
|
* @var string 'PaginatorControllerComment'
|
2010-12-15 02:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
public $alias = 'PaginatorControllerComment';
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class PaginatorTest extends CakeTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fixtures property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $fixtures = array('core.post', 'core.comment');
|
|
|
|
|
2010-12-19 06:42:23 +00:00
|
|
|
/**
|
|
|
|
* setup
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setUp() {
|
|
|
|
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-15 02:21:39 +00:00
|
|
|
/**
|
|
|
|
* testPaginate method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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();
|
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(1, 2, 3));
|
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(1, 2, 3, 4, 5, 6));
|
|
|
|
|
|
|
|
$Controller->modelClass = null;
|
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
$Controller->uses[0] = 'Plugin.PaginatorControllerPost';
|
|
|
|
$results = Set::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(1, 2, 3));
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('page' => '-1');
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(1, 2, 3));
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'asc');
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(1, 2, 3));
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'desc');
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(3, 2, 1));
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'id', 'direction' => 'desc');
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(3, 2, 1));
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc');
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1, 'Invalid field in query %s');
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(1, 2, 3));
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array(
|
|
|
|
'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
|
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
|
|
|
$this->assertEqual($Controller->PaginatorControllerPost->lastQuery['order'][0], array('PaginatorControllerPost.author_id' => 'asc'));
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(1, 3, 2));
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
|
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 17:58:07 +00:00
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1, 'XSS exploit opened');
|
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');
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
|
|
|
|
$this->assertIdentical($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');
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
|
|
|
|
$this->assertIdentical($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
|
|
|
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['limit'], 1);
|
2010-12-15 04:27:12 +00:00
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
|
|
|
|
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testPaginateExtraParams method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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'));
|
2010-12-15 04:27:12 +00:00
|
|
|
$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']));
|
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,
|
|
|
|
'paramType' => 'named'
|
|
|
|
),
|
2010-12-18 22:28:28 +00:00
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$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']));
|
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');
|
|
|
|
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
|
|
|
|
$this->assertEqual($Controller->PaginatorControllerPost->lastQuery['conditions'], array('PaginatorControllerPost.id > ' => '1'));
|
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
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
$this->assertEqual($Controller->PaginatorControllerPost->lastQuery['limit'], 12);
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($paging['options']['limit'], 12);
|
|
|
|
|
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'
|
|
|
|
);
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
|
|
|
|
$this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
|
|
|
|
|
|
|
|
$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
|
|
|
);
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
|
|
|
|
$this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
|
2010-12-19 17:58:07 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$Controller->PaginatorControllerPost->lastQuery['conditions'],
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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(
|
2010-12-19 17:58:07 +00:00
|
|
|
'order' => 'PaginatorControllerPost.id DESC',
|
|
|
|
'maxLimit' => 10,
|
|
|
|
'paramType' => 'named'
|
2010-12-19 05:38:20 +00:00
|
|
|
);
|
2010-12-15 04:27:12 +00:00
|
|
|
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
2010-12-19 17:58:07 +00:00
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['order'], 'PaginatorControllerPost.id DESC');
|
2010-12-15 02:21:39 +00:00
|
|
|
$this->assertEqual($results, array(3, 2, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test paginate() and virtualField interactions
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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');
|
|
|
|
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.offset_test'), array(4, 3, 2));
|
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');
|
|
|
|
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.offset_test'), array(2, 3, 4));
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 03:00:26 +00:00
|
|
|
/**
|
|
|
|
* Tests for missing models
|
|
|
|
*
|
|
|
|
* @expectedException MissingModelException
|
|
|
|
*/
|
2010-12-15 02:21:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* testPaginateMaxLimit
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function testPaginateMaxLimit() {
|
2010-12-19 06:42:23 +00:00
|
|
|
$Controller = new Controller($this->request);
|
2010-12-18 22:28:28 +00:00
|
|
|
|
|
|
|
$Controller->uses = array('PaginatorControllerPost', 'ControllerComment');
|
2010-12-18 22:17:43 +00:00
|
|
|
$Controller->passedArgs[] = '1';
|
|
|
|
$Controller->params['url'] = array();
|
|
|
|
$Controller->constructClasses();
|
2010-12-18 22:28:28 +00:00
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array(
|
|
|
|
'contain' => array('ControllerComment'), 'limit' => '1000'
|
|
|
|
);
|
2010-12-18 22:28:28 +00:00
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 100);
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array(
|
|
|
|
'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
|
|
|
|
);
|
2010-12-18 22:28:28 +00:00
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 100);
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
|
2010-12-18 22:28:28 +00:00
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 10);
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
|
2010-12-19 05:38:20 +00:00
|
|
|
$Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
|
2010-12-18 22:28:28 +00:00
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 1000);
|
|
|
|
|
2010-12-19 06:57:51 +00:00
|
|
|
$Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
|
2010-12-18 22:28:28 +00:00
|
|
|
$result = $Controller->paginate('PaginatorControllerPost');
|
|
|
|
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 2000);
|
2010-12-18 22:17:43 +00:00
|
|
|
}
|
2010-12-19 06:42:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that option merging prefers specific models
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testMergeOptionsModelSpecific() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testMergeOptionsNamedParams() {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test merging options from the querystring.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testMergeOptionsQueryString() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testMergeOptionsDefaultWhiteList() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testMergeOptionsExtraWhitelist() {
|
|
|
|
$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', array('fields'));
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* test that invalid directions are ignored.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidateSortInvalidDirection() {
|
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that virtual fields work.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidateSortVirtualField() {
|
|
|
|
$model = $this->getMock('Model');
|
|
|
|
$model->alias = 'model';
|
|
|
|
|
|
|
|
$model->expects($this->at(0))
|
|
|
|
->method('hasField')
|
|
|
|
->with('something')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
/**
|
|
|
|
* test that maxLimit is respected
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testCheckLimit() {
|
|
|
|
$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-15 02:21:39 +00:00
|
|
|
}
|