PaginatorComponentTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. *
  130. * @return void
  131. */
  132. public function testPaginateCustomFinderOptions() {
  133. $this->loadFixtures('Post');
  134. $settings = [
  135. 'PaginatorPosts' => [
  136. 'finder' => 'author',
  137. 'author_id' => 1
  138. ]
  139. ];
  140. $table = TableRegistry::get('PaginatorPosts');
  141. $expected = $table->find('author', ['conditions' => ['PaginatorPosts.author_id' => $settings['PaginatorPosts']['author_id']]])
  142. ->count();
  143. $result = $this->Paginator->paginate($table, $settings)->count();
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  148. *
  149. * @return void
  150. */
  151. public function testPaginateCustomFinder() {
  152. $settings = array(
  153. 'PaginatorPosts' => array(
  154. 'finder' => 'popular',
  155. 'fields' => array('id', 'title'),
  156. 'maxLimit' => 10,
  157. )
  158. );
  159. $table = $this->_getMockPosts(['findPopular']);
  160. $query = $this->_getMockFindQuery();
  161. $table->expects($this->any())
  162. ->method('findPopular')
  163. ->will($this->returnValue($query));
  164. $this->Paginator->paginate($table, $settings);
  165. $this->assertEquals('popular', $this->request->params['paging']['PaginatorPosts']['finder']);
  166. }
  167. /**
  168. * test that flat default pagination parameters work.
  169. *
  170. * @return void
  171. */
  172. public function testDefaultPaginateParams() {
  173. $settings = array(
  174. 'order' => ['PaginatorPosts.id' => 'DESC'],
  175. 'maxLimit' => 10,
  176. );
  177. $table = $this->_getMockPosts(['find']);
  178. $query = $this->_getMockFindQuery();
  179. $table->expects($this->once())
  180. ->method('find')
  181. ->with('all')
  182. ->will($this->returnValue($query));
  183. $query->expects($this->once())
  184. ->method('applyOptions')
  185. ->with([
  186. 'limit' => 10,
  187. 'page' => 1,
  188. 'order' => ['PaginatorPosts.id' => 'DESC'],
  189. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  190. ]);
  191. $this->Paginator->paginate($table, $settings);
  192. }
  193. /**
  194. * test that default sort and default direction are injected into request
  195. *
  196. * @return void
  197. */
  198. public function testDefaultPaginateParamsIntoRequest() {
  199. $settings = array(
  200. 'order' => ['PaginatorPosts.id' => 'DESC'],
  201. 'maxLimit' => 10,
  202. );
  203. $table = $this->_getMockPosts(['find']);
  204. $query = $this->_getMockFindQuery();
  205. $table->expects($this->once())
  206. ->method('find')
  207. ->with('all')
  208. ->will($this->returnValue($query));
  209. $query->expects($this->once())
  210. ->method('applyOptions')
  211. ->with([
  212. 'limit' => 10,
  213. 'page' => 1,
  214. 'order' => ['PaginatorPosts.id' => 'DESC'],
  215. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  216. ]);
  217. $this->Paginator->paginate($table, $settings);
  218. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
  219. $this->assertEquals('DESC', $this->request->params['paging']['PaginatorPosts']['directionDefault']);
  220. }
  221. /**
  222. * test that option merging prefers specific models
  223. *
  224. * @return void
  225. */
  226. public function testMergeOptionsModelSpecific() {
  227. $settings = array(
  228. 'page' => 1,
  229. 'limit' => 20,
  230. 'maxLimit' => 100,
  231. 'Posts' => array(
  232. 'page' => 1,
  233. 'limit' => 10,
  234. 'maxLimit' => 50,
  235. ),
  236. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  237. );
  238. $result = $this->Paginator->mergeOptions('Silly', $settings);
  239. $this->assertEquals($settings, $result);
  240. $result = $this->Paginator->mergeOptions('Posts', $settings);
  241. $expected = array('page' => 1, 'limit' => 10, 'maxLimit' => 50, 'whitelist' => ['limit', 'sort', 'page', 'direction']);
  242. $this->assertEquals($expected, $result);
  243. }
  244. /**
  245. * test mergeOptions with customFind key
  246. *
  247. * @return void
  248. */
  249. public function testMergeOptionsCustomFindKey() {
  250. $this->request->query = [
  251. 'page' => 10,
  252. 'limit' => 10
  253. ];
  254. $settings = [
  255. 'page' => 1,
  256. 'limit' => 20,
  257. 'maxLimit' => 100,
  258. 'finder' => 'myCustomFind'
  259. ];
  260. $result = $this->Paginator->mergeOptions('Post', $settings);
  261. $expected = array(
  262. 'page' => 10,
  263. 'limit' => 10,
  264. 'maxLimit' => 100,
  265. 'finder' => 'myCustomFind',
  266. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  267. );
  268. $this->assertEquals($expected, $result);
  269. }
  270. /**
  271. * test merging options from the querystring.
  272. *
  273. * @return void
  274. */
  275. public function testMergeOptionsQueryString() {
  276. $this->request->query = array(
  277. 'page' => 99,
  278. 'limit' => 75
  279. );
  280. $settings = array(
  281. 'page' => 1,
  282. 'limit' => 20,
  283. 'maxLimit' => 100,
  284. );
  285. $result = $this->Paginator->mergeOptions('Post', $settings);
  286. $expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']);
  287. $this->assertEquals($expected, $result);
  288. }
  289. /**
  290. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  291. *
  292. * @return void
  293. */
  294. public function testMergeOptionsDefaultWhiteList() {
  295. $this->request->query = array(
  296. 'page' => 10,
  297. 'limit' => 10,
  298. 'fields' => array('bad.stuff'),
  299. 'recursive' => 1000,
  300. 'conditions' => array('bad.stuff'),
  301. 'contain' => array('bad')
  302. );
  303. $settings = array(
  304. 'page' => 1,
  305. 'limit' => 20,
  306. 'maxLimit' => 100,
  307. );
  308. $result = $this->Paginator->mergeOptions('Post', $settings);
  309. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']);
  310. $this->assertEquals($expected, $result);
  311. }
  312. /**
  313. * test that modifying the whitelist works.
  314. *
  315. * @return void
  316. */
  317. public function testMergeOptionsExtraWhitelist() {
  318. $this->request->query = array(
  319. 'page' => 10,
  320. 'limit' => 10,
  321. 'fields' => array('bad.stuff'),
  322. 'recursive' => 1000,
  323. 'conditions' => array('bad.stuff'),
  324. 'contain' => array('bad')
  325. );
  326. $settings = array(
  327. 'page' => 1,
  328. 'limit' => 20,
  329. 'maxLimit' => 100,
  330. );
  331. $this->Paginator->config('whitelist', ['fields']);
  332. $result = $this->Paginator->mergeOptions('Post', $settings);
  333. $expected = array(
  334. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => array('bad.stuff'), 'whitelist' => ['limit', 'sort', 'page', 'direction', 'fields']
  335. );
  336. $this->assertEquals($expected, $result);
  337. }
  338. /**
  339. * test mergeOptions with limit > maxLimit in code.
  340. *
  341. * @return void
  342. */
  343. public function testMergeOptionsMaxLimit() {
  344. $settings = array(
  345. 'limit' => 200,
  346. 'paramType' => 'named',
  347. );
  348. $result = $this->Paginator->mergeOptions('Post', $settings);
  349. $expected = array(
  350. 'page' => 1,
  351. 'limit' => 200,
  352. 'maxLimit' => 200,
  353. 'paramType' => 'named',
  354. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  355. );
  356. $this->assertEquals($expected, $result);
  357. $settings = array(
  358. 'maxLimit' => 10,
  359. 'paramType' => 'named',
  360. );
  361. $result = $this->Paginator->mergeOptions('Post', $settings);
  362. $expected = array(
  363. 'page' => 1,
  364. 'limit' => 20,
  365. 'maxLimit' => 10,
  366. 'paramType' => 'named',
  367. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  368. );
  369. $this->assertEquals($expected, $result);
  370. }
  371. /**
  372. * Integration test to ensure that validateSort is being used by paginate()
  373. *
  374. * @return void
  375. */
  376. public function testValidateSortInvalid() {
  377. $table = $this->_getMockPosts(['find']);
  378. $query = $this->_getMockFindQuery();
  379. $table->expects($this->once())
  380. ->method('find')
  381. ->with('all')
  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('Post');
  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 (\Cake\Error\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\Error\NotFoundException
  440. * @return void
  441. */
  442. public function testOutOfVeryBigPageNumberGetsClamped() {
  443. $this->loadFixtures('Post');
  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('Post');
  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('Post');
  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('Post');
  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('Post');
  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(['find']);
  702. $query = $this->_getMockFindQuery();
  703. $table->expects($this->once())
  704. ->method('find')
  705. ->with('published')
  706. ->will($this->returnValue($query));
  707. $query->expects($this->once())->method('applyOptions')
  708. ->with(['limit' => 2, 'page' => 1, 'order' => [], 'whitelist' => ['limit', 'sort', 'page', 'direction']]);
  709. $this->Paginator->paginate($table, $settings);
  710. }
  711. /**
  712. * Tests that it is possible to pass an already made query object to
  713. * paginate()
  714. *
  715. * @return void
  716. */
  717. public function testPaginateQuery() {
  718. $this->request->query = array('page' => '-1');
  719. $settings = array(
  720. 'PaginatorPosts' => array(
  721. 'contain' => array('PaginatorAuthor'),
  722. 'maxLimit' => 10,
  723. 'group' => 'PaginatorPosts.published',
  724. 'order' => array('PaginatorPosts.id' => 'ASC')
  725. )
  726. );
  727. $table = $this->_getMockPosts(['find']);
  728. $query = $this->_getMockFindQuery($table);
  729. $table->expects($this->never())->method('find');
  730. $query->expects($this->once())
  731. ->method('applyOptions')
  732. ->with([
  733. 'contain' => ['PaginatorAuthor'],
  734. 'group' => 'PaginatorPosts.published',
  735. 'limit' => 10,
  736. 'order' => ['PaginatorPosts.id' => 'ASC'],
  737. 'page' => 1,
  738. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  739. ]);
  740. $this->Paginator->paginate($query, $settings);
  741. }
  742. /**
  743. * Tests that passing a query object with a limit clause set will
  744. * overwrite it with the passed defaults.
  745. *
  746. * @return void
  747. */
  748. public function testPaginateQueryWithLimit() {
  749. $this->request->query = array('page' => '-1');
  750. $settings = array(
  751. 'PaginatorPosts' => array(
  752. 'contain' => array('PaginatorAuthor'),
  753. 'maxLimit' => 10,
  754. 'limit' => 5,
  755. 'group' => 'PaginatorPosts.published',
  756. 'order' => array('PaginatorPosts.id' => 'ASC')
  757. )
  758. );
  759. $table = $this->_getMockPosts(['find']);
  760. $query = $this->_getMockFindQuery($table);
  761. $query->limit(2);
  762. $table->expects($this->never())->method('find');
  763. $query->expects($this->once())
  764. ->method('applyOptions')
  765. ->with([
  766. 'contain' => ['PaginatorAuthor'],
  767. 'group' => 'PaginatorPosts.published',
  768. 'limit' => 5,
  769. 'order' => ['PaginatorPosts.id' => 'ASC'],
  770. 'page' => 1,
  771. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  772. ]);
  773. $this->Paginator->paginate($query, $settings);
  774. }
  775. /**
  776. * Helper method for making mocks.
  777. *
  778. * @param array $methods
  779. * @return Table
  780. */
  781. protected function _getMockPosts($methods = []) {
  782. return $this->getMock(
  783. 'TestApp\Model\Table\PaginatorPostsTable',
  784. $methods,
  785. [[
  786. 'connection' => ConnectionManager::get('test'),
  787. 'alias' => 'PaginatorPosts',
  788. 'schema' => [
  789. 'id' => ['type' => 'integer'],
  790. 'author_id' => ['type' => 'integer', 'null' => false],
  791. 'title' => ['type' => 'string', 'null' => false],
  792. 'body' => 'text',
  793. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  794. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  795. ]
  796. ]]
  797. );
  798. }
  799. /**
  800. * Helper method for mocking queries.
  801. *
  802. * @return Query
  803. */
  804. protected function _getMockFindQuery($table = null) {
  805. $query = $this->getMockBuilder('Cake\ORM\Query')
  806. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  807. ->disableOriginalConstructor()
  808. ->getMock();
  809. $results = $this->getMock('Cake\ORM\ResultSet', [], [], '', false);
  810. $query->expects($this->any())
  811. ->method('count')
  812. ->will($this->returnValue(2));
  813. $query->expects($this->any())
  814. ->method('all')
  815. ->will($this->returnValue($results));
  816. $query->expects($this->any())
  817. ->method('count')
  818. ->will($this->returnValue(2));
  819. $query->repository($table);
  820. return $query;
  821. }
  822. }