PaginatorComponentTest.php 44 KB

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