PaginatorTest.php 47 KB

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