PaginatorComponentTest.php 24 KB

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