PaginatorComponentTest.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. 'sort' => 'PaginatorPosts.id',
  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 = $this->getTableLocator()->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 = $this->getTableLocator()->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 = $this->getTableLocator()->get('Articles');
  247. $articles->belongsToMany('Tags');
  248. $tags = $this->getTableLocator()->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. 'sort' => 'PaginatorPosts.id',
  288. ]);
  289. $this->Paginator->paginate($table, $settings);
  290. }
  291. /**
  292. * test that default sort and default direction are injected into request
  293. *
  294. * @return void
  295. */
  296. public function testDefaultPaginateParamsIntoRequest()
  297. {
  298. $settings = [
  299. 'order' => ['PaginatorPosts.id' => 'DESC'],
  300. 'maxLimit' => 10,
  301. ];
  302. $table = $this->_getMockPosts(['query']);
  303. $query = $this->_getMockFindQuery();
  304. $table->expects($this->once())
  305. ->method('query')
  306. ->will($this->returnValue($query));
  307. $query->expects($this->once())
  308. ->method('applyOptions')
  309. ->with([
  310. 'limit' => 10,
  311. 'page' => 1,
  312. 'order' => ['PaginatorPosts.id' => 'DESC'],
  313. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  314. 'scope' => null,
  315. 'sort' => 'PaginatorPosts.id',
  316. ]);
  317. $this->Paginator->paginate($table, $settings);
  318. $this->assertEquals('PaginatorPosts.id', $this->controller->request->getParam('paging.PaginatorPosts.sortDefault'));
  319. $this->assertEquals('DESC', $this->controller->request->getParam('paging.PaginatorPosts.directionDefault'));
  320. }
  321. /**
  322. * test that option merging prefers specific models
  323. *
  324. * @return void
  325. */
  326. public function testMergeOptionsModelSpecific()
  327. {
  328. $settings = [
  329. 'page' => 1,
  330. 'limit' => 20,
  331. 'maxLimit' => 100,
  332. 'Posts' => [
  333. 'page' => 1,
  334. 'limit' => 10,
  335. 'maxLimit' => 50,
  336. ],
  337. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  338. ];
  339. $result = $this->Paginator->mergeOptions('Silly', $settings);
  340. $this->assertEquals($settings, $result);
  341. $result = $this->Paginator->mergeOptions('Posts', $settings);
  342. $expected = ['page' => 1, 'limit' => 10, 'maxLimit' => 50, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  343. $this->assertEquals($expected, $result);
  344. }
  345. /**
  346. * test mergeOptions with custom scope
  347. *
  348. * @return void
  349. */
  350. public function testMergeOptionsCustomScope()
  351. {
  352. $this->controller->request = $this->controller->request->withQueryParams([
  353. 'page' => 10,
  354. 'limit' => 10,
  355. 'scope' => [
  356. 'page' => 2,
  357. 'limit' => 5,
  358. ]
  359. ]);
  360. $settings = [
  361. 'page' => 1,
  362. 'limit' => 20,
  363. 'maxLimit' => 100,
  364. 'finder' => 'myCustomFind',
  365. ];
  366. $result = $this->Paginator->mergeOptions('Post', $settings);
  367. $expected = [
  368. 'page' => 10,
  369. 'limit' => 10,
  370. 'maxLimit' => 100,
  371. 'finder' => 'myCustomFind',
  372. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  373. ];
  374. $this->assertEquals($expected, $result);
  375. $settings = [
  376. 'page' => 1,
  377. 'limit' => 20,
  378. 'maxLimit' => 100,
  379. 'finder' => 'myCustomFind',
  380. 'scope' => 'non-existent',
  381. ];
  382. $result = $this->Paginator->mergeOptions('Post', $settings);
  383. $expected = [
  384. 'page' => 1,
  385. 'limit' => 20,
  386. 'maxLimit' => 100,
  387. 'finder' => 'myCustomFind',
  388. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  389. 'scope' => 'non-existent',
  390. ];
  391. $this->assertEquals($expected, $result);
  392. $settings = [
  393. 'page' => 1,
  394. 'limit' => 20,
  395. 'maxLimit' => 100,
  396. 'finder' => 'myCustomFind',
  397. 'scope' => 'scope',
  398. ];
  399. $result = $this->Paginator->mergeOptions('Post', $settings);
  400. $expected = [
  401. 'page' => 2,
  402. 'limit' => 5,
  403. 'maxLimit' => 100,
  404. 'finder' => 'myCustomFind',
  405. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  406. 'scope' => 'scope',
  407. ];
  408. $this->assertEquals($expected, $result);
  409. }
  410. /**
  411. * test mergeOptions with customFind key
  412. *
  413. * @return void
  414. */
  415. public function testMergeOptionsCustomFindKey()
  416. {
  417. $this->controller->request = $this->controller->request->withQueryParams([
  418. 'page' => 10,
  419. 'limit' => 10
  420. ]);
  421. $settings = [
  422. 'page' => 1,
  423. 'limit' => 20,
  424. 'maxLimit' => 100,
  425. 'finder' => 'myCustomFind'
  426. ];
  427. $result = $this->Paginator->mergeOptions('Post', $settings);
  428. $expected = [
  429. 'page' => 10,
  430. 'limit' => 10,
  431. 'maxLimit' => 100,
  432. 'finder' => 'myCustomFind',
  433. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  434. ];
  435. $this->assertEquals($expected, $result);
  436. }
  437. /**
  438. * test merging options from the querystring.
  439. *
  440. * @return void
  441. */
  442. public function testMergeOptionsQueryString()
  443. {
  444. $this->controller->request = $this->controller->request->withQueryParams([
  445. 'page' => 99,
  446. 'limit' => 75
  447. ]);
  448. $settings = [
  449. 'page' => 1,
  450. 'limit' => 20,
  451. 'maxLimit' => 100,
  452. ];
  453. $result = $this->Paginator->mergeOptions('Post', $settings);
  454. $expected = ['page' => 99, 'limit' => 75, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  455. $this->assertEquals($expected, $result);
  456. }
  457. /**
  458. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  459. *
  460. * @return void
  461. */
  462. public function testMergeOptionsDefaultWhiteList()
  463. {
  464. $this->controller->request = $this->controller->request->withQueryParams([
  465. 'page' => 10,
  466. 'limit' => 10,
  467. 'fields' => ['bad.stuff'],
  468. 'recursive' => 1000,
  469. 'conditions' => ['bad.stuff'],
  470. 'contain' => ['bad']
  471. ]);
  472. $settings = [
  473. 'page' => 1,
  474. 'limit' => 20,
  475. 'maxLimit' => 100,
  476. ];
  477. $result = $this->Paginator->mergeOptions('Post', $settings);
  478. $expected = ['page' => 10, 'limit' => 10, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  479. $this->assertEquals($expected, $result);
  480. }
  481. /**
  482. * test that modifying the whitelist works.
  483. *
  484. * @return void
  485. */
  486. public function testMergeOptionsExtraWhitelist()
  487. {
  488. $this->controller->request = $this->controller->request->withQueryParams([
  489. 'page' => 10,
  490. 'limit' => 10,
  491. 'fields' => ['bad.stuff'],
  492. 'recursive' => 1000,
  493. 'conditions' => ['bad.stuff'],
  494. 'contain' => ['bad']
  495. ]);
  496. $settings = [
  497. 'page' => 1,
  498. 'limit' => 20,
  499. 'maxLimit' => 100,
  500. ];
  501. $this->Paginator->setConfig('whitelist', ['fields']);
  502. $result = $this->Paginator->mergeOptions('Post', $settings);
  503. $expected = [
  504. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => ['bad.stuff'], 'whitelist' => ['limit', 'sort', 'page', 'direction', 'fields']
  505. ];
  506. $this->assertEquals($expected, $result);
  507. }
  508. /**
  509. * test mergeOptions with limit > maxLimit in code.
  510. *
  511. * @return void
  512. */
  513. public function testMergeOptionsMaxLimit()
  514. {
  515. $settings = [
  516. 'limit' => 200,
  517. 'paramType' => 'named',
  518. ];
  519. $result = $this->Paginator->mergeOptions('Post', $settings);
  520. $expected = [
  521. 'page' => 1,
  522. 'limit' => 100,
  523. 'maxLimit' => 100,
  524. 'paramType' => 'named',
  525. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  526. ];
  527. $this->assertEquals($expected, $result);
  528. $settings = [
  529. 'maxLimit' => 10,
  530. 'paramType' => 'named',
  531. ];
  532. $result = $this->Paginator->mergeOptions('Post', $settings);
  533. $expected = [
  534. 'page' => 1,
  535. 'limit' => 10,
  536. 'maxLimit' => 10,
  537. 'paramType' => 'named',
  538. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  539. ];
  540. $this->assertEquals($expected, $result);
  541. }
  542. /**
  543. * test getDefaults with limit > maxLimit in code.
  544. *
  545. * @return void
  546. */
  547. public function testGetDefaultMaxLimit()
  548. {
  549. $settings = [
  550. 'page' => 1,
  551. 'limit' => 2,
  552. 'maxLimit' => 10,
  553. 'order' => [
  554. 'Users.username' => 'asc'
  555. ],
  556. ];
  557. $result = $this->Paginator->mergeOptions('Post', $settings);
  558. $expected = [
  559. 'page' => 1,
  560. 'limit' => 2,
  561. 'maxLimit' => 10,
  562. 'order' => [
  563. 'Users.username' => 'asc'
  564. ],
  565. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  566. ];
  567. $this->assertEquals($expected, $result);
  568. $settings = [
  569. 'page' => 1,
  570. 'limit' => 100,
  571. 'maxLimit' => 10,
  572. 'order' => [
  573. 'Users.username' => 'asc'
  574. ],
  575. ];
  576. $result = $this->Paginator->mergeOptions('Post', $settings);
  577. $expected = [
  578. 'page' => 1,
  579. 'limit' => 10,
  580. 'maxLimit' => 10,
  581. 'order' => [
  582. 'Users.username' => 'asc'
  583. ],
  584. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  585. ];
  586. $this->assertEquals($expected, $result);
  587. }
  588. /**
  589. * Integration test to ensure that validateSort is being used by paginate()
  590. *
  591. * @return void
  592. */
  593. public function testValidateSortInvalid()
  594. {
  595. $table = $this->_getMockPosts(['query']);
  596. $query = $this->_getMockFindQuery();
  597. $table->expects($this->once())
  598. ->method('query')
  599. ->will($this->returnValue($query));
  600. $query->expects($this->once())->method('applyOptions')
  601. ->with([
  602. 'limit' => 20,
  603. 'page' => 1,
  604. 'order' => ['PaginatorPosts.id' => 'asc'],
  605. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  606. 'scope' => null,
  607. 'sort' => 'id',
  608. ]);
  609. $this->controller->request = $this->controller->request->withQueryParams([
  610. 'page' => 1,
  611. 'sort' => 'id',
  612. 'direction' => 'herp'
  613. ]);
  614. $this->Paginator->paginate($table);
  615. $this->assertEquals('id', $this->controller->request->getParam('paging.PaginatorPosts.sort'));
  616. $this->assertEquals('asc', $this->controller->request->getParam('paging.PaginatorPosts.direction'));
  617. }
  618. /**
  619. * test that invalid directions are ignored.
  620. *
  621. * @return void
  622. */
  623. public function testValidateSortInvalidDirection()
  624. {
  625. $model = $this->getMockRepository();
  626. $model->expects($this->any())
  627. ->method('getAlias')
  628. ->will($this->returnValue('model'));
  629. $model->expects($this->any())
  630. ->method('hasField')
  631. ->will($this->returnValue(true));
  632. $options = ['sort' => 'something', 'direction' => 'boogers'];
  633. $result = $this->Paginator->validateSort($model, $options);
  634. $this->assertEquals('asc', $result['order']['model.something']);
  635. }
  636. /**
  637. * Test empty pagination result.
  638. *
  639. * @return void
  640. */
  641. public function testEmptyPaginationResult()
  642. {
  643. $this->loadFixtures('Posts');
  644. $table = $this->getTableLocator()->get('PaginatorPosts');
  645. $table->deleteAll('1=1');
  646. $this->Paginator->paginate($table);
  647. $this->assertSame(
  648. 0,
  649. $this->controller->request->getParam('paging.PaginatorPosts.count'),
  650. 'Count should be 0'
  651. );
  652. $this->assertSame(
  653. 1,
  654. $this->controller->request->getParam('paging.PaginatorPosts.page'),
  655. 'Page number should not be 0'
  656. );
  657. $this->assertSame(
  658. 1,
  659. $this->controller->request->getParam('paging.PaginatorPosts.pageCount'),
  660. 'Page count number should not be 0'
  661. );
  662. }
  663. /**
  664. * Test that a really large page number gets clamped to the max page size.
  665. *
  666. * @return void
  667. */
  668. public function testOutOfRangePageNumberGetsClamped()
  669. {
  670. $this->loadFixtures('Posts');
  671. $this->controller->request = $this->controller->request->withQueryParams(['page' => 3000]);
  672. $table = $this->getTableLocator()->get('PaginatorPosts');
  673. $e = null;
  674. try {
  675. $this->Paginator->paginate($table);
  676. } catch (NotFoundException $e) {
  677. }
  678. $this->assertEquals(
  679. 1,
  680. $this->controller->request->getParam('paging.PaginatorPosts.page'),
  681. 'Page number should not be 0'
  682. );
  683. $this->assertNotNull($e);
  684. $this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious());
  685. }
  686. /**
  687. * Test that a out of bounds request still knows about the page size
  688. *
  689. * @return void
  690. */
  691. public function testOutOfRangePageNumberStillProvidesPageCount()
  692. {
  693. $this->loadFixtures('Posts');
  694. $this->controller->request = $this->controller->request->withQueryParams([
  695. 'limit' => 1,
  696. 'page' => '4',
  697. ]);
  698. $table = $this->getTableLocator()->get('PaginatorPosts');
  699. $e = null;
  700. try {
  701. $this->Paginator->paginate($table);
  702. } catch (NotFoundException $e) {
  703. }
  704. $this->assertEquals(
  705. 3,
  706. $this->controller->request->getParam('paging.PaginatorPosts.pageCount'),
  707. 'Page count number should not be 0'
  708. );
  709. $this->assertNotNull($e);
  710. $this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious());
  711. }
  712. /**
  713. * Test that a really REALLY large page number gets clamped to the max page size.
  714. *
  715. * @return void
  716. */
  717. public function testOutOfVeryBigPageNumberGetsClamped()
  718. {
  719. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  720. $this->loadFixtures('Posts');
  721. $this->controller->request = $this->controller->request->withQueryParams([
  722. 'page' => '3000000000000000000000000',
  723. ]);
  724. $table = $this->getTableLocator()->get('PaginatorPosts');
  725. $this->Paginator->paginate($table);
  726. }
  727. /**
  728. * test that fields not in whitelist won't be part of order conditions.
  729. *
  730. * @return void
  731. */
  732. public function testValidateSortWhitelistFailure()
  733. {
  734. $model = $this->getMockRepository();
  735. $model->expects($this->any())
  736. ->method('getAlias')
  737. ->will($this->returnValue('model'));
  738. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  739. $options = [
  740. 'sort' => 'body',
  741. 'direction' => 'asc',
  742. 'sortWhitelist' => ['title', 'id']
  743. ];
  744. $result = $this->Paginator->validateSort($model, $options);
  745. $this->assertEquals([], $result['order']);
  746. }
  747. /**
  748. * test that fields in the whitelist are not validated
  749. *
  750. * @return void
  751. */
  752. public function testValidateSortWhitelistTrusted()
  753. {
  754. $model = $this->getMockRepository();
  755. $model->expects($this->any())
  756. ->method('getAlias')
  757. ->will($this->returnValue('model'));
  758. $model->expects($this->once())
  759. ->method('hasField')
  760. ->will($this->returnValue(true));
  761. $options = [
  762. 'sort' => 'body',
  763. 'direction' => 'asc',
  764. 'sortWhitelist' => ['body']
  765. ];
  766. $result = $this->Paginator->validateSort($model, $options);
  767. $expected = ['model.body' => 'asc'];
  768. $this->assertEquals(
  769. $expected,
  770. $result['order'],
  771. 'Trusted fields in schema should be prefixed'
  772. );
  773. }
  774. /**
  775. * test that whitelist as empty array does not allow any sorting
  776. *
  777. * @return void
  778. */
  779. public function testValidateSortWhitelistEmpty()
  780. {
  781. $model = $this->getMockRepository();
  782. $model->expects($this->any())
  783. ->method('getAlias')
  784. ->will($this->returnValue('model'));
  785. $model->expects($this->any())->method('hasField')
  786. ->will($this->returnValue(true));
  787. $options = [
  788. 'order' => [
  789. 'body' => 'asc',
  790. 'foo.bar' => 'asc'
  791. ],
  792. 'sort' => 'body',
  793. 'direction' => 'asc',
  794. 'sortWhitelist' => []
  795. ];
  796. $result = $this->Paginator->validateSort($model, $options);
  797. $this->assertSame([], $result['order'], 'No sort should be applied');
  798. }
  799. /**
  800. * test that fields in the whitelist are not validated
  801. *
  802. * @return void
  803. */
  804. public function testValidateSortWhitelistNotInSchema()
  805. {
  806. $model = $this->getMockRepository();
  807. $model->expects($this->any())
  808. ->method('getAlias')
  809. ->will($this->returnValue('model'));
  810. $model->expects($this->once())->method('hasField')
  811. ->will($this->returnValue(false));
  812. $options = [
  813. 'sort' => 'score',
  814. 'direction' => 'asc',
  815. 'sortWhitelist' => ['score']
  816. ];
  817. $result = $this->Paginator->validateSort($model, $options);
  818. $expected = ['score' => 'asc'];
  819. $this->assertEquals(
  820. $expected,
  821. $result['order'],
  822. 'Trusted fields not in schema should not be altered'
  823. );
  824. }
  825. /**
  826. * test that multiple fields in the whitelist are not validated and properly aliased.
  827. *
  828. * @return void
  829. */
  830. public function testValidateSortWhitelistMultiple()
  831. {
  832. $model = $this->getMockRepository();
  833. $model->expects($this->any())
  834. ->method('getAlias')
  835. ->will($this->returnValue('model'));
  836. $model->expects($this->once())
  837. ->method('hasField')
  838. ->will($this->returnValue(true));
  839. $options = [
  840. 'order' => [
  841. 'body' => 'asc',
  842. 'foo.bar' => 'asc'
  843. ],
  844. 'sortWhitelist' => ['body', 'foo.bar']
  845. ];
  846. $result = $this->Paginator->validateSort($model, $options);
  847. $expected = [
  848. 'model.body' => 'asc',
  849. 'foo.bar' => 'asc'
  850. ];
  851. $this->assertEquals($expected, $result['order']);
  852. }
  853. /**
  854. * test that multiple sort works.
  855. *
  856. * @return void
  857. */
  858. public function testValidateSortMultiple()
  859. {
  860. $model = $this->getMockRepository();
  861. $model->expects($this->any())
  862. ->method('getAlias')
  863. ->will($this->returnValue('model'));
  864. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  865. $options = [
  866. 'order' => [
  867. 'author_id' => 'asc',
  868. 'title' => 'asc'
  869. ]
  870. ];
  871. $result = $this->Paginator->validateSort($model, $options);
  872. $expected = [
  873. 'model.author_id' => 'asc',
  874. 'model.title' => 'asc'
  875. ];
  876. $this->assertEquals($expected, $result['order']);
  877. }
  878. /**
  879. * Tests that order strings can used by Paginator
  880. *
  881. * @return void
  882. */
  883. public function testValidateSortWithString()
  884. {
  885. $model = $this->getMockRepository();
  886. $model->expects($this->any())
  887. ->method('getAlias')
  888. ->will($this->returnValue('model'));
  889. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  890. $options = [
  891. 'order' => 'model.author_id DESC'
  892. ];
  893. $result = $this->Paginator->validateSort($model, $options);
  894. $expected = 'model.author_id DESC';
  895. $this->assertEquals($expected, $result['order']);
  896. }
  897. /**
  898. * Test that no sort doesn't trigger an error.
  899. *
  900. * @return void
  901. */
  902. public function testValidateSortNoSort()
  903. {
  904. $model = $this->getMockRepository();
  905. $model->expects($this->any())
  906. ->method('getAlias')
  907. ->will($this->returnValue('model'));
  908. $model->expects($this->any())->method('hasField')
  909. ->will($this->returnValue(true));
  910. $options = [
  911. 'direction' => 'asc',
  912. 'sortWhitelist' => ['title', 'id'],
  913. ];
  914. $result = $this->Paginator->validateSort($model, $options);
  915. $this->assertEquals([], $result['order']);
  916. }
  917. /**
  918. * Test sorting with incorrect aliases on valid fields.
  919. *
  920. * @return void
  921. */
  922. public function testValidateSortInvalidAlias()
  923. {
  924. $model = $this->getMockRepository();
  925. $model->expects($this->any())
  926. ->method('getAlias')
  927. ->will($this->returnValue('model'));
  928. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  929. $options = ['sort' => 'Derp.id'];
  930. $result = $this->Paginator->validateSort($model, $options);
  931. $this->assertEquals([], $result['order']);
  932. }
  933. /**
  934. * @return array
  935. */
  936. public function checkLimitProvider()
  937. {
  938. return [
  939. 'out of bounds' => [
  940. ['limit' => 1000000, 'maxLimit' => 100],
  941. 100,
  942. ],
  943. 'limit is nan' => [
  944. ['limit' => 'sheep!', 'maxLimit' => 100],
  945. 1,
  946. ],
  947. 'negative limit' => [
  948. ['limit' => '-1', 'maxLimit' => 100],
  949. 1,
  950. ],
  951. 'unset limit' => [
  952. ['limit' => null, 'maxLimit' => 100],
  953. 1,
  954. ],
  955. 'limit = 0' => [
  956. ['limit' => 0, 'maxLimit' => 100],
  957. 1,
  958. ],
  959. 'limit = 0 v2' => [
  960. ['limit' => 0, 'maxLimit' => 0],
  961. 1,
  962. ],
  963. 'limit = null' => [
  964. ['limit' => null, 'maxLimit' => 0],
  965. 1,
  966. ],
  967. 'bad input, results in 1' => [
  968. ['limit' => null, 'maxLimit' => null],
  969. 1,
  970. ],
  971. 'bad input, results in 1 v2' => [
  972. ['limit' => false, 'maxLimit' => false],
  973. 1,
  974. ],
  975. ];
  976. }
  977. /**
  978. * test that maxLimit is respected
  979. *
  980. * @dataProvider checkLimitProvider
  981. * @return void
  982. */
  983. public function testCheckLimit($input, $expected)
  984. {
  985. $result = $this->Paginator->checkLimit($input);
  986. $this->assertSame($expected, $result['limit']);
  987. }
  988. /**
  989. * Integration test for checkLimit() being applied inside paginate()
  990. *
  991. * @return void
  992. */
  993. public function testPaginateMaxLimit()
  994. {
  995. $this->loadFixtures('Posts');
  996. $table = $this->getTableLocator()->get('PaginatorPosts');
  997. $settings = [
  998. 'maxLimit' => 100,
  999. ];
  1000. $this->controller->request = $this->controller->request->withQueryParams([
  1001. 'limit' => '1000'
  1002. ]);
  1003. $this->Paginator->paginate($table, $settings);
  1004. $this->assertEquals(100, $this->controller->request->getParam('paging.PaginatorPosts.limit'));
  1005. $this->assertEquals(100, $this->controller->request->getParam('paging.PaginatorPosts.perPage'));
  1006. $this->controller->request = $this->controller->request->withQueryParams([
  1007. 'limit' => '10'
  1008. ]);
  1009. $this->Paginator->paginate($table, $settings);
  1010. $this->assertEquals(10, $this->controller->request->getParam('paging.PaginatorPosts.limit'));
  1011. $this->assertEquals(10, $this->controller->request->getParam('paging.PaginatorPosts.perPage'));
  1012. }
  1013. /**
  1014. * test paginate() and custom find, to make sure the correct count is returned.
  1015. *
  1016. * @return void
  1017. */
  1018. public function testPaginateCustomFind()
  1019. {
  1020. $this->loadFixtures('Posts');
  1021. $titleExtractor = function ($result) {
  1022. $ids = [];
  1023. foreach ($result as $record) {
  1024. $ids[] = $record->title;
  1025. }
  1026. return $ids;
  1027. };
  1028. $table = $this->getTableLocator()->get('PaginatorPosts');
  1029. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  1030. $result = $table->save(new Entity($data));
  1031. $this->assertNotEmpty($result);
  1032. $result = $this->Paginator->paginate($table);
  1033. $this->assertCount(4, $result, '4 rows should come back');
  1034. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  1035. $result = $this->controller->request->getParam('paging.PaginatorPosts');
  1036. $this->assertEquals(4, $result['current']);
  1037. $this->assertEquals(4, $result['count']);
  1038. $settings = ['finder' => 'published'];
  1039. $result = $this->Paginator->paginate($table, $settings);
  1040. $this->assertCount(3, $result, '3 rows should come back');
  1041. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  1042. $result = $this->controller->request->getParam('paging.PaginatorPosts');
  1043. $this->assertEquals(3, $result['current']);
  1044. $this->assertEquals(3, $result['count']);
  1045. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  1046. $result = $this->Paginator->paginate($table, $settings);
  1047. $this->assertCount(1, $result, '1 rows should come back');
  1048. $this->assertEquals(['Third Post'], $titleExtractor($result));
  1049. $result = $this->controller->request->getParam('paging.PaginatorPosts');
  1050. $this->assertEquals(1, $result['current']);
  1051. $this->assertEquals(3, $result['count']);
  1052. $this->assertEquals(2, $result['pageCount']);
  1053. $settings = ['finder' => 'published', 'limit' => 2];
  1054. $result = $this->Paginator->paginate($table, $settings);
  1055. $this->assertCount(2, $result, '2 rows should come back');
  1056. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  1057. $result = $this->controller->request->getParam('paging.PaginatorPosts');
  1058. $this->assertEquals(2, $result['current']);
  1059. $this->assertEquals(3, $result['count']);
  1060. $this->assertEquals(2, $result['pageCount']);
  1061. $this->assertTrue($result['nextPage']);
  1062. $this->assertFalse($result['prevPage']);
  1063. $this->assertEquals(2, $result['perPage']);
  1064. $this->assertNull($result['limit']);
  1065. }
  1066. /**
  1067. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  1068. *
  1069. * @return void
  1070. */
  1071. public function testPaginateCustomFindFieldsArray()
  1072. {
  1073. $this->loadFixtures('Posts');
  1074. $table = $this->getTableLocator()->get('PaginatorPosts');
  1075. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  1076. $table->save(new Entity($data));
  1077. $settings = [
  1078. 'finder' => 'list',
  1079. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  1080. 'limit' => 2
  1081. ];
  1082. $results = $this->Paginator->paginate($table, $settings);
  1083. $result = $results->toArray();
  1084. $expected = [
  1085. 1 => 'First Post',
  1086. 2 => 'Second Post',
  1087. ];
  1088. $this->assertEquals($expected, $result);
  1089. $result = $this->controller->request->getParam('paging.PaginatorPosts');
  1090. $this->assertEquals(2, $result['current']);
  1091. $this->assertEquals(3, $result['count']);
  1092. $this->assertEquals(2, $result['pageCount']);
  1093. $this->assertTrue($result['nextPage']);
  1094. $this->assertFalse($result['prevPage']);
  1095. }
  1096. /**
  1097. * test paginate() and custom finders to ensure the count + find
  1098. * use the custom type.
  1099. *
  1100. * @return void
  1101. */
  1102. public function testPaginateCustomFindCount()
  1103. {
  1104. $settings = [
  1105. 'finder' => 'published',
  1106. 'limit' => 2
  1107. ];
  1108. $table = $this->_getMockPosts(['query']);
  1109. $query = $this->_getMockFindQuery();
  1110. $table->expects($this->once())
  1111. ->method('query')
  1112. ->will($this->returnValue($query));
  1113. $query->expects($this->once())->method('applyOptions')
  1114. ->with([
  1115. 'limit' => 2,
  1116. 'page' => 1,
  1117. 'order' => [],
  1118. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1119. 'scope' => null,
  1120. 'sort' => null,
  1121. ]);
  1122. $this->Paginator->paginate($table, $settings);
  1123. }
  1124. /**
  1125. * Tests that it is possible to pass an already made query object to
  1126. * paginate()
  1127. *
  1128. * @return void
  1129. */
  1130. public function testPaginateQuery()
  1131. {
  1132. $this->controller->request = $this->controller->request->withQueryParams(['page' => '-1']);
  1133. $settings = [
  1134. 'PaginatorPosts' => [
  1135. 'contain' => ['PaginatorAuthor'],
  1136. 'maxLimit' => 10,
  1137. 'group' => 'PaginatorPosts.published',
  1138. 'order' => ['PaginatorPosts.id' => 'ASC']
  1139. ]
  1140. ];
  1141. $table = $this->_getMockPosts(['find']);
  1142. $query = $this->_getMockFindQuery($table);
  1143. $table->expects($this->never())->method('find');
  1144. $query->expects($this->once())
  1145. ->method('applyOptions')
  1146. ->with([
  1147. 'contain' => ['PaginatorAuthor'],
  1148. 'group' => 'PaginatorPosts.published',
  1149. 'limit' => 10,
  1150. 'order' => ['PaginatorPosts.id' => 'ASC'],
  1151. 'page' => 1,
  1152. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1153. 'scope' => null,
  1154. 'sort' => 'PaginatorPosts.id',
  1155. ]);
  1156. $this->Paginator->paginate($query, $settings);
  1157. }
  1158. /**
  1159. * test paginate() with bind()
  1160. *
  1161. * @return void
  1162. */
  1163. public function testPaginateQueryWithBindValue()
  1164. {
  1165. $config = ConnectionManager::getConfig('test');
  1166. $this->skipIf(strpos($config['driver'], 'Sqlserver') !== false, 'Test temporarily broken in SQLServer');
  1167. $this->loadFixtures('Posts');
  1168. $table = $this->getTableLocator()->get('PaginatorPosts');
  1169. $query = $table->find()
  1170. ->where(['PaginatorPosts.author_id BETWEEN :start AND :end'])
  1171. ->bind(':start', 1)
  1172. ->bind(':end', 2);
  1173. $results = $this->Paginator->paginate($query, []);
  1174. $result = $results->toArray();
  1175. $this->assertCount(2, $result);
  1176. $this->assertEquals('First Post', $result[0]->title);
  1177. $this->assertEquals('Third Post', $result[1]->title);
  1178. }
  1179. /**
  1180. * Tests that passing a query object with a limit clause set will
  1181. * overwrite it with the passed defaults.
  1182. *
  1183. * @return void
  1184. */
  1185. public function testPaginateQueryWithLimit()
  1186. {
  1187. $this->controller->request = $this->controller->request->withQueryParams(['page' => '-1']);
  1188. $settings = [
  1189. 'PaginatorPosts' => [
  1190. 'contain' => ['PaginatorAuthor'],
  1191. 'maxLimit' => 10,
  1192. 'limit' => 5,
  1193. 'group' => 'PaginatorPosts.published',
  1194. 'order' => ['PaginatorPosts.id' => 'ASC']
  1195. ]
  1196. ];
  1197. $table = $this->_getMockPosts(['find']);
  1198. $query = $this->_getMockFindQuery($table);
  1199. $query->limit(2);
  1200. $table->expects($this->never())->method('find');
  1201. $query->expects($this->once())
  1202. ->method('applyOptions')
  1203. ->with([
  1204. 'contain' => ['PaginatorAuthor'],
  1205. 'group' => 'PaginatorPosts.published',
  1206. 'limit' => 5,
  1207. 'order' => ['PaginatorPosts.id' => 'ASC'],
  1208. 'page' => 1,
  1209. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1210. 'scope' => null,
  1211. 'sort' => 'PaginatorPosts.id',
  1212. ]);
  1213. $this->Paginator->paginate($query, $settings);
  1214. }
  1215. /**
  1216. * Helper method for making mocks.
  1217. *
  1218. * @param array $methods
  1219. * @return \Cake\ORM\Table|\PHPUnit_Framework_MockObject_MockObject
  1220. */
  1221. protected function _getMockPosts($methods = [])
  1222. {
  1223. return $this->getMockBuilder('TestApp\Model\Table\PaginatorPostsTable')
  1224. ->setMethods($methods)
  1225. ->setConstructorArgs([[
  1226. 'connection' => ConnectionManager::get('test'),
  1227. 'alias' => 'PaginatorPosts',
  1228. 'schema' => [
  1229. 'id' => ['type' => 'integer'],
  1230. 'author_id' => ['type' => 'integer', 'null' => false],
  1231. 'title' => ['type' => 'string', 'null' => false],
  1232. 'body' => 'text',
  1233. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  1234. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  1235. ]
  1236. ]])
  1237. ->getMock();
  1238. }
  1239. /**
  1240. * Helper method for mocking queries.
  1241. *
  1242. * @param string|null $table
  1243. *
  1244. * @return \Cake\ORM\Query|\PHPUnit_Framework_MockObject_MockObject
  1245. */
  1246. protected function _getMockFindQuery($table = null)
  1247. {
  1248. $query = $this->getMockBuilder('Cake\ORM\Query')
  1249. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  1250. ->disableOriginalConstructor()
  1251. ->getMock();
  1252. $results = $this->getMockBuilder('Cake\ORM\ResultSet')
  1253. ->disableOriginalConstructor()
  1254. ->getMock();
  1255. $query->expects($this->any())
  1256. ->method('count')
  1257. ->will($this->returnValue(2));
  1258. $query->expects($this->any())
  1259. ->method('all')
  1260. ->will($this->returnValue($results));
  1261. $query->expects($this->any())
  1262. ->method('count')
  1263. ->will($this->returnValue(2));
  1264. $query->repository($table);
  1265. return $query;
  1266. }
  1267. protected function getMockRepository()
  1268. {
  1269. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')
  1270. ->setMethods([
  1271. 'getAlias', 'hasField', 'alias', 'find', 'get', 'query', 'updateAll', 'deleteAll',
  1272. 'exists', 'save', 'delete', 'newEntity', 'newEntities', 'patchEntity', 'patchEntities'
  1273. ])
  1274. ->getMock();
  1275. return $model;
  1276. }
  1277. }