PaginatorTest.php 47 KB

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