PaginatorComponentTest.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://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\Datasource\ConnectionManager;
  20. use Cake\Datasource\EntityInterface;
  21. use Cake\Datasource\Paginator;
  22. use Cake\Http\ServerRequest;
  23. use Cake\Network\Exception\NotFoundException;
  24. use Cake\ORM\Entity;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\TestSuite\TestCase;
  27. use stdClass;
  28. /**
  29. * PaginatorTestController class
  30. */
  31. class PaginatorTestController extends Controller
  32. {
  33. /**
  34. * components property
  35. *
  36. * @var array
  37. */
  38. public $components = ['Paginator'];
  39. }
  40. /**
  41. * Custom paginator
  42. */
  43. class CustomPaginator extends Paginator
  44. {
  45. }
  46. class PaginatorComponentTest extends TestCase
  47. {
  48. /**
  49. * fixtures property
  50. *
  51. * @var array
  52. */
  53. public $fixtures = [
  54. 'core.posts', 'core.articles', 'core.articles_tags',
  55. 'core.authors', 'core.authors_tags', 'core.tags'
  56. ];
  57. /**
  58. * Don't load data for fixtures for all tests
  59. *
  60. * @var bool
  61. */
  62. public $autoFixtures = false;
  63. /**
  64. * setup
  65. *
  66. * @return void
  67. */
  68. public function setUp()
  69. {
  70. parent::setUp();
  71. static::setAppNamespace();
  72. $this->request = new ServerRequest('controller_posts/index');
  73. $this->request->params['pass'] = [];
  74. $controller = new Controller($this->request);
  75. $this->registry = new ComponentRegistry($controller);
  76. $this->Paginator = new PaginatorComponent($this->registry, []);
  77. $this->Post = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. }
  81. /**
  82. * tearDown
  83. *
  84. * @return void
  85. */
  86. public function tearDown()
  87. {
  88. parent::tearDown();
  89. TableRegistry::clear();
  90. }
  91. /**
  92. * testPaginatorSetting
  93. *
  94. * @return void
  95. */
  96. public function testPaginatorSetting()
  97. {
  98. $paginator = new CustomPaginator();
  99. $component = new PaginatorComponent($this->registry, [
  100. 'paginator' => $paginator
  101. ]);
  102. $this->assertSame($paginator, $component->getPaginator());
  103. $component = new PaginatorComponent($this->registry, []);
  104. $this->assertNotSame($paginator, $component->getPaginator());
  105. $component->setPaginator($paginator);
  106. $this->assertSame($paginator, $component->getPaginator());
  107. }
  108. /**
  109. * Test that an exception is thrown when paginator option is invalid.
  110. *
  111. * @expectedException \InvalidArgumentException
  112. * @expectedExceptionMessage Paginator must be an instance of Cake\Datasource\Paginator
  113. * @return void
  114. */
  115. public function testInvalidPaginatorOption()
  116. {
  117. new PaginatorComponent($this->registry, [
  118. 'paginator' => new stdClass()
  119. ]);
  120. }
  121. /**
  122. * Test that non-numeric values are rejected for page, and limit
  123. *
  124. * @return void
  125. */
  126. public function testPageParamCasting()
  127. {
  128. $this->Post->expects($this->any())
  129. ->method('alias')
  130. ->will($this->returnValue('Posts'));
  131. $query = $this->_getMockFindQuery();
  132. $this->Post->expects($this->any())
  133. ->method('find')
  134. ->will($this->returnValue($query));
  135. $this->request->query = ['page' => '1 " onclick="alert(\'xss\');">'];
  136. $settings = ['limit' => 1, 'maxLimit' => 10];
  137. $this->Paginator->paginate($this->Post, $settings);
  138. $this->assertSame(1, $this->request->params['paging']['Posts']['page'], 'XSS exploit opened');
  139. }
  140. /**
  141. * test that unknown keys in the default settings are
  142. * passed to the find operations.
  143. *
  144. * @return void
  145. */
  146. public function testPaginateExtraParams()
  147. {
  148. $this->request->query = ['page' => '-1'];
  149. $settings = [
  150. 'PaginatorPosts' => [
  151. 'contain' => ['PaginatorAuthor'],
  152. 'maxLimit' => 10,
  153. 'group' => 'PaginatorPosts.published',
  154. 'order' => ['PaginatorPosts.id' => 'ASC']
  155. ],
  156. ];
  157. $table = $this->_getMockPosts(['query']);
  158. $query = $this->_getMockFindQuery();
  159. $table->expects($this->once())
  160. ->method('query')
  161. ->will($this->returnValue($query));
  162. $query->expects($this->once())
  163. ->method('applyOptions')
  164. ->with([
  165. 'contain' => ['PaginatorAuthor'],
  166. 'group' => 'PaginatorPosts.published',
  167. 'limit' => 10,
  168. 'order' => ['PaginatorPosts.id' => 'ASC'],
  169. 'page' => 1,
  170. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  171. 'scope' => null,
  172. ]);
  173. $this->Paginator->paginate($table, $settings);
  174. }
  175. /**
  176. * Test to make sure options get sent to custom finder methods via paginate
  177. *
  178. * @return void
  179. */
  180. public function testPaginateCustomFinderOptions()
  181. {
  182. $this->loadFixtures('Posts');
  183. $settings = [
  184. 'PaginatorPosts' => [
  185. 'finder' => ['author' => ['author_id' => 1]]
  186. ]
  187. ];
  188. $table = TableRegistry::get('PaginatorPosts');
  189. $expected = $table
  190. ->find('author', [
  191. 'conditions' => [
  192. 'PaginatorPosts.author_id' => 1
  193. ]
  194. ])
  195. ->count();
  196. $result = $this->Paginator->paginate($table, $settings)->count();
  197. $this->assertEquals($expected, $result);
  198. }
  199. /**
  200. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  201. *
  202. * @return void
  203. */
  204. public function testPaginateCustomFinder()
  205. {
  206. $settings = [
  207. 'PaginatorPosts' => [
  208. 'finder' => 'popular',
  209. 'fields' => ['id', 'title'],
  210. 'maxLimit' => 10,
  211. ]
  212. ];
  213. $table = $this->_getMockPosts(['findPopular']);
  214. $query = $this->_getMockFindQuery();
  215. $table->expects($this->any())
  216. ->method('findPopular')
  217. ->will($this->returnValue($query));
  218. $this->Paginator->paginate($table, $settings);
  219. $this->assertEquals('popular', $this->request->params['paging']['PaginatorPosts']['finder']);
  220. }
  221. /**
  222. * Test that nested eager loaders don't trigger invalid SQL errors.
  223. *
  224. * @return void
  225. */
  226. public function testPaginateNestedEagerLoader()
  227. {
  228. $this->loadFixtures('Articles', 'Tags', 'Authors', 'ArticlesTags', 'AuthorsTags');
  229. $articles = TableRegistry::get('Articles');
  230. $articles->belongsToMany('Tags');
  231. $tags = TableRegistry::get('Tags');
  232. $tags->belongsToMany('Authors');
  233. $articles->eventManager()->on('Model.beforeFind', function ($event, $query) {
  234. $query ->matching('Tags', function ($q) {
  235. return $q->matching('Authors', function ($q) {
  236. return $q->where(['Authors.name' => 'larry']);
  237. });
  238. });
  239. });
  240. $results = $this->Paginator->paginate($articles, []);
  241. $result = $results->first();
  242. $this->assertInstanceOf(EntityInterface::class, $result);
  243. $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Tags']);
  244. $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Authors']);
  245. }
  246. /**
  247. * test that flat default pagination parameters work.
  248. *
  249. * @return void
  250. */
  251. public function testDefaultPaginateParams()
  252. {
  253. $settings = [
  254. 'order' => ['PaginatorPosts.id' => 'DESC'],
  255. 'maxLimit' => 10,
  256. ];
  257. $table = $this->_getMockPosts(['query']);
  258. $query = $this->_getMockFindQuery();
  259. $table->expects($this->once())
  260. ->method('query')
  261. ->will($this->returnValue($query));
  262. $query->expects($this->once())
  263. ->method('applyOptions')
  264. ->with([
  265. 'limit' => 10,
  266. 'page' => 1,
  267. 'order' => ['PaginatorPosts.id' => 'DESC'],
  268. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  269. 'scope' => null,
  270. ]);
  271. $this->Paginator->paginate($table, $settings);
  272. }
  273. /**
  274. * test that default sort and default direction are injected into request
  275. *
  276. * @return void
  277. */
  278. public function testDefaultPaginateParamsIntoRequest()
  279. {
  280. $settings = [
  281. 'order' => ['PaginatorPosts.id' => 'DESC'],
  282. 'maxLimit' => 10,
  283. ];
  284. $table = $this->_getMockPosts(['query']);
  285. $query = $this->_getMockFindQuery();
  286. $table->expects($this->once())
  287. ->method('query')
  288. ->will($this->returnValue($query));
  289. $query->expects($this->once())
  290. ->method('applyOptions')
  291. ->with([
  292. 'limit' => 10,
  293. 'page' => 1,
  294. 'order' => ['PaginatorPosts.id' => 'DESC'],
  295. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  296. 'scope' => null,
  297. ]);
  298. $this->Paginator->paginate($table, $settings);
  299. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
  300. $this->assertEquals('DESC', $this->request->params['paging']['PaginatorPosts']['directionDefault']);
  301. }
  302. /**
  303. * test that option merging prefers specific models
  304. *
  305. * @return void
  306. */
  307. public function testMergeOptionsModelSpecific()
  308. {
  309. $settings = [
  310. 'page' => 1,
  311. 'limit' => 20,
  312. 'maxLimit' => 100,
  313. 'Posts' => [
  314. 'page' => 1,
  315. 'limit' => 10,
  316. 'maxLimit' => 50,
  317. ],
  318. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  319. ];
  320. $result = $this->Paginator->mergeOptions('Silly', $settings);
  321. $this->assertEquals($settings, $result);
  322. $result = $this->Paginator->mergeOptions('Posts', $settings);
  323. $expected = ['page' => 1, 'limit' => 10, 'maxLimit' => 50, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  324. $this->assertEquals($expected, $result);
  325. }
  326. /**
  327. * test mergeOptions with custom scope
  328. *
  329. * @return void
  330. */
  331. public function testMergeOptionsCustomScope()
  332. {
  333. $this->request->query = [
  334. 'page' => 10,
  335. 'limit' => 10,
  336. 'scope' => [
  337. 'page' => 2,
  338. 'limit' => 5,
  339. ]
  340. ];
  341. $settings = [
  342. 'page' => 1,
  343. 'limit' => 20,
  344. 'maxLimit' => 100,
  345. 'finder' => 'myCustomFind',
  346. ];
  347. $result = $this->Paginator->mergeOptions('Post', $settings);
  348. $expected = [
  349. 'page' => 10,
  350. 'limit' => 10,
  351. 'maxLimit' => 100,
  352. 'finder' => 'myCustomFind',
  353. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  354. ];
  355. $this->assertEquals($expected, $result);
  356. $settings = [
  357. 'page' => 1,
  358. 'limit' => 20,
  359. 'maxLimit' => 100,
  360. 'finder' => 'myCustomFind',
  361. 'scope' => 'non-existent',
  362. ];
  363. $result = $this->Paginator->mergeOptions('Post', $settings);
  364. $expected = [
  365. 'page' => 1,
  366. 'limit' => 20,
  367. 'maxLimit' => 100,
  368. 'finder' => 'myCustomFind',
  369. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  370. 'scope' => 'non-existent',
  371. ];
  372. $this->assertEquals($expected, $result);
  373. $settings = [
  374. 'page' => 1,
  375. 'limit' => 20,
  376. 'maxLimit' => 100,
  377. 'finder' => 'myCustomFind',
  378. 'scope' => 'scope',
  379. ];
  380. $result = $this->Paginator->mergeOptions('Post', $settings);
  381. $expected = [
  382. 'page' => 2,
  383. 'limit' => 5,
  384. 'maxLimit' => 100,
  385. 'finder' => 'myCustomFind',
  386. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  387. 'scope' => 'scope',
  388. ];
  389. $this->assertEquals($expected, $result);
  390. }
  391. /**
  392. * test mergeOptions with customFind key
  393. *
  394. * @return void
  395. */
  396. public function testMergeOptionsCustomFindKey()
  397. {
  398. $this->request->query = [
  399. 'page' => 10,
  400. 'limit' => 10
  401. ];
  402. $settings = [
  403. 'page' => 1,
  404. 'limit' => 20,
  405. 'maxLimit' => 100,
  406. 'finder' => 'myCustomFind'
  407. ];
  408. $result = $this->Paginator->mergeOptions('Post', $settings);
  409. $expected = [
  410. 'page' => 10,
  411. 'limit' => 10,
  412. 'maxLimit' => 100,
  413. 'finder' => 'myCustomFind',
  414. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  415. ];
  416. $this->assertEquals($expected, $result);
  417. }
  418. /**
  419. * test merging options from the querystring.
  420. *
  421. * @return void
  422. */
  423. public function testMergeOptionsQueryString()
  424. {
  425. $this->request->query = [
  426. 'page' => 99,
  427. 'limit' => 75
  428. ];
  429. $settings = [
  430. 'page' => 1,
  431. 'limit' => 20,
  432. 'maxLimit' => 100,
  433. ];
  434. $result = $this->Paginator->mergeOptions('Post', $settings);
  435. $expected = ['page' => 99, 'limit' => 75, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  436. $this->assertEquals($expected, $result);
  437. }
  438. /**
  439. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  440. *
  441. * @return void
  442. */
  443. public function testMergeOptionsDefaultWhiteList()
  444. {
  445. $this->request->query = [
  446. 'page' => 10,
  447. 'limit' => 10,
  448. 'fields' => ['bad.stuff'],
  449. 'recursive' => 1000,
  450. 'conditions' => ['bad.stuff'],
  451. 'contain' => ['bad']
  452. ];
  453. $settings = [
  454. 'page' => 1,
  455. 'limit' => 20,
  456. 'maxLimit' => 100,
  457. ];
  458. $result = $this->Paginator->mergeOptions('Post', $settings);
  459. $expected = ['page' => 10, 'limit' => 10, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
  460. $this->assertEquals($expected, $result);
  461. }
  462. /**
  463. * test that modifying the whitelist works.
  464. *
  465. * @return void
  466. */
  467. public function testMergeOptionsExtraWhitelist()
  468. {
  469. $this->request->query = [
  470. 'page' => 10,
  471. 'limit' => 10,
  472. 'fields' => ['bad.stuff'],
  473. 'recursive' => 1000,
  474. 'conditions' => ['bad.stuff'],
  475. 'contain' => ['bad']
  476. ];
  477. $settings = [
  478. 'page' => 1,
  479. 'limit' => 20,
  480. 'maxLimit' => 100,
  481. ];
  482. $this->Paginator->config('whitelist', ['fields']);
  483. $result = $this->Paginator->mergeOptions('Post', $settings);
  484. $expected = [
  485. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => ['bad.stuff'], 'whitelist' => ['limit', 'sort', 'page', 'direction', 'fields']
  486. ];
  487. $this->assertEquals($expected, $result);
  488. }
  489. /**
  490. * test mergeOptions with limit > maxLimit in code.
  491. *
  492. * @return void
  493. */
  494. public function testMergeOptionsMaxLimit()
  495. {
  496. $settings = [
  497. 'limit' => 200,
  498. 'paramType' => 'named',
  499. ];
  500. $result = $this->Paginator->mergeOptions('Post', $settings);
  501. $expected = [
  502. 'page' => 1,
  503. 'limit' => 100,
  504. 'maxLimit' => 100,
  505. 'paramType' => 'named',
  506. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  507. ];
  508. $this->assertEquals($expected, $result);
  509. $settings = [
  510. 'maxLimit' => 10,
  511. 'paramType' => 'named',
  512. ];
  513. $result = $this->Paginator->mergeOptions('Post', $settings);
  514. $expected = [
  515. 'page' => 1,
  516. 'limit' => 10,
  517. 'maxLimit' => 10,
  518. 'paramType' => 'named',
  519. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  520. ];
  521. $this->assertEquals($expected, $result);
  522. }
  523. /**
  524. * test getDefaults with limit > maxLimit in code.
  525. *
  526. * @return void
  527. */
  528. public function testGetDefaultMaxLimit()
  529. {
  530. $settings = [
  531. 'page' => 1,
  532. 'limit' => 2,
  533. 'maxLimit' => 10,
  534. 'order' => [
  535. 'Users.username' => 'asc'
  536. ],
  537. ];
  538. $result = $this->Paginator->mergeOptions('Post', $settings);
  539. $expected = [
  540. 'page' => 1,
  541. 'limit' => 2,
  542. 'maxLimit' => 10,
  543. 'order' => [
  544. 'Users.username' => 'asc'
  545. ],
  546. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  547. ];
  548. $this->assertEquals($expected, $result);
  549. $settings = [
  550. 'page' => 1,
  551. 'limit' => 100,
  552. 'maxLimit' => 10,
  553. 'order' => [
  554. 'Users.username' => 'asc'
  555. ],
  556. ];
  557. $result = $this->Paginator->mergeOptions('Post', $settings);
  558. $expected = [
  559. 'page' => 1,
  560. 'limit' => 10,
  561. 'maxLimit' => 10,
  562. 'order' => [
  563. 'Users.username' => 'asc'
  564. ],
  565. 'whitelist' => ['limit', 'sort', 'page', 'direction']
  566. ];
  567. $this->assertEquals($expected, $result);
  568. }
  569. /**
  570. * Integration test to ensure that validateSort is being used by paginate()
  571. *
  572. * @return void
  573. */
  574. public function testValidateSortInvalid()
  575. {
  576. $table = $this->_getMockPosts(['query']);
  577. $query = $this->_getMockFindQuery();
  578. $table->expects($this->once())
  579. ->method('query')
  580. ->will($this->returnValue($query));
  581. $query->expects($this->once())->method('applyOptions')
  582. ->with([
  583. 'limit' => 20,
  584. 'page' => 1,
  585. 'order' => ['PaginatorPosts.id' => 'asc'],
  586. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  587. 'scope' => null,
  588. ]);
  589. $this->request->query = [
  590. 'page' => 1,
  591. 'sort' => 'id',
  592. 'direction' => 'herp'
  593. ];
  594. $this->Paginator->paginate($table);
  595. $this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sort']);
  596. $this->assertEquals('asc', $this->request->params['paging']['PaginatorPosts']['direction']);
  597. }
  598. /**
  599. * test that invalid directions are ignored.
  600. *
  601. * @return void
  602. */
  603. public function testValidateSortInvalidDirection()
  604. {
  605. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  606. $model->expects($this->any())
  607. ->method('alias')
  608. ->will($this->returnValue('model'));
  609. $model->expects($this->any())
  610. ->method('hasField')
  611. ->will($this->returnValue(true));
  612. $options = ['sort' => 'something', 'direction' => 'boogers'];
  613. $result = $this->Paginator->validateSort($model, $options);
  614. $this->assertEquals('asc', $result['order']['model.something']);
  615. }
  616. /**
  617. * Test empty pagination result.
  618. *
  619. * @return void
  620. */
  621. public function testEmptyPaginationResult()
  622. {
  623. $this->loadFixtures('Posts');
  624. $table = TableRegistry::get('PaginatorPosts');
  625. $table->deleteAll('1=1');
  626. $this->Paginator->paginate($table);
  627. $this->assertSame(
  628. 0,
  629. $this->request->params['paging']['PaginatorPosts']['count'],
  630. 'Count should be 0'
  631. );
  632. $this->assertSame(
  633. 1,
  634. $this->request->params['paging']['PaginatorPosts']['page'],
  635. 'Page number should not be 0'
  636. );
  637. $this->assertSame(
  638. 1,
  639. $this->request->params['paging']['PaginatorPosts']['pageCount'],
  640. 'Page count number should not be 0'
  641. );
  642. }
  643. /**
  644. * Test that a really large page number gets clamped to the max page size.
  645. *
  646. * @return void
  647. */
  648. public function testOutOfRangePageNumberGetsClamped()
  649. {
  650. $this->loadFixtures('Posts');
  651. $this->request->query['page'] = 3000;
  652. $table = TableRegistry::get('PaginatorPosts');
  653. try {
  654. $this->Paginator->paginate($table);
  655. $this->fail('No exception raised');
  656. } catch (NotFoundException $e) {
  657. $this->assertEquals(
  658. 1,
  659. $this->request->params['paging']['PaginatorPosts']['page'],
  660. 'Page number should not be 0'
  661. );
  662. }
  663. }
  664. /**
  665. * Test that a out of bounds request still knows about the page size
  666. *
  667. * @return void
  668. */
  669. public function testOutOfRangePageNumberStillProvidesPageCount()
  670. {
  671. $this->loadFixtures('Posts');
  672. $this->request->query['limit'] = 1;
  673. $this->request->query['page'] = 4;
  674. $table = TableRegistry::get('PaginatorPosts');
  675. try {
  676. $this->Paginator->paginate($table);
  677. $this->fail('No exception raised');
  678. } catch (NotFoundException $e) {
  679. $this->assertEquals(
  680. 3,
  681. $this->request->params['paging']['PaginatorPosts']['pageCount'],
  682. 'Page count number should not be 0'
  683. );
  684. }
  685. }
  686. /**
  687. * Test that a really REALLY large page number gets clamped to the max page size.
  688. *
  689. * @expectedException \Cake\Network\Exception\NotFoundException
  690. * @return void
  691. */
  692. public function testOutOfVeryBigPageNumberGetsClamped()
  693. {
  694. $this->loadFixtures('Posts');
  695. $this->request->query = [
  696. 'page' => '3000000000000000000000000',
  697. ];
  698. $table = TableRegistry::get('PaginatorPosts');
  699. $this->Paginator->paginate($table);
  700. }
  701. /**
  702. * test that fields not in whitelist won't be part of order conditions.
  703. *
  704. * @return void
  705. */
  706. public function testValidateSortWhitelistFailure()
  707. {
  708. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  709. $model->expects($this->any())
  710. ->method('alias')
  711. ->will($this->returnValue('model'));
  712. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  713. $options = [
  714. 'sort' => 'body',
  715. 'direction' => 'asc',
  716. 'sortWhitelist' => ['title', 'id']
  717. ];
  718. $result = $this->Paginator->validateSort($model, $options);
  719. $this->assertEquals([], $result['order']);
  720. }
  721. /**
  722. * test that fields in the whitelist are not validated
  723. *
  724. * @return void
  725. */
  726. public function testValidateSortWhitelistTrusted()
  727. {
  728. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  729. $model->expects($this->any())
  730. ->method('alias')
  731. ->will($this->returnValue('model'));
  732. $model->expects($this->once())
  733. ->method('hasField')
  734. ->will($this->returnValue(true));
  735. $options = [
  736. 'sort' => 'body',
  737. 'direction' => 'asc',
  738. 'sortWhitelist' => ['body']
  739. ];
  740. $result = $this->Paginator->validateSort($model, $options);
  741. $expected = ['model.body' => 'asc'];
  742. $this->assertEquals(
  743. $expected,
  744. $result['order'],
  745. 'Trusted fields in schema should be prefixed'
  746. );
  747. }
  748. /**
  749. * test that whitelist as empty array does not allow any sorting
  750. *
  751. * @return void
  752. */
  753. public function testValidateSortWhitelistEmpty()
  754. {
  755. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  756. $model->expects($this->any())
  757. ->method('alias')
  758. ->will($this->returnValue('model'));
  759. $model->expects($this->any())->method('hasField')
  760. ->will($this->returnValue(true));
  761. $options = [
  762. 'order' => [
  763. 'body' => 'asc',
  764. 'foo.bar' => 'asc'
  765. ],
  766. 'sort' => 'body',
  767. 'direction' => 'asc',
  768. 'sortWhitelist' => []
  769. ];
  770. $result = $this->Paginator->validateSort($model, $options);
  771. $this->assertSame([], $result['order'], 'No sort should be applied');
  772. }
  773. /**
  774. * test that fields in the whitelist are not validated
  775. *
  776. * @return void
  777. */
  778. public function testValidateSortWhitelistNotInSchema()
  779. {
  780. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  781. $model->expects($this->any())
  782. ->method('alias')
  783. ->will($this->returnValue('model'));
  784. $model->expects($this->once())->method('hasField')
  785. ->will($this->returnValue(false));
  786. $options = [
  787. 'sort' => 'score',
  788. 'direction' => 'asc',
  789. 'sortWhitelist' => ['score']
  790. ];
  791. $result = $this->Paginator->validateSort($model, $options);
  792. $expected = ['score' => 'asc'];
  793. $this->assertEquals(
  794. $expected,
  795. $result['order'],
  796. 'Trusted fields not in schema should not be altered'
  797. );
  798. }
  799. /**
  800. * test that multiple fields in the whitelist are not validated and properly aliased.
  801. *
  802. * @return void
  803. */
  804. public function testValidateSortWhitelistMultiple()
  805. {
  806. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  807. $model->expects($this->any())
  808. ->method('alias')
  809. ->will($this->returnValue('model'));
  810. $model->expects($this->once())
  811. ->method('hasField')
  812. ->will($this->returnValue(true));
  813. $options = [
  814. 'order' => [
  815. 'body' => 'asc',
  816. 'foo.bar' => 'asc'
  817. ],
  818. 'sortWhitelist' => ['body', 'foo.bar']
  819. ];
  820. $result = $this->Paginator->validateSort($model, $options);
  821. $expected = [
  822. 'model.body' => 'asc',
  823. 'foo.bar' => 'asc'
  824. ];
  825. $this->assertEquals($expected, $result['order']);
  826. }
  827. /**
  828. * test that multiple sort works.
  829. *
  830. * @return void
  831. */
  832. public function testValidateSortMultiple()
  833. {
  834. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  835. $model->expects($this->any())
  836. ->method('alias')
  837. ->will($this->returnValue('model'));
  838. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  839. $options = [
  840. 'order' => [
  841. 'author_id' => 'asc',
  842. 'title' => 'asc'
  843. ]
  844. ];
  845. $result = $this->Paginator->validateSort($model, $options);
  846. $expected = [
  847. 'model.author_id' => 'asc',
  848. 'model.title' => 'asc'
  849. ];
  850. $this->assertEquals($expected, $result['order']);
  851. }
  852. /**
  853. * test that multiple sort works in combination with query.
  854. *
  855. * @return void
  856. */
  857. public function testValidateSortMultipleWithQuery()
  858. {
  859. $this->loadFixtures('Posts');
  860. $table = TableRegistry::get('PaginatorPosts');
  861. $titleExtractor = function ($result) {
  862. $ids = [];
  863. foreach ($result as $record) {
  864. $ids[] = $record->title;
  865. }
  866. return $ids;
  867. };
  868. // Define two columns as default sort order
  869. $settings = [
  870. 'order' => [
  871. 'author_id' => 'asc',
  872. 'title' => 'asc'
  873. ]
  874. ];
  875. // Test with empty query
  876. $this->request->query = [];
  877. $result = $this->Paginator->paginate($table, $settings);
  878. $this->assertCount(3, $result, '3 rows should come back');
  879. $this->assertEquals(['First Post', 'Third Post', 'Second Post'], $titleExtractor($result));
  880. $this->assertEquals(
  881. ['PaginatorPosts.author_id' => 'asc', 'PaginatorPosts.title' => 'asc'],
  882. $this->request->params['paging']['PaginatorPosts']['totalOrder']
  883. );
  884. // Test overwriting a sort field defined in the settings
  885. $this->request->query = [
  886. 'sort' => 'author_id',
  887. 'direction' => 'desc'
  888. ];
  889. $result = $this->Paginator->paginate($table, $settings);
  890. $this->assertCount(3, $result, '3 rows should come back');
  891. $this->assertEquals(['Second Post', 'First Post', 'Third Post'], $titleExtractor($result));
  892. $this->assertEquals(
  893. ['PaginatorPosts.author_id' => 'desc', 'PaginatorPosts.title' => 'asc'],
  894. $this->request->params['paging']['PaginatorPosts']['totalOrder']
  895. );
  896. // Test sorting by a field not defined in the settings
  897. $this->request->query = [
  898. 'sort' => 'id',
  899. 'direction' => 'asc'
  900. ];
  901. $result = $this->Paginator->paginate($table, $settings);
  902. $this->assertCount(3, $result, '3 rows should come back');
  903. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  904. $this->assertEquals(
  905. ['PaginatorPosts.id' => 'asc', 'PaginatorPosts.author_id' => 'asc', 'PaginatorPosts.title' => 'asc'],
  906. $this->request->params['paging']['PaginatorPosts']['totalOrder']
  907. );
  908. }
  909. /**
  910. * Tests that order strings can used by Paginator
  911. *
  912. * @return void
  913. */
  914. public function testValidateSortWithString()
  915. {
  916. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  917. $model->expects($this->any())
  918. ->method('alias')
  919. ->will($this->returnValue('model'));
  920. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  921. $options = [
  922. 'order' => 'model.author_id DESC'
  923. ];
  924. $result = $this->Paginator->validateSort($model, $options);
  925. $expected = 'model.author_id DESC';
  926. $this->assertEquals($expected, $result['order']);
  927. }
  928. /**
  929. * Test that no sort doesn't trigger an error.
  930. *
  931. * @return void
  932. */
  933. public function testValidateSortNoSort()
  934. {
  935. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  936. $model->expects($this->any())
  937. ->method('alias')
  938. ->will($this->returnValue('model'));
  939. $model->expects($this->any())->method('hasField')
  940. ->will($this->returnValue(true));
  941. $options = [
  942. 'direction' => 'asc',
  943. 'sortWhitelist' => ['title', 'id'],
  944. ];
  945. $result = $this->Paginator->validateSort($model, $options);
  946. $this->assertEquals([], $result['order']);
  947. }
  948. /**
  949. * Test sorting with incorrect aliases on valid fields.
  950. *
  951. * @return void
  952. */
  953. public function testValidateSortInvalidAlias()
  954. {
  955. $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
  956. $model->expects($this->any())
  957. ->method('alias')
  958. ->will($this->returnValue('model'));
  959. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  960. $options = ['sort' => 'Derp.id'];
  961. $result = $this->Paginator->validateSort($model, $options);
  962. $this->assertEquals([], $result['order']);
  963. }
  964. /**
  965. * @return array
  966. */
  967. public function checkLimitProvider()
  968. {
  969. return [
  970. 'out of bounds' => [
  971. ['limit' => 1000000, 'maxLimit' => 100],
  972. 100,
  973. ],
  974. 'limit is nan' => [
  975. ['limit' => 'sheep!', 'maxLimit' => 100],
  976. 1,
  977. ],
  978. 'negative limit' => [
  979. ['limit' => '-1', 'maxLimit' => 100],
  980. 1,
  981. ],
  982. 'unset limit' => [
  983. ['limit' => null, 'maxLimit' => 100],
  984. 1,
  985. ],
  986. 'limit = 0' => [
  987. ['limit' => 0, 'maxLimit' => 100],
  988. 1,
  989. ],
  990. 'limit = 0 v2' => [
  991. ['limit' => 0, 'maxLimit' => 0],
  992. 1,
  993. ],
  994. 'limit = null' => [
  995. ['limit' => null, 'maxLimit' => 0],
  996. 1,
  997. ],
  998. 'bad input, results in 1' => [
  999. ['limit' => null, 'maxLimit' => null],
  1000. 1,
  1001. ],
  1002. 'bad input, results in 1 v2' => [
  1003. ['limit' => false, 'maxLimit' => false],
  1004. 1,
  1005. ],
  1006. ];
  1007. }
  1008. /**
  1009. * test that maxLimit is respected
  1010. *
  1011. * @dataProvider checkLimitProvider
  1012. * @return void
  1013. */
  1014. public function testCheckLimit($input, $expected)
  1015. {
  1016. $result = $this->Paginator->checkLimit($input);
  1017. $this->assertSame($expected, $result['limit']);
  1018. }
  1019. /**
  1020. * Integration test for checkLimit() being applied inside paginate()
  1021. *
  1022. * @return void
  1023. */
  1024. public function testPaginateMaxLimit()
  1025. {
  1026. $this->loadFixtures('Posts');
  1027. $table = TableRegistry::get('PaginatorPosts');
  1028. $settings = [
  1029. 'maxLimit' => 100,
  1030. ];
  1031. $this->request->query = [
  1032. 'limit' => '1000'
  1033. ];
  1034. $this->Paginator->paginate($table, $settings);
  1035. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['limit']);
  1036. $this->assertEquals(100, $this->request->params['paging']['PaginatorPosts']['perPage']);
  1037. $this->request->query = [
  1038. 'limit' => '10'
  1039. ];
  1040. $this->Paginator->paginate($table, $settings);
  1041. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['limit']);
  1042. $this->assertEquals(10, $this->request->params['paging']['PaginatorPosts']['perPage']);
  1043. }
  1044. /**
  1045. * test paginate() and custom find, to make sure the correct count is returned.
  1046. *
  1047. * @return void
  1048. */
  1049. public function testPaginateCustomFind()
  1050. {
  1051. $this->loadFixtures('Posts');
  1052. $titleExtractor = function ($result) {
  1053. $ids = [];
  1054. foreach ($result as $record) {
  1055. $ids[] = $record->title;
  1056. }
  1057. return $ids;
  1058. };
  1059. $table = TableRegistry::get('PaginatorPosts');
  1060. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  1061. $result = $table->save(new Entity($data));
  1062. $this->assertNotEmpty($result);
  1063. $result = $this->Paginator->paginate($table);
  1064. $this->assertCount(4, $result, '4 rows should come back');
  1065. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  1066. $result = $this->request->params['paging']['PaginatorPosts'];
  1067. $this->assertEquals(4, $result['current']);
  1068. $this->assertEquals(4, $result['count']);
  1069. $settings = ['finder' => 'published'];
  1070. $result = $this->Paginator->paginate($table, $settings);
  1071. $this->assertCount(3, $result, '3 rows should come back');
  1072. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  1073. $result = $this->request->params['paging']['PaginatorPosts'];
  1074. $this->assertEquals(3, $result['current']);
  1075. $this->assertEquals(3, $result['count']);
  1076. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  1077. $result = $this->Paginator->paginate($table, $settings);
  1078. $this->assertCount(1, $result, '1 rows should come back');
  1079. $this->assertEquals(['Third Post'], $titleExtractor($result));
  1080. $result = $this->request->params['paging']['PaginatorPosts'];
  1081. $this->assertEquals(1, $result['current']);
  1082. $this->assertEquals(3, $result['count']);
  1083. $this->assertEquals(2, $result['pageCount']);
  1084. $settings = ['finder' => 'published', 'limit' => 2];
  1085. $result = $this->Paginator->paginate($table, $settings);
  1086. $this->assertCount(2, $result, '2 rows should come back');
  1087. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  1088. $result = $this->request->params['paging']['PaginatorPosts'];
  1089. $this->assertEquals(2, $result['current']);
  1090. $this->assertEquals(3, $result['count']);
  1091. $this->assertEquals(2, $result['pageCount']);
  1092. $this->assertTrue($result['nextPage']);
  1093. $this->assertFalse($result['prevPage']);
  1094. $this->assertEquals(2, $result['perPage']);
  1095. $this->assertNull($result['limit']);
  1096. }
  1097. /**
  1098. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  1099. *
  1100. * @return void
  1101. */
  1102. public function testPaginateCustomFindFieldsArray()
  1103. {
  1104. $this->loadFixtures('Posts');
  1105. $table = TableRegistry::get('PaginatorPosts');
  1106. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  1107. $table->save(new Entity($data));
  1108. $settings = [
  1109. 'finder' => 'list',
  1110. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  1111. 'limit' => 2
  1112. ];
  1113. $results = $this->Paginator->paginate($table, $settings);
  1114. $result = $results->toArray();
  1115. $expected = [
  1116. 1 => 'First Post',
  1117. 2 => 'Second Post',
  1118. ];
  1119. $this->assertEquals($expected, $result);
  1120. $result = $this->request->params['paging']['PaginatorPosts'];
  1121. $this->assertEquals(2, $result['current']);
  1122. $this->assertEquals(3, $result['count']);
  1123. $this->assertEquals(2, $result['pageCount']);
  1124. $this->assertTrue($result['nextPage']);
  1125. $this->assertFalse($result['prevPage']);
  1126. }
  1127. /**
  1128. * test paginate() and custom finders to ensure the count + find
  1129. * use the custom type.
  1130. *
  1131. * @return void
  1132. */
  1133. public function testPaginateCustomFindCount()
  1134. {
  1135. $settings = [
  1136. 'finder' => 'published',
  1137. 'limit' => 2
  1138. ];
  1139. $table = $this->_getMockPosts(['query']);
  1140. $query = $this->_getMockFindQuery();
  1141. $table->expects($this->once())
  1142. ->method('query')
  1143. ->will($this->returnValue($query));
  1144. $query->expects($this->once())->method('applyOptions')
  1145. ->with([
  1146. 'limit' => 2,
  1147. 'page' => 1,
  1148. 'order' => [],
  1149. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1150. 'scope' => null,
  1151. ]);
  1152. $this->Paginator->paginate($table, $settings);
  1153. }
  1154. /**
  1155. * Tests that it is possible to pass an already made query object to
  1156. * paginate()
  1157. *
  1158. * @return void
  1159. */
  1160. public function testPaginateQuery()
  1161. {
  1162. $this->request->query = ['page' => '-1'];
  1163. $settings = [
  1164. 'PaginatorPosts' => [
  1165. 'contain' => ['PaginatorAuthor'],
  1166. 'maxLimit' => 10,
  1167. 'group' => 'PaginatorPosts.published',
  1168. 'order' => ['PaginatorPosts.id' => 'ASC']
  1169. ]
  1170. ];
  1171. $table = $this->_getMockPosts(['find']);
  1172. $query = $this->_getMockFindQuery($table);
  1173. $table->expects($this->never())->method('find');
  1174. $query->expects($this->once())
  1175. ->method('applyOptions')
  1176. ->with([
  1177. 'contain' => ['PaginatorAuthor'],
  1178. 'group' => 'PaginatorPosts.published',
  1179. 'limit' => 10,
  1180. 'order' => ['PaginatorPosts.id' => 'ASC'],
  1181. 'page' => 1,
  1182. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1183. 'scope' => null,
  1184. ]);
  1185. $this->Paginator->paginate($query, $settings);
  1186. }
  1187. /**
  1188. * test paginate() with bind()
  1189. *
  1190. * @return void
  1191. */
  1192. public function testPaginateQueryWithBindValue()
  1193. {
  1194. $config = ConnectionManager::config('test');
  1195. $this->skipIf(strpos($config['driver'], 'Sqlserver') !== false, 'Test temporarily broken in SQLServer');
  1196. $this->loadFixtures('Posts');
  1197. $table = TableRegistry::get('PaginatorPosts');
  1198. $query = $table->find()
  1199. ->where(['PaginatorPosts.author_id BETWEEN :start AND :end'])
  1200. ->bind(':start', 1)
  1201. ->bind(':end', 2);
  1202. $results = $this->Paginator->paginate($query, []);
  1203. $result = $results->toArray();
  1204. $this->assertCount(2, $result);
  1205. $this->assertEquals('First Post', $result[0]->title);
  1206. $this->assertEquals('Third Post', $result[1]->title);
  1207. }
  1208. /**
  1209. * Tests that passing a query object with a limit clause set will
  1210. * overwrite it with the passed defaults.
  1211. *
  1212. * @return void
  1213. */
  1214. public function testPaginateQueryWithLimit()
  1215. {
  1216. $this->request->query = ['page' => '-1'];
  1217. $settings = [
  1218. 'PaginatorPosts' => [
  1219. 'contain' => ['PaginatorAuthor'],
  1220. 'maxLimit' => 10,
  1221. 'limit' => 5,
  1222. 'group' => 'PaginatorPosts.published',
  1223. 'order' => ['PaginatorPosts.id' => 'ASC']
  1224. ]
  1225. ];
  1226. $table = $this->_getMockPosts(['find']);
  1227. $query = $this->_getMockFindQuery($table);
  1228. $query->limit(2);
  1229. $table->expects($this->never())->method('find');
  1230. $query->expects($this->once())
  1231. ->method('applyOptions')
  1232. ->with([
  1233. 'contain' => ['PaginatorAuthor'],
  1234. 'group' => 'PaginatorPosts.published',
  1235. 'limit' => 5,
  1236. 'order' => ['PaginatorPosts.id' => 'ASC'],
  1237. 'page' => 1,
  1238. 'whitelist' => ['limit', 'sort', 'page', 'direction'],
  1239. 'scope' => null,
  1240. ]);
  1241. $this->Paginator->paginate($query, $settings);
  1242. }
  1243. /**
  1244. * Helper method for making mocks.
  1245. *
  1246. * @param array $methods
  1247. * @return \Cake\ORM\Table|\PHPUnit_Framework_MockObject_MockObject
  1248. */
  1249. protected function _getMockPosts($methods = [])
  1250. {
  1251. return $this->getMockBuilder('TestApp\Model\Table\PaginatorPostsTable')
  1252. ->setMethods($methods)
  1253. ->setConstructorArgs([[
  1254. 'connection' => ConnectionManager::get('test'),
  1255. 'alias' => 'PaginatorPosts',
  1256. 'schema' => [
  1257. 'id' => ['type' => 'integer'],
  1258. 'author_id' => ['type' => 'integer', 'null' => false],
  1259. 'title' => ['type' => 'string', 'null' => false],
  1260. 'body' => 'text',
  1261. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  1262. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  1263. ]
  1264. ]])
  1265. ->getMock();
  1266. }
  1267. /**
  1268. * Helper method for mocking queries.
  1269. *
  1270. * @param string|null $table
  1271. *
  1272. * @return \Cake\ORM\Query|\PHPUnit_Framework_MockObject_MockObject
  1273. */
  1274. protected function _getMockFindQuery($table = null)
  1275. {
  1276. $query = $this->getMockBuilder('Cake\ORM\Query')
  1277. ->setMethods(['total', 'all', 'count', 'applyOptions'])
  1278. ->disableOriginalConstructor()
  1279. ->getMock();
  1280. $results = $this->getMockBuilder('Cake\ORM\ResultSet')
  1281. ->disableOriginalConstructor()
  1282. ->getMock();
  1283. $query->expects($this->any())
  1284. ->method('count')
  1285. ->will($this->returnValue(2));
  1286. $query->expects($this->any())
  1287. ->method('all')
  1288. ->will($this->returnValue($results));
  1289. $query->expects($this->any())
  1290. ->method('count')
  1291. ->will($this->returnValue(2));
  1292. $query->repository($table);
  1293. return $query;
  1294. }
  1295. }