PaginatorComponentTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. if ($table) {
  1265. $query->repository($table);
  1266. }
  1267. return $query;
  1268. }
  1269. protected function getMockRepository()
  1270. {
  1271. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')
  1272. ->setMethods([
  1273. 'getAlias', 'hasField', 'alias', 'find', 'get', 'query', 'updateAll', 'deleteAll',
  1274. 'exists', 'save', 'delete', 'newEntity', 'newEntities', 'patchEntity', 'patchEntities'
  1275. ])
  1276. ->getMock();
  1277. return $model;
  1278. }
  1279. }