PaginatorTest.php 42 KB

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