PaginatorComponentTest.php 43 KB

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