PaginatorComponentTest.php 43 KB

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