PaginatorComponentTest.php 44 KB

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