PaginatorComponentTest.php 24 KB

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