PaginatorComponentTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 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->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->request->query = [
  562. 'limit' => '10'
  563. ];
  564. $this->Paginator->paginate($table, $settings);
  565. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
  566. }
  567. /**
  568. * test paginate() and custom find, to make sure the correct count is returned.
  569. *
  570. * @return void
  571. */
  572. public function testPaginateCustomFind() {
  573. $this->loadFixtures('Post');
  574. $idExtractor = function ($result) {
  575. $ids = [];
  576. foreach ($result as $record) {
  577. $ids[] = $record->id;
  578. }
  579. return $ids;
  580. };
  581. $table = TableRegistry::get('PaginatorPosts');
  582. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  583. $result = $table->save(new \Cake\ORM\Entity($data));
  584. $this->assertNotEmpty($result);
  585. $result = $this->Paginator->paginate($table);
  586. $this->assertCount(4, $result, '4 rows should come back');
  587. $this->assertEquals(array(1, 2, 3, 4), $idExtractor($result));
  588. $result = $this->request->params['paging']['PaginatorPosts'];
  589. $this->assertEquals(4, $result['current']);
  590. $this->assertEquals(4, $result['count']);
  591. $settings = array('findType' => 'published');
  592. $result = $this->Paginator->paginate($table, $settings);
  593. $this->assertCount(3, $result, '3 rows should come back');
  594. $this->assertEquals(array(1, 2, 3), $idExtractor($result));
  595. $result = $this->request->params['paging']['PaginatorPosts'];
  596. $this->assertEquals(3, $result['current']);
  597. $this->assertEquals(3, $result['count']);
  598. $settings = array('findType' => 'published', 'limit' => 2);
  599. $result = $this->Paginator->paginate($table, $settings);
  600. $this->assertCount(2, $result, '2 rows should come back');
  601. $this->assertEquals(array(1, 2), $idExtractor($result));
  602. $result = $this->request->params['paging']['PaginatorPosts'];
  603. $this->assertEquals(2, $result['current']);
  604. $this->assertEquals(3, $result['count']);
  605. $this->assertEquals(2, $result['pageCount']);
  606. $this->assertTrue($result['nextPage']);
  607. $this->assertFalse($result['prevPage']);
  608. }
  609. /**
  610. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  611. *
  612. * @return void
  613. */
  614. public function testPaginateCustomFindFieldsArray() {
  615. $this->loadFixtures('Post');
  616. $table = TableRegistry::get('PaginatorPosts');
  617. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  618. $table->save(new \Cake\ORM\Entity($data));
  619. $settings = [
  620. 'findType' => 'list',
  621. 'conditions' => array('PaginatorPosts.published' => 'Y'),
  622. 'limit' => 2
  623. ];
  624. $results = $this->Paginator->paginate($table, $settings);
  625. $result = $results->toArray();
  626. $expected = array(
  627. 1 => 'First Post',
  628. 2 => 'Second Post',
  629. );
  630. $this->assertEquals($expected, $result);
  631. $result = $this->request->params['paging']['PaginatorPosts'];
  632. $this->assertEquals(2, $result['current']);
  633. $this->assertEquals(3, $result['count']);
  634. $this->assertEquals(2, $result['pageCount']);
  635. $this->assertTrue($result['nextPage']);
  636. $this->assertFalse($result['prevPage']);
  637. }
  638. /**
  639. * test paginate() and custom finders to ensure the count + find
  640. * use the custom type.
  641. *
  642. * @return void
  643. */
  644. public function testPaginateCustomFindCount() {
  645. $settings = array(
  646. 'findType' => 'published',
  647. 'limit' => 2
  648. );
  649. $table = $this->_getMockPosts(['find']);
  650. $query = $this->_getMockFindQuery();
  651. $table->expects($this->once())
  652. ->method('find')
  653. ->with('published')
  654. ->will($this->returnValue($query));
  655. $query->expects($this->once())->method('applyOptions')
  656. ->with(['limit' => 2, 'page' => 1, 'order' => []]);
  657. $this->Paginator->paginate($table, $settings);
  658. }
  659. /**
  660. * Tests that it is possible to pass an already made query object to
  661. * paginate()
  662. *
  663. * @return void
  664. */
  665. public function testPaginateQuery() {
  666. $this->request->query = array('page' => '-1');
  667. $settings = array(
  668. 'PaginatorPosts' => array(
  669. 'contain' => array('PaginatorAuthor'),
  670. 'maxLimit' => 10,
  671. 'group' => 'PaginatorPosts.published',
  672. 'order' => array('PaginatorPosts.id' => 'ASC')
  673. )
  674. );
  675. $table = $this->_getMockPosts(['find']);
  676. $query = $this->_getMockFindQuery($table);
  677. $table->expects($this->never())->method('find');
  678. $query->expects($this->once())
  679. ->method('applyOptions')
  680. ->with([
  681. 'contain' => ['PaginatorAuthor'],
  682. 'group' => 'PaginatorPosts.published',
  683. 'limit' => 10,
  684. 'order' => ['PaginatorPosts.id' => 'ASC'],
  685. 'page' => 1,
  686. ]);
  687. $this->Paginator->paginate($query, $settings);
  688. }
  689. /**
  690. * Tests that passing a query object with a limit clause set will
  691. * overwrite it with the passed defaults.
  692. *
  693. * @return void
  694. */
  695. public function testPaginateQueryWithLimit() {
  696. $this->request->query = array('page' => '-1');
  697. $settings = array(
  698. 'PaginatorPosts' => array(
  699. 'contain' => array('PaginatorAuthor'),
  700. 'maxLimit' => 10,
  701. 'limit' => 5,
  702. 'group' => 'PaginatorPosts.published',
  703. 'order' => array('PaginatorPosts.id' => 'ASC')
  704. )
  705. );
  706. $table = $this->_getMockPosts(['find']);
  707. $query = $this->_getMockFindQuery($table);
  708. $query->limit(2);
  709. $table->expects($this->never())->method('find');
  710. $query->expects($this->once())
  711. ->method('applyOptions')
  712. ->with([
  713. 'contain' => ['PaginatorAuthor'],
  714. 'group' => 'PaginatorPosts.published',
  715. 'limit' => 5,
  716. 'order' => ['PaginatorPosts.id' => 'ASC'],
  717. 'page' => 1,
  718. ]);
  719. $this->Paginator->paginate($query, $settings);
  720. }
  721. /**
  722. * Helper method for making mocks.
  723. *
  724. * @param array $methods
  725. * @return Table
  726. */
  727. protected function _getMockPosts($methods = []) {
  728. return $this->getMock(
  729. 'TestApp\Model\Table\PaginatorPostsTable',
  730. $methods,
  731. [[
  732. 'connection' => ConnectionManager::get('test'),
  733. 'alias' => 'PaginatorPosts',
  734. 'schema' => [
  735. 'id' => ['type' => 'integer'],
  736. 'author_id' => ['type' => 'integer', 'null' => false],
  737. 'title' => ['type' => 'string', 'null' => false],
  738. 'body' => 'text',
  739. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  740. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  741. ]
  742. ]]
  743. );
  744. }
  745. /**
  746. * Helper method for mocking queries.
  747. *
  748. * @return Query
  749. */
  750. protected function _getMockFindQuery($table = null) {
  751. $query = $this->getMockBuilder('Cake\ORM\Query')
  752. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  753. ->disableOriginalConstructor()
  754. ->getMock();
  755. $results = $this->getMock('Cake\ORM\ResultSet', [], [], '', false);
  756. $query->expects($this->any())
  757. ->method('count')
  758. ->will($this->returnValue(2));
  759. $query->expects($this->any())
  760. ->method('all')
  761. ->will($this->returnValue($results));
  762. $query->expects($this->any())
  763. ->method('count')
  764. ->will($this->returnValue(2));
  765. $query->repository($table);
  766. return $query;
  767. }
  768. }