PaginatorComponentTest.php 44 KB

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