PaginatorComponentTest.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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\Component\PaginatorComponent;
  18. use Cake\Controller\ComponentRegistry;
  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\RepositoryInterface;
  24. use Cake\Event\EventInterface;
  25. use Cake\Http\Exception\NotFoundException;
  26. use Cake\Http\ServerRequest;
  27. use Cake\ORM\Entity;
  28. use Cake\ORM\Query;
  29. use Cake\TestSuite\TestCase;
  30. use stdClass;
  31. use TestApp\Datasource\CustomPaginator;
  32. class PaginatorComponentTest extends TestCase
  33. {
  34. /**
  35. * fixtures property
  36. *
  37. * @var array
  38. */
  39. protected $fixtures = [
  40. 'core.Posts', 'core.Articles', 'core.ArticlesTags',
  41. 'core.Authors', 'core.AuthorsTags', 'core.Tags',
  42. ];
  43. /**
  44. * Don't load data for fixtures for all tests
  45. *
  46. * @var bool
  47. */
  48. public $autoFixtures = false;
  49. /**
  50. * @var \Cake\Controller\Controller
  51. */
  52. protected $controller;
  53. /**
  54. * @var \Cake\Controller\ComponentRegistry
  55. */
  56. protected $registry;
  57. /**
  58. * @var \Cake\Controller\Component\PaginatorComponent
  59. */
  60. protected $Paginator;
  61. /**
  62. * @var \PHPUnit\Framework\MockObject\MockObject
  63. */
  64. protected $Post;
  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. * testPaginatorSetting
  82. *
  83. * @return void
  84. */
  85. public function testPaginatorSetting(): void
  86. {
  87. $paginator = new CustomPaginator();
  88. $component = new PaginatorComponent($this->registry, [
  89. 'paginator' => $paginator,
  90. ]);
  91. $this->assertSame($paginator, $component->getPaginator());
  92. $component = new PaginatorComponent($this->registry, []);
  93. $this->assertNotSame($paginator, $component->getPaginator());
  94. $component->setPaginator($paginator);
  95. $this->assertSame($paginator, $component->getPaginator());
  96. }
  97. /**
  98. * Test that an exception is thrown when paginator option is invalid.
  99. *
  100. * @return void
  101. */
  102. public function testInvalidPaginatorOption(): void
  103. {
  104. $this->expectException(\InvalidArgumentException::class);
  105. $this->expectExceptionMessage('Paginator must be an instance of Cake\Datasource\Paginator');
  106. new PaginatorComponent($this->registry, [
  107. 'paginator' => new stdClass(),
  108. ]);
  109. }
  110. /**
  111. * Test that non-numeric values are rejected for page, and limit
  112. *
  113. * @return void
  114. */
  115. public function testPageParamCasting(): void
  116. {
  117. $this->Post->expects($this->any())
  118. ->method('getAlias')
  119. ->will($this->returnValue('Posts'));
  120. $query = $this->_getMockFindQuery();
  121. $this->Post->expects($this->any())
  122. ->method('find')
  123. ->will($this->returnValue($query));
  124. $this->controller->setRequest($this->controller->getRequest()->withQueryParams(['page' => '1 " onclick="alert(\'xss\');">']));
  125. $settings = ['limit' => 1, 'maxLimit' => 10];
  126. $this->Paginator->paginate($this->Post, $settings);
  127. $params = $this->controller->getRequest()->getAttribute('paging');
  128. $this->assertSame(1, $params['Posts']['page'], 'XSS exploit opened');
  129. }
  130. /**
  131. * test that unknown keys in the default settings are
  132. * passed to the find operations.
  133. *
  134. * @return void
  135. */
  136. public function testPaginateExtraParams(): void
  137. {
  138. $this->controller->setRequest($this->controller->getRequest()->withQueryParams(['page' => '-1']));
  139. $settings = [
  140. 'PaginatorPosts' => [
  141. 'contain' => ['PaginatorAuthor'],
  142. 'maxLimit' => 10,
  143. 'group' => 'PaginatorPosts.published',
  144. 'order' => ['PaginatorPosts.id' => 'ASC'],
  145. ],
  146. ];
  147. $table = $this->_getMockPosts(['query']);
  148. $query = $this->_getMockFindQuery();
  149. $table->expects($this->once())
  150. ->method('query')
  151. ->will($this->returnValue($query));
  152. $query->expects($this->once())
  153. ->method('applyOptions')
  154. ->with([
  155. 'contain' => ['PaginatorAuthor'],
  156. 'group' => 'PaginatorPosts.published',
  157. 'limit' => 10,
  158. 'order' => ['PaginatorPosts.id' => 'ASC'],
  159. 'page' => 1,
  160. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  161. 'scope' => null,
  162. 'sort' => 'PaginatorPosts.id',
  163. ]);
  164. $this->Paginator->paginate($table, $settings);
  165. }
  166. /**
  167. * Test to make sure options get sent to custom finder methods via paginate
  168. *
  169. * @return void
  170. */
  171. public function testPaginateCustomFinderOptions(): void
  172. {
  173. $this->loadFixtures('Posts');
  174. $settings = [
  175. 'PaginatorPosts' => [
  176. 'finder' => ['author' => ['author_id' => 1]],
  177. ],
  178. ];
  179. $table = $this->getTableLocator()->get('PaginatorPosts');
  180. $expected = $table
  181. ->find('author', [
  182. 'conditions' => [
  183. 'PaginatorPosts.author_id' => 1,
  184. ],
  185. ])
  186. ->count();
  187. $result = $this->Paginator->paginate($table, $settings)->count();
  188. $this->assertEquals($expected, $result);
  189. }
  190. /**
  191. * testRequestParamsSetting
  192. *
  193. * @return void
  194. * @see https://github.com/cakephp/cakephp/issues/11655
  195. */
  196. public function testRequestParamsSetting(): void
  197. {
  198. $this->loadFixtures('Posts');
  199. $settings = [
  200. 'PaginatorPosts' => [
  201. 'limit' => 10,
  202. ],
  203. ];
  204. $table = $this->getTableLocator()->get('PaginatorPosts');
  205. $this->Paginator->paginate($table, $settings);
  206. $params = $this->controller->getRequest()->getAttribute('paging');
  207. $this->assertArrayHasKey('PaginatorPosts', $params);
  208. $this->assertArrayNotHasKey(0, $params);
  209. }
  210. /**
  211. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  212. *
  213. * @return void
  214. */
  215. public function testPaginateCustomFinder(): void
  216. {
  217. $settings = [
  218. 'PaginatorPosts' => [
  219. 'finder' => 'popular',
  220. 'fields' => ['id', 'title'],
  221. 'maxLimit' => 10,
  222. ],
  223. ];
  224. $table = $this->_getMockPosts(['findPopular']);
  225. $query = $this->_getMockFindQuery();
  226. $table->expects($this->any())
  227. ->method('findPopular')
  228. ->will($this->returnValue($query));
  229. $this->Paginator->paginate($table, $settings);
  230. $params = $this->controller->getRequest()->getAttribute('paging');
  231. $this->assertSame('popular', $params['PaginatorPosts']['finder']);
  232. }
  233. /**
  234. * Test that nested eager loaders don't trigger invalid SQL errors.
  235. *
  236. * @return void
  237. */
  238. public function testPaginateNestedEagerLoader(): void
  239. {
  240. $this->loadFixtures('Articles', 'Tags', 'Authors', 'ArticlesTags', 'AuthorsTags');
  241. $articles = $this->getTableLocator()->get('Articles');
  242. $articles->belongsToMany('Tags');
  243. $tags = $this->getTableLocator()->get('Tags');
  244. $tags->belongsToMany('Authors');
  245. $articles->getEventManager()->on('Model.beforeFind', function (EventInterface $event, Query $query): void {
  246. $query ->matching('Tags', function (Query $q) {
  247. return $q->matching('Authors', function (Query $q) {
  248. return $q->where(['Authors.name' => 'larry']);
  249. });
  250. });
  251. });
  252. $results = $this->Paginator->paginate($articles, []);
  253. $result = $results->first();
  254. $this->assertInstanceOf(EntityInterface::class, $result);
  255. $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Tags']);
  256. $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Authors']);
  257. }
  258. /**
  259. * test that flat default pagination parameters work.
  260. *
  261. * @return void
  262. */
  263. public function testDefaultPaginateParams(): void
  264. {
  265. $settings = [
  266. 'order' => ['PaginatorPosts.id' => 'DESC'],
  267. 'maxLimit' => 10,
  268. ];
  269. $table = $this->_getMockPosts(['query']);
  270. $query = $this->_getMockFindQuery();
  271. $table->expects($this->once())
  272. ->method('query')
  273. ->will($this->returnValue($query));
  274. $query->expects($this->once())
  275. ->method('applyOptions')
  276. ->with([
  277. 'limit' => 10,
  278. 'page' => 1,
  279. 'order' => ['PaginatorPosts.id' => 'DESC'],
  280. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  281. 'scope' => null,
  282. 'sort' => 'PaginatorPosts.id',
  283. ]);
  284. $this->Paginator->paginate($table, $settings);
  285. }
  286. /**
  287. * test that default sort and default direction are injected into request
  288. *
  289. * @return void
  290. */
  291. public function testDefaultPaginateParamsIntoRequest(): void
  292. {
  293. $settings = [
  294. 'order' => ['PaginatorPosts.id' => 'DESC'],
  295. 'maxLimit' => 10,
  296. ];
  297. $table = $this->_getMockPosts(['query']);
  298. $query = $this->_getMockFindQuery();
  299. $table->expects($this->once())
  300. ->method('query')
  301. ->will($this->returnValue($query));
  302. $query->expects($this->once())
  303. ->method('applyOptions')
  304. ->with([
  305. 'limit' => 10,
  306. 'page' => 1,
  307. 'order' => ['PaginatorPosts.id' => 'DESC'],
  308. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  309. 'scope' => null,
  310. 'sort' => 'PaginatorPosts.id',
  311. ]);
  312. $this->Paginator->paginate($table, $settings);
  313. $params = $this->controller->getRequest()->getAttribute('paging');
  314. $this->assertSame('PaginatorPosts.id', $params['PaginatorPosts']['sortDefault']);
  315. $this->assertSame('DESC', $params['PaginatorPosts']['directionDefault']);
  316. }
  317. /**
  318. * test that option merging prefers specific models
  319. *
  320. * @return void
  321. */
  322. public function testMergeOptionsModelSpecific(): void
  323. {
  324. $settings = [
  325. 'page' => 1,
  326. 'limit' => 20,
  327. 'maxLimit' => 100,
  328. 'Posts' => [
  329. 'page' => 1,
  330. 'limit' => 10,
  331. 'maxLimit' => 50,
  332. ],
  333. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  334. ];
  335. $result = $this->Paginator->mergeOptions('Silly', $settings);
  336. $this->assertEquals($settings, $result);
  337. $result = $this->Paginator->mergeOptions('Posts', $settings);
  338. $expected = ['page' => 1, 'limit' => 10, 'maxLimit' => 50, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  339. $this->assertEquals($expected, $result);
  340. }
  341. /**
  342. * test mergeOptions with custom scope
  343. *
  344. * @return void
  345. */
  346. public function testMergeOptionsCustomScope(): void
  347. {
  348. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  349. 'page' => 10,
  350. 'limit' => 10,
  351. 'scope' => [
  352. 'page' => 2,
  353. 'limit' => 5,
  354. ],
  355. ]));
  356. $settings = [
  357. 'page' => 1,
  358. 'limit' => 20,
  359. 'maxLimit' => 100,
  360. 'finder' => 'myCustomFind',
  361. ];
  362. $result = $this->Paginator->mergeOptions('Post', $settings);
  363. $expected = [
  364. 'page' => 10,
  365. 'limit' => 10,
  366. 'maxLimit' => 100,
  367. 'finder' => 'myCustomFind',
  368. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  369. ];
  370. $this->assertEquals($expected, $result);
  371. $settings = [
  372. 'page' => 1,
  373. 'limit' => 20,
  374. 'maxLimit' => 100,
  375. 'finder' => 'myCustomFind',
  376. 'scope' => 'non-existent',
  377. ];
  378. $result = $this->Paginator->mergeOptions('Post', $settings);
  379. $expected = [
  380. 'page' => 1,
  381. 'limit' => 20,
  382. 'maxLimit' => 100,
  383. 'finder' => 'myCustomFind',
  384. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  385. 'scope' => 'non-existent',
  386. ];
  387. $this->assertEquals($expected, $result);
  388. $settings = [
  389. 'page' => 1,
  390. 'limit' => 20,
  391. 'maxLimit' => 100,
  392. 'finder' => 'myCustomFind',
  393. 'scope' => 'scope',
  394. ];
  395. $result = $this->Paginator->mergeOptions('Post', $settings);
  396. $expected = [
  397. 'page' => 2,
  398. 'limit' => 5,
  399. 'maxLimit' => 100,
  400. 'finder' => 'myCustomFind',
  401. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  402. 'scope' => 'scope',
  403. ];
  404. $this->assertEquals($expected, $result);
  405. }
  406. /**
  407. * test mergeOptions with customFind key
  408. *
  409. * @return void
  410. */
  411. public function testMergeOptionsCustomFindKey(): void
  412. {
  413. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  414. 'page' => 10,
  415. 'limit' => 10,
  416. ]));
  417. $settings = [
  418. 'page' => 1,
  419. 'limit' => 20,
  420. 'maxLimit' => 100,
  421. 'finder' => 'myCustomFind',
  422. ];
  423. $result = $this->Paginator->mergeOptions('Post', $settings);
  424. $expected = [
  425. 'page' => 10,
  426. 'limit' => 10,
  427. 'maxLimit' => 100,
  428. 'finder' => 'myCustomFind',
  429. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  430. ];
  431. $this->assertEquals($expected, $result);
  432. }
  433. /**
  434. * test merging options from the querystring.
  435. *
  436. * @return void
  437. */
  438. public function testMergeOptionsQueryString(): void
  439. {
  440. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  441. 'page' => 99,
  442. 'limit' => 75,
  443. ]));
  444. $settings = [
  445. 'page' => 1,
  446. 'limit' => 20,
  447. 'maxLimit' => 100,
  448. ];
  449. $result = $this->Paginator->mergeOptions('Post', $settings);
  450. $expected = ['page' => 99, 'limit' => 75, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  451. $this->assertEquals($expected, $result);
  452. }
  453. /**
  454. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  455. *
  456. * @return void
  457. */
  458. public function testMergeOptionsDefaultWhiteList(): void
  459. {
  460. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  461. 'page' => 10,
  462. 'limit' => 10,
  463. 'fields' => ['bad.stuff'],
  464. 'recursive' => 1000,
  465. 'conditions' => ['bad.stuff'],
  466. 'contain' => ['bad'],
  467. ]));
  468. $settings = [
  469. 'page' => 1,
  470. 'limit' => 20,
  471. 'maxLimit' => 100,
  472. ];
  473. $result = $this->Paginator->mergeOptions('Post', $settings);
  474. $expected = ['page' => 10, 'limit' => 10, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  475. $this->assertEquals($expected, $result);
  476. }
  477. /**
  478. * test that modifying the whitelist works.
  479. *
  480. * @return void
  481. */
  482. public function testMergeOptionsExtraWhitelist(): void
  483. {
  484. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  485. 'page' => 10,
  486. 'limit' => 10,
  487. 'fields' => ['bad.stuff'],
  488. 'recursive' => 1000,
  489. 'conditions' => ['bad.stuff'],
  490. 'contain' => ['bad'],
  491. ]));
  492. $settings = [
  493. 'page' => 1,
  494. 'limit' => 20,
  495. 'maxLimit' => 100,
  496. ];
  497. $this->Paginator->setConfig('whitelist', ['fields']);
  498. $result = $this->Paginator->mergeOptions('Post', $settings);
  499. $expected = [
  500. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => ['bad.stuff'], 'whitelist' => ['limit', 'sort', 'page', 'direction', 'fields'],
  501. ];
  502. $this->assertEquals($expected, $result);
  503. }
  504. /**
  505. * test mergeOptions with limit > maxLimit in code.
  506. *
  507. * @return void
  508. */
  509. public function testMergeOptionsMaxLimit(): void
  510. {
  511. $settings = [
  512. 'limit' => 200,
  513. 'paramType' => 'named',
  514. ];
  515. $result = $this->Paginator->mergeOptions('Post', $settings);
  516. $expected = [
  517. 'page' => 1,
  518. 'limit' => 100,
  519. 'maxLimit' => 100,
  520. 'paramType' => 'named',
  521. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  522. ];
  523. $this->assertEquals($expected, $result);
  524. $settings = [
  525. 'maxLimit' => 10,
  526. 'paramType' => 'named',
  527. ];
  528. $result = $this->Paginator->mergeOptions('Post', $settings);
  529. $expected = [
  530. 'page' => 1,
  531. 'limit' => 10,
  532. 'maxLimit' => 10,
  533. 'paramType' => 'named',
  534. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  535. ];
  536. $this->assertEquals($expected, $result);
  537. }
  538. /**
  539. * test getDefaults with limit > maxLimit in code.
  540. *
  541. * @return void
  542. */
  543. public function testGetDefaultMaxLimit(): void
  544. {
  545. $settings = [
  546. 'page' => 1,
  547. 'limit' => 2,
  548. 'maxLimit' => 10,
  549. 'order' => [
  550. 'Users.username' => 'asc',
  551. ],
  552. ];
  553. $result = $this->Paginator->mergeOptions('Post', $settings);
  554. $expected = [
  555. 'page' => 1,
  556. 'limit' => 2,
  557. 'maxLimit' => 10,
  558. 'order' => [
  559. 'Users.username' => 'asc',
  560. ],
  561. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  562. ];
  563. $this->assertEquals($expected, $result);
  564. $settings = [
  565. 'page' => 1,
  566. 'limit' => 100,
  567. 'maxLimit' => 10,
  568. 'order' => [
  569. 'Users.username' => 'asc',
  570. ],
  571. ];
  572. $result = $this->Paginator->mergeOptions('Post', $settings);
  573. $expected = [
  574. 'page' => 1,
  575. 'limit' => 10,
  576. 'maxLimit' => 10,
  577. 'order' => [
  578. 'Users.username' => 'asc',
  579. ],
  580. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  581. ];
  582. $this->assertEquals($expected, $result);
  583. }
  584. /**
  585. * Integration test to ensure that validateSort is being used by paginate()
  586. *
  587. * @return void
  588. */
  589. public function testValidateSortInvalid(): void
  590. {
  591. $table = $this->_getMockPosts(['query']);
  592. $query = $this->_getMockFindQuery();
  593. $table->expects($this->once())
  594. ->method('query')
  595. ->will($this->returnValue($query));
  596. $query->expects($this->once())->method('applyOptions')
  597. ->with([
  598. 'limit' => 20,
  599. 'page' => 1,
  600. 'order' => ['PaginatorPosts.id' => 'asc'],
  601. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  602. 'scope' => null,
  603. 'sort' => 'id',
  604. ]);
  605. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  606. 'page' => 1,
  607. 'sort' => 'id',
  608. 'direction' => 'herp',
  609. ]));
  610. $this->Paginator->paginate($table);
  611. $params = $this->controller->getRequest()->getAttribute('paging');
  612. $this->assertSame('id', $params['PaginatorPosts']['sort']);
  613. $this->assertSame('asc', $params['PaginatorPosts']['direction']);
  614. }
  615. /**
  616. * test that invalid directions are ignored.
  617. *
  618. * @return void
  619. */
  620. public function testValidateSortInvalidDirection(): void
  621. {
  622. $model = $this->getMockRepository();
  623. $model->expects($this->any())
  624. ->method('getAlias')
  625. ->will($this->returnValue('model'));
  626. $model->expects($this->any())
  627. ->method('hasField')
  628. ->will($this->returnValue(true));
  629. $options = ['sort' => 'something', 'direction' => 'boogers'];
  630. $result = $this->Paginator->validateSort($model, $options);
  631. $this->assertSame('asc', $result['order']['model.something']);
  632. }
  633. /**
  634. * Test empty pagination result.
  635. *
  636. * @return void
  637. */
  638. public function testEmptyPaginationResult(): void
  639. {
  640. $this->loadFixtures('Posts');
  641. $table = $this->getTableLocator()->get('PaginatorPosts');
  642. $table->deleteAll('1=1');
  643. $this->Paginator->paginate($table);
  644. $params = $this->controller->getRequest()->getAttribute('paging');
  645. $this->assertSame(
  646. 0,
  647. $params['PaginatorPosts']['count'],
  648. 'Count should be 0'
  649. );
  650. $this->assertSame(
  651. 1,
  652. $params['PaginatorPosts']['page'],
  653. 'Page number should not be 0'
  654. );
  655. $this->assertSame(
  656. 1,
  657. $params['PaginatorPosts']['pageCount'],
  658. 'Page count number should not be 0'
  659. );
  660. }
  661. /**
  662. * Test that a really large page number gets clamped to the max page size.
  663. *
  664. * @return void
  665. */
  666. public function testOutOfRangePageNumberGetsClamped(): void
  667. {
  668. $this->loadFixtures('Posts');
  669. $this->controller->setRequest($this->controller->getRequest()->withQueryParams(['page' => 3000]));
  670. $table = $this->getTableLocator()->get('PaginatorPosts');
  671. $e = null;
  672. try {
  673. $this->Paginator->paginate($table);
  674. } catch (NotFoundException $e) {
  675. }
  676. $params = $this->controller->getRequest()->getAttribute('paging');
  677. $this->assertEquals(
  678. 1,
  679. $params['PaginatorPosts']['page'],
  680. 'Page number should not be 0'
  681. );
  682. $this->assertNotNull($e);
  683. $this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious());
  684. }
  685. /**
  686. * Test that a out of bounds request still knows about the page size
  687. *
  688. * @return void
  689. */
  690. public function testOutOfRangePageNumberStillProvidesPageCount(): void
  691. {
  692. $this->loadFixtures('Posts');
  693. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  694. 'limit' => 1,
  695. 'page' => '4',
  696. ]));
  697. $table = $this->getTableLocator()->get('PaginatorPosts');
  698. $e = null;
  699. try {
  700. $this->Paginator->paginate($table);
  701. } catch (NotFoundException $e) {
  702. }
  703. $params = $this->controller->getRequest()->getAttribute('paging');
  704. $this->assertEquals(
  705. 3,
  706. $params['PaginatorPosts']['pageCount'],
  707. 'Page count number should not be 0'
  708. );
  709. $this->assertNotNull($e);
  710. $this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious());
  711. }
  712. /**
  713. * Test that a really REALLY large page number gets clamped to the max page size.
  714. *
  715. * @return void
  716. */
  717. public function testOutOfVeryBigPageNumberGetsClamped(): void
  718. {
  719. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  720. $this->loadFixtures('Posts');
  721. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  722. 'page' => '3000000000000000000000000',
  723. ]));
  724. $table = $this->getTableLocator()->get('PaginatorPosts');
  725. $this->Paginator->paginate($table);
  726. }
  727. /**
  728. * test that fields not in whitelist won't be part of order conditions.
  729. *
  730. * @return void
  731. */
  732. public function testValidateSortWhitelistFailure(): void
  733. {
  734. $model = $this->getMockRepository();
  735. $model->expects($this->any())
  736. ->method('getAlias')
  737. ->will($this->returnValue('model'));
  738. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  739. $options = [
  740. 'sort' => 'body',
  741. 'direction' => 'asc',
  742. 'sortWhitelist' => ['title', 'id'],
  743. ];
  744. $result = $this->Paginator->validateSort($model, $options);
  745. $this->assertEquals([], $result['order']);
  746. }
  747. /**
  748. * test that fields in the whitelist are not validated
  749. *
  750. * @return void
  751. */
  752. public function testValidateSortWhitelistTrusted(): void
  753. {
  754. $model = $this->getMockRepository();
  755. $model->expects($this->any())
  756. ->method('getAlias')
  757. ->will($this->returnValue('model'));
  758. $model->expects($this->once())
  759. ->method('hasField')
  760. ->will($this->returnValue(true));
  761. $options = [
  762. 'sort' => 'body',
  763. 'direction' => 'asc',
  764. 'sortWhitelist' => ['body'],
  765. ];
  766. $result = $this->Paginator->validateSort($model, $options);
  767. $expected = ['model.body' => 'asc'];
  768. $this->assertEquals(
  769. $expected,
  770. $result['order'],
  771. 'Trusted fields in schema should be prefixed'
  772. );
  773. }
  774. /**
  775. * test that whitelist as empty array does not allow any sorting
  776. *
  777. * @return void
  778. */
  779. public function testValidateSortWhitelistEmpty(): void
  780. {
  781. $model = $this->getMockRepository();
  782. $model->expects($this->any())
  783. ->method('getAlias')
  784. ->will($this->returnValue('model'));
  785. $model->expects($this->any())->method('hasField')
  786. ->will($this->returnValue(true));
  787. $options = [
  788. 'order' => [
  789. 'body' => 'asc',
  790. 'foo.bar' => 'asc',
  791. ],
  792. 'sort' => 'body',
  793. 'direction' => 'asc',
  794. 'sortWhitelist' => [],
  795. ];
  796. $result = $this->Paginator->validateSort($model, $options);
  797. $this->assertSame([], $result['order'], 'No sort should be applied');
  798. }
  799. /**
  800. * test that fields in the whitelist are not validated
  801. *
  802. * @return void
  803. */
  804. public function testValidateSortWhitelistNotInSchema(): void
  805. {
  806. $model = $this->getMockRepository();
  807. $model->expects($this->any())
  808. ->method('getAlias')
  809. ->will($this->returnValue('model'));
  810. $model->expects($this->once())->method('hasField')
  811. ->will($this->returnValue(false));
  812. $options = [
  813. 'sort' => 'score',
  814. 'direction' => 'asc',
  815. 'sortWhitelist' => ['score'],
  816. ];
  817. $result = $this->Paginator->validateSort($model, $options);
  818. $expected = ['score' => 'asc'];
  819. $this->assertEquals(
  820. $expected,
  821. $result['order'],
  822. 'Trusted fields not in schema should not be altered'
  823. );
  824. }
  825. /**
  826. * test that multiple fields in the whitelist are not validated and properly aliased.
  827. *
  828. * @return void
  829. */
  830. public function testValidateSortWhitelistMultiple(): void
  831. {
  832. $model = $this->getMockRepository();
  833. $model->expects($this->any())
  834. ->method('getAlias')
  835. ->will($this->returnValue('model'));
  836. $model->expects($this->once())
  837. ->method('hasField')
  838. ->will($this->returnValue(true));
  839. $options = [
  840. 'order' => [
  841. 'body' => 'asc',
  842. 'foo.bar' => 'asc',
  843. ],
  844. 'sortWhitelist' => ['body', 'foo.bar'],
  845. ];
  846. $result = $this->Paginator->validateSort($model, $options);
  847. $expected = [
  848. 'model.body' => 'asc',
  849. 'foo.bar' => 'asc',
  850. ];
  851. $this->assertEquals($expected, $result['order']);
  852. }
  853. /**
  854. * test that multiple sort works.
  855. *
  856. * @return void
  857. */
  858. public function testValidateSortMultiple(): void
  859. {
  860. $model = $this->getMockRepository();
  861. $model->expects($this->any())
  862. ->method('getAlias')
  863. ->will($this->returnValue('model'));
  864. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  865. $options = [
  866. 'order' => [
  867. 'author_id' => 'asc',
  868. 'title' => 'asc',
  869. ],
  870. ];
  871. $result = $this->Paginator->validateSort($model, $options);
  872. $expected = [
  873. 'model.author_id' => 'asc',
  874. 'model.title' => 'asc',
  875. ];
  876. $this->assertEquals($expected, $result['order']);
  877. }
  878. /**
  879. * Tests that order strings can used by Paginator
  880. *
  881. * @return void
  882. */
  883. public function testValidateSortWithString(): void
  884. {
  885. $model = $this->getMockRepository();
  886. $model->expects($this->any())
  887. ->method('getAlias')
  888. ->will($this->returnValue('model'));
  889. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  890. $options = [
  891. 'order' => 'model.author_id DESC',
  892. ];
  893. $result = $this->Paginator->validateSort($model, $options);
  894. $expected = 'model.author_id DESC';
  895. $this->assertEquals($expected, $result['order']);
  896. }
  897. /**
  898. * Test that no sort doesn't trigger an error.
  899. *
  900. * @return void
  901. */
  902. public function testValidateSortNoSort(): void
  903. {
  904. $model = $this->getMockRepository();
  905. $model->expects($this->any())
  906. ->method('getAlias')
  907. ->will($this->returnValue('model'));
  908. $model->expects($this->any())->method('hasField')
  909. ->will($this->returnValue(true));
  910. $options = [
  911. 'direction' => 'asc',
  912. 'sortWhitelist' => ['title', 'id'],
  913. ];
  914. $result = $this->Paginator->validateSort($model, $options);
  915. $this->assertEquals([], $result['order']);
  916. }
  917. /**
  918. * Test sorting with incorrect aliases on valid fields.
  919. *
  920. * @return void
  921. */
  922. public function testValidateSortInvalidAlias(): void
  923. {
  924. $model = $this->getMockRepository();
  925. $model->expects($this->any())
  926. ->method('getAlias')
  927. ->will($this->returnValue('model'));
  928. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  929. $options = ['sort' => 'Derp.id'];
  930. $result = $this->Paginator->validateSort($model, $options);
  931. $this->assertEquals([], $result['order']);
  932. }
  933. /**
  934. * @return array
  935. */
  936. public function checkLimitProvider(): array
  937. {
  938. return [
  939. 'out of bounds' => [
  940. ['limit' => 1000000, 'maxLimit' => 100],
  941. 100,
  942. ],
  943. 'limit is nan' => [
  944. ['limit' => 'sheep!', 'maxLimit' => 100],
  945. 1,
  946. ],
  947. 'negative limit' => [
  948. ['limit' => '-1', 'maxLimit' => 100],
  949. 1,
  950. ],
  951. 'unset limit' => [
  952. ['limit' => null, 'maxLimit' => 100],
  953. 1,
  954. ],
  955. 'limit = 0' => [
  956. ['limit' => 0, 'maxLimit' => 100],
  957. 1,
  958. ],
  959. 'limit = 0 v2' => [
  960. ['limit' => 0, 'maxLimit' => 0],
  961. 1,
  962. ],
  963. 'limit = null' => [
  964. ['limit' => null, 'maxLimit' => 0],
  965. 1,
  966. ],
  967. 'bad input, results in 1' => [
  968. ['limit' => null, 'maxLimit' => null],
  969. 1,
  970. ],
  971. 'bad input, results in 1 v2' => [
  972. ['limit' => false, 'maxLimit' => false],
  973. 1,
  974. ],
  975. ];
  976. }
  977. /**
  978. * test that maxLimit is respected
  979. *
  980. * @dataProvider checkLimitProvider
  981. * @return void
  982. */
  983. public function testCheckLimit($input, $expected): void
  984. {
  985. $result = $this->Paginator->checkLimit($input);
  986. $this->assertSame($expected, $result['limit']);
  987. }
  988. /**
  989. * Integration test for checkLimit() being applied inside paginate()
  990. *
  991. * @return void
  992. */
  993. public function testPaginateMaxLimit(): void
  994. {
  995. $this->loadFixtures('Posts');
  996. $table = $this->getTableLocator()->get('PaginatorPosts');
  997. $settings = [
  998. 'maxLimit' => 100,
  999. ];
  1000. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  1001. 'limit' => '1000',
  1002. ]));
  1003. $this->Paginator->paginate($table, $settings);
  1004. $params = $this->controller->getRequest()->getAttribute('paging');
  1005. $this->assertEquals(100, $params['PaginatorPosts']['limit']);
  1006. $this->assertEquals(100, $params['PaginatorPosts']['perPage']);
  1007. $this->controller->setRequest($this->controller->getRequest()->withQueryParams([
  1008. 'limit' => '10',
  1009. ]));
  1010. $this->Paginator->paginate($table, $settings);
  1011. $params = $this->controller->getRequest()->getAttribute('paging');
  1012. $this->assertEquals(10, $params['PaginatorPosts']['limit']);
  1013. $this->assertEquals(10, $params['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()->getAttribute('paging');
  1038. $this->assertEquals(4, $result['PaginatorPosts']['current']);
  1039. $this->assertEquals(4, $result['PaginatorPosts']['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()->getAttribute('paging');
  1045. $this->assertEquals(3, $result['PaginatorPosts']['current']);
  1046. $this->assertEquals(3, $result['PaginatorPosts']['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()->getAttribute('paging');
  1052. $this->assertEquals(1, $result['PaginatorPosts']['current']);
  1053. $this->assertEquals(3, $result['PaginatorPosts']['count']);
  1054. $this->assertEquals(2, $result['PaginatorPosts']['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()->getAttribute('paging');
  1060. $this->assertEquals(2, $result['PaginatorPosts']['current']);
  1061. $this->assertEquals(3, $result['PaginatorPosts']['count']);
  1062. $this->assertEquals(2, $result['PaginatorPosts']['pageCount']);
  1063. $this->assertTrue($result['PaginatorPosts']['nextPage']);
  1064. $this->assertFalse($result['PaginatorPosts']['prevPage']);
  1065. $this->assertEquals(2, $result['PaginatorPosts']['perPage']);
  1066. $this->assertNull($result['PaginatorPosts']['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()->getAttribute('paging');
  1092. $this->assertEquals(2, $result['PaginatorPosts']['current']);
  1093. $this->assertEquals(3, $result['PaginatorPosts']['count']);
  1094. $this->assertEquals(2, $result['PaginatorPosts']['pageCount']);
  1095. $this->assertTrue($result['PaginatorPosts']['nextPage']);
  1096. $this->assertFalse($result['PaginatorPosts']['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->assertSame('First Post', $result[0]->title);
  1181. $this->assertSame('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. /** @var \Cake\ORM\Query|\PHPUnit\Framework\MockObject\MockObject $query */
  1255. $query = $this->getMockBuilder('Cake\ORM\Query')
  1256. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  1257. ->disableOriginalConstructor()
  1258. ->getMock();
  1259. $results = $this->getMockBuilder('Cake\ORM\ResultSet')
  1260. ->disableOriginalConstructor()
  1261. ->getMock();
  1262. $query->expects($this->any())
  1263. ->method('count')
  1264. ->will($this->returnValue(2));
  1265. $query->expects($this->any())
  1266. ->method('all')
  1267. ->will($this->returnValue($results));
  1268. $query->expects($this->any())
  1269. ->method('count')
  1270. ->will($this->returnValue(2));
  1271. if ($table) {
  1272. $query->repository($table);
  1273. }
  1274. return $query;
  1275. }
  1276. /**
  1277. * @return \Cake\Datasource\RepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
  1278. */
  1279. protected function getMockRepository()
  1280. {
  1281. $model = $this->getMockBuilder(RepositoryInterface::class)
  1282. ->setMethods([
  1283. 'getAlias', 'setAlias', 'setRegistryAlias', 'getRegistryAlias', 'hasField',
  1284. 'find', 'get', 'query', 'updateAll', 'deleteAll', 'newEmptyEntity',
  1285. 'exists', 'save', 'delete', 'newEntity', 'newEntities', 'patchEntity', 'patchEntities',
  1286. ])
  1287. ->getMock();
  1288. return $model;
  1289. }
  1290. }