PaginatorComponentTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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 CakePHP(tm) v 2.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\Database\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', 'core.author');
  45. /**
  46. * setup
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. Configure::write('App.namespace', 'TestApp');
  53. $this->request = new Request('controller_posts/index');
  54. $this->request->params['pass'] = array();
  55. $controller = new Controller($this->request);
  56. $registry = new ComponentRegistry($controller);
  57. $this->Paginator = new PaginatorComponent($registry, []);
  58. $this->Post = $this->getMock('Cake\ORM\Table', [], [], '', false);
  59. }
  60. /**
  61. * tearDown
  62. *
  63. * @return void
  64. */
  65. public function tearDown() {
  66. parent::tearDown();
  67. TableRegistry::clear();
  68. }
  69. /**
  70. * Test that non-numeric values are rejected for page, and limit
  71. *
  72. * @return void
  73. */
  74. public function testPageParamCasting() {
  75. $this->Post->expects($this->any())
  76. ->method('alias')
  77. ->will($this->returnValue('Posts'));
  78. $query = $this->_getMockFindQuery();
  79. $this->Post->expects($this->any())
  80. ->method('find')
  81. ->will($this->returnValue($query));
  82. $this->request->query = array('page' => '1 " onclick="alert(\'xss\');">');
  83. $settings = array('limit' => 1, 'maxLimit' => 10);
  84. $this->Paginator->paginate($this->Post, $settings);
  85. $this->assertSame(1, $this->request->params['paging']['Posts']['page'], 'XSS exploit opened');
  86. }
  87. /**
  88. * test that unknown keys in the default settings are
  89. * passed to the find operations.
  90. *
  91. * @return void
  92. */
  93. public function testPaginateExtraParams() {
  94. $this->request->query = array('page' => '-1');
  95. $settings = array(
  96. 'PaginatorPosts' => array(
  97. 'contain' => array('PaginatorAuthor'),
  98. 'maxLimit' => 10,
  99. 'group' => 'PaginatorPosts.published',
  100. 'order' => array('PaginatorPosts.id' => 'ASC')
  101. ),
  102. );
  103. $table = $this->_getMockPosts(['find']);
  104. $query = $this->_getMockFindQuery();
  105. $table->expects($this->at(0))
  106. ->method('find')
  107. ->with('all', [
  108. 'conditions' => [],
  109. 'contain' => ['PaginatorAuthor'],
  110. 'fields' => null,
  111. 'group' => 'PaginatorPosts.published',
  112. 'limit' => 10,
  113. 'order' => ['PaginatorPosts.id' => 'ASC'],
  114. 'page' => 1,
  115. ])
  116. ->will($this->returnValue($query));
  117. $table->expects($this->at(1))
  118. ->method('find')
  119. ->with('all', [
  120. 'conditions' => [],
  121. 'contain' => ['PaginatorAuthor'],
  122. 'group' => 'PaginatorPosts.published',
  123. ])
  124. ->will($this->returnValue($query));
  125. $this->Paginator->paginate($table, $settings);
  126. }
  127. /**
  128. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  129. *
  130. * @return void
  131. */
  132. public function testPaginateCustomFinder() {
  133. $settings = array(
  134. 'PaginatorPosts' => array(
  135. 'findType' => 'popular',
  136. 'fields' => array('id', 'title'),
  137. 'maxLimit' => 10,
  138. )
  139. );
  140. $table = $this->_getMockPosts(['findPopular']);
  141. $query = $this->_getMockFindQuery();
  142. $table->expects($this->any())
  143. ->method('findPopular')
  144. ->will($this->returnValue($query));
  145. $this->Paginator->paginate($table, $settings);
  146. $this->assertEquals('popular', $this->request->params['paging']['PaginatorPosts']['findType']);
  147. }
  148. /**
  149. * test that flat default pagination parameters work.
  150. *
  151. * @return void
  152. */
  153. public function testDefaultPaginateParams() {
  154. $settings = array(
  155. 'order' => ['PaginatorPosts.id' => 'DESC'],
  156. 'maxLimit' => 10,
  157. );
  158. $table = $this->_getMockPosts(['find']);
  159. $query = $this->_getMockFindQuery();
  160. $table->expects($this->at(0))
  161. ->method('find')
  162. ->with('all', [
  163. 'conditions' => [],
  164. 'fields' => null,
  165. 'limit' => 10,
  166. 'page' => 1,
  167. 'order' => ['PaginatorPosts.id' => 'DESC']
  168. ])
  169. ->will($this->returnValue($query));
  170. $table->expects($this->at(1))
  171. ->method('find')
  172. ->will($this->returnValue($query));
  173. $this->Paginator->paginate($table, $settings);
  174. }
  175. /**
  176. * test that option merging prefers specific models
  177. *
  178. * @return void
  179. */
  180. public function testMergeOptionsModelSpecific() {
  181. $settings = array(
  182. 'page' => 1,
  183. 'limit' => 20,
  184. 'maxLimit' => 100,
  185. 'Posts' => array(
  186. 'page' => 1,
  187. 'limit' => 10,
  188. 'maxLimit' => 50,
  189. )
  190. );
  191. $result = $this->Paginator->mergeOptions('Silly', $settings);
  192. $this->assertEquals($settings, $result);
  193. $result = $this->Paginator->mergeOptions('Posts', $settings);
  194. $expected = array('page' => 1, 'limit' => 10, 'maxLimit' => 50);
  195. $this->assertEquals($expected, $result);
  196. }
  197. /**
  198. * test mergeOptions with customFind key
  199. *
  200. * @return void
  201. */
  202. public function testMergeOptionsCustomFindKey() {
  203. $this->request->query = [
  204. 'page' => 10,
  205. 'limit' => 10
  206. ];
  207. $settings = [
  208. 'page' => 1,
  209. 'limit' => 20,
  210. 'maxLimit' => 100,
  211. 'findType' => 'myCustomFind'
  212. ];
  213. $result = $this->Paginator->mergeOptions('Post', $settings);
  214. $expected = array(
  215. 'page' => 10,
  216. 'limit' => 10,
  217. 'maxLimit' => 100,
  218. 'findType' => 'myCustomFind'
  219. );
  220. $this->assertEquals($expected, $result);
  221. }
  222. /**
  223. * test merging options from the querystring.
  224. *
  225. * @return void
  226. */
  227. public function testMergeOptionsQueryString() {
  228. $this->request->query = array(
  229. 'page' => 99,
  230. 'limit' => 75
  231. );
  232. $settings = array(
  233. 'page' => 1,
  234. 'limit' => 20,
  235. 'maxLimit' => 100,
  236. );
  237. $result = $this->Paginator->mergeOptions('Post', $settings);
  238. $expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100);
  239. $this->assertEquals($expected, $result);
  240. }
  241. /**
  242. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  243. *
  244. * @return void
  245. */
  246. public function testMergeOptionsDefaultWhiteList() {
  247. $this->request->query = array(
  248. 'page' => 10,
  249. 'limit' => 10,
  250. 'fields' => array('bad.stuff'),
  251. 'recursive' => 1000,
  252. 'conditions' => array('bad.stuff'),
  253. 'contain' => array('bad')
  254. );
  255. $settings = array(
  256. 'page' => 1,
  257. 'limit' => 20,
  258. 'maxLimit' => 100,
  259. );
  260. $result = $this->Paginator->mergeOptions('Post', $settings);
  261. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100);
  262. $this->assertEquals($expected, $result);
  263. }
  264. /**
  265. * test that modifying the whitelist works.
  266. *
  267. * @return void
  268. */
  269. public function testMergeOptionsExtraWhitelist() {
  270. $this->request->query = array(
  271. 'page' => 10,
  272. 'limit' => 10,
  273. 'fields' => array('bad.stuff'),
  274. 'recursive' => 1000,
  275. 'conditions' => array('bad.stuff'),
  276. 'contain' => array('bad')
  277. );
  278. $settings = array(
  279. 'page' => 1,
  280. 'limit' => 20,
  281. 'maxLimit' => 100,
  282. );
  283. $this->Paginator->whitelist[] = 'fields';
  284. $result = $this->Paginator->mergeOptions('Post', $settings);
  285. $expected = array(
  286. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => array('bad.stuff')
  287. );
  288. $this->assertEquals($expected, $result);
  289. }
  290. /**
  291. * test mergeOptions with limit > maxLimit in code.
  292. *
  293. * @return void
  294. */
  295. public function testMergeOptionsMaxLimit() {
  296. $settings = array(
  297. 'limit' => 200,
  298. 'paramType' => 'named',
  299. );
  300. $result = $this->Paginator->mergeOptions('Post', $settings);
  301. $expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 200, 'paramType' => 'named');
  302. $this->assertEquals($expected, $result);
  303. $settings = array(
  304. 'maxLimit' => 10,
  305. 'paramType' => 'named',
  306. );
  307. $result = $this->Paginator->mergeOptions('Post', $settings);
  308. $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
  309. $this->assertEquals($expected, $result);
  310. }
  311. /**
  312. * Integration test to ensure that validateSort is being used by paginate()
  313. *
  314. * @return void
  315. */
  316. public function testValidateSortInvalid() {
  317. $table = $this->_getMockPosts(['find']);
  318. $query = $this->_getMockFindQuery();
  319. $table->expects($this->at(0))
  320. ->method('find')
  321. ->with('all', [
  322. 'fields' => null,
  323. 'limit' => 20,
  324. 'conditions' => [],
  325. 'page' => 1,
  326. 'order' => ['PaginatorPosts.id' => 'asc'],
  327. ])
  328. ->will($this->returnValue($query));
  329. $table->expects($this->at(1))
  330. ->method('find')
  331. ->will($this->returnValue($query));
  332. $this->request->query = [
  333. 'page' => 1,
  334. 'sort' => 'id',
  335. 'direction' => 'herp'
  336. ];
  337. $this->Paginator->paginate($table);
  338. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sort']);
  339. $this->assertEquals('asc', $this->request->params['paging']['PaginatorPosts']['direction']);
  340. }
  341. /**
  342. * test that invalid directions are ignored.
  343. *
  344. * @return void
  345. */
  346. public function testValidateSortInvalidDirection() {
  347. $model = $this->getMock('Cake\ORM\Table');
  348. $model->expects($this->any())
  349. ->method('alias')
  350. ->will($this->returnValue('model'));
  351. $model->expects($this->any())
  352. ->method('hasField')
  353. ->will($this->returnValue(true));
  354. $options = array('sort' => 'something', 'direction' => 'boogers');
  355. $result = $this->Paginator->validateSort($model, $options);
  356. $this->assertEquals('asc', $result['order']['model.something']);
  357. }
  358. /**
  359. * Test that a really large page number gets clamped to the max page size.
  360. *
  361. * @return void
  362. */
  363. public function testOutOfRangePageNumberGetsClamped() {
  364. $this->request->query['page'] = 3000;
  365. $table = TableRegistry::get('PaginatorPosts');
  366. try {
  367. $this->Paginator->paginate($table);
  368. $this->fail('No exception raised');
  369. } catch (\Cake\Error\NotFoundException $e) {
  370. $this->assertEquals(
  371. 1,
  372. $this->request->params['paging']['PaginatorPosts']['page'],
  373. 'Page number should not be 0'
  374. );
  375. }
  376. }
  377. /**
  378. * Test that a really REALLY large page number gets clamped to the max page size.
  379. *
  380. * @expectedException Cake\Error\NotFoundException
  381. * @return void
  382. */
  383. public function testOutOfVeryBigPageNumberGetsClamped() {
  384. $this->request->query = [
  385. 'page' => '3000000000000000000000000',
  386. ];
  387. $table = TableRegistry::get('PaginatorPosts');
  388. $this->Paginator->paginate($table);
  389. }
  390. /**
  391. * test that fields not in whitelist won't be part of order conditions.
  392. *
  393. * @return void
  394. */
  395. public function testValidateSortWhitelistFailure() {
  396. $model = $this->getMock('Cake\ORM\Table');
  397. $model->expects($this->any())
  398. ->method('alias')
  399. ->will($this->returnValue('model'));
  400. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  401. $options = array(
  402. 'sort' => 'body',
  403. 'direction' => 'asc',
  404. 'sortWhitelist' => ['title', 'id']
  405. );
  406. $result = $this->Paginator->validateSort($model, $options);
  407. $this->assertEquals([], $result['order']);
  408. }
  409. /**
  410. * test that fields in the whitelist are not validated
  411. *
  412. * @return void
  413. */
  414. public function testValidateSortWhitelistTrusted() {
  415. $model = $this->getMock('Cake\ORM\Table');
  416. $model->expects($this->any())
  417. ->method('alias')
  418. ->will($this->returnValue('model'));
  419. $model->expects($this->never())->method('hasField');
  420. $options = array(
  421. 'sort' => 'body',
  422. 'direction' => 'asc',
  423. 'sortWhitelist' => ['body']
  424. );
  425. $result = $this->Paginator->validateSort($model, $options);
  426. $expected = array('body' => 'asc');
  427. $this->assertEquals($expected, $result['order']);
  428. }
  429. /**
  430. * test that multiple sort works.
  431. *
  432. * @return void
  433. */
  434. public function testValidateSortMultiple() {
  435. $model = $this->getMock('Cake\ORM\Table');
  436. $model->expects($this->any())
  437. ->method('alias')
  438. ->will($this->returnValue('model'));
  439. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  440. $options = array(
  441. 'order' => array(
  442. 'author_id' => 'asc',
  443. 'title' => 'asc'
  444. )
  445. );
  446. $result = $this->Paginator->validateSort($model, $options);
  447. $expected = array(
  448. 'model.author_id' => 'asc',
  449. 'model.title' => 'asc'
  450. );
  451. $this->assertEquals($expected, $result['order']);
  452. }
  453. /**
  454. * Test that no sort doesn't trigger an error.
  455. *
  456. * @return void
  457. */
  458. public function testValidateSortNoSort() {
  459. $model = $this->getMock('Cake\ORM\Table');
  460. $model->expects($this->any())
  461. ->method('alias')
  462. ->will($this->returnValue('model'));
  463. $model->expects($this->any())->method('hasField')
  464. ->will($this->returnValue(true));
  465. $options = array(
  466. 'direction' => 'asc',
  467. 'sortWhitelist' => ['title', 'id'],
  468. );
  469. $result = $this->Paginator->validateSort($model, $options);
  470. $this->assertEquals([], $result['order']);
  471. }
  472. /**
  473. * Test sorting with incorrect aliases on valid fields.
  474. *
  475. * @return void
  476. */
  477. public function testValidateSortInvalidAlias() {
  478. $model = $this->getMock('Cake\ORM\Table');
  479. $model->expects($this->any())
  480. ->method('alias')
  481. ->will($this->returnValue('model'));
  482. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  483. $options = array('sort' => 'Derp.id');
  484. $result = $this->Paginator->validateSort($model, $options);
  485. $this->assertEquals(array(), $result['order']);
  486. }
  487. /**
  488. * test that maxLimit is respected
  489. *
  490. * @return void
  491. */
  492. public function testCheckLimit() {
  493. $result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
  494. $this->assertEquals(100, $result['limit']);
  495. $result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
  496. $this->assertEquals(1, $result['limit']);
  497. $result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
  498. $this->assertEquals(1, $result['limit']);
  499. $result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
  500. $this->assertEquals(1, $result['limit']);
  501. $result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
  502. $this->assertEquals(1, $result['limit']);
  503. }
  504. /**
  505. * Integration test for checkLimit() being applied inside paginate()
  506. *
  507. * @return void
  508. */
  509. public function testPaginateMaxLimit() {
  510. $table = TableRegistry::get('PaginatorPosts');
  511. $settings = [
  512. 'maxLimit' => 100,
  513. ];
  514. $this->request->query = [
  515. 'limit' => '1000'
  516. ];
  517. $this->Paginator->paginate($table, $settings);
  518. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['limit']);
  519. $this->request->query = [
  520. 'limit' => '10'
  521. ];
  522. $this->Paginator->paginate($table, $settings);
  523. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
  524. }
  525. /**
  526. * test paginate() and custom find, to make sure the correct count is returned.
  527. *
  528. * @return void
  529. */
  530. public function testPaginateCustomFind() {
  531. $idExtractor = function ($result) {
  532. $ids = [];
  533. foreach ($result as $record) {
  534. $ids[] = $record->id;
  535. }
  536. return $ids;
  537. };
  538. $table = TableRegistry::get('PaginatorPosts');
  539. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  540. $result = $table->save(new \Cake\ORM\Entity($data));
  541. $this->assertNotEmpty($result);
  542. $result = $this->Paginator->paginate($table);
  543. $this->assertCount(4, $result, '4 rows should come back');
  544. $this->assertEquals(array(1, 2, 3, 4), $idExtractor($result));
  545. $result = $this->request->params['paging']['PaginatorPosts'];
  546. $this->assertEquals(4, $result['current']);
  547. $this->assertEquals(4, $result['count']);
  548. $settings = array('findType' => 'published');
  549. $result = $this->Paginator->paginate($table, $settings);
  550. $this->assertCount(3, $result, '3 rows should come back');
  551. $this->assertEquals(array(1, 2, 3), $idExtractor($result));
  552. $result = $this->request->params['paging']['PaginatorPosts'];
  553. $this->assertEquals(3, $result['current']);
  554. $this->assertEquals(3, $result['count']);
  555. $settings = array('findType' => 'published', 'limit' => 2);
  556. $result = $this->Paginator->paginate($table, $settings);
  557. $this->assertCount(2, $result, '2 rows should come back');
  558. $this->assertEquals(array(1, 2), $idExtractor($result));
  559. $result = $this->request->params['paging']['PaginatorPosts'];
  560. $this->assertEquals(2, $result['current']);
  561. $this->assertEquals(3, $result['count']);
  562. $this->assertEquals(2, $result['pageCount']);
  563. $this->assertTrue($result['nextPage']);
  564. $this->assertFalse($result['prevPage']);
  565. }
  566. /**
  567. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  568. *
  569. * @return void
  570. */
  571. public function testPaginateCustomFindFieldsArray() {
  572. $table = TableRegistry::get('PaginatorPosts');
  573. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  574. $table->save(new \Cake\ORM\Entity($data));
  575. $settings = [
  576. 'findType' => 'list',
  577. 'conditions' => array('PaginatorPosts.published' => 'Y'),
  578. 'limit' => 2
  579. ];
  580. $results = $this->Paginator->paginate($table, $settings);
  581. $result = $results->toArray();
  582. $expected = array(
  583. 1 => 'First Post',
  584. 2 => 'Second Post',
  585. );
  586. $this->assertEquals($expected, $result);
  587. $result = $this->request->params['paging']['PaginatorPosts'];
  588. $this->assertEquals(2, $result['current']);
  589. $this->assertEquals(3, $result['count']);
  590. $this->assertEquals(2, $result['pageCount']);
  591. $this->assertTrue($result['nextPage']);
  592. $this->assertFalse($result['prevPage']);
  593. }
  594. /**
  595. * test paginate() and custom finders to ensure the count + find
  596. * use the custom type.
  597. *
  598. * @return void
  599. */
  600. public function testPaginateCustomFindCount() {
  601. $settings = array(
  602. 'findType' => 'published',
  603. 'limit' => 2
  604. );
  605. $table = $this->_getMockPosts(['find']);
  606. $query = $this->_getMockFindQuery();
  607. $table->expects($this->at(0))
  608. ->method('find')
  609. ->with('published', [
  610. 'conditions' => [],
  611. 'order' => [],
  612. 'limit' => 2,
  613. 'fields' => null,
  614. 'page' => 1,
  615. ])
  616. ->will($this->returnValue($query));
  617. $table->expects($this->at(1))
  618. ->method('find')
  619. ->with('published', [
  620. 'conditions' => [],
  621. ])
  622. ->will($this->returnValue($query));
  623. $this->Paginator->paginate($table, $settings);
  624. }
  625. /**
  626. * Helper method for making mocks.
  627. *
  628. * @return Table
  629. */
  630. protected function _getMockPosts($methods = []) {
  631. return $this->getMock(
  632. 'TestApp\Model\Table\PaginatorPostsTable',
  633. $methods,
  634. [['connection' => ConnectionManager::get('test'), 'alias' => 'PaginatorPosts']]
  635. );
  636. }
  637. /**
  638. * Helper method for mocking queries.
  639. *
  640. * @return Query
  641. */
  642. protected function _getMockFindQuery() {
  643. $query = $this->getMock('Cake\ORM\Query', ['total', 'all'], [], '', false);
  644. $results = $this->getMock('Cake\ORM\ResultSet', [], [], '', false);
  645. $results->expects($this->any())
  646. ->method('count')
  647. ->will($this->returnValue(2));
  648. $query->expects($this->any())
  649. ->method('all')
  650. ->will($this->returnValue($results));
  651. $query->expects($this->any())
  652. ->method('count')
  653. ->will($this->returnValue(2));
  654. return $query;
  655. }
  656. }