PaginatorTest.php 44 KB

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