PaginatorTest.php 43 KB

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