PaginatorComponentTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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');
  45. /**
  46. * Don't load data for fixtures for all tests
  47. *
  48. * @var boolean
  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->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->request->query = [
  407. 'page' => '3000000000000000000000000',
  408. ];
  409. $table = TableRegistry::get('PaginatorPosts');
  410. $this->Paginator->paginate($table);
  411. }
  412. /**
  413. * test that fields not in whitelist won't be part of order conditions.
  414. *
  415. * @return void
  416. */
  417. public function testValidateSortWhitelistFailure() {
  418. $model = $this->getMock('Cake\ORM\Table');
  419. $model->expects($this->any())
  420. ->method('alias')
  421. ->will($this->returnValue('model'));
  422. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  423. $options = array(
  424. 'sort' => 'body',
  425. 'direction' => 'asc',
  426. 'sortWhitelist' => ['title', 'id']
  427. );
  428. $result = $this->Paginator->validateSort($model, $options);
  429. $this->assertEquals([], $result['order']);
  430. }
  431. /**
  432. * test that fields in the whitelist are not validated
  433. *
  434. * @return void
  435. */
  436. public function testValidateSortWhitelistTrusted() {
  437. $model = $this->getMock('Cake\ORM\Table');
  438. $model->expects($this->any())
  439. ->method('alias')
  440. ->will($this->returnValue('model'));
  441. $model->expects($this->never())->method('hasField');
  442. $options = array(
  443. 'sort' => 'body',
  444. 'direction' => 'asc',
  445. 'sortWhitelist' => ['body']
  446. );
  447. $result = $this->Paginator->validateSort($model, $options);
  448. $expected = array('body' => 'asc');
  449. $this->assertEquals($expected, $result['order']);
  450. }
  451. /**
  452. * test that multiple sort works.
  453. *
  454. * @return void
  455. */
  456. public function testValidateSortMultiple() {
  457. $model = $this->getMock('Cake\ORM\Table');
  458. $model->expects($this->any())
  459. ->method('alias')
  460. ->will($this->returnValue('model'));
  461. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  462. $options = array(
  463. 'order' => array(
  464. 'author_id' => 'asc',
  465. 'title' => 'asc'
  466. )
  467. );
  468. $result = $this->Paginator->validateSort($model, $options);
  469. $expected = array(
  470. 'model.author_id' => 'asc',
  471. 'model.title' => 'asc'
  472. );
  473. $this->assertEquals($expected, $result['order']);
  474. }
  475. /**
  476. * Tests that order strings can used by Paginator
  477. *
  478. * @return void
  479. */
  480. public function testValidateSortWithString() {
  481. $model = $this->getMock('Cake\ORM\Table');
  482. $model->expects($this->any())
  483. ->method('alias')
  484. ->will($this->returnValue('model'));
  485. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  486. $options = array(
  487. 'order' => 'model.author_id DESC'
  488. );
  489. $result = $this->Paginator->validateSort($model, $options);
  490. $expected = 'model.author_id DESC';
  491. $this->assertEquals($expected, $result['order']);
  492. }
  493. /**
  494. * Test that no sort doesn't trigger an error.
  495. *
  496. * @return void
  497. */
  498. public function testValidateSortNoSort() {
  499. $model = $this->getMock('Cake\ORM\Table');
  500. $model->expects($this->any())
  501. ->method('alias')
  502. ->will($this->returnValue('model'));
  503. $model->expects($this->any())->method('hasField')
  504. ->will($this->returnValue(true));
  505. $options = array(
  506. 'direction' => 'asc',
  507. 'sortWhitelist' => ['title', 'id'],
  508. );
  509. $result = $this->Paginator->validateSort($model, $options);
  510. $this->assertEquals([], $result['order']);
  511. }
  512. /**
  513. * Test sorting with incorrect aliases on valid fields.
  514. *
  515. * @return void
  516. */
  517. public function testValidateSortInvalidAlias() {
  518. $model = $this->getMock('Cake\ORM\Table');
  519. $model->expects($this->any())
  520. ->method('alias')
  521. ->will($this->returnValue('model'));
  522. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  523. $options = array('sort' => 'Derp.id');
  524. $result = $this->Paginator->validateSort($model, $options);
  525. $this->assertEquals(array(), $result['order']);
  526. }
  527. /**
  528. * test that maxLimit is respected
  529. *
  530. * @return void
  531. */
  532. public function testCheckLimit() {
  533. $result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
  534. $this->assertEquals(100, $result['limit']);
  535. $result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
  536. $this->assertEquals(1, $result['limit']);
  537. $result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
  538. $this->assertEquals(1, $result['limit']);
  539. $result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
  540. $this->assertEquals(1, $result['limit']);
  541. $result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
  542. $this->assertEquals(1, $result['limit']);
  543. }
  544. /**
  545. * Integration test for checkLimit() being applied inside paginate()
  546. *
  547. * @return void
  548. */
  549. public function testPaginateMaxLimit() {
  550. $table = TableRegistry::get('PaginatorPosts');
  551. $settings = [
  552. 'maxLimit' => 100,
  553. ];
  554. $this->request->query = [
  555. 'limit' => '1000'
  556. ];
  557. $this->Paginator->paginate($table, $settings);
  558. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['limit']);
  559. $this->request->query = [
  560. 'limit' => '10'
  561. ];
  562. $this->Paginator->paginate($table, $settings);
  563. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
  564. }
  565. /**
  566. * test paginate() and custom find, to make sure the correct count is returned.
  567. *
  568. * @return void
  569. */
  570. public function testPaginateCustomFind() {
  571. $this->loadFixtures('Post');
  572. $idExtractor = function ($result) {
  573. $ids = [];
  574. foreach ($result as $record) {
  575. $ids[] = $record->id;
  576. }
  577. return $ids;
  578. };
  579. $table = TableRegistry::get('PaginatorPosts');
  580. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  581. $result = $table->save(new \Cake\ORM\Entity($data));
  582. $this->assertNotEmpty($result);
  583. $result = $this->Paginator->paginate($table);
  584. $this->assertCount(4, $result, '4 rows should come back');
  585. $this->assertEquals(array(1, 2, 3, 4), $idExtractor($result));
  586. $result = $this->request->params['paging']['PaginatorPosts'];
  587. $this->assertEquals(4, $result['current']);
  588. $this->assertEquals(4, $result['count']);
  589. $settings = array('findType' => 'published');
  590. $result = $this->Paginator->paginate($table, $settings);
  591. $this->assertCount(3, $result, '3 rows should come back');
  592. $this->assertEquals(array(1, 2, 3), $idExtractor($result));
  593. $result = $this->request->params['paging']['PaginatorPosts'];
  594. $this->assertEquals(3, $result['current']);
  595. $this->assertEquals(3, $result['count']);
  596. $settings = array('findType' => 'published', 'limit' => 2);
  597. $result = $this->Paginator->paginate($table, $settings);
  598. $this->assertCount(2, $result, '2 rows should come back');
  599. $this->assertEquals(array(1, 2), $idExtractor($result));
  600. $result = $this->request->params['paging']['PaginatorPosts'];
  601. $this->assertEquals(2, $result['current']);
  602. $this->assertEquals(3, $result['count']);
  603. $this->assertEquals(2, $result['pageCount']);
  604. $this->assertTrue($result['nextPage']);
  605. $this->assertFalse($result['prevPage']);
  606. }
  607. /**
  608. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  609. *
  610. * @return void
  611. */
  612. public function testPaginateCustomFindFieldsArray() {
  613. $this->loadFixtures('Post');
  614. $table = TableRegistry::get('PaginatorPosts');
  615. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  616. $table->save(new \Cake\ORM\Entity($data));
  617. $settings = [
  618. 'findType' => 'list',
  619. 'conditions' => array('PaginatorPosts.published' => 'Y'),
  620. 'limit' => 2
  621. ];
  622. $results = $this->Paginator->paginate($table, $settings);
  623. $result = $results->toArray();
  624. $expected = array(
  625. 1 => 'First Post',
  626. 2 => 'Second Post',
  627. );
  628. $this->assertEquals($expected, $result);
  629. $result = $this->request->params['paging']['PaginatorPosts'];
  630. $this->assertEquals(2, $result['current']);
  631. $this->assertEquals(3, $result['count']);
  632. $this->assertEquals(2, $result['pageCount']);
  633. $this->assertTrue($result['nextPage']);
  634. $this->assertFalse($result['prevPage']);
  635. }
  636. /**
  637. * test paginate() and custom finders to ensure the count + find
  638. * use the custom type.
  639. *
  640. * @return void
  641. */
  642. public function testPaginateCustomFindCount() {
  643. $settings = array(
  644. 'findType' => 'published',
  645. 'limit' => 2
  646. );
  647. $table = $this->_getMockPosts(['find']);
  648. $query = $this->_getMockFindQuery();
  649. $table->expects($this->once())
  650. ->method('find')
  651. ->with('published')
  652. ->will($this->returnValue($query));
  653. $query->expects($this->once())->method('applyOptions')
  654. ->with(['limit' => 2, 'page' => 1, 'order' => []]);
  655. $this->Paginator->paginate($table, $settings);
  656. }
  657. /**
  658. * Tests that it is possible to pass an already made query object to
  659. * paginate()
  660. *
  661. * @return void
  662. */
  663. public function testPaginateQuery() {
  664. $this->request->query = array('page' => '-1');
  665. $settings = array(
  666. 'PaginatorPosts' => array(
  667. 'contain' => array('PaginatorAuthor'),
  668. 'maxLimit' => 10,
  669. 'group' => 'PaginatorPosts.published',
  670. 'order' => array('PaginatorPosts.id' => 'ASC')
  671. )
  672. );
  673. $table = $this->_getMockPosts(['find']);
  674. $query = $this->_getMockFindQuery($table);
  675. $table->expects($this->never())->method('find');
  676. $query->expects($this->once())
  677. ->method('applyOptions')
  678. ->with([
  679. 'contain' => ['PaginatorAuthor'],
  680. 'group' => 'PaginatorPosts.published',
  681. 'limit' => 10,
  682. 'order' => ['PaginatorPosts.id' => 'ASC'],
  683. 'page' => 1,
  684. ]);
  685. $this->Paginator->paginate($query, $settings);
  686. }
  687. /**
  688. * Tests that passing a query object with a limit clause set will
  689. * overwrite it with the passed defaults.
  690. *
  691. * @return void
  692. */
  693. public function testPaginateQueryWithLimit() {
  694. $this->request->query = array('page' => '-1');
  695. $settings = array(
  696. 'PaginatorPosts' => array(
  697. 'contain' => array('PaginatorAuthor'),
  698. 'maxLimit' => 10,
  699. 'limit' => 5,
  700. 'group' => 'PaginatorPosts.published',
  701. 'order' => array('PaginatorPosts.id' => 'ASC')
  702. )
  703. );
  704. $table = $this->_getMockPosts(['find']);
  705. $query = $this->_getMockFindQuery($table);
  706. $query->limit(2);
  707. $table->expects($this->never())->method('find');
  708. $query->expects($this->once())
  709. ->method('applyOptions')
  710. ->with([
  711. 'contain' => ['PaginatorAuthor'],
  712. 'group' => 'PaginatorPosts.published',
  713. 'limit' => 5,
  714. 'order' => ['PaginatorPosts.id' => 'ASC'],
  715. 'page' => 1,
  716. ]);
  717. $this->Paginator->paginate($query, $settings);
  718. }
  719. /**
  720. * Helper method for making mocks.
  721. *
  722. * @param array $methods
  723. * @return Table
  724. */
  725. protected function _getMockPosts($methods = []) {
  726. return $this->getMock(
  727. 'TestApp\Model\Table\PaginatorPostsTable',
  728. $methods,
  729. [[
  730. 'connection' => ConnectionManager::get('test'),
  731. 'alias' => 'PaginatorPosts',
  732. 'schema' => [
  733. 'id' => ['type' => 'integer'],
  734. 'author_id' => ['type' => 'integer', 'null' => false],
  735. 'title' => ['type' => 'string', 'null' => false],
  736. 'body' => 'text',
  737. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  738. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  739. ]
  740. ]]
  741. );
  742. }
  743. /**
  744. * Helper method for mocking queries.
  745. *
  746. * @return Query
  747. */
  748. protected function _getMockFindQuery($table = null) {
  749. $query = $this->getMockBuilder('Cake\ORM\Query')
  750. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  751. ->disableOriginalConstructor()
  752. ->getMock();
  753. $results = $this->getMock('Cake\ORM\ResultSet', [], [], '', false);
  754. $query->expects($this->any())
  755. ->method('count')
  756. ->will($this->returnValue(2));
  757. $query->expects($this->any())
  758. ->method('all')
  759. ->will($this->returnValue($results));
  760. $query->expects($this->any())
  761. ->method('count')
  762. ->will($this->returnValue(2));
  763. $query->repository($table);
  764. return $query;
  765. }
  766. }