PaginatorComponentTest.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\PaginatorComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\Configure;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\Http\ServerRequest;
  22. use Cake\Network\Exception\NotFoundException;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\TableRegistry;
  25. use Cake\TestSuite\TestCase;
  26. /**
  27. * PaginatorTestController class
  28. */
  29. class PaginatorTestController extends Controller
  30. {
  31. /**
  32. * components property
  33. *
  34. * @var array
  35. */
  36. public $components = ['Paginator'];
  37. }
  38. class PaginatorComponentTest extends TestCase
  39. {
  40. /**
  41. * fixtures property
  42. *
  43. * @var array
  44. */
  45. public $fixtures = ['core.posts'];
  46. /**
  47. * Don't load data for fixtures for all tests
  48. *
  49. * @var bool
  50. */
  51. public $autoFixtures = false;
  52. /**
  53. * setup
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. parent::setUp();
  60. Configure::write('App.namespace', 'TestApp');
  61. $this->request = new ServerRequest('controller_posts/index');
  62. $this->request->params['pass'] = [];
  63. $controller = new Controller($this->request);
  64. $registry = new ComponentRegistry($controller);
  65. $this->Paginator = new PaginatorComponent($registry, []);
  66. $this->Post = $this->getMockBuilder('Cake\ORM\Table')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. }
  70. /**
  71. * tearDown
  72. *
  73. * @return void
  74. */
  75. public function tearDown()
  76. {
  77. parent::tearDown();
  78. TableRegistry::clear();
  79. }
  80. /**
  81. * Test that non-numeric values are rejected for page, and limit
  82. *
  83. * @return void
  84. */
  85. public function testPageParamCasting()
  86. {
  87. $this->Post->expects($this->any())
  88. ->method('alias')
  89. ->will($this->returnValue('Posts'));
  90. $query = $this->_getMockFindQuery();
  91. $this->Post->expects($this->any())
  92. ->method('find')
  93. ->will($this->returnValue($query));
  94. $this->request->query = ['page' => '1 " onclick="alert(\'xss\');">'];
  95. $settings = ['limit' => 1, 'maxLimit' => 10];
  96. $this->Paginator->paginate($this->Post, $settings);
  97. $this->assertSame(1, $this->request->params['paging']['Posts']['page'], 'XSS exploit opened');
  98. }
  99. /**
  100. * test that unknown keys in the default settings are
  101. * passed to the find operations.
  102. *
  103. * @return void
  104. */
  105. public function testPaginateExtraParams()
  106. {
  107. $this->request->query = ['page' => '-1'];
  108. $settings = [
  109. 'PaginatorPosts' => [
  110. 'contain' => ['PaginatorAuthor'],
  111. 'maxLimit' => 10,
  112. 'group' => 'PaginatorPosts.published',
  113. 'order' => ['PaginatorPosts.id' => 'ASC']
  114. ],
  115. ];
  116. $table = $this->_getMockPosts(['query']);
  117. $query = $this->_getMockFindQuery();
  118. $table->expects($this->once())
  119. ->method('query')
  120. ->will($this->returnValue($query));
  121. $query->expects($this->once())
  122. ->method('applyOptions')
  123. ->with([
  124. 'contain' => ['PaginatorAuthor'],
  125. 'group' => 'PaginatorPosts.published',
  126. 'limit' => 10,
  127. 'order' => ['PaginatorPosts.id' => 'ASC'],
  128. 'page' => 1,
  129. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  130. 'scope' => null,
  131. ]);
  132. $this->Paginator->paginate($table, $settings);
  133. }
  134. /**
  135. * Test to make sure options get sent to custom finder methods via paginate
  136. *
  137. * @return void
  138. */
  139. public function testPaginateCustomFinderOptions()
  140. {
  141. $this->loadFixtures('Posts');
  142. $settings = [
  143. 'PaginatorPosts' => [
  144. 'finder' => ['author' => ['author_id' => 1]]
  145. ]
  146. ];
  147. $table = TableRegistry::get('PaginatorPosts');
  148. $expected = $table
  149. ->find('author', [
  150. 'conditions' => [
  151. 'PaginatorPosts.author_id' => 1
  152. ]
  153. ])
  154. ->count();
  155. $result = $this->Paginator->paginate($table, $settings)->count();
  156. $this->assertEquals($expected, $result);
  157. }
  158. /**
  159. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  160. *
  161. * @return void
  162. */
  163. public function testPaginateCustomFinder()
  164. {
  165. $settings = [
  166. 'PaginatorPosts' => [
  167. 'finder' => 'popular',
  168. 'fields' => ['id', 'title'],
  169. 'maxLimit' => 10,
  170. ]
  171. ];
  172. $table = $this->_getMockPosts(['findPopular']);
  173. $query = $this->_getMockFindQuery();
  174. $table->expects($this->any())
  175. ->method('findPopular')
  176. ->will($this->returnValue($query));
  177. $this->Paginator->paginate($table, $settings);
  178. $this->assertEquals('popular', $this->request->params['paging']['PaginatorPosts']['finder']);
  179. }
  180. /**
  181. * test that flat default pagination parameters work.
  182. *
  183. * @return void
  184. */
  185. public function testDefaultPaginateParams()
  186. {
  187. $settings = [
  188. 'order' => ['PaginatorPosts.id' => 'DESC'],
  189. 'maxLimit' => 10,
  190. ];
  191. $table = $this->_getMockPosts(['query']);
  192. $query = $this->_getMockFindQuery();
  193. $table->expects($this->once())
  194. ->method('query')
  195. ->will($this->returnValue($query));
  196. $query->expects($this->once())
  197. ->method('applyOptions')
  198. ->with([
  199. 'limit' => 10,
  200. 'page' => 1,
  201. 'order' => ['PaginatorPosts.id' => 'DESC'],
  202. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  203. 'scope' => null,
  204. ]);
  205. $this->Paginator->paginate($table, $settings);
  206. }
  207. /**
  208. * test that default sort and default direction are injected into request
  209. *
  210. * @return void
  211. */
  212. public function testDefaultPaginateParamsIntoRequest()
  213. {
  214. $settings = [
  215. 'order' => ['PaginatorPosts.id' => 'DESC'],
  216. 'maxLimit' => 10,
  217. ];
  218. $table = $this->_getMockPosts(['query']);
  219. $query = $this->_getMockFindQuery();
  220. $table->expects($this->once())
  221. ->method('query')
  222. ->will($this->returnValue($query));
  223. $query->expects($this->once())
  224. ->method('applyOptions')
  225. ->with([
  226. 'limit' => 10,
  227. 'page' => 1,
  228. 'order' => ['PaginatorPosts.id' => 'DESC'],
  229. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  230. 'scope' => null,
  231. ]);
  232. $this->Paginator->paginate($table, $settings);
  233. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
  234. $this->assertEquals('DESC', $this->request->params['paging']['PaginatorPosts']['directionDefault']);
  235. }
  236. /**
  237. * test that option merging prefers specific models
  238. *
  239. * @return void
  240. */
  241. public function testMergeOptionsModelSpecific()
  242. {
  243. $settings = [
  244. 'page' => 1,
  245. 'limit' => 20,
  246. 'maxLimit' => 100,
  247. 'Posts' => [
  248. 'page' => 1,
  249. 'limit' => 10,
  250. 'maxLimit' => 50,
  251. ],
  252. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  253. ];
  254. $result = $this->Paginator->mergeOptions('Silly', $settings);
  255. $this->assertEquals($settings, $result);
  256. $result = $this->Paginator->mergeOptions('Posts', $settings);
  257. $expected = ['page' => 1, 'limit' => 10, 'maxLimit' => 50, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  258. $this->assertEquals($expected, $result);
  259. }
  260. /**
  261. * test mergeOptions with custom scope
  262. *
  263. * @return void
  264. */
  265. public function testMergeOptionsCustomScope()
  266. {
  267. $this->request->query = [
  268. 'page' => 10,
  269. 'limit' => 10,
  270. 'scope' => [
  271. 'page' => 2,
  272. 'limit' => 5,
  273. ]
  274. ];
  275. $settings = [
  276. 'page' => 1,
  277. 'limit' => 20,
  278. 'maxLimit' => 100,
  279. 'finder' => 'myCustomFind',
  280. ];
  281. $result = $this->Paginator->mergeOptions('Post', $settings);
  282. $expected = [
  283. 'page' => 10,
  284. 'limit' => 10,
  285. 'maxLimit' => 100,
  286. 'finder' => 'myCustomFind',
  287. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  288. ];
  289. $this->assertEquals($expected, $result);
  290. $settings = [
  291. 'page' => 1,
  292. 'limit' => 20,
  293. 'maxLimit' => 100,
  294. 'finder' => 'myCustomFind',
  295. 'scope' => 'non-existent',
  296. ];
  297. $result = $this->Paginator->mergeOptions('Post', $settings);
  298. $expected = [
  299. 'page' => 1,
  300. 'limit' => 20,
  301. 'maxLimit' => 100,
  302. 'finder' => 'myCustomFind',
  303. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  304. 'scope' => 'non-existent',
  305. ];
  306. $this->assertEquals($expected, $result);
  307. $settings = [
  308. 'page' => 1,
  309. 'limit' => 20,
  310. 'maxLimit' => 100,
  311. 'finder' => 'myCustomFind',
  312. 'scope' => 'scope',
  313. ];
  314. $result = $this->Paginator->mergeOptions('Post', $settings);
  315. $expected = [
  316. 'page' => 2,
  317. 'limit' => 5,
  318. 'maxLimit' => 100,
  319. 'finder' => 'myCustomFind',
  320. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  321. 'scope' => 'scope',
  322. ];
  323. $this->assertEquals($expected, $result);
  324. }
  325. /**
  326. * test mergeOptions with customFind key
  327. *
  328. * @return void
  329. */
  330. public function testMergeOptionsCustomFindKey()
  331. {
  332. $this->request->query = [
  333. 'page' => 10,
  334. 'limit' => 10
  335. ];
  336. $settings = [
  337. 'page' => 1,
  338. 'limit' => 20,
  339. 'maxLimit' => 100,
  340. 'finder' => 'myCustomFind'
  341. ];
  342. $result = $this->Paginator->mergeOptions('Post', $settings);
  343. $expected = [
  344. 'page' => 10,
  345. 'limit' => 10,
  346. 'maxLimit' => 100,
  347. 'finder' => 'myCustomFind',
  348. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  349. ];
  350. $this->assertEquals($expected, $result);
  351. }
  352. /**
  353. * test merging options from the querystring.
  354. *
  355. * @return void
  356. */
  357. public function testMergeOptionsQueryString()
  358. {
  359. $this->request->query = [
  360. 'page' => 99,
  361. 'limit' => 75
  362. ];
  363. $settings = [
  364. 'page' => 1,
  365. 'limit' => 20,
  366. 'maxLimit' => 100,
  367. ];
  368. $result = $this->Paginator->mergeOptions('Post', $settings);
  369. $expected = ['page' => 99, 'limit' => 75, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  370. $this->assertEquals($expected, $result);
  371. }
  372. /**
  373. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  374. *
  375. * @return void
  376. */
  377. public function testMergeOptionsDefaultWhiteList()
  378. {
  379. $this->request->query = [
  380. 'page' => 10,
  381. 'limit' => 10,
  382. 'fields' => ['bad.stuff'],
  383. 'recursive' => 1000,
  384. 'conditions' => ['bad.stuff'],
  385. 'contain' => ['bad']
  386. ];
  387. $settings = [
  388. 'page' => 1,
  389. 'limit' => 20,
  390. 'maxLimit' => 100,
  391. ];
  392. $result = $this->Paginator->mergeOptions('Post', $settings);
  393. $expected = ['page' => 10, 'limit' => 10, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  394. $this->assertEquals($expected, $result);
  395. }
  396. /**
  397. * test that modifying the whitelist works.
  398. *
  399. * @return void
  400. */
  401. public function testMergeOptionsExtraWhitelist()
  402. {
  403. $this->request->query = [
  404. 'page' => 10,
  405. 'limit' => 10,
  406. 'fields' => ['bad.stuff'],
  407. 'recursive' => 1000,
  408. 'conditions' => ['bad.stuff'],
  409. 'contain' => ['bad']
  410. ];
  411. $settings = [
  412. 'page' => 1,
  413. 'limit' => 20,
  414. 'maxLimit' => 100,
  415. ];
  416. $this->Paginator->config('whitelist', ['fields']);
  417. $result = $this->Paginator->mergeOptions('Post', $settings);
  418. $expected = [
  419. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => ['bad.stuff'], 'whitelist' => ['limit', 'sort', 'page', 'direction', 'fields']
  420. ];
  421. $this->assertEquals($expected, $result);
  422. }
  423. /**
  424. * test mergeOptions with limit > maxLimit in code.
  425. *
  426. * @return void
  427. */
  428. public function testMergeOptionsMaxLimit()
  429. {
  430. $settings = [
  431. 'limit' => 200,
  432. 'paramType' => 'named',
  433. ];
  434. $result = $this->Paginator->mergeOptions('Post', $settings);
  435. $expected = [
  436. 'page' => 1,
  437. 'limit' => 100,
  438. 'maxLimit' => 100,
  439. 'paramType' => 'named',
  440. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  441. ];
  442. $this->assertEquals($expected, $result);
  443. $settings = [
  444. 'maxLimit' => 10,
  445. 'paramType' => 'named',
  446. ];
  447. $result = $this->Paginator->mergeOptions('Post', $settings);
  448. $expected = [
  449. 'page' => 1,
  450. 'limit' => 10,
  451. 'maxLimit' => 10,
  452. 'paramType' => 'named',
  453. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  454. ];
  455. $this->assertEquals($expected, $result);
  456. }
  457. /**
  458. * test getDefaults with limit > maxLimit in code.
  459. *
  460. * @return void
  461. */
  462. public function testGetDefaultMaxLimit()
  463. {
  464. $settings = [
  465. 'page' => 1,
  466. 'limit' => 2,
  467. 'maxLimit' => 10,
  468. 'order' => [
  469. 'Users.username' => 'asc'
  470. ],
  471. ];
  472. $result = $this->Paginator->mergeOptions('Post', $settings);
  473. $expected = [
  474. 'page' => 1,
  475. 'limit' => 2,
  476. 'maxLimit' => 10,
  477. 'order' => [
  478. 'Users.username' => 'asc'
  479. ],
  480. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  481. ];
  482. $this->assertEquals($expected, $result);
  483. $settings = [
  484. 'page' => 1,
  485. 'limit' => 100,
  486. 'maxLimit' => 10,
  487. 'order' => [
  488. 'Users.username' => 'asc'
  489. ],
  490. ];
  491. $result = $this->Paginator->mergeOptions('Post', $settings);
  492. $expected = [
  493. 'page' => 1,
  494. 'limit' => 10,
  495. 'maxLimit' => 10,
  496. 'order' => [
  497. 'Users.username' => 'asc'
  498. ],
  499. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  500. ];
  501. $this->assertEquals($expected, $result);
  502. }
  503. /**
  504. * Integration test to ensure that validateSort is being used by paginate()
  505. *
  506. * @return void
  507. */
  508. public function testValidateSortInvalid()
  509. {
  510. $table = $this->_getMockPosts(['query']);
  511. $query = $this->_getMockFindQuery();
  512. $table->expects($this->once())
  513. ->method('query')
  514. ->will($this->returnValue($query));
  515. $query->expects($this->once())->method('applyOptions')
  516. ->with([
  517. 'limit' => 20,
  518. 'page' => 1,
  519. 'order' => ['PaginatorPosts.id' => 'asc'],
  520. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  521. 'scope' => null,
  522. ]);
  523. $this->request->query = [
  524. 'page' => 1,
  525. 'sort' => 'id',
  526. 'direction' => 'herp'
  527. ];
  528. $this->Paginator->paginate($table);
  529. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sort']);
  530. $this->assertEquals('asc', $this->request->params['paging']['PaginatorPosts']['direction']);
  531. }
  532. /**
  533. * test that invalid directions are ignored.
  534. *
  535. * @return void
  536. */
  537. public function testValidateSortInvalidDirection()
  538. {
  539. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  540. $model->expects($this->any())
  541. ->method('alias')
  542. ->will($this->returnValue('model'));
  543. $model->expects($this->any())
  544. ->method('hasField')
  545. ->will($this->returnValue(true));
  546. $options = ['sort' => 'something', 'direction' => 'boogers'];
  547. $result = $this->Paginator->validateSort($model, $options);
  548. $this->assertEquals('asc', $result['order']['model.something']);
  549. }
  550. /**
  551. * Test that a really large page number gets clamped to the max page size.
  552. *
  553. * @return void
  554. */
  555. public function testOutOfRangePageNumberGetsClamped()
  556. {
  557. $this->loadFixtures('Posts');
  558. $this->request->query['page'] = 3000;
  559. $table = TableRegistry::get('PaginatorPosts');
  560. try {
  561. $this->Paginator->paginate($table);
  562. $this->fail('No exception raised');
  563. } catch (NotFoundException $e) {
  564. $this->assertEquals(
  565. 1,
  566. $this->request->params['paging']['PaginatorPosts']['page'],
  567. 'Page number should not be 0'
  568. );
  569. }
  570. }
  571. /**
  572. * Test that a really REALLY large page number gets clamped to the max page size.
  573. *
  574. * @expectedException \Cake\Network\Exception\NotFoundException
  575. * @return void
  576. */
  577. public function testOutOfVeryBigPageNumberGetsClamped()
  578. {
  579. $this->loadFixtures('Posts');
  580. $this->request->query = [
  581. 'page' => '3000000000000000000000000',
  582. ];
  583. $table = TableRegistry::get('PaginatorPosts');
  584. $this->Paginator->paginate($table);
  585. }
  586. /**
  587. * test that fields not in whitelist won't be part of order conditions.
  588. *
  589. * @return void
  590. */
  591. public function testValidateSortWhitelistFailure()
  592. {
  593. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  594. $model->expects($this->any())
  595. ->method('alias')
  596. ->will($this->returnValue('model'));
  597. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  598. $options = [
  599. 'sort' => 'body',
  600. 'direction' => 'asc',
  601. 'sortWhitelist' => ['title', 'id']
  602. ];
  603. $result = $this->Paginator->validateSort($model, $options);
  604. $this->assertEquals([], $result['order']);
  605. }
  606. /**
  607. * test that fields in the whitelist are not validated
  608. *
  609. * @return void
  610. */
  611. public function testValidateSortWhitelistTrusted()
  612. {
  613. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  614. $model->expects($this->any())
  615. ->method('alias')
  616. ->will($this->returnValue('model'));
  617. $model->expects($this->once())
  618. ->method('hasField')
  619. ->will($this->returnValue(true));
  620. $options = [
  621. 'sort' => 'body',
  622. 'direction' => 'asc',
  623. 'sortWhitelist' => ['body']
  624. ];
  625. $result = $this->Paginator->validateSort($model, $options);
  626. $expected = ['model.body' => 'asc'];
  627. $this->assertEquals(
  628. $expected,
  629. $result['order'],
  630. 'Trusted fields in schema should be prefixed'
  631. );
  632. }
  633. /**
  634. * test that whitelist as empty array does not allow any sorting
  635. *
  636. * @return void
  637. */
  638. public function testValidateSortWhitelistEmpty()
  639. {
  640. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  641. $model->expects($this->any())
  642. ->method('alias')
  643. ->will($this->returnValue('model'));
  644. $model->expects($this->any())->method('hasField')
  645. ->will($this->returnValue(true));
  646. $options = [
  647. 'order' => [
  648. 'body' => 'asc',
  649. 'foo.bar' => 'asc'
  650. ],
  651. 'sort' => 'body',
  652. 'direction' => 'asc',
  653. 'sortWhitelist' => []
  654. ];
  655. $result = $this->Paginator->validateSort($model, $options);
  656. $this->assertSame([], $result['order'], 'No sort should be applied');
  657. }
  658. /**
  659. * test that fields in the whitelist are not validated
  660. *
  661. * @return void
  662. */
  663. public function testValidateSortWhitelistNotInSchema()
  664. {
  665. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  666. $model->expects($this->any())
  667. ->method('alias')
  668. ->will($this->returnValue('model'));
  669. $model->expects($this->once())->method('hasField')
  670. ->will($this->returnValue(false));
  671. $options = [
  672. 'sort' => 'score',
  673. 'direction' => 'asc',
  674. 'sortWhitelist' => ['score']
  675. ];
  676. $result = $this->Paginator->validateSort($model, $options);
  677. $expected = ['score' => 'asc'];
  678. $this->assertEquals(
  679. $expected,
  680. $result['order'],
  681. 'Trusted fields not in schema should not be altered'
  682. );
  683. }
  684. /**
  685. * test that multiple fields in the whitelist are not validated and properly aliased.
  686. *
  687. * @return void
  688. */
  689. public function testValidateSortWhitelistMultiple()
  690. {
  691. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  692. $model->expects($this->any())
  693. ->method('alias')
  694. ->will($this->returnValue('model'));
  695. $model->expects($this->once())
  696. ->method('hasField')
  697. ->will($this->returnValue(true));
  698. $options = [
  699. 'order' => [
  700. 'body' => 'asc',
  701. 'foo.bar' => 'asc'
  702. ],
  703. 'sortWhitelist' => ['body', 'foo.bar']
  704. ];
  705. $result = $this->Paginator->validateSort($model, $options);
  706. $expected = [
  707. 'model.body' => 'asc',
  708. 'foo.bar' => 'asc'
  709. ];
  710. $this->assertEquals($expected, $result['order']);
  711. }
  712. /**
  713. * test that multiple sort works.
  714. *
  715. * @return void
  716. */
  717. public function testValidateSortMultiple()
  718. {
  719. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  720. $model->expects($this->any())
  721. ->method('alias')
  722. ->will($this->returnValue('model'));
  723. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  724. $options = [
  725. 'order' => [
  726. 'author_id' => 'asc',
  727. 'title' => 'asc'
  728. ]
  729. ];
  730. $result = $this->Paginator->validateSort($model, $options);
  731. $expected = [
  732. 'model.author_id' => 'asc',
  733. 'model.title' => 'asc'
  734. ];
  735. $this->assertEquals($expected, $result['order']);
  736. }
  737. /**
  738. * Tests that order strings can used by Paginator
  739. *
  740. * @return void
  741. */
  742. public function testValidateSortWithString()
  743. {
  744. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  745. $model->expects($this->any())
  746. ->method('alias')
  747. ->will($this->returnValue('model'));
  748. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  749. $options = [
  750. 'order' => 'model.author_id DESC'
  751. ];
  752. $result = $this->Paginator->validateSort($model, $options);
  753. $expected = 'model.author_id DESC';
  754. $this->assertEquals($expected, $result['order']);
  755. }
  756. /**
  757. * Test that no sort doesn't trigger an error.
  758. *
  759. * @return void
  760. */
  761. public function testValidateSortNoSort()
  762. {
  763. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  764. $model->expects($this->any())
  765. ->method('alias')
  766. ->will($this->returnValue('model'));
  767. $model->expects($this->any())->method('hasField')
  768. ->will($this->returnValue(true));
  769. $options = [
  770. 'direction' => 'asc',
  771. 'sortWhitelist' => ['title', 'id'],
  772. ];
  773. $result = $this->Paginator->validateSort($model, $options);
  774. $this->assertEquals([], $result['order']);
  775. }
  776. /**
  777. * Test sorting with incorrect aliases on valid fields.
  778. *
  779. * @return void
  780. */
  781. public function testValidateSortInvalidAlias()
  782. {
  783. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  784. $model->expects($this->any())
  785. ->method('alias')
  786. ->will($this->returnValue('model'));
  787. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  788. $options = ['sort' => 'Derp.id'];
  789. $result = $this->Paginator->validateSort($model, $options);
  790. $this->assertEquals([], $result['order']);
  791. }
  792. /**
  793. * @return array
  794. */
  795. public function checkLimitProvider()
  796. {
  797. return [
  798. 'out of bounds' => [
  799. ['limit' => 1000000, 'maxLimit' => 100],
  800. 100,
  801. ],
  802. 'limit is nan' => [
  803. ['limit' => 'sheep!', 'maxLimit' => 100],
  804. 1,
  805. ],
  806. 'negative limit' => [
  807. ['limit' => '-1', 'maxLimit' => 100],
  808. 1,
  809. ],
  810. 'unset limit' => [
  811. ['limit' => null, 'maxLimit' => 100],
  812. 1,
  813. ],
  814. 'limit = 0' => [
  815. ['limit' => 0, 'maxLimit' => 100],
  816. 1,
  817. ],
  818. 'limit = 0 v2' => [
  819. ['limit' => 0, 'maxLimit' => 0],
  820. 1,
  821. ],
  822. 'limit = null' => [
  823. ['limit' => null, 'maxLimit' => 0],
  824. 1,
  825. ],
  826. 'bad input, results in 1' => [
  827. ['limit' => null, 'maxLimit' => null],
  828. 1,
  829. ],
  830. 'bad input, results in 1 v2' => [
  831. ['limit' => false, 'maxLimit' => false],
  832. 1,
  833. ],
  834. ];
  835. }
  836. /**
  837. * test that maxLimit is respected
  838. *
  839. * @dataProvider checkLimitProvider
  840. * @return void
  841. */
  842. public function testCheckLimit($input, $expected)
  843. {
  844. $result = $this->Paginator->checkLimit($input);
  845. $this->assertSame($expected, $result['limit']);
  846. }
  847. /**
  848. * Integration test for checkLimit() being applied inside paginate()
  849. *
  850. * @return void
  851. */
  852. public function testPaginateMaxLimit()
  853. {
  854. $this->loadFixtures('Posts');
  855. $table = TableRegistry::get('PaginatorPosts');
  856. $settings = [
  857. 'maxLimit' => 100,
  858. ];
  859. $this->request->query = [
  860. 'limit' => '1000'
  861. ];
  862. $this->Paginator->paginate($table, $settings);
  863. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['limit']);
  864. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['perPage']);
  865. $this->request->query = [
  866. 'limit' => '10'
  867. ];
  868. $this->Paginator->paginate($table, $settings);
  869. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
  870. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['perPage']);
  871. }
  872. /**
  873. * test paginate() and custom find, to make sure the correct count is returned.
  874. *
  875. * @return void
  876. */
  877. public function testPaginateCustomFind()
  878. {
  879. $this->loadFixtures('Posts');
  880. $titleExtractor = function ($result) {
  881. $ids = [];
  882. foreach ($result as $record) {
  883. $ids[] = $record->title;
  884. }
  885. return $ids;
  886. };
  887. $table = TableRegistry::get('PaginatorPosts');
  888. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  889. $result = $table->save(new Entity($data));
  890. $this->assertNotEmpty($result);
  891. $result = $this->Paginator->paginate($table);
  892. $this->assertCount(4, $result, '4 rows should come back');
  893. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  894. $result = $this->request->params['paging']['PaginatorPosts'];
  895. $this->assertEquals(4, $result['current']);
  896. $this->assertEquals(4, $result['count']);
  897. $settings = ['finder' => 'published'];
  898. $result = $this->Paginator->paginate($table, $settings);
  899. $this->assertCount(3, $result, '3 rows should come back');
  900. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  901. $result = $this->request->params['paging']['PaginatorPosts'];
  902. $this->assertEquals(3, $result['current']);
  903. $this->assertEquals(3, $result['count']);
  904. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  905. $result = $this->Paginator->paginate($table, $settings);
  906. $this->assertCount(1, $result, '1 rows should come back');
  907. $this->assertEquals(['Third Post'], $titleExtractor($result));
  908. $result = $this->request->params['paging']['PaginatorPosts'];
  909. $this->assertEquals(1, $result['current']);
  910. $this->assertEquals(3, $result['count']);
  911. $this->assertEquals(2, $result['pageCount']);
  912. $settings = ['finder' => 'published', 'limit' => 2];
  913. $result = $this->Paginator->paginate($table, $settings);
  914. $this->assertCount(2, $result, '2 rows should come back');
  915. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  916. $result = $this->request->params['paging']['PaginatorPosts'];
  917. $this->assertEquals(2, $result['current']);
  918. $this->assertEquals(3, $result['count']);
  919. $this->assertEquals(2, $result['pageCount']);
  920. $this->assertTrue($result['nextPage']);
  921. $this->assertFalse($result['prevPage']);
  922. $this->assertEquals(2, $result['perPage']);
  923. $this->assertNull($result['limit']);
  924. }
  925. /**
  926. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  927. *
  928. * @return void
  929. */
  930. public function testPaginateCustomFindFieldsArray()
  931. {
  932. $this->loadFixtures('Posts');
  933. $table = TableRegistry::get('PaginatorPosts');
  934. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  935. $table->save(new Entity($data));
  936. $settings = [
  937. 'finder' => 'list',
  938. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  939. 'limit' => 2
  940. ];
  941. $results = $this->Paginator->paginate($table, $settings);
  942. $result = $results->toArray();
  943. $expected = [
  944. 1 => 'First Post',
  945. 2 => 'Second Post',
  946. ];
  947. $this->assertEquals($expected, $result);
  948. $result = $this->request->params['paging']['PaginatorPosts'];
  949. $this->assertEquals(2, $result['current']);
  950. $this->assertEquals(3, $result['count']);
  951. $this->assertEquals(2, $result['pageCount']);
  952. $this->assertTrue($result['nextPage']);
  953. $this->assertFalse($result['prevPage']);
  954. }
  955. /**
  956. * test paginate() and custom finders to ensure the count + find
  957. * use the custom type.
  958. *
  959. * @return void
  960. */
  961. public function testPaginateCustomFindCount()
  962. {
  963. $settings = [
  964. 'finder' => 'published',
  965. 'limit' => 2
  966. ];
  967. $table = $this->_getMockPosts(['query']);
  968. $query = $this->_getMockFindQuery();
  969. $table->expects($this->once())
  970. ->method('query')
  971. ->will($this->returnValue($query));
  972. $query->expects($this->once())->method('applyOptions')
  973. ->with([
  974. 'limit' => 2,
  975. 'page' => 1,
  976. 'order' => [],
  977. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  978. 'scope' => null,
  979. ]);
  980. $this->Paginator->paginate($table, $settings);
  981. }
  982. /**
  983. * Tests that it is possible to pass an already made query object to
  984. * paginate()
  985. *
  986. * @return void
  987. */
  988. public function testPaginateQuery()
  989. {
  990. $this->request->query = ['page' => '-1'];
  991. $settings = [
  992. 'PaginatorPosts' => [
  993. 'contain' => ['PaginatorAuthor'],
  994. 'maxLimit' => 10,
  995. 'group' => 'PaginatorPosts.published',
  996. 'order' => ['PaginatorPosts.id' => 'ASC']
  997. ]
  998. ];
  999. $table = $this->_getMockPosts(['find']);
  1000. $query = $this->_getMockFindQuery($table);
  1001. $table->expects($this->never())->method('find');
  1002. $query->expects($this->once())
  1003. ->method('applyOptions')
  1004. ->with([
  1005. 'contain' => ['PaginatorAuthor'],
  1006. 'group' => 'PaginatorPosts.published',
  1007. 'limit' => 10,
  1008. 'order' => ['PaginatorPosts.id' => 'ASC'],
  1009. 'page' => 1,
  1010. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1011. 'scope' => null,
  1012. ]);
  1013. $this->Paginator->paginate($query, $settings);
  1014. }
  1015. /**
  1016. * test paginate() with bind()
  1017. *
  1018. * @return void
  1019. */
  1020. public function testPaginateQueryWithBindValue()
  1021. {
  1022. $config = ConnectionManager::config('test');
  1023. $this->skipIf(strpos($config['driver'], 'Sqlserver') !== false, 'Test temporarily broken in SQLServer');
  1024. $this->loadFixtures('Posts');
  1025. $table = TableRegistry::get('PaginatorPosts');
  1026. $query = $table->find()
  1027. ->where(['PaginatorPosts.author_id BETWEEN :start AND :end'])
  1028. ->bind(':start', 1)
  1029. ->bind(':end', 2);
  1030. $results = $this->Paginator->paginate($query, []);
  1031. $result = $results->toArray();
  1032. $this->assertCount(2, $result);
  1033. $this->assertEquals('First Post', $result[0]->title);
  1034. $this->assertEquals('Third Post', $result[1]->title);
  1035. }
  1036. /**
  1037. * Tests that passing a query object with a limit clause set will
  1038. * overwrite it with the passed defaults.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testPaginateQueryWithLimit()
  1043. {
  1044. $this->request->query = ['page' => '-1'];
  1045. $settings = [
  1046. 'PaginatorPosts' => [
  1047. 'contain' => ['PaginatorAuthor'],
  1048. 'maxLimit' => 10,
  1049. 'limit' => 5,
  1050. 'group' => 'PaginatorPosts.published',
  1051. 'order' => ['PaginatorPosts.id' => 'ASC']
  1052. ]
  1053. ];
  1054. $table = $this->_getMockPosts(['find']);
  1055. $query = $this->_getMockFindQuery($table);
  1056. $query->limit(2);
  1057. $table->expects($this->never())->method('find');
  1058. $query->expects($this->once())
  1059. ->method('applyOptions')
  1060. ->with([
  1061. 'contain' => ['PaginatorAuthor'],
  1062. 'group' => 'PaginatorPosts.published',
  1063. 'limit' => 5,
  1064. 'order' => ['PaginatorPosts.id' => 'ASC'],
  1065. 'page' => 1,
  1066. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1067. 'scope' => null,
  1068. ]);
  1069. $this->Paginator->paginate($query, $settings);
  1070. }
  1071. /**
  1072. * Helper method for making mocks.
  1073. *
  1074. * @param array $methods
  1075. * @return \Cake\ORM\Table
  1076. */
  1077. protected function _getMockPosts($methods = [])
  1078. {
  1079. return $this->getMockBuilder('TestApp\Model\Table\PaginatorPostsTable')
  1080. ->setMethods($methods)
  1081. ->setConstructorArgs([[
  1082. 'connection' => ConnectionManager::get('test'),
  1083. 'alias' => 'PaginatorPosts',
  1084. 'schema' => [
  1085. 'id' => ['type' => 'integer'],
  1086. 'author_id' => ['type' => 'integer', 'null' => false],
  1087. 'title' => ['type' => 'string', 'null' => false],
  1088. 'body' => 'text',
  1089. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  1090. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  1091. ]
  1092. ]])
  1093. ->getMock();
  1094. }
  1095. /**
  1096. * Helper method for mocking queries.
  1097. *
  1098. * @return \Cake\ORM\Query
  1099. */
  1100. protected function _getMockFindQuery($table = null)
  1101. {
  1102. $query = $this->getMockBuilder('Cake\ORM\Query')
  1103. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  1104. ->disableOriginalConstructor()
  1105. ->getMock();
  1106. $results = $this->getMockBuilder('Cake\ORM\ResultSet')
  1107. ->disableOriginalConstructor()
  1108. ->getMock();
  1109. $query->expects($this->any())
  1110. ->method('count')
  1111. ->will($this->returnValue(2));
  1112. $query->expects($this->any())
  1113. ->method('all')
  1114. ->will($this->returnValue($results));
  1115. $query->expects($this->any())
  1116. ->method('count')
  1117. ->will($this->returnValue(2));
  1118. $query->repository($table);
  1119. return $query;
  1120. }
  1121. }