| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311 |
- <?php
- /**
- * PaginatorComponentTest file
- *
- * Series of tests for paginator component.
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.Controller.Component
- * @since CakePHP(tm) v 2.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('Controller', 'Controller');
- App::uses('PaginatorComponent', 'Controller/Component');
- App::uses('CakeRequest', 'Network');
- App::uses('CakeResponse', 'Network');
- /**
- * PaginatorTestController class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class PaginatorTestController extends Controller {
- /**
- * name property
- *
- * @var string 'PaginatorTest'
- */
- public $name = 'PaginatorTest';
- /**
- * components property
- *
- * @var array
- */
- public $components = array('Paginator');
- }
- /**
- * PaginatorControllerPost class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class PaginatorControllerPost extends CakeTestModel {
- /**
- * name property
- *
- * @var string 'PaginatorControllerPost'
- */
- public $name = 'PaginatorControllerPost';
- /**
- * useTable property
- *
- * @var string 'posts'
- */
- public $useTable = 'posts';
- /**
- * invalidFields property
- *
- * @var array
- */
- public $invalidFields = array('name' => 'error_msg');
- /**
- * lastQueries property
- *
- * @var array
- */
- public $lastQueries = array();
- /**
- * belongsTo property
- *
- * @var array
- */
- public $belongsTo = array('PaginatorAuthor' => array('foreignKey' => 'author_id'));
- /**
- * beforeFind method
- *
- * @param mixed $query
- * @return void
- */
- public function beforeFind($query) {
- array_unshift($this->lastQueries, $query);
- }
- /**
- * find method
- *
- * @param mixed $type
- * @param array $options
- * @return void
- */
- public function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
- if ($conditions === 'popular') {
- $conditions = array($this->name . '.' . $this->primaryKey . ' > ' => '1');
- $options = Hash::merge($fields, compact('conditions'));
- return parent::find('all', $options);
- }
- return parent::find($conditions, $fields);
- }
- }
- /**
- * ControllerPaginateModel class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class ControllerPaginateModel extends CakeTestModel {
- /**
- * name property
- *
- * @var string 'ControllerPaginateModel'
- */
- public $name = 'ControllerPaginateModel';
- /**
- * useTable property
- *
- * @var string 'comments'
- */
- public $useTable = 'comments';
- /**
- * paginate method
- *
- * @return boolean
- */
- public function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
- $this->extra = $extra;
- return true;
- }
- /**
- * paginateCount
- *
- * @return void
- */
- public function paginateCount($conditions, $recursive, $extra) {
- $this->extraCount = $extra;
- }
- }
- /**
- * PaginatorControllerComment class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class PaginatorControllerComment extends CakeTestModel {
- /**
- * name property
- *
- * @var string 'Comment'
- */
- public $name = 'Comment';
- /**
- * useTable property
- *
- * @var string 'comments'
- */
- public $useTable = 'comments';
- /**
- * alias property
- *
- * @var string 'PaginatorControllerComment'
- */
- public $alias = 'PaginatorControllerComment';
- }
- /**
- * PaginatorAuthor class
- *
- * @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'
- );
- }
- /**
- * PaginatorCustomPost class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class PaginatorCustomPost extends CakeTestModel {
- /**
- * 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
- */
- protected function _findPublished($state, $query, $results = array()) {
- if ($state === 'before') {
- $query['conditions']['published'] = 'Y';
- return $query;
- }
- return $results;
- }
- /**
- * _findTotals custom find
- *
- * @return array
- */
- protected function _findTotals($state, $query, $results = array()) {
- if ($state === 'before') {
- $query['fields'] = array('author_id');
- $this->virtualFields['total_posts'] = "COUNT({$this->alias}.id)";
- $query['fields'][] = 'total_posts';
- $query['group'] = array('author_id');
- $query['order'] = array('author_id' => 'ASC');
- return $query;
- }
- $this->virtualFields = array();
- return $results;
- }
- /**
- * _findTotalsOperation custom find
- *
- * @return array
- */
- protected function _findTotalsOperation($state, $query, $results = array()) {
- 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';
- $query['group'] = array('author_id', 'Author.user');
- $query['order'] = array('author_id' => 'ASC');
- return $query;
- }
- $this->virtualFields = array();
- return $results;
- }
- }
- class PaginatorComponentTest extends CakeTestCase {
- /**
- * fixtures property
- *
- * @var array
- */
- public $fixtures = array('core.post', 'core.comment', 'core.author');
- /**
- * setup
- *
- * @return void
- */
- public 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;
- $this->Controller->Post = $this->getMock('Model');
- $this->Controller->Post->alias = 'Post';
- }
- /**
- * testPaginate method
- *
- * @return void
- */
- public function testPaginate() {
- $Controller = new PaginatorTestController($this->request);
- $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
- $Controller->request->params['pass'] = array('1');
- $Controller->request->query = array();
- $Controller->constructClasses();
- $Controller->Paginator->settings = array(
- 'order' => array('PaginatorControllerComment.id' => 'ASC')
- );
- $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
- $this->assertEquals(array(1, 2, 3, 4, 5, 6), $results);
- $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);
- $Controller->modelClass = null;
- $Controller->uses[0] = 'Plugin.PaginatorControllerPost';
- $results = Hash::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
- $this->assertEquals(array(1, 2, 3), $results);
- $Controller->request->params['named'] = array('page' => '-1');
- $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
- $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertEquals(array(1, 2, 3), $results);
- $Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'asc');
- $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
- $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertEquals(array(1, 2, 3), $results);
- $Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'desc');
- $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
- $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertEquals(array(3, 2, 1), $results);
- $Controller->request->params['named'] = array('sort' => 'id', 'direction' => 'desc');
- $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
- $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertEquals(array(3, 2, 1), $results);
- $Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc');
- $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertEquals(array(), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0], 'no order should be set.');
- $Controller->request->params['named'] = array(
- 'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
- );
- $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
- $this->assertEquals(array('PaginatorControllerPost.author_id' => 'asc'), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0]);
- $this->assertEquals(array(1, 3, 2), $results);
- $Controller->request->params['named'] = array();
- $Controller->Paginator->settings = array('limit' => 0, 'maxLimit' => 10, 'paramType' => 'named');
- $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
- $Controller->request->params['named'] = array();
- $Controller->Paginator->settings = array('limit' => 'garbage!', 'maxLimit' => 10, 'paramType' => 'named');
- $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
- $Controller->request->params['named'] = array();
- $Controller->Paginator->settings = array('limit' => '-1', 'maxLimit' => 10, 'paramType' => 'named');
- $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['limit'], 1);
- $this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
- $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
- $Controller->Paginator->settings = array('conditions' => array('PaginatorAuthor.user' => 'mariano'));
- $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertSame(2, $Controller->params['paging']['PaginatorControllerPost']['count']);
- }
- /**
- * Test that non-numeric values are rejected for page, and limit
- *
- * @return void
- */
- public function testPageParamCasting() {
- $this->Controller->Post->expects($this->at(0))
- ->method('hasMethod')
- ->with('paginate')
- ->will($this->returnValue(false));
- $this->Controller->Post->expects($this->at(1))
- ->method('find')
- ->will($this->returnValue(array('stuff')));
- $this->Controller->Post->expects($this->at(2))
- ->method('hasMethod')
- ->with('paginateCount')
- ->will($this->returnValue(false));
- $this->Controller->Post->expects($this->at(3))
- ->method('find')
- ->will($this->returnValue(2));
- $this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
- $this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
- $this->Paginator->paginate('Post');
- $this->assertSame(1, $this->request->params['paging']['Post']['page'], 'XSS exploit opened');
- }
- /**
- * testPaginateExtraParams method
- *
- * @return void
- */
- public function testPaginateExtraParams() {
- $Controller = new PaginatorTestController($this->request);
- $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
- $Controller->request->params['pass'] = array('1');
- $Controller->params['url'] = array();
- $Controller->constructClasses();
- $Controller->request->params['named'] = array('page' => '-1', 'contain' => array('PaginatorControllerComment'));
- $Controller->Paginator->settings = array(
- 'order' => array('PaginatorControllerPost.id' => 'ASC')
- );
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
- $this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
- $Controller->request->params['named'] = array('page' => '-1');
- $Controller->Paginator->settings = array(
- 'PaginatorControllerPost' => array(
- 'contain' => array('PaginatorControllerComment'),
- 'maxLimit' => 10,
- 'paramType' => 'named',
- 'order' => array('PaginatorControllerPost.id' => 'ASC')
- ),
- );
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
- $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
- $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
- $Controller->Paginator->settings = array(
- 'PaginatorControllerPost' => array(
- 'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
- ),
- );
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
- $this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[1]['conditions']);
- $Controller->request->params['named'] = array('limit' => 12);
- $Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $paging = $Controller->params['paging']['PaginatorControllerPost'];
- $this->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[1]['limit']);
- $this->assertEquals(12, $paging['options']['limit']);
- $Controller = new PaginatorTestController($this->request);
- $Controller->uses = array('ControllerPaginateModel');
- $Controller->request->query = array();
- $Controller->constructClasses();
- $Controller->Paginator->settings = array(
- 'ControllerPaginateModel' => array(
- 'contain' => array('ControllerPaginateModel'),
- 'group' => 'Comment.author_id',
- 'maxLimit' => 10,
- 'paramType' => 'named'
- )
- );
- $result = $Controller->Paginator->paginate('ControllerPaginateModel');
- $expected = array(
- 'contain' => array('ControllerPaginateModel'),
- 'group' => 'Comment.author_id',
- 'maxLimit' => 10,
- 'paramType' => 'named'
- );
- $this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
- $this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
- $Controller->Paginator->settings = array(
- 'ControllerPaginateModel' => array(
- 'foo', 'contain' => array('ControllerPaginateModel'),
- 'group' => 'Comment.author_id',
- 'maxLimit' => 10,
- 'paramType' => 'named'
- )
- );
- $Controller->Paginator->paginate('ControllerPaginateModel');
- $expected = array(
- 'contain' => array('ControllerPaginateModel'),
- 'group' => 'Comment.author_id',
- 'type' => 'foo',
- 'maxLimit' => 10,
- 'paramType' => 'named'
- );
- $this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
- $this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
- }
- /**
- * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
- *
- * @return void
- */
- public function testPaginateSpecialType() {
- $Controller = new PaginatorTestController($this->request);
- $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
- $Controller->passedArgs[] = '1';
- $Controller->params['url'] = array();
- $Controller->constructClasses();
- $Controller->Paginator->settings = array(
- 'PaginatorControllerPost' => array(
- 'popular',
- 'fields' => array('id', 'title'),
- 'maxLimit' => 10,
- 'paramType' => 'named'
- )
- );
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
- $this->assertEquals(
- $Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
- array('PaginatorControllerPost.id > ' => '1')
- );
- $this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
- }
- /**
- * testDefaultPaginateParams method
- *
- * @return void
- */
- public function testDefaultPaginateParams() {
- $Controller = new PaginatorTestController($this->request);
- $Controller->modelClass = 'PaginatorControllerPost';
- $Controller->params['url'] = array();
- $Controller->constructClasses();
- $Controller->Paginator->settings = array(
- 'order' => 'PaginatorControllerPost.id DESC',
- 'maxLimit' => 10,
- 'paramType' => 'named'
- );
- $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
- $this->assertEquals('PaginatorControllerPost.id DESC', $Controller->params['paging']['PaginatorControllerPost']['order']);
- $this->assertEquals(array(3, 2, 1), $results);
- }
- /**
- * test paginate() and virtualField interactions
- *
- * @return void
- */
- public function testPaginateOrderVirtualField() {
- $Controller = new PaginatorTestController($this->request);
- $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
- $Controller->params['url'] = array();
- $Controller->constructClasses();
- $Controller->PaginatorControllerPost->virtualFields = array(
- 'offset_test' => 'PaginatorControllerPost.id + 1'
- );
- $Controller->Paginator->settings = array(
- 'fields' => array('id', 'title', 'offset_test'),
- 'order' => array('offset_test' => 'DESC'),
- 'maxLimit' => 10,
- 'paramType' => 'named'
- );
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(array(4, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
- $Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(array(2, 3, 4), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
- }
- /**
- * 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');
- $this->assertEquals(array(4, 2, 2), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
- $Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
- $result = $Controller->Paginator->paginate('PaginatorControllerPost');
- $this->assertEquals(array(2, 2, 4), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
- }
- /**
- * Tests for missing models
- *
- * @expectedException MissingModelException
- */
- public function testPaginateMissingModel() {
- $Controller = new PaginatorTestController($this->request);
- $Controller->constructClasses();
- $Controller->Paginator->paginate('MissingModel');
- }
- /**
- * test that option merging prefers specific models
- *
- * @return void
- */
- public function testMergeOptionsModelSpecific() {
- $this->Paginator->settings = array(
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'paramType' => 'named',
- 'Post' => array(
- 'page' => 1,
- 'limit' => 10,
- 'maxLimit' => 50,
- 'paramType' => 'named',
- )
- );
- $result = $this->Paginator->mergeOptions('Silly');
- $this->assertEquals($this->Paginator->settings, $result);
- $result = $this->Paginator->mergeOptions('Post');
- $expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
- $this->assertEquals($expected, $result);
- }
- /**
- * test mergeOptions with named params.
- *
- * @return void
- */
- public function testMergeOptionsNamedParams() {
- $this->request->params['named'] = array(
- 'page' => 10,
- 'limit' => 10
- );
- $this->Paginator->settings = array(
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'paramType' => 'named',
- );
- $result = $this->Paginator->mergeOptions('Post');
- $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
- $this->assertEquals($expected, $result);
- }
- /**
- * 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);
- }
- /**
- * test merging options from the querystring.
- *
- * @return void
- */
- public 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',
- );
- $result = $this->Paginator->mergeOptions('Post');
- $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
- */
- public 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',
- );
- $result = $this->Paginator->mergeOptions('Post');
- $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
- $this->assertEquals($expected, $result);
- }
- /**
- * test that modifying the whitelist works.
- *
- * @return void
- */
- public 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',
- );
- $this->Paginator->whitelist[] = 'fields';
- $result = $this->Paginator->mergeOptions('Post');
- $expected = array(
- 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * 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);
- }
- /**
- * test that invalid directions are ignored.
- *
- * @return void
- */
- public 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 a really large page number gets clamped to the max page size.
- *
- * @expectedException NotFoundException
- * @return void
- */
- 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');
- }
- /**
- * 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;
- $Controller->paginate = array(
- 'conditions' => array('PaginatorControllerPost.id >' => 100)
- );
- 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();
- }
- /**
- * test that fields not in whitelist won't be part of order conditions.
- *
- * @return void
- */
- public function testValidateSortWhitelistFailure() {
- $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']);
- }
- /**
- * test that virtual fields work.
- *
- * @return void
- */
- public 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']);
- }
- /**
- * 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']);
- }
- /**
- * 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']);
- }
- /**
- * test that maxLimit is respected
- *
- * @return void
- */
- public 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']);
- }
- /**
- * testPaginateMaxLimit
- *
- * @return void
- */
- public function testPaginateMaxLimit() {
- $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');
- $this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
- $Controller->request->params['named'] = array(
- 'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
- );
- $result = $Controller->paginate('PaginatorControllerPost');
- $this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
- $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
- $result = $Controller->paginate('PaginatorControllerPost');
- $this->assertEquals(10, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
- $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
- $Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
- $result = $Controller->paginate('PaginatorControllerPost');
- $this->assertEquals(1000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
- $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
- $result = $Controller->paginate('PaginatorControllerPost');
- $this->assertEquals(2000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
- }
- /**
- * test paginate() and virtualField overlapping with real fields.
- *
- * @return void
- */
- public function testPaginateOrderVirtualFieldSharedWithRealField() {
- $Controller = new Controller($this->request);
- $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
- $Controller->constructClasses();
- $Controller->PaginatorControllerComment->virtualFields = array(
- 'title' => 'PaginatorControllerComment.comment'
- );
- $Controller->PaginatorControllerComment->bindModel(array(
- 'belongsTo' => array(
- 'PaginatorControllerPost' => array(
- 'className' => 'PaginatorControllerPost',
- 'foreignKey' => 'article_id'
- )
- )
- ), false);
- $Controller->paginate = array(
- 'fields' => array('PaginatorControllerComment.id', 'title', 'PaginatorControllerPost.title'),
- );
- $Controller->passedArgs = array('sort' => 'PaginatorControllerPost.title', 'dir' => 'asc');
- $result = $Controller->paginate('PaginatorControllerComment');
- $this->assertEquals(array(1, 2, 3, 4, 5, 6), Hash::extract($result, '{n}.PaginatorControllerComment.id'));
- }
- /**
- * test paginate() and custom find, to make sure the correct count is returned.
- *
- * @return void
- */
- public function testPaginateCustomFind() {
- $Controller = new Controller($this->request);
- $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();
- $this->assertEquals(array(1, 2, 3, 4), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
- $result = $Controller->params['paging']['PaginatorCustomPost'];
- $this->assertEquals(4, $result['current']);
- $this->assertEquals(4, $result['count']);
- $Controller->paginate = array('published');
- $result = $Controller->paginate();
- $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
- $result = $Controller->params['paging']['PaginatorCustomPost'];
- $this->assertEquals(3, $result['current']);
- $this->assertEquals(3, $result['count']);
- $Controller->paginate = array('published', 'limit' => 2);
- $result = $Controller->paginate();
- $this->assertEquals(array(1, 2), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
- $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() {
- $Controller = new Controller($this->request);
- $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']);
- }
- /**
- * test paginate() and custom find with customFind key, to make sure the correct count is returned.
- *
- * @return void
- */
- public function testPaginateCustomFindWithCustomFindKey() {
- $Controller = new Controller($this->request);
- $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']);
- }
- /**
- * test paginate() and custom find with fields array, to make sure the correct count is returned.
- *
- * @return void
- */
- public function testPaginateCustomFindGroupBy() {
- $Controller = new Controller($this->request);
- $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']);
- }
- /**
- * test paginate() and custom find with returning other query on count operation,
- * to make sure the correct count is returned.
- *
- * @return void
- */
- public function testPaginateCustomFindCount() {
- $Controller = new Controller($this->request);
- $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(
- 'author_id' => '1',
- 'total_posts' => '2'
- ),
- 'Author' => array(
- 'user' => 'mariano',
- )
- ),
- array(
- 'PaginatorCustomPost' => array(
- 'author_id' => '2',
- 'total_posts' => '1'
- ),
- 'Author' => array(
- 'user' => 'nate'
- )
- )
- );
- $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']);
- }
- }
|