PaginatorComponentTest.php 31 KB

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