PaginatorComponentTest.php 44 KB

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