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. public $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. * 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 (EventInterface $event, Query $query): void {
  253. $query ->matching('Tags', function (Query $q) {
  254. return $q->matching('Authors', function (Query $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. /** @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', 'createEntity',
  1285. 'exists', 'save', 'delete', 'newEntity', 'newEntities', 'patchEntity', 'patchEntities',
  1286. ])
  1287. ->getMock();
  1288. return $model;
  1289. }
  1290. }