PaginatorTest.php 46 KB

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