PaginatorComponentTest.php 25 KB

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