PaginatorComponentTest.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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\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 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->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' => 200,
  438. 'maxLimit' => 200,
  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' => 20,
  451. 'maxLimit' => 10,
  452. 'paramType' => 'named',
  453. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  454. ];
  455. $this->assertEquals($expected, $result);
  456. }
  457. /**
  458. * Integration test to ensure that validateSort is being used by paginate()
  459. *
  460. * @return void
  461. */
  462. public function testValidateSortInvalid()
  463. {
  464. $table = $this->_getMockPosts(['query']);
  465. $query = $this->_getMockFindQuery();
  466. $table->expects($this->once())
  467. ->method('query')
  468. ->will($this->returnValue($query));
  469. $query->expects($this->once())->method('applyOptions')
  470. ->with([
  471. 'limit' => 20,
  472. 'page' => 1,
  473. 'order' => ['PaginatorPosts.id' => 'asc'],
  474. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  475. 'scope' => null,
  476. ]);
  477. $this->request->query = [
  478. 'page' => 1,
  479. 'sort' => 'id',
  480. 'direction' => 'herp'
  481. ];
  482. $this->Paginator->paginate($table);
  483. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sort']);
  484. $this->assertEquals('asc', $this->request->params['paging']['PaginatorPosts']['direction']);
  485. }
  486. /**
  487. * test that invalid directions are ignored.
  488. *
  489. * @return void
  490. */
  491. public function testValidateSortInvalidDirection()
  492. {
  493. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  494. $model->expects($this->any())
  495. ->method('alias')
  496. ->will($this->returnValue('model'));
  497. $model->expects($this->any())
  498. ->method('hasField')
  499. ->will($this->returnValue(true));
  500. $options = ['sort' => 'something', 'direction' => 'boogers'];
  501. $result = $this->Paginator->validateSort($model, $options);
  502. $this->assertEquals('asc', $result['order']['model.something']);
  503. }
  504. /**
  505. * Test that a really large page number gets clamped to the max page size.
  506. *
  507. * @return void
  508. */
  509. public function testOutOfRangePageNumberGetsClamped()
  510. {
  511. $this->loadFixtures('Posts');
  512. $this->request->query['page'] = 3000;
  513. $table = TableRegistry::get('PaginatorPosts');
  514. try {
  515. $this->Paginator->paginate($table);
  516. $this->fail('No exception raised');
  517. } catch (NotFoundException $e) {
  518. $this->assertEquals(
  519. 1,
  520. $this->request->params['paging']['PaginatorPosts']['page'],
  521. 'Page number should not be 0'
  522. );
  523. }
  524. }
  525. /**
  526. * Test that a really REALLY large page number gets clamped to the max page size.
  527. *
  528. * @expectedException \Cake\Network\Exception\NotFoundException
  529. * @return void
  530. */
  531. public function testOutOfVeryBigPageNumberGetsClamped()
  532. {
  533. $this->loadFixtures('Posts');
  534. $this->request->query = [
  535. 'page' => '3000000000000000000000000',
  536. ];
  537. $table = TableRegistry::get('PaginatorPosts');
  538. $this->Paginator->paginate($table);
  539. }
  540. /**
  541. * test that fields not in whitelist won't be part of order conditions.
  542. *
  543. * @return void
  544. */
  545. public function testValidateSortWhitelistFailure()
  546. {
  547. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  548. $model->expects($this->any())
  549. ->method('alias')
  550. ->will($this->returnValue('model'));
  551. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  552. $options = [
  553. 'sort' => 'body',
  554. 'direction' => 'asc',
  555. 'sortWhitelist' => ['title', 'id']
  556. ];
  557. $result = $this->Paginator->validateSort($model, $options);
  558. $this->assertEquals([], $result['order']);
  559. }
  560. /**
  561. * test that fields in the whitelist are not validated
  562. *
  563. * @return void
  564. */
  565. public function testValidateSortWhitelistTrusted()
  566. {
  567. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  568. $model->expects($this->any())
  569. ->method('alias')
  570. ->will($this->returnValue('model'));
  571. $model->expects($this->once())
  572. ->method('hasField')
  573. ->will($this->returnValue(true));
  574. $options = [
  575. 'sort' => 'body',
  576. 'direction' => 'asc',
  577. 'sortWhitelist' => ['body']
  578. ];
  579. $result = $this->Paginator->validateSort($model, $options);
  580. $expected = ['model.body' => 'asc'];
  581. $this->assertEquals(
  582. $expected,
  583. $result['order'],
  584. 'Trusted fields in schema should be prefixed'
  585. );
  586. }
  587. /**
  588. * test that whitelist as empty array does not allow any sorting
  589. *
  590. * @return void
  591. */
  592. public function testValidateSortWhitelistEmpty()
  593. {
  594. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  595. $model->expects($this->any())
  596. ->method('alias')
  597. ->will($this->returnValue('model'));
  598. $model->expects($this->any())->method('hasField')
  599. ->will($this->returnValue(true));
  600. $options = [
  601. 'order' => [
  602. 'body' => 'asc',
  603. 'foo.bar' => 'asc'
  604. ],
  605. 'sort' => 'body',
  606. 'direction' => 'asc',
  607. 'sortWhitelist' => []
  608. ];
  609. $result = $this->Paginator->validateSort($model, $options);
  610. $this->assertSame([], $result['order'], 'No sort should be applied');
  611. }
  612. /**
  613. * test that fields in the whitelist are not validated
  614. *
  615. * @return void
  616. */
  617. public function testValidateSortWhitelistNotInSchema()
  618. {
  619. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  620. $model->expects($this->any())
  621. ->method('alias')
  622. ->will($this->returnValue('model'));
  623. $model->expects($this->once())->method('hasField')
  624. ->will($this->returnValue(false));
  625. $options = [
  626. 'sort' => 'score',
  627. 'direction' => 'asc',
  628. 'sortWhitelist' => ['score']
  629. ];
  630. $result = $this->Paginator->validateSort($model, $options);
  631. $expected = ['score' => 'asc'];
  632. $this->assertEquals(
  633. $expected,
  634. $result['order'],
  635. 'Trusted fields not in schema should not be altered'
  636. );
  637. }
  638. /**
  639. * test that multiple fields in the whitelist are not validated and properly aliased.
  640. *
  641. * @return void
  642. */
  643. public function testValidateSortWhitelistMultiple()
  644. {
  645. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  646. $model->expects($this->any())
  647. ->method('alias')
  648. ->will($this->returnValue('model'));
  649. $model->expects($this->once())
  650. ->method('hasField')
  651. ->will($this->returnValue(true));
  652. $options = [
  653. 'order' => [
  654. 'body' => 'asc',
  655. 'foo.bar' => 'asc'
  656. ],
  657. 'sortWhitelist' => ['body', 'foo.bar']
  658. ];
  659. $result = $this->Paginator->validateSort($model, $options);
  660. $expected = [
  661. 'model.body' => 'asc',
  662. 'foo.bar' => 'asc'
  663. ];
  664. $this->assertEquals($expected, $result['order']);
  665. }
  666. /**
  667. * test that multiple sort works.
  668. *
  669. * @return void
  670. */
  671. public function testValidateSortMultiple()
  672. {
  673. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  674. $model->expects($this->any())
  675. ->method('alias')
  676. ->will($this->returnValue('model'));
  677. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  678. $options = [
  679. 'order' => [
  680. 'author_id' => 'asc',
  681. 'title' => 'asc'
  682. ]
  683. ];
  684. $result = $this->Paginator->validateSort($model, $options);
  685. $expected = [
  686. 'model.author_id' => 'asc',
  687. 'model.title' => 'asc'
  688. ];
  689. $this->assertEquals($expected, $result['order']);
  690. }
  691. /**
  692. * Tests that order strings can used by Paginator
  693. *
  694. * @return void
  695. */
  696. public function testValidateSortWithString()
  697. {
  698. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  699. $model->expects($this->any())
  700. ->method('alias')
  701. ->will($this->returnValue('model'));
  702. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  703. $options = [
  704. 'order' => 'model.author_id DESC'
  705. ];
  706. $result = $this->Paginator->validateSort($model, $options);
  707. $expected = 'model.author_id DESC';
  708. $this->assertEquals($expected, $result['order']);
  709. }
  710. /**
  711. * Test that no sort doesn't trigger an error.
  712. *
  713. * @return void
  714. */
  715. public function testValidateSortNoSort()
  716. {
  717. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  718. $model->expects($this->any())
  719. ->method('alias')
  720. ->will($this->returnValue('model'));
  721. $model->expects($this->any())->method('hasField')
  722. ->will($this->returnValue(true));
  723. $options = [
  724. 'direction' => 'asc',
  725. 'sortWhitelist' => ['title', 'id'],
  726. ];
  727. $result = $this->Paginator->validateSort($model, $options);
  728. $this->assertEquals([], $result['order']);
  729. }
  730. /**
  731. * Test sorting with incorrect aliases on valid fields.
  732. *
  733. * @return void
  734. */
  735. public function testValidateSortInvalidAlias()
  736. {
  737. $model = $this->getMockBuilder('Cake\ORM\Table')->getMock();
  738. $model->expects($this->any())
  739. ->method('alias')
  740. ->will($this->returnValue('model'));
  741. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  742. $options = ['sort' => 'Derp.id'];
  743. $result = $this->Paginator->validateSort($model, $options);
  744. $this->assertEquals([], $result['order']);
  745. }
  746. /**
  747. * @return array
  748. */
  749. public function checkLimitProvider()
  750. {
  751. return [
  752. 'out of bounds' => [
  753. ['limit' => 1000000, 'maxLimit' => 100],
  754. 100,
  755. ],
  756. 'limit is nan' => [
  757. ['limit' => 'sheep!', 'maxLimit' => 100],
  758. 1,
  759. ],
  760. 'negative limit' => [
  761. ['limit' => '-1', 'maxLimit' => 100],
  762. 1,
  763. ],
  764. 'unset limit' => [
  765. ['limit' => null, 'maxLimit' => 100],
  766. 1,
  767. ],
  768. 'limit = 0' => [
  769. ['limit' => 0, 'maxLimit' => 100],
  770. 1,
  771. ],
  772. 'limit = 0 v2' => [
  773. ['limit' => 0, 'maxLimit' => 0],
  774. 1,
  775. ],
  776. 'limit = null' => [
  777. ['limit' => null, 'maxLimit' => 0],
  778. 1,
  779. ],
  780. 'bad input, results in 1' => [
  781. ['limit' => null, 'maxLimit' => null],
  782. 1,
  783. ],
  784. 'bad input, results in 1 v2' => [
  785. ['limit' => false, 'maxLimit' => false],
  786. 1,
  787. ],
  788. ];
  789. }
  790. /**
  791. * test that maxLimit is respected
  792. *
  793. * @dataProvider checkLimitProvider
  794. * @return void
  795. */
  796. public function testCheckLimit($input, $expected)
  797. {
  798. $result = $this->Paginator->checkLimit($input);
  799. $this->assertSame($expected, $result['limit']);
  800. }
  801. /**
  802. * Integration test for checkLimit() being applied inside paginate()
  803. *
  804. * @return void
  805. */
  806. public function testPaginateMaxLimit()
  807. {
  808. $this->loadFixtures('Posts');
  809. $table = TableRegistry::get('PaginatorPosts');
  810. $settings = [
  811. 'maxLimit' => 100,
  812. ];
  813. $this->request->query = [
  814. 'limit' => '1000'
  815. ];
  816. $this->Paginator->paginate($table, $settings);
  817. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['limit']);
  818. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['perPage']);
  819. $this->request->query = [
  820. 'limit' => '10'
  821. ];
  822. $this->Paginator->paginate($table, $settings);
  823. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
  824. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['perPage']);
  825. }
  826. /**
  827. * test paginate() and custom find, to make sure the correct count is returned.
  828. *
  829. * @return void
  830. */
  831. public function testPaginateCustomFind()
  832. {
  833. $this->loadFixtures('Posts');
  834. $titleExtractor = function ($result) {
  835. $ids = [];
  836. foreach ($result as $record) {
  837. $ids[] = $record->title;
  838. }
  839. return $ids;
  840. };
  841. $table = TableRegistry::get('PaginatorPosts');
  842. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  843. $result = $table->save(new Entity($data));
  844. $this->assertNotEmpty($result);
  845. $result = $this->Paginator->paginate($table);
  846. $this->assertCount(4, $result, '4 rows should come back');
  847. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  848. $result = $this->request->params['paging']['PaginatorPosts'];
  849. $this->assertEquals(4, $result['current']);
  850. $this->assertEquals(4, $result['count']);
  851. $settings = ['finder' => 'published'];
  852. $result = $this->Paginator->paginate($table, $settings);
  853. $this->assertCount(3, $result, '3 rows should come back');
  854. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  855. $result = $this->request->params['paging']['PaginatorPosts'];
  856. $this->assertEquals(3, $result['current']);
  857. $this->assertEquals(3, $result['count']);
  858. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  859. $result = $this->Paginator->paginate($table, $settings);
  860. $this->assertCount(1, $result, '1 rows should come back');
  861. $this->assertEquals(['Third Post'], $titleExtractor($result));
  862. $result = $this->request->params['paging']['PaginatorPosts'];
  863. $this->assertEquals(1, $result['current']);
  864. $this->assertEquals(3, $result['count']);
  865. $this->assertEquals(2, $result['pageCount']);
  866. $settings = ['finder' => 'published', 'limit' => 2];
  867. $result = $this->Paginator->paginate($table, $settings);
  868. $this->assertCount(2, $result, '2 rows should come back');
  869. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  870. $result = $this->request->params['paging']['PaginatorPosts'];
  871. $this->assertEquals(2, $result['current']);
  872. $this->assertEquals(3, $result['count']);
  873. $this->assertEquals(2, $result['pageCount']);
  874. $this->assertTrue($result['nextPage']);
  875. $this->assertFalse($result['prevPage']);
  876. $this->assertEquals(2, $result['perPage']);
  877. $this->assertNull($result['limit']);
  878. }
  879. /**
  880. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  881. *
  882. * @return void
  883. */
  884. public function testPaginateCustomFindFieldsArray()
  885. {
  886. $this->loadFixtures('Posts');
  887. $table = TableRegistry::get('PaginatorPosts');
  888. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  889. $table->save(new Entity($data));
  890. $settings = [
  891. 'finder' => 'list',
  892. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  893. 'limit' => 2
  894. ];
  895. $results = $this->Paginator->paginate($table, $settings);
  896. $result = $results->toArray();
  897. $expected = [
  898. 1 => 'First Post',
  899. 2 => 'Second Post',
  900. ];
  901. $this->assertEquals($expected, $result);
  902. $result = $this->request->params['paging']['PaginatorPosts'];
  903. $this->assertEquals(2, $result['current']);
  904. $this->assertEquals(3, $result['count']);
  905. $this->assertEquals(2, $result['pageCount']);
  906. $this->assertTrue($result['nextPage']);
  907. $this->assertFalse($result['prevPage']);
  908. }
  909. /**
  910. * test paginate() and custom finders to ensure the count + find
  911. * use the custom type.
  912. *
  913. * @return void
  914. */
  915. public function testPaginateCustomFindCount()
  916. {
  917. $settings = [
  918. 'finder' => 'published',
  919. 'limit' => 2
  920. ];
  921. $table = $this->_getMockPosts(['query']);
  922. $query = $this->_getMockFindQuery();
  923. $table->expects($this->once())
  924. ->method('query')
  925. ->will($this->returnValue($query));
  926. $query->expects($this->once())->method('applyOptions')
  927. ->with([
  928. 'limit' => 2,
  929. 'page' => 1,
  930. 'order' => [],
  931. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  932. 'scope' => null,
  933. ]);
  934. $this->Paginator->paginate($table, $settings);
  935. }
  936. /**
  937. * Tests that it is possible to pass an already made query object to
  938. * paginate()
  939. *
  940. * @return void
  941. */
  942. public function testPaginateQuery()
  943. {
  944. $this->request->query = ['page' => '-1'];
  945. $settings = [
  946. 'PaginatorPosts' => [
  947. 'contain' => ['PaginatorAuthor'],
  948. 'maxLimit' => 10,
  949. 'group' => 'PaginatorPosts.published',
  950. 'order' => ['PaginatorPosts.id' => 'ASC']
  951. ]
  952. ];
  953. $table = $this->_getMockPosts(['find']);
  954. $query = $this->_getMockFindQuery($table);
  955. $table->expects($this->never())->method('find');
  956. $query->expects($this->once())
  957. ->method('applyOptions')
  958. ->with([
  959. 'contain' => ['PaginatorAuthor'],
  960. 'group' => 'PaginatorPosts.published',
  961. 'limit' => 10,
  962. 'order' => ['PaginatorPosts.id' => 'ASC'],
  963. 'page' => 1,
  964. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  965. 'scope' => null,
  966. ]);
  967. $this->Paginator->paginate($query, $settings);
  968. }
  969. /**
  970. * test paginate() with bind()
  971. *
  972. * @return void
  973. */
  974. public function testPaginateQueryWithBindValue()
  975. {
  976. $config = ConnectionManager::config('test');
  977. $this->skipIf(strpos($config['driver'], 'Sqlserver') !== false, 'Test temporarily broken in SQLServer');
  978. $this->loadFixtures('Posts');
  979. $table = TableRegistry::get('PaginatorPosts');
  980. $query = $table->find()
  981. ->where(['PaginatorPosts.author_id BETWEEN :start AND :end'])
  982. ->bind(':start', 1)
  983. ->bind(':end', 2);
  984. $results = $this->Paginator->paginate($query, []);
  985. $result = $results->toArray();
  986. $this->assertCount(2, $result);
  987. $this->assertEquals('First Post', $result[0]->title);
  988. $this->assertEquals('Third Post', $result[1]->title);
  989. }
  990. /**
  991. * Tests that passing a query object with a limit clause set will
  992. * overwrite it with the passed defaults.
  993. *
  994. * @return void
  995. */
  996. public function testPaginateQueryWithLimit()
  997. {
  998. $this->request->query = ['page' => '-1'];
  999. $settings = [
  1000. 'PaginatorPosts' => [
  1001. 'contain' => ['PaginatorAuthor'],
  1002. 'maxLimit' => 10,
  1003. 'limit' => 5,
  1004. 'group' => 'PaginatorPosts.published',
  1005. 'order' => ['PaginatorPosts.id' => 'ASC']
  1006. ]
  1007. ];
  1008. $table = $this->_getMockPosts(['find']);
  1009. $query = $this->_getMockFindQuery($table);
  1010. $query->limit(2);
  1011. $table->expects($this->never())->method('find');
  1012. $query->expects($this->once())
  1013. ->method('applyOptions')
  1014. ->with([
  1015. 'contain' => ['PaginatorAuthor'],
  1016. 'group' => 'PaginatorPosts.published',
  1017. 'limit' => 5,
  1018. 'order' => ['PaginatorPosts.id' => 'ASC'],
  1019. 'page' => 1,
  1020. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1021. 'scope' => null,
  1022. ]);
  1023. $this->Paginator->paginate($query, $settings);
  1024. }
  1025. /**
  1026. * Helper method for making mocks.
  1027. *
  1028. * @param array $methods
  1029. * @return \Cake\ORM\Table
  1030. */
  1031. protected function _getMockPosts($methods = [])
  1032. {
  1033. return $this->getMockBuilder('TestApp\Model\Table\PaginatorPostsTable')
  1034. ->setMethods($methods)
  1035. ->setConstructorArgs([[
  1036. 'connection' => ConnectionManager::get('test'),
  1037. 'alias' => 'PaginatorPosts',
  1038. 'schema' => [
  1039. 'id' => ['type' => 'integer'],
  1040. 'author_id' => ['type' => 'integer', 'null' => false],
  1041. 'title' => ['type' => 'string', 'null' => false],
  1042. 'body' => 'text',
  1043. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  1044. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  1045. ]
  1046. ]])
  1047. ->getMock();
  1048. }
  1049. /**
  1050. * Helper method for mocking queries.
  1051. *
  1052. * @return \Cake\ORM\Query
  1053. */
  1054. protected function _getMockFindQuery($table = null)
  1055. {
  1056. $query = $this->getMockBuilder('Cake\ORM\Query')
  1057. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  1058. ->disableOriginalConstructor()
  1059. ->getMock();
  1060. $results = $this->getMockBuilder('Cake\ORM\ResultSet')
  1061. ->disableOriginalConstructor()
  1062. ->getMock();
  1063. $query->expects($this->any())
  1064. ->method('count')
  1065. ->will($this->returnValue(2));
  1066. $query->expects($this->any())
  1067. ->method('all')
  1068. ->will($this->returnValue($results));
  1069. $query->expects($this->any())
  1070. ->method('count')
  1071. ->will($this->returnValue(2));
  1072. $query->repository($table);
  1073. return $query;
  1074. }
  1075. }