PaginatorComponentTest.php 33 KB

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