PaginatorComponentTest.php 43 KB

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