PaginatorTest.php 44 KB

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