PaginatorComponentTest.php 20 KB

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