PaginatorComponentTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 2.0.0
  13. * @license http://www.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\Core\Configure;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\Error;
  22. use Cake\Network\Request;
  23. use Cake\ORM\TableRegistry;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Hash;
  26. /**
  27. * PaginatorTestController class
  28. *
  29. */
  30. class PaginatorTestController extends Controller {
  31. /**
  32. * components property
  33. *
  34. * @var array
  35. */
  36. public $components = array('Paginator');
  37. }
  38. class PaginatorComponentTest extends TestCase {
  39. /**
  40. * fixtures property
  41. *
  42. * @var array
  43. */
  44. public $fixtures = array('core.post');
  45. /**
  46. * Don't load data for fixtures for all tests
  47. *
  48. * @var bool
  49. */
  50. public $autoFixtures = false;
  51. /**
  52. * setup
  53. *
  54. * @return void
  55. */
  56. public function setUp() {
  57. parent::setUp();
  58. Configure::write('App.namespace', 'TestApp');
  59. $this->request = new Request('controller_posts/index');
  60. $this->request->params['pass'] = array();
  61. $controller = new Controller($this->request);
  62. $registry = new ComponentRegistry($controller);
  63. $this->Paginator = new PaginatorComponent($registry, []);
  64. $this->Post = $this->getMock('Cake\ORM\Table', [], [], '', false);
  65. }
  66. /**
  67. * tearDown
  68. *
  69. * @return void
  70. */
  71. public function tearDown() {
  72. parent::tearDown();
  73. TableRegistry::clear();
  74. }
  75. /**
  76. * Test that non-numeric values are rejected for page, and limit
  77. *
  78. * @return void
  79. */
  80. public function testPageParamCasting() {
  81. $this->Post->expects($this->any())
  82. ->method('alias')
  83. ->will($this->returnValue('Posts'));
  84. $query = $this->_getMockFindQuery();
  85. $this->Post->expects($this->any())
  86. ->method('find')
  87. ->will($this->returnValue($query));
  88. $this->request->query = array('page' => '1 " onclick="alert(\'xss\');">');
  89. $settings = array('limit' => 1, 'maxLimit' => 10);
  90. $this->Paginator->paginate($this->Post, $settings);
  91. $this->assertSame(1, $this->request->params['paging']['Posts']['page'], 'XSS exploit opened');
  92. }
  93. /**
  94. * test that unknown keys in the default settings are
  95. * passed to the find operations.
  96. *
  97. * @return void
  98. */
  99. public function testPaginateExtraParams() {
  100. $this->request->query = array('page' => '-1');
  101. $settings = array(
  102. 'PaginatorPosts' => array(
  103. 'contain' => array('PaginatorAuthor'),
  104. 'maxLimit' => 10,
  105. 'group' => 'PaginatorPosts.published',
  106. 'order' => array('PaginatorPosts.id' => 'ASC')
  107. ),
  108. );
  109. $table = $this->_getMockPosts(['find']);
  110. $query = $this->_getMockFindQuery();
  111. $table->expects($this->once())
  112. ->method('find')
  113. ->with('all')
  114. ->will($this->returnValue($query));
  115. $query->expects($this->once())
  116. ->method('applyOptions')
  117. ->with([
  118. 'contain' => ['PaginatorAuthor'],
  119. 'group' => 'PaginatorPosts.published',
  120. 'limit' => 10,
  121. 'order' => ['PaginatorPosts.id' => 'ASC'],
  122. 'page' => 1,
  123. ]);
  124. $this->Paginator->paginate($table, $settings);
  125. }
  126. /**
  127. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  128. *
  129. * @return void
  130. */
  131. public function testPaginateCustomFinder() {
  132. $settings = array(
  133. 'PaginatorPosts' => array(
  134. 'findType' => 'popular',
  135. 'fields' => array('id', 'title'),
  136. 'maxLimit' => 10,
  137. )
  138. );
  139. $table = $this->_getMockPosts(['findPopular']);
  140. $query = $this->_getMockFindQuery();
  141. $table->expects($this->any())
  142. ->method('findPopular')
  143. ->will($this->returnValue($query));
  144. $this->Paginator->paginate($table, $settings);
  145. $this->assertEquals('popular', $this->request->params['paging']['PaginatorPosts']['findType']);
  146. }
  147. /**
  148. * test that flat default pagination parameters work.
  149. *
  150. * @return void
  151. */
  152. public function testDefaultPaginateParams() {
  153. $settings = array(
  154. 'order' => ['PaginatorPosts.id' => 'DESC'],
  155. 'maxLimit' => 10,
  156. );
  157. $table = $this->_getMockPosts(['find']);
  158. $query = $this->_getMockFindQuery();
  159. $table->expects($this->once())
  160. ->method('find')
  161. ->with('all')
  162. ->will($this->returnValue($query));
  163. $query->expects($this->once())
  164. ->method('applyOptions')
  165. ->with([
  166. 'limit' => 10,
  167. 'page' => 1,
  168. 'order' => ['PaginatorPosts.id' => 'DESC']
  169. ]);
  170. $this->Paginator->paginate($table, $settings);
  171. }
  172. /**
  173. * test that default sort and default direction are injected into request
  174. *
  175. * @return void
  176. */
  177. public function testDefaultPaginateParamsIntoRequest() {
  178. $settings = array(
  179. 'order' => ['PaginatorPosts.id' => 'DESC'],
  180. 'maxLimit' => 10,
  181. );
  182. $table = $this->_getMockPosts(['find']);
  183. $query = $this->_getMockFindQuery();
  184. $table->expects($this->once())
  185. ->method('find')
  186. ->with('all')
  187. ->will($this->returnValue($query));
  188. $query->expects($this->once())
  189. ->method('applyOptions')
  190. ->with([
  191. 'limit' => 10,
  192. 'page' => 1,
  193. 'order' => ['PaginatorPosts.id' => 'DESC']
  194. ]);
  195. $this->Paginator->paginate($table, $settings);
  196. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
  197. $this->assertEquals('DESC', $this->request->params['paging']['PaginatorPosts']['directionDefault']);
  198. }
  199. /**
  200. * test that option merging prefers specific models
  201. *
  202. * @return void
  203. */
  204. public function testMergeOptionsModelSpecific() {
  205. $settings = array(
  206. 'page' => 1,
  207. 'limit' => 20,
  208. 'maxLimit' => 100,
  209. 'Posts' => array(
  210. 'page' => 1,
  211. 'limit' => 10,
  212. 'maxLimit' => 50,
  213. )
  214. );
  215. $result = $this->Paginator->mergeOptions('Silly', $settings);
  216. $this->assertEquals($settings, $result);
  217. $result = $this->Paginator->mergeOptions('Posts', $settings);
  218. $expected = array('page' => 1, 'limit' => 10, 'maxLimit' => 50);
  219. $this->assertEquals($expected, $result);
  220. }
  221. /**
  222. * test mergeOptions with customFind key
  223. *
  224. * @return void
  225. */
  226. public function testMergeOptionsCustomFindKey() {
  227. $this->request->query = [
  228. 'page' => 10,
  229. 'limit' => 10
  230. ];
  231. $settings = [
  232. 'page' => 1,
  233. 'limit' => 20,
  234. 'maxLimit' => 100,
  235. 'findType' => 'myCustomFind'
  236. ];
  237. $result = $this->Paginator->mergeOptions('Post', $settings);
  238. $expected = array(
  239. 'page' => 10,
  240. 'limit' => 10,
  241. 'maxLimit' => 100,
  242. 'findType' => 'myCustomFind'
  243. );
  244. $this->assertEquals($expected, $result);
  245. }
  246. /**
  247. * test merging options from the querystring.
  248. *
  249. * @return void
  250. */
  251. public function testMergeOptionsQueryString() {
  252. $this->request->query = array(
  253. 'page' => 99,
  254. 'limit' => 75
  255. );
  256. $settings = array(
  257. 'page' => 1,
  258. 'limit' => 20,
  259. 'maxLimit' => 100,
  260. );
  261. $result = $this->Paginator->mergeOptions('Post', $settings);
  262. $expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100);
  263. $this->assertEquals($expected, $result);
  264. }
  265. /**
  266. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  267. *
  268. * @return void
  269. */
  270. public function testMergeOptionsDefaultWhiteList() {
  271. $this->request->query = array(
  272. 'page' => 10,
  273. 'limit' => 10,
  274. 'fields' => array('bad.stuff'),
  275. 'recursive' => 1000,
  276. 'conditions' => array('bad.stuff'),
  277. 'contain' => array('bad')
  278. );
  279. $settings = array(
  280. 'page' => 1,
  281. 'limit' => 20,
  282. 'maxLimit' => 100,
  283. );
  284. $result = $this->Paginator->mergeOptions('Post', $settings);
  285. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100);
  286. $this->assertEquals($expected, $result);
  287. }
  288. /**
  289. * test that modifying the whitelist works.
  290. *
  291. * @return void
  292. */
  293. public function testMergeOptionsExtraWhitelist() {
  294. $this->request->query = array(
  295. 'page' => 10,
  296. 'limit' => 10,
  297. 'fields' => array('bad.stuff'),
  298. 'recursive' => 1000,
  299. 'conditions' => array('bad.stuff'),
  300. 'contain' => array('bad')
  301. );
  302. $settings = array(
  303. 'page' => 1,
  304. 'limit' => 20,
  305. 'maxLimit' => 100,
  306. );
  307. $this->Paginator->config('whitelist', ['fields']);
  308. $result = $this->Paginator->mergeOptions('Post', $settings);
  309. $expected = array(
  310. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => array('bad.stuff')
  311. );
  312. $this->assertEquals($expected, $result);
  313. }
  314. /**
  315. * test mergeOptions with limit > maxLimit in code.
  316. *
  317. * @return void
  318. */
  319. public function testMergeOptionsMaxLimit() {
  320. $settings = array(
  321. 'limit' => 200,
  322. 'paramType' => 'named',
  323. );
  324. $result = $this->Paginator->mergeOptions('Post', $settings);
  325. $expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 200, 'paramType' => 'named');
  326. $this->assertEquals($expected, $result);
  327. $settings = array(
  328. 'maxLimit' => 10,
  329. 'paramType' => 'named',
  330. );
  331. $result = $this->Paginator->mergeOptions('Post', $settings);
  332. $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
  333. $this->assertEquals($expected, $result);
  334. }
  335. /**
  336. * Integration test to ensure that validateSort is being used by paginate()
  337. *
  338. * @return void
  339. */
  340. public function testValidateSortInvalid() {
  341. $table = $this->_getMockPosts(['find']);
  342. $query = $this->_getMockFindQuery();
  343. $table->expects($this->once())
  344. ->method('find')
  345. ->with('all')
  346. ->will($this->returnValue($query));
  347. $query->expects($this->once())->method('applyOptions')
  348. ->with([
  349. 'limit' => 20,
  350. 'page' => 1,
  351. 'order' => ['PaginatorPosts.id' => 'asc']
  352. ]);
  353. $this->request->query = [
  354. 'page' => 1,
  355. 'sort' => 'id',
  356. 'direction' => 'herp'
  357. ];
  358. $this->Paginator->paginate($table);
  359. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sort']);
  360. $this->assertEquals('asc', $this->request->params['paging']['PaginatorPosts']['direction']);
  361. }
  362. /**
  363. * test that invalid directions are ignored.
  364. *
  365. * @return void
  366. */
  367. public function testValidateSortInvalidDirection() {
  368. $model = $this->getMock('Cake\ORM\Table');
  369. $model->expects($this->any())
  370. ->method('alias')
  371. ->will($this->returnValue('model'));
  372. $model->expects($this->any())
  373. ->method('hasField')
  374. ->will($this->returnValue(true));
  375. $options = array('sort' => 'something', 'direction' => 'boogers');
  376. $result = $this->Paginator->validateSort($model, $options);
  377. $this->assertEquals('asc', $result['order']['model.something']);
  378. }
  379. /**
  380. * Test that a really large page number gets clamped to the max page size.
  381. *
  382. * @return void
  383. */
  384. public function testOutOfRangePageNumberGetsClamped() {
  385. $this->loadFixtures('Post');
  386. $this->request->query['page'] = 3000;
  387. $table = TableRegistry::get('PaginatorPosts');
  388. try {
  389. $this->Paginator->paginate($table);
  390. $this->fail('No exception raised');
  391. } catch (\Cake\Error\NotFoundException $e) {
  392. $this->assertEquals(
  393. 1,
  394. $this->request->params['paging']['PaginatorPosts']['page'],
  395. 'Page number should not be 0'
  396. );
  397. }
  398. }
  399. /**
  400. * Test that a really REALLY large page number gets clamped to the max page size.
  401. *
  402. * @expectedException \Cake\Error\NotFoundException
  403. * @return void
  404. */
  405. public function testOutOfVeryBigPageNumberGetsClamped() {
  406. $this->loadFixtures('Post');
  407. $this->request->query = [
  408. 'page' => '3000000000000000000000000',
  409. ];
  410. $table = TableRegistry::get('PaginatorPosts');
  411. $this->Paginator->paginate($table);
  412. }
  413. /**
  414. * test that fields not in whitelist won't be part of order conditions.
  415. *
  416. * @return void
  417. */
  418. public function testValidateSortWhitelistFailure() {
  419. $model = $this->getMock('Cake\ORM\Table');
  420. $model->expects($this->any())
  421. ->method('alias')
  422. ->will($this->returnValue('model'));
  423. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  424. $options = array(
  425. 'sort' => 'body',
  426. 'direction' => 'asc',
  427. 'sortWhitelist' => ['title', 'id']
  428. );
  429. $result = $this->Paginator->validateSort($model, $options);
  430. $this->assertEquals([], $result['order']);
  431. }
  432. /**
  433. * test that fields in the whitelist are not validated
  434. *
  435. * @return void
  436. */
  437. public function testValidateSortWhitelistTrusted() {
  438. $model = $this->getMock('Cake\ORM\Table');
  439. $model->expects($this->any())
  440. ->method('alias')
  441. ->will($this->returnValue('model'));
  442. $model->expects($this->never())->method('hasField');
  443. $options = array(
  444. 'sort' => 'body',
  445. 'direction' => 'asc',
  446. 'sortWhitelist' => ['body']
  447. );
  448. $result = $this->Paginator->validateSort($model, $options);
  449. $expected = array('body' => 'asc');
  450. $this->assertEquals($expected, $result['order']);
  451. }
  452. /**
  453. * test that multiple sort works.
  454. *
  455. * @return void
  456. */
  457. public function testValidateSortMultiple() {
  458. $model = $this->getMock('Cake\ORM\Table');
  459. $model->expects($this->any())
  460. ->method('alias')
  461. ->will($this->returnValue('model'));
  462. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  463. $options = array(
  464. 'order' => array(
  465. 'author_id' => 'asc',
  466. 'title' => 'asc'
  467. )
  468. );
  469. $result = $this->Paginator->validateSort($model, $options);
  470. $expected = array(
  471. 'model.author_id' => 'asc',
  472. 'model.title' => 'asc'
  473. );
  474. $this->assertEquals($expected, $result['order']);
  475. }
  476. /**
  477. * Tests that order strings can used by Paginator
  478. *
  479. * @return void
  480. */
  481. public function testValidateSortWithString() {
  482. $model = $this->getMock('Cake\ORM\Table');
  483. $model->expects($this->any())
  484. ->method('alias')
  485. ->will($this->returnValue('model'));
  486. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  487. $options = array(
  488. 'order' => 'model.author_id DESC'
  489. );
  490. $result = $this->Paginator->validateSort($model, $options);
  491. $expected = 'model.author_id DESC';
  492. $this->assertEquals($expected, $result['order']);
  493. }
  494. /**
  495. * Test that no sort doesn't trigger an error.
  496. *
  497. * @return void
  498. */
  499. public function testValidateSortNoSort() {
  500. $model = $this->getMock('Cake\ORM\Table');
  501. $model->expects($this->any())
  502. ->method('alias')
  503. ->will($this->returnValue('model'));
  504. $model->expects($this->any())->method('hasField')
  505. ->will($this->returnValue(true));
  506. $options = array(
  507. 'direction' => 'asc',
  508. 'sortWhitelist' => ['title', 'id'],
  509. );
  510. $result = $this->Paginator->validateSort($model, $options);
  511. $this->assertEquals([], $result['order']);
  512. }
  513. /**
  514. * Test sorting with incorrect aliases on valid fields.
  515. *
  516. * @return void
  517. */
  518. public function testValidateSortInvalidAlias() {
  519. $model = $this->getMock('Cake\ORM\Table');
  520. $model->expects($this->any())
  521. ->method('alias')
  522. ->will($this->returnValue('model'));
  523. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  524. $options = array('sort' => 'Derp.id');
  525. $result = $this->Paginator->validateSort($model, $options);
  526. $this->assertEquals(array(), $result['order']);
  527. }
  528. /**
  529. * test that maxLimit is respected
  530. *
  531. * @return void
  532. */
  533. public function testCheckLimit() {
  534. $result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
  535. $this->assertEquals(100, $result['limit']);
  536. $result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
  537. $this->assertEquals(1, $result['limit']);
  538. $result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
  539. $this->assertEquals(1, $result['limit']);
  540. $result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
  541. $this->assertEquals(1, $result['limit']);
  542. $result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
  543. $this->assertEquals(1, $result['limit']);
  544. }
  545. /**
  546. * Integration test for checkLimit() being applied inside paginate()
  547. *
  548. * @return void
  549. */
  550. public function testPaginateMaxLimit() {
  551. $this->loadFixtures('Post');
  552. $table = TableRegistry::get('PaginatorPosts');
  553. $settings = [
  554. 'maxLimit' => 100,
  555. ];
  556. $this->request->query = [
  557. 'limit' => '1000'
  558. ];
  559. $this->Paginator->paginate($table, $settings);
  560. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['limit']);
  561. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['perPage']);
  562. $this->request->query = [
  563. 'limit' => '10'
  564. ];
  565. $this->Paginator->paginate($table, $settings);
  566. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
  567. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['perPage']);
  568. }
  569. /**
  570. * test paginate() and custom find, to make sure the correct count is returned.
  571. *
  572. * @return void
  573. */
  574. public function testPaginateCustomFind() {
  575. $this->loadFixtures('Post');
  576. $idExtractor = function ($result) {
  577. $ids = [];
  578. foreach ($result as $record) {
  579. $ids[] = $record->id;
  580. }
  581. return $ids;
  582. };
  583. $table = TableRegistry::get('PaginatorPosts');
  584. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  585. $result = $table->save(new \Cake\ORM\Entity($data));
  586. $this->assertNotEmpty($result);
  587. $result = $this->Paginator->paginate($table);
  588. $this->assertCount(4, $result, '4 rows should come back');
  589. $this->assertEquals(array(1, 2, 3, 4), $idExtractor($result));
  590. $result = $this->request->params['paging']['PaginatorPosts'];
  591. $this->assertEquals(4, $result['current']);
  592. $this->assertEquals(4, $result['count']);
  593. $settings = array('findType' => 'published');
  594. $result = $this->Paginator->paginate($table, $settings);
  595. $this->assertCount(3, $result, '3 rows should come back');
  596. $this->assertEquals(array(1, 2, 3), $idExtractor($result));
  597. $result = $this->request->params['paging']['PaginatorPosts'];
  598. $this->assertEquals(3, $result['current']);
  599. $this->assertEquals(3, $result['count']);
  600. $settings = array('findType' => 'published', 'limit' => 2);
  601. $result = $this->Paginator->paginate($table, $settings);
  602. $this->assertCount(2, $result, '2 rows should come back');
  603. $this->assertEquals(array(1, 2), $idExtractor($result));
  604. $result = $this->request->params['paging']['PaginatorPosts'];
  605. $this->assertEquals(2, $result['current']);
  606. $this->assertEquals(3, $result['count']);
  607. $this->assertEquals(2, $result['pageCount']);
  608. $this->assertTrue($result['nextPage']);
  609. $this->assertFalse($result['prevPage']);
  610. $this->assertEquals(2, $result['perPage']);
  611. $this->assertNull($result['limit']);
  612. }
  613. /**
  614. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  615. *
  616. * @return void
  617. */
  618. public function testPaginateCustomFindFieldsArray() {
  619. $this->loadFixtures('Post');
  620. $table = TableRegistry::get('PaginatorPosts');
  621. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  622. $table->save(new \Cake\ORM\Entity($data));
  623. $settings = [
  624. 'findType' => 'list',
  625. 'conditions' => array('PaginatorPosts.published' => 'Y'),
  626. 'limit' => 2
  627. ];
  628. $results = $this->Paginator->paginate($table, $settings);
  629. $result = $results->toArray();
  630. $expected = array(
  631. 1 => 'First Post',
  632. 2 => 'Second Post',
  633. );
  634. $this->assertEquals($expected, $result);
  635. $result = $this->request->params['paging']['PaginatorPosts'];
  636. $this->assertEquals(2, $result['current']);
  637. $this->assertEquals(3, $result['count']);
  638. $this->assertEquals(2, $result['pageCount']);
  639. $this->assertTrue($result['nextPage']);
  640. $this->assertFalse($result['prevPage']);
  641. }
  642. /**
  643. * test paginate() and custom finders to ensure the count + find
  644. * use the custom type.
  645. *
  646. * @return void
  647. */
  648. public function testPaginateCustomFindCount() {
  649. $settings = array(
  650. 'findType' => 'published',
  651. 'limit' => 2
  652. );
  653. $table = $this->_getMockPosts(['find']);
  654. $query = $this->_getMockFindQuery();
  655. $table->expects($this->once())
  656. ->method('find')
  657. ->with('published')
  658. ->will($this->returnValue($query));
  659. $query->expects($this->once())->method('applyOptions')
  660. ->with(['limit' => 2, 'page' => 1, 'order' => []]);
  661. $this->Paginator->paginate($table, $settings);
  662. }
  663. /**
  664. * Tests that it is possible to pass an already made query object to
  665. * paginate()
  666. *
  667. * @return void
  668. */
  669. public function testPaginateQuery() {
  670. $this->request->query = array('page' => '-1');
  671. $settings = array(
  672. 'PaginatorPosts' => array(
  673. 'contain' => array('PaginatorAuthor'),
  674. 'maxLimit' => 10,
  675. 'group' => 'PaginatorPosts.published',
  676. 'order' => array('PaginatorPosts.id' => 'ASC')
  677. )
  678. );
  679. $table = $this->_getMockPosts(['find']);
  680. $query = $this->_getMockFindQuery($table);
  681. $table->expects($this->never())->method('find');
  682. $query->expects($this->once())
  683. ->method('applyOptions')
  684. ->with([
  685. 'contain' => ['PaginatorAuthor'],
  686. 'group' => 'PaginatorPosts.published',
  687. 'limit' => 10,
  688. 'order' => ['PaginatorPosts.id' => 'ASC'],
  689. 'page' => 1,
  690. ]);
  691. $this->Paginator->paginate($query, $settings);
  692. }
  693. /**
  694. * Tests that passing a query object with a limit clause set will
  695. * overwrite it with the passed defaults.
  696. *
  697. * @return void
  698. */
  699. public function testPaginateQueryWithLimit() {
  700. $this->request->query = array('page' => '-1');
  701. $settings = array(
  702. 'PaginatorPosts' => array(
  703. 'contain' => array('PaginatorAuthor'),
  704. 'maxLimit' => 10,
  705. 'limit' => 5,
  706. 'group' => 'PaginatorPosts.published',
  707. 'order' => array('PaginatorPosts.id' => 'ASC')
  708. )
  709. );
  710. $table = $this->_getMockPosts(['find']);
  711. $query = $this->_getMockFindQuery($table);
  712. $query->limit(2);
  713. $table->expects($this->never())->method('find');
  714. $query->expects($this->once())
  715. ->method('applyOptions')
  716. ->with([
  717. 'contain' => ['PaginatorAuthor'],
  718. 'group' => 'PaginatorPosts.published',
  719. 'limit' => 5,
  720. 'order' => ['PaginatorPosts.id' => 'ASC'],
  721. 'page' => 1,
  722. ]);
  723. $this->Paginator->paginate($query, $settings);
  724. }
  725. /**
  726. * Helper method for making mocks.
  727. *
  728. * @param array $methods
  729. * @return Table
  730. */
  731. protected function _getMockPosts($methods = []) {
  732. return $this->getMock(
  733. 'TestApp\Model\Table\PaginatorPostsTable',
  734. $methods,
  735. [[
  736. 'connection' => ConnectionManager::get('test'),
  737. 'alias' => 'PaginatorPosts',
  738. 'schema' => [
  739. 'id' => ['type' => 'integer'],
  740. 'author_id' => ['type' => 'integer', 'null' => false],
  741. 'title' => ['type' => 'string', 'null' => false],
  742. 'body' => 'text',
  743. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  744. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  745. ]
  746. ]]
  747. );
  748. }
  749. /**
  750. * Helper method for mocking queries.
  751. *
  752. * @return Query
  753. */
  754. protected function _getMockFindQuery($table = null) {
  755. $query = $this->getMockBuilder('Cake\ORM\Query')
  756. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  757. ->disableOriginalConstructor()
  758. ->getMock();
  759. $results = $this->getMock('Cake\ORM\ResultSet', [], [], '', false);
  760. $query->expects($this->any())
  761. ->method('count')
  762. ->will($this->returnValue(2));
  763. $query->expects($this->any())
  764. ->method('all')
  765. ->will($this->returnValue($results));
  766. $query->expects($this->any())
  767. ->method('count')
  768. ->will($this->returnValue(2));
  769. $query->repository($table);
  770. return $query;
  771. }
  772. }