PaginatorComponentTest.php 25 KB

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