PaginatorComponentTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <?php
  2. /**
  3. * PaginatorComponentTest file
  4. *
  5. * Series of tests for paginator component.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @package Cake.Test.Case.Controller.Component
  18. * @since CakePHP(tm) v 2.0
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Controller', 'Controller');
  22. App::uses('PaginatorComponent', 'Controller/Component');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('CakeResponse', 'Network');
  25. /**
  26. * PaginatorTestController class
  27. *
  28. * @package Cake.Test.Case.Controller.Component
  29. */
  30. class PaginatorTestController extends Controller {
  31. /**
  32. * name property
  33. *
  34. * @var string 'PaginatorTest'
  35. */
  36. public $name = 'PaginatorTest';
  37. /**
  38. * uses property
  39. *
  40. * @var array
  41. */
  42. //public $uses = null;
  43. /**
  44. * components property
  45. *
  46. * @var array
  47. */
  48. public $components = array('Paginator');
  49. }
  50. /**
  51. * PaginatorControllerPost class
  52. *
  53. * @package Cake.Test.Case.Controller.Component
  54. */
  55. class PaginatorControllerPost extends CakeTestModel {
  56. /**
  57. * name property
  58. *
  59. * @var string 'PaginatorControllerPost'
  60. */
  61. public $name = 'PaginatorControllerPost';
  62. /**
  63. * useTable property
  64. *
  65. * @var string 'posts'
  66. */
  67. public $useTable = 'posts';
  68. /**
  69. * invalidFields property
  70. *
  71. * @var array
  72. */
  73. public $invalidFields = array('name' => 'error_msg');
  74. /**
  75. * lastQueries property
  76. *
  77. * @var array
  78. */
  79. public $lastQueries = array();
  80. /**
  81. * belongsTo property
  82. *
  83. * @var array
  84. */
  85. public $belongsTo = array('PaginatorAuthor' => array('foreignKey' => 'author_id'));
  86. /**
  87. * beforeFind method
  88. *
  89. * @param mixed $query
  90. * @return void
  91. */
  92. public function beforeFind($query) {
  93. array_unshift($this->lastQueries, $query);
  94. }
  95. /**
  96. * find method
  97. *
  98. * @param mixed $type
  99. * @param array $options
  100. * @return void
  101. */
  102. public function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
  103. if ($conditions == 'popular') {
  104. $conditions = array($this->name . '.' . $this->primaryKey .' > ' => '1');
  105. $options = Set::merge($fields, compact('conditions'));
  106. return parent::find('all', $options);
  107. }
  108. return parent::find($conditions, $fields);
  109. }
  110. }
  111. /**
  112. * ControllerPaginateModel class
  113. *
  114. * @package Cake.Test.Case.Controller.Component
  115. */
  116. class ControllerPaginateModel extends CakeTestModel {
  117. /**
  118. * name property
  119. *
  120. * @var string 'ControllerPaginateModel'
  121. */
  122. public $name = 'ControllerPaginateModel';
  123. /**
  124. * useTable property
  125. *
  126. * @var string 'comments'
  127. */
  128. public $useTable = 'comments';
  129. /**
  130. * paginate method
  131. *
  132. * @return void
  133. */
  134. public function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
  135. $this->extra = $extra;
  136. }
  137. /**
  138. * paginateCount
  139. *
  140. * @return void
  141. */
  142. public function paginateCount($conditions, $recursive, $extra) {
  143. $this->extraCount = $extra;
  144. }
  145. }
  146. /**
  147. * PaginatorControllerCommentclass
  148. *
  149. * @package Cake.Test.Case.Controller.Component
  150. */
  151. class PaginatorControllerComment extends CakeTestModel {
  152. /**
  153. * name property
  154. *
  155. * @var string 'Comment'
  156. */
  157. public $name = 'Comment';
  158. /**
  159. * useTable property
  160. *
  161. * @var string 'comments'
  162. */
  163. public $useTable = 'comments';
  164. /**
  165. * alias property
  166. *
  167. * @var string 'PaginatorControllerComment'
  168. */
  169. public $alias = 'PaginatorControllerComment';
  170. }
  171. /**
  172. * PaginatorAuthorclass
  173. *
  174. * @package Cake.Test.Case.Controller.Component
  175. */
  176. class PaginatorAuthor extends CakeTestModel {
  177. /**
  178. * name property
  179. *
  180. * @var string 'PaginatorAuthor'
  181. */
  182. public $name = 'PaginatorAuthor';
  183. /**
  184. * useTable property
  185. *
  186. * @var string 'authors'
  187. */
  188. public $useTable = 'authors';
  189. /**
  190. * alias property
  191. *
  192. * @var string 'PaginatorAuthor'
  193. */
  194. public $alias = 'PaginatorAuthor';
  195. /**
  196. * alias property
  197. *
  198. * @var string 'PaginatorAuthor'
  199. */
  200. public $virtualFields = array(
  201. 'joined_offset' => 'PaginatorAuthor.id + 1'
  202. );
  203. }
  204. class PaginatorComponentTest extends CakeTestCase {
  205. /**
  206. * fixtures property
  207. *
  208. * @var array
  209. */
  210. public $fixtures = array('core.post', 'core.comment', 'core.author');
  211. /**
  212. * setup
  213. *
  214. * @return void
  215. */
  216. public function setUp() {
  217. parent::setUp();
  218. $this->request = new CakeRequest('controller_posts/index');
  219. $this->request->params['pass'] = $this->request->params['named'] = array();
  220. $this->Controller = new Controller($this->request);
  221. $this->Paginator = new PaginatorComponent($this->getMock('ComponentCollection'), array());
  222. $this->Paginator->Controller = $this->Controller;
  223. $this->Controller->Post = $this->getMock('Model');
  224. $this->Controller->Post->alias = 'Post';
  225. }
  226. /**
  227. * testPaginate method
  228. *
  229. * @return void
  230. */
  231. public function testPaginate() {
  232. $Controller = new PaginatorTestController($this->request);
  233. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  234. $Controller->request->params['pass'] = array('1');
  235. $Controller->request->query = array();
  236. $Controller->constructClasses();
  237. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  238. $this->assertEquals($results, array(1, 2, 3));
  239. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
  240. $this->assertEquals($results, array(1, 2, 3, 4, 5, 6));
  241. $Controller->modelClass = null;
  242. $Controller->uses[0] = 'Plugin.PaginatorControllerPost';
  243. $results = Set::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
  244. $this->assertEquals($results, array(1, 2, 3));
  245. $Controller->request->params['named'] = array('page' => '-1');
  246. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  247. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  248. $this->assertEquals($results, array(1, 2, 3));
  249. $Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'asc');
  250. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  251. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  252. $this->assertEquals($results, array(1, 2, 3));
  253. $Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'desc');
  254. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  255. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  256. $this->assertEquals($results, array(3, 2, 1));
  257. $Controller->request->params['named'] = array('sort' => 'id', 'direction' => 'desc');
  258. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  259. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  260. $this->assertEquals($results, array(3, 2, 1));
  261. $Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc');
  262. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  263. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['page'], 1, 'Invalid field in query %s');
  264. $this->assertEquals($results, array(1, 2, 3));
  265. $Controller->request->params['named'] = array(
  266. 'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
  267. );
  268. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  269. $this->assertEquals($Controller->PaginatorControllerPost->lastQueries[1]['order'][0], array('PaginatorControllerPost.author_id' => 'asc'));
  270. $this->assertEquals($results, array(1, 3, 2));
  271. $Controller->request->params['named'] = array();
  272. $Controller->Paginator->settings = array('limit' => 0, 'maxLimit' => 10, 'paramType' => 'named');
  273. $Controller->Paginator->paginate('PaginatorControllerPost');
  274. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  275. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
  276. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
  277. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
  278. $Controller->request->params['named'] = array();
  279. $Controller->Paginator->settings = array('limit' => 'garbage!', 'maxLimit' => 10, 'paramType' => 'named');
  280. $Controller->Paginator->paginate('PaginatorControllerPost');
  281. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  282. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
  283. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
  284. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
  285. $Controller->request->params['named'] = array();
  286. $Controller->Paginator->settings = array('limit' => '-1', 'maxLimit' => 10, 'paramType' => 'named');
  287. $Controller->Paginator->paginate('PaginatorControllerPost');
  288. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['limit'], 1);
  289. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  290. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
  291. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
  292. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
  293. }
  294. /**
  295. * Test that non-numeric values are rejected for page, and limit
  296. *
  297. * @return void
  298. */
  299. public function testPageParamCasting() {
  300. $this->Controller->Post->expects($this->at(0))
  301. ->method('hasMethod')
  302. ->with('paginate')
  303. ->will($this->returnValue(false));
  304. $this->Controller->Post->expects($this->at(1))
  305. ->method('find')
  306. ->will($this->returnValue(array('stuff')));
  307. $this->Controller->Post->expects($this->at(2))
  308. ->method('hasMethod')
  309. ->with('paginateCount')
  310. ->will($this->returnValue(false));
  311. $this->Controller->Post->expects($this->at(3))
  312. ->method('find')
  313. ->will($this->returnValue(2));
  314. $this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
  315. $this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
  316. $this->Paginator->paginate('Post');
  317. $this->assertSame(1, $this->request->params['paging']['Post']['page'], 'XSS exploit opened');
  318. }
  319. /**
  320. * testPaginateExtraParams method
  321. *
  322. * @return void
  323. */
  324. public function testPaginateExtraParams() {
  325. $Controller = new PaginatorTestController($this->request);
  326. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  327. $Controller->request->params['pass'] = array('1');
  328. $Controller->params['url'] = array();
  329. $Controller->constructClasses();
  330. $Controller->request->params['named'] = array('page' => '-1', 'contain' => array('PaginatorControllerComment'));
  331. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  332. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  333. $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3));
  334. $this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
  335. $Controller->request->params['named'] = array('page' => '-1');
  336. $Controller->Paginator->settings = array(
  337. 'PaginatorControllerPost' => array(
  338. 'contain' => array('PaginatorControllerComment'),
  339. 'maxLimit' => 10,
  340. 'paramType' => 'named'
  341. ),
  342. );
  343. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  344. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
  345. $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3));
  346. $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
  347. $Controller->Paginator->settings = array(
  348. 'PaginatorControllerPost' => array(
  349. 'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
  350. ),
  351. );
  352. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  353. $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
  354. $this->assertEquals($Controller->PaginatorControllerPost->lastQueries[1]['conditions'], array('PaginatorControllerPost.id > ' => '1'));
  355. $Controller->request->params['named'] = array('limit' => 12);
  356. $Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
  357. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  358. $paging = $Controller->params['paging']['PaginatorControllerPost'];
  359. $this->assertEquals($Controller->PaginatorControllerPost->lastQueries[1]['limit'], 12);
  360. $this->assertEquals($paging['options']['limit'], 12);
  361. $Controller = new PaginatorTestController($this->request);
  362. $Controller->uses = array('ControllerPaginateModel');
  363. $Controller->request->query = array();
  364. $Controller->constructClasses();
  365. $Controller->Paginator->settings = array(
  366. 'ControllerPaginateModel' => array(
  367. 'contain' => array('ControllerPaginateModel'),
  368. 'group' => 'Comment.author_id',
  369. 'maxLimit' => 10,
  370. 'paramType' => 'named'
  371. )
  372. );
  373. $result = $Controller->Paginator->paginate('ControllerPaginateModel');
  374. $expected = array(
  375. 'contain' => array('ControllerPaginateModel'),
  376. 'group' => 'Comment.author_id',
  377. 'maxLimit' => 10,
  378. 'paramType' => 'named'
  379. );
  380. $this->assertEquals($Controller->ControllerPaginateModel->extra, $expected);
  381. $this->assertEquals($Controller->ControllerPaginateModel->extraCount, $expected);
  382. $Controller->Paginator->settings = array(
  383. 'ControllerPaginateModel' => array(
  384. 'foo', 'contain' => array('ControllerPaginateModel'),
  385. 'group' => 'Comment.author_id',
  386. 'maxLimit' => 10,
  387. 'paramType' => 'named'
  388. )
  389. );
  390. $Controller->Paginator->paginate('ControllerPaginateModel');
  391. $expected = array(
  392. 'contain' => array('ControllerPaginateModel'),
  393. 'group' => 'Comment.author_id',
  394. 'type' => 'foo',
  395. 'maxLimit' => 10,
  396. 'paramType' => 'named'
  397. );
  398. $this->assertEquals($Controller->ControllerPaginateModel->extra, $expected);
  399. $this->assertEquals($Controller->ControllerPaginateModel->extraCount, $expected);
  400. }
  401. /**
  402. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  403. *
  404. * @return void
  405. */
  406. public function testPaginateSpecialType() {
  407. $Controller = new PaginatorTestController($this->request);
  408. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  409. $Controller->passedArgs[] = '1';
  410. $Controller->params['url'] = array();
  411. $Controller->constructClasses();
  412. $Controller->Paginator->settings = array(
  413. 'PaginatorControllerPost' => array(
  414. 'popular',
  415. 'fields' => array('id', 'title'),
  416. 'maxLimit' => 10,
  417. 'paramType' => 'named'
  418. )
  419. );
  420. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  421. $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
  422. $this->assertEquals(
  423. $Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
  424. array('PaginatorControllerPost.id > ' => '1')
  425. );
  426. $this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
  427. }
  428. /**
  429. * testDefaultPaginateParams method
  430. *
  431. * @return void
  432. */
  433. public function testDefaultPaginateParams() {
  434. $Controller = new PaginatorTestController($this->request);
  435. $Controller->modelClass = 'PaginatorControllerPost';
  436. $Controller->params['url'] = array();
  437. $Controller->constructClasses();
  438. $Controller->Paginator->settings = array(
  439. 'order' => 'PaginatorControllerPost.id DESC',
  440. 'maxLimit' => 10,
  441. 'paramType' => 'named'
  442. );
  443. $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  444. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['order'], 'PaginatorControllerPost.id DESC');
  445. $this->assertEquals($results, array(3, 2, 1));
  446. }
  447. /**
  448. * test paginate() and virtualField interactions
  449. *
  450. * @return void
  451. */
  452. public function testPaginateOrderVirtualField() {
  453. $Controller = new PaginatorTestController($this->request);
  454. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  455. $Controller->params['url'] = array();
  456. $Controller->constructClasses();
  457. $Controller->PaginatorControllerPost->virtualFields = array(
  458. 'offset_test' => 'PaginatorControllerPost.id + 1'
  459. );
  460. $Controller->Paginator->settings = array(
  461. 'fields' => array('id', 'title', 'offset_test'),
  462. 'order' => array('offset_test' => 'DESC'),
  463. 'maxLimit' => 10,
  464. 'paramType' => 'named'
  465. );
  466. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  467. $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerPost.offset_test'), array(4, 3, 2));
  468. $Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
  469. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  470. $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerPost.offset_test'), array(2, 3, 4));
  471. }
  472. /**
  473. * test paginate() and virtualField on joined model
  474. *
  475. * @return void
  476. */
  477. public function testPaginateOrderVirtualFieldJoinedModel() {
  478. $Controller = new PaginatorTestController($this->request);
  479. $Controller->uses = array('PaginatorControllerPost');
  480. $Controller->params['url'] = array();
  481. $Controller->constructClasses();
  482. $Controller->PaginatorControllerPost->recursive = 0;
  483. $Controller->Paginator->settings = array(
  484. 'order' => array('PaginatorAuthor.joined_offset' => 'DESC'),
  485. 'maxLimit' => 10,
  486. 'paramType' => 'named'
  487. );
  488. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  489. $this->assertEquals(Set::extract($result, '{n}.PaginatorAuthor.joined_offset'), array(4, 2, 2));
  490. $Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
  491. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  492. $this->assertEquals(Set::extract($result, '{n}.PaginatorAuthor.joined_offset'), array(2, 2, 4));
  493. }
  494. /**
  495. * Tests for missing models
  496. *
  497. * @expectedException MissingModelException
  498. */
  499. public function testPaginateMissingModel() {
  500. $Controller = new PaginatorTestController($this->request);
  501. $Controller->constructClasses();
  502. $Controller->Paginator->paginate('MissingModel');
  503. }
  504. /**
  505. * test that option merging prefers specific models
  506. *
  507. * @return void
  508. */
  509. public function testMergeOptionsModelSpecific() {
  510. $this->Paginator->settings = array(
  511. 'page' => 1,
  512. 'limit' => 20,
  513. 'maxLimit' => 100,
  514. 'paramType' => 'named',
  515. 'Post' => array(
  516. 'page' => 1,
  517. 'limit' => 10,
  518. 'maxLimit' => 50,
  519. 'paramType' => 'named',
  520. )
  521. );
  522. $result = $this->Paginator->mergeOptions('Silly');
  523. $this->assertEquals($this->Paginator->settings, $result);
  524. $result = $this->Paginator->mergeOptions('Post');
  525. $expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
  526. $this->assertEquals($expected, $result);
  527. }
  528. /**
  529. * test mergeOptions with named params.
  530. *
  531. * @return void
  532. */
  533. public function testMergeOptionsNamedParams() {
  534. $this->request->params['named'] = array(
  535. 'page' => 10,
  536. 'limit' => 10
  537. );
  538. $this->Paginator->settings = array(
  539. 'page' => 1,
  540. 'limit' => 20,
  541. 'maxLimit' => 100,
  542. 'paramType' => 'named',
  543. );
  544. $result = $this->Paginator->mergeOptions('Post');
  545. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
  546. $this->assertEquals($expected, $result);
  547. }
  548. /**
  549. * test merging options from the querystring.
  550. *
  551. * @return void
  552. */
  553. public function testMergeOptionsQueryString() {
  554. $this->request->params['named'] = array(
  555. 'page' => 10,
  556. 'limit' => 10
  557. );
  558. $this->request->query = array(
  559. 'page' => 99,
  560. 'limit' => 75
  561. );
  562. $this->Paginator->settings = array(
  563. 'page' => 1,
  564. 'limit' => 20,
  565. 'maxLimit' => 100,
  566. 'paramType' => 'querystring',
  567. );
  568. $result = $this->Paginator->mergeOptions('Post');
  569. $expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'paramType' => 'querystring');
  570. $this->assertEquals($expected, $result);
  571. }
  572. /**
  573. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  574. *
  575. * @return void
  576. */
  577. public function testMergeOptionsDefaultWhiteList() {
  578. $this->request->params['named'] = array(
  579. 'page' => 10,
  580. 'limit' => 10,
  581. 'fields' => array('bad.stuff'),
  582. 'recursive' => 1000,
  583. 'conditions' => array('bad.stuff'),
  584. 'contain' => array('bad')
  585. );
  586. $this->Paginator->settings = array(
  587. 'page' => 1,
  588. 'limit' => 20,
  589. 'maxLimit' => 100,
  590. 'paramType' => 'named',
  591. );
  592. $result = $this->Paginator->mergeOptions('Post');
  593. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
  594. $this->assertEquals($expected, $result);
  595. }
  596. /**
  597. * test that modifying the whitelist works.
  598. *
  599. * @return void
  600. */
  601. public function testMergeOptionsExtraWhitelist() {
  602. $this->request->params['named'] = array(
  603. 'page' => 10,
  604. 'limit' => 10,
  605. 'fields' => array('bad.stuff'),
  606. 'recursive' => 1000,
  607. 'conditions' => array('bad.stuff'),
  608. 'contain' => array('bad')
  609. );
  610. $this->Paginator->settings = array(
  611. 'page' => 1,
  612. 'limit' => 20,
  613. 'maxLimit' => 100,
  614. 'paramType' => 'named',
  615. );
  616. $this->Paginator->whitelist[] = 'fields';
  617. $result = $this->Paginator->mergeOptions('Post');
  618. $expected = array(
  619. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
  620. );
  621. $this->assertEquals($expected, $result);
  622. }
  623. /**
  624. * test that invalid directions are ignored.
  625. *
  626. * @return void
  627. */
  628. public function testValidateSortInvalidDirection() {
  629. $model = $this->getMock('Model');
  630. $model->alias = 'model';
  631. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  632. $options = array('sort' => 'something', 'direction' => 'boogers');
  633. $result = $this->Paginator->validateSort($model, $options);
  634. $this->assertEquals('asc', $result['order']['model.something']);
  635. }
  636. /**
  637. * test that fields not in whitelist won't be part of order conditions.
  638. *
  639. * @return void
  640. */
  641. public function testValidateSortWhitelistFailure() {
  642. $model = $this->getMock('Model');
  643. $model->alias = 'model';
  644. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  645. $options = array('sort' => 'body', 'direction' => 'asc');
  646. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  647. $this->assertNull($result['order']);
  648. }
  649. /**
  650. * test that virtual fields work.
  651. *
  652. * @return void
  653. */
  654. public function testValidateSortVirtualField() {
  655. $model = $this->getMock('Model');
  656. $model->alias = 'model';
  657. $model->expects($this->at(0))
  658. ->method('hasField')
  659. ->with('something')
  660. ->will($this->returnValue(false));
  661. $model->expects($this->at(1))
  662. ->method('hasField')
  663. ->with('something', true)
  664. ->will($this->returnValue(true));
  665. $options = array('sort' => 'something', 'direction' => 'desc');
  666. $result = $this->Paginator->validateSort($model, $options);
  667. $this->assertEquals('desc', $result['order']['something']);
  668. }
  669. /**
  670. * test that multiple sort works.
  671. *
  672. * @return void
  673. */
  674. public function testValidateSortMultiple() {
  675. $model = $this->getMock('Model');
  676. $model->alias = 'model';
  677. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  678. $options = array('order' => array(
  679. 'author_id' => 'asc',
  680. 'title' => 'asc'
  681. ));
  682. $result = $this->Paginator->validateSort($model, $options);
  683. $expected = array(
  684. 'model.author_id' => 'asc',
  685. 'model.title' => 'asc'
  686. );
  687. $this->assertEquals($expected, $result['order']);
  688. }
  689. /**
  690. * Test that no sort doesn't trigger an error.
  691. *
  692. * @return void
  693. */
  694. public function testValidateSortNoSort() {
  695. $model = $this->getMock('Model');
  696. $model->alias = 'model';
  697. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  698. $options = array('direction' => 'asc');
  699. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  700. $this->assertFalse(isset($result['order']));
  701. $options = array('order' => 'invalid desc');
  702. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  703. $this->assertEquals($options['order'], $result['order']);
  704. }
  705. /**
  706. * test that maxLimit is respected
  707. *
  708. * @return void
  709. */
  710. public function testCheckLimit() {
  711. $result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
  712. $this->assertEquals(100, $result['limit']);
  713. $result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
  714. $this->assertEquals(1, $result['limit']);
  715. $result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
  716. $this->assertEquals(1, $result['limit']);
  717. $result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
  718. $this->assertEquals(1, $result['limit']);
  719. $result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
  720. $this->assertEquals(1, $result['limit']);
  721. }
  722. /**
  723. * testPaginateMaxLimit
  724. *
  725. * @return void
  726. */
  727. public function testPaginateMaxLimit() {
  728. $Controller = new Controller($this->request);
  729. $Controller->uses = array('PaginatorControllerPost', 'ControllerComment');
  730. $Controller->passedArgs[] = '1';
  731. $Controller->constructClasses();
  732. $Controller->request->params['named'] = array(
  733. 'contain' => array('ControllerComment'), 'limit' => '1000'
  734. );
  735. $result = $Controller->paginate('PaginatorControllerPost');
  736. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 100);
  737. $Controller->request->params['named'] = array(
  738. 'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
  739. );
  740. $result = $Controller->paginate('PaginatorControllerPost');
  741. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 100);
  742. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
  743. $result = $Controller->paginate('PaginatorControllerPost');
  744. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 10);
  745. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
  746. $Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
  747. $result = $Controller->paginate('PaginatorControllerPost');
  748. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 1000);
  749. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
  750. $result = $Controller->paginate('PaginatorControllerPost');
  751. $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 2000);
  752. }
  753. /**
  754. * test paginate() and virtualField overlapping with real fields.
  755. *
  756. * @return void
  757. */
  758. public function testPaginateOrderVirtualFieldSharedWithRealField() {
  759. $Controller = new Controller($this->request);
  760. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  761. $Controller->constructClasses();
  762. $Controller->PaginatorControllerComment->virtualFields = array(
  763. 'title' => 'PaginatorControllerComment.comment'
  764. );
  765. $Controller->PaginatorControllerComment->bindModel(array(
  766. 'belongsTo' => array(
  767. 'PaginatorControllerPost' => array(
  768. 'className' => 'PaginatorControllerPost',
  769. 'foreignKey' => 'article_id'
  770. )
  771. )
  772. ), false);
  773. $Controller->paginate = array(
  774. 'fields' => array('PaginatorControllerComment.id', 'title', 'PaginatorControllerPost.title'),
  775. );
  776. $Controller->passedArgs = array('sort' => 'PaginatorControllerPost.title', 'dir' => 'asc');
  777. $result = $Controller->paginate('PaginatorControllerComment');
  778. $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerComment.id'), array(1, 2, 3, 4, 5, 6));
  779. }
  780. }