PaginatorComponentTest.php 38 KB

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