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