PaginatorTest.php 43 KB

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