| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.9.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Datasource;
- use Cake\Core\Configure;
- use Cake\Datasource\ConnectionManager;
- use Cake\Datasource\EntityInterface;
- use Cake\Datasource\Exception\PageOutOfBoundsException;
- use Cake\Datasource\Paginator;
- use Cake\ORM\Entity;
- trait PaginatorTestTrait
- {
- /**
- * @var \Cake\Datasource\Paginator
- */
- public $Paginator;
- /**
- * setup
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- Configure::write('App.namespace', 'TestApp');
- $this->Paginator = new Paginator();
- $this->Post = $this->getMockRepository();
- }
- /**
- * tearDown
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- $this->getTableLocator()->clear();
- }
- /**
- * Test that non-numeric values are rejected for page, and limit
- *
- * @return void
- */
- public function testPageParamCasting()
- {
- $this->Post->expects($this->any())
- ->method('getAlias')
- ->will($this->returnValue('Posts'));
- $query = $this->_getMockFindQuery();
- $this->Post->expects($this->any())
- ->method('find')
- ->will($this->returnValue($query));
- $params = ['page' => '1 " onclick="alert(\'xss\');">'];
- $settings = ['limit' => 1, 'maxLimit' => 10];
- $this->Paginator->paginate($this->Post, $params, $settings);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertSame(1, $pagingParams['Posts']['page'], 'XSS exploit opened');
- }
- /**
- * test that unknown keys in the default settings are
- * passed to the find operations.
- *
- * @return void
- */
- public function testPaginateExtraParams()
- {
- $params = ['page' => '-1'];
- $settings = [
- 'PaginatorPosts' => [
- 'contain' => ['PaginatorAuthor'],
- 'maxLimit' => 10,
- 'group' => 'PaginatorPosts.published',
- 'order' => ['PaginatorPosts.id' => 'ASC'],
- ],
- ];
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('applyOptions')
- ->with([
- 'contain' => ['PaginatorAuthor'],
- 'group' => 'PaginatorPosts.published',
- 'limit' => 10,
- 'order' => ['PaginatorPosts.id' => 'ASC'],
- 'page' => 1,
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => 'PaginatorPosts.id',
- ]);
- $this->Paginator->paginate($table, $params, $settings);
- }
- /**
- * Test to make sure options get sent to custom finder methods via paginate
- *
- * @return void
- */
- public function testPaginateCustomFinderOptions()
- {
- $this->loadFixtures('Posts');
- $settings = [
- 'PaginatorPosts' => [
- 'finder' => ['author' => ['author_id' => 1]],
- ],
- ];
- $table = $this->getTableLocator()->get('PaginatorPosts');
- $expected = $table
- ->find('author', [
- 'conditions' => [
- 'PaginatorPosts.author_id' => 1,
- ],
- ])
- ->count();
- $result = $this->Paginator->paginate($table, [], $settings)->count();
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that nested eager loaders don't trigger invalid SQL errors.
- *
- * @return void
- */
- public function testPaginateNestedEagerLoader()
- {
- $this->loadFixtures('Articles', 'Tags', 'Authors', 'ArticlesTags', 'AuthorsTags');
- $articles = $this->getTableLocator()->get('Articles');
- $articles->belongsToMany('Tags');
- $tags = $this->getTableLocator()->get('Tags');
- $tags->belongsToMany('Authors');
- $articles->getEventManager()->on('Model.beforeFind', function ($event, $query) {
- $query ->matching('Tags', function ($q) {
- return $q->matching('Authors', function ($q) {
- return $q->where(['Authors.name' => 'larry']);
- });
- });
- });
- $results = $this->Paginator->paginate($articles);
- $result = $results->first();
- $this->assertInstanceOf(EntityInterface::class, $result);
- $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Tags']);
- $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Authors']);
- }
- /**
- * test that flat default pagination parameters work.
- *
- * @return void
- */
- public function testDefaultPaginateParams()
- {
- $settings = [
- 'order' => ['PaginatorPosts.id' => 'DESC'],
- 'maxLimit' => 10,
- ];
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('applyOptions')
- ->with([
- 'limit' => 10,
- 'page' => 1,
- 'order' => ['PaginatorPosts.id' => 'DESC'],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => 'PaginatorPosts.id',
- ]);
- $this->Paginator->paginate($table, [], $settings);
- }
- /**
- * Tests that flat default pagination parameters work for multi order.
- *
- * @return void
- */
- public function testDefaultPaginateParamsMultiOrder()
- {
- $settings = [
- 'order' => ['PaginatorPosts.id' => 'DESC', 'PaginatorPosts.title' => 'ASC'],
- ];
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('applyOptions')
- ->with([
- 'limit' => 20,
- 'page' => 1,
- 'order' => $settings['order'],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => null,
- ]);
- $this->Paginator->paginate($table, [], $settings);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertNull($pagingParams['PaginatorPosts']['direction']);
- $this->assertFalse($pagingParams['PaginatorPosts']['sortDefault']);
- $this->assertFalse($pagingParams['PaginatorPosts']['directionDefault']);
- }
- /**
- * test that default sort and default direction are injected into request
- *
- * @return void
- */
- public function testDefaultPaginateParamsIntoRequest()
- {
- $settings = [
- 'order' => ['PaginatorPosts.id' => 'DESC'],
- 'maxLimit' => 10,
- ];
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('applyOptions')
- ->with([
- 'limit' => 10,
- 'page' => 1,
- 'order' => ['PaginatorPosts.id' => 'DESC'],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => 'PaginatorPosts.id',
- ]);
- $this->Paginator->paginate($table, [], $settings);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertEquals('PaginatorPosts.id', $pagingParams['PaginatorPosts']['sortDefault']);
- $this->assertEquals('DESC', $pagingParams['PaginatorPosts']['directionDefault']);
- }
- /**
- * test that option merging prefers specific models
- *
- * @return void
- */
- public function testMergeOptionsModelSpecific()
- {
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'Posts' => [
- 'page' => 1,
- 'limit' => 10,
- 'maxLimit' => 50,
- ],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- ];
- $defaults = $this->Paginator->getDefaults('Silly', $settings);
- $result = $this->Paginator->mergeOptions([], $defaults);
- $this->assertEquals($settings, $result);
- $defaults = $this->Paginator->getDefaults('Posts', $settings);
- $result = $this->Paginator->mergeOptions([], $defaults);
- $expected = ['page' => 1, 'limit' => 10, 'maxLimit' => 50, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
- $this->assertEquals($expected, $result);
- }
- /**
- * test mergeOptions with custom scope
- *
- * @return void
- */
- public function testMergeOptionsCustomScope()
- {
- $params = [
- 'page' => 10,
- 'limit' => 10,
- 'scope' => [
- 'page' => 2,
- 'limit' => 5,
- ],
- ];
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions($params, $defaults);
- $expected = [
- 'page' => 10,
- 'limit' => 10,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- ];
- $this->assertEquals($expected, $result);
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- 'scope' => 'non-existent',
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions($params, $defaults);
- $expected = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => 'non-existent',
- ];
- $this->assertEquals($expected, $result);
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- 'scope' => 'scope',
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions($params, $defaults);
- $expected = [
- 'page' => 2,
- 'limit' => 5,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => 'scope',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test mergeOptions with customFind key
- *
- * @return void
- */
- public function testMergeOptionsCustomFindKey()
- {
- $params = [
- 'page' => 10,
- 'limit' => 10,
- ];
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions($params, $defaults);
- $expected = [
- 'page' => 10,
- 'limit' => 10,
- 'maxLimit' => 100,
- 'finder' => 'myCustomFind',
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test merging options from the querystring.
- *
- * @return void
- */
- public function testMergeOptionsQueryString()
- {
- $params = [
- 'page' => 99,
- 'limit' => 75,
- ];
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions($params, $defaults);
- $expected = ['page' => 99, 'limit' => 75, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
- $this->assertEquals($expected, $result);
- }
- /**
- * test that the default whitelist doesn't let people screw with things they should not be allowed to.
- *
- * @return void
- */
- public function testMergeOptionsDefaultWhiteList()
- {
- $params = [
- 'page' => 10,
- 'limit' => 10,
- 'fields' => ['bad.stuff'],
- 'recursive' => 1000,
- 'conditions' => ['bad.stuff'],
- 'contain' => ['bad'],
- ];
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions($params, $defaults);
- $expected = ['page' => 10, 'limit' => 10, 'maxLimit' => 100, 'whitelist' => ['limit', 'sort', 'page', 'direction']];
- $this->assertEquals($expected, $result);
- }
- /**
- * test that modifying the whitelist works.
- *
- * @return void
- */
- public function testMergeOptionsExtraWhitelist()
- {
- $params = [
- 'page' => 10,
- 'limit' => 10,
- 'fields' => ['bad.stuff'],
- 'recursive' => 1000,
- 'conditions' => ['bad.stuff'],
- 'contain' => ['bad'],
- ];
- $settings = [
- 'page' => 1,
- 'limit' => 20,
- 'maxLimit' => 100,
- ];
- $this->Paginator->setConfig('whitelist', ['fields']);
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions($params, $defaults);
- $expected = [
- 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'fields' => ['bad.stuff'], 'whitelist' => ['limit', 'sort', 'page', 'direction', 'fields'],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test mergeOptions with limit > maxLimit in code.
- *
- * @return void
- */
- public function testMergeOptionsMaxLimit()
- {
- $settings = [
- 'limit' => 200,
- 'paramType' => 'named',
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions([], $defaults);
- $expected = [
- 'page' => 1,
- 'limit' => 100,
- 'maxLimit' => 100,
- 'paramType' => 'named',
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- ];
- $this->assertEquals($expected, $result);
- $settings = [
- 'maxLimit' => 10,
- 'paramType' => 'named',
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions([], $defaults);
- $expected = [
- 'page' => 1,
- 'limit' => 10,
- 'maxLimit' => 10,
- 'paramType' => 'named',
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test getDefaults with limit > maxLimit in code.
- *
- * @return void
- */
- public function testGetDefaultMaxLimit()
- {
- $settings = [
- 'page' => 1,
- 'limit' => 2,
- 'maxLimit' => 10,
- 'order' => [
- 'Users.username' => 'asc',
- ],
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions([], $defaults);
- $expected = [
- 'page' => 1,
- 'limit' => 2,
- 'maxLimit' => 10,
- 'order' => [
- 'Users.username' => 'asc',
- ],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- ];
- $this->assertEquals($expected, $result);
- $settings = [
- 'page' => 1,
- 'limit' => 100,
- 'maxLimit' => 10,
- 'order' => [
- 'Users.username' => 'asc',
- ],
- ];
- $defaults = $this->Paginator->getDefaults('Post', $settings);
- $result = $this->Paginator->mergeOptions([], $defaults);
- $expected = [
- 'page' => 1,
- 'limit' => 10,
- 'maxLimit' => 10,
- 'order' => [
- 'Users.username' => 'asc',
- ],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Integration test to ensure that validateSort is being used by paginate()
- *
- * @return void
- */
- public function testValidateSortInvalid()
- {
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())->method('applyOptions')
- ->with([
- 'limit' => 20,
- 'page' => 1,
- 'order' => ['PaginatorPosts.id' => 'asc'],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => 'id',
- ]);
- $params = [
- 'page' => 1,
- 'sort' => 'id',
- 'direction' => 'herp',
- ];
- $this->Paginator->paginate($table, $params);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertEquals('id', $pagingParams['PaginatorPosts']['sort']);
- $this->assertEquals('asc', $pagingParams['PaginatorPosts']['direction']);
- }
- /**
- * test that invalid directions are ignored.
- *
- * @return void
- */
- public function testValidateSortInvalidDirection()
- {
- $model = $this->getMockRepository();
- $model->expects($this->any())
- ->method('getAlias')
- ->will($this->returnValue('model'));
- $model->expects($this->any())
- ->method('hasField')
- ->will($this->returnValue(true));
- $options = ['sort' => 'something', 'direction' => 'boogers'];
- $result = $this->Paginator->validateSort($model, $options);
- $this->assertEquals('asc', $result['order']['model.something']);
- }
- /**
- * Test that "sort" and "direction" in paging params is properly set based
- * on initial value of "order" in paging settings.
- *
- * @return void
- */
- public function testValidaSortInitialSortAndDirection()
- {
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())->method('applyOptions')
- ->with([
- 'limit' => 20,
- 'page' => 1,
- 'order' => ['PaginatorPosts.id' => 'asc'],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'sort' => 'id',
- 'scope' => null,
- 'sortWhitelist' => ['id'],
- ]);
- $options = [
- 'order' => [
- 'id' => 'asc',
- ],
- 'sortWhitelist' => ['id'],
- ];
- $this->Paginator->paginate($table, [], $options);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertEquals('id', $pagingParams['PaginatorPosts']['sort']);
- $this->assertEquals('asc', $pagingParams['PaginatorPosts']['direction']);
- }
- /**
- * Test that "sort" and "direction" in paging params is properly set based
- * on initial value of "order" in paging settings.
- *
- * @return void
- */
- public function testValidateSortAndDirectionAliased()
- {
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())->method('applyOptions')
- ->with([
- 'limit' => 20,
- 'page' => 1,
- 'order' => ['PaginatorPosts.title' => 'asc'],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'sort' => 'title',
- 'scope' => null,
- ]);
- $options = [
- 'order' => [
- 'Articles.title' => 'desc',
- ],
- ];
- $queryParams = [
- 'page' => 1,
- 'sort' => 'title',
- 'direction' => 'asc',
- ];
- $this->Paginator->paginate($table, $queryParams, $options);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertEquals('title', $pagingParams['PaginatorPosts']['sort']);
- $this->assertEquals('asc', $pagingParams['PaginatorPosts']['direction']);
- $this->assertEquals('Articles.title', $pagingParams['PaginatorPosts']['sortDefault']);
- $this->assertEquals('desc', $pagingParams['PaginatorPosts']['directionDefault']);
- }
- /**
- * testValidateSortRetainsOriginalSortValue
- *
- * @return void
- * @see https://github.com/cakephp/cakephp/issues/11740
- */
- public function testValidateSortRetainsOriginalSortValue()
- {
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())->method('applyOptions')
- ->with([
- 'limit' => 20,
- 'page' => 1,
- 'order' => ['PaginatorPosts.id' => 'asc'],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sortWhitelist' => ['id'],
- 'sort' => 'id',
- ]);
- $params = [
- 'page' => 1,
- 'sort' => 'id',
- 'direction' => 'herp',
- ];
- $options = [
- 'sortWhitelist' => ['id'],
- ];
- $this->Paginator->paginate($table, $params, $options);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertEquals('id', $pagingParams['PaginatorPosts']['sort']);
- }
- /**
- * Test that a really large page number gets clamped to the max page size.
- *
- * @return void
- */
- public function testOutOfRangePageNumberGetsClamped()
- {
- $this->loadFixtures('Posts');
- $params['page'] = 3000;
- $table = $this->getTableLocator()->get('PaginatorPosts');
- try {
- $this->Paginator->paginate($table, $params);
- $this->fail('No exception raised');
- } catch (PageOutOfBoundsException $exception) {
- $this->assertEquals(
- 'Page number 3000 could not be found.',
- $exception->getMessage()
- );
- $this->assertSame(
- [
- 'requestedPage' => 3000,
- 'pagingParams' => $this->Paginator->getPagingParams(),
- ],
- $exception->getAttributes()
- );
- }
- }
- /**
- * Test that a really REALLY large page number gets clamped to the max page size.
- *
- * @return void
- */
- public function testOutOfVeryBigPageNumberGetsClamped()
- {
- $this->expectException(\Cake\Datasource\Exception\PageOutOfBoundsException::class);
- $this->loadFixtures('Posts');
- $params = [
- 'page' => '3000000000000000000000000',
- ];
- $table = $this->getTableLocator()->get('PaginatorPosts');
- $this->Paginator->paginate($table, $params);
- }
- /**
- * test that fields not in whitelist won't be part of order conditions.
- *
- * @return void
- */
- public function testValidateSortWhitelistFailure()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'sort' => 'body',
- 'direction' => 'asc',
- 'sortWhitelist' => ['title', 'id'],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $this->assertEquals([], $result['order']);
- }
- /**
- * test that fields in the whitelist are not validated
- *
- * @return void
- */
- public function testValidateSortWhitelistTrusted()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'sort' => 'body',
- 'direction' => 'asc',
- 'sortWhitelist' => ['body'],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = ['model.body' => 'asc'];
- $this->assertEquals(
- $expected,
- $result['order'],
- 'Trusted fields in schema should be prefixed'
- );
- }
- /**
- * test that whitelist as empty array does not allow any sorting
- *
- * @return void
- */
- public function testValidateSortWhitelistEmpty()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'order' => [
- 'body' => 'asc',
- 'foo.bar' => 'asc',
- ],
- 'sort' => 'body',
- 'direction' => 'asc',
- 'sortWhitelist' => [],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $this->assertSame([], $result['order'], 'No sort should be applied');
- }
- /**
- * test that fields in the whitelist are not validated
- *
- * @return void
- */
- public function testValidateSortWhitelistNotInSchema()
- {
- $model = $this->getMockRepository();
- $model->expects($this->any())
- ->method('getAlias')
- ->will($this->returnValue('model'));
- $model->expects($this->once())->method('hasField')
- ->will($this->returnValue(false));
- $options = [
- 'sort' => 'score',
- 'direction' => 'asc',
- 'sortWhitelist' => ['score'],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = ['score' => 'asc'];
- $this->assertEquals(
- $expected,
- $result['order'],
- 'Trusted fields not in schema should not be altered'
- );
- }
- /**
- * test that multiple fields in the whitelist are not validated and properly aliased.
- *
- * @return void
- */
- public function testValidateSortWhitelistMultiple()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'order' => [
- 'body' => 'asc',
- 'foo.bar' => 'asc',
- ],
- 'sortWhitelist' => ['body', 'foo.bar'],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = [
- 'model.body' => 'asc',
- 'foo.bar' => 'asc',
- ];
- $this->assertEquals($expected, $result['order']);
- }
- /**
- * @return \Cake\Datasource\RepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected function getMockRepository()
- {
- $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')
- ->setMethods([
- 'getAlias', 'hasField', 'alias', 'find', 'get', 'query', 'updateAll', 'deleteAll',
- 'exists', 'save', 'delete', 'newEntity', 'newEntities', 'patchEntity', 'patchEntities',
- ])
- ->getMock();
- return $model;
- }
- /**
- * @param string $modelAlias Model alias to use.
- * @return \Cake\Datasource\RepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected function mockAliasHasFieldModel($modelAlias = 'model')
- {
- $model = $this->getMockRepository();
- $model->expects($this->any())
- ->method('getAlias')
- ->will($this->returnValue($modelAlias));
- $model->expects($this->any())
- ->method('hasField')
- ->will($this->returnValue(true));
- return $model;
- }
- /**
- * test that multiple sort works.
- *
- * @return void
- */
- public function testValidateSortMultiple()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'order' => [
- 'author_id' => 'asc',
- 'title' => 'asc',
- ],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = [
- 'model.author_id' => 'asc',
- 'model.title' => 'asc',
- ];
- $this->assertEquals($expected, $result['order']);
- }
- /**
- * test that multiple sort adds in query data.
- *
- * @return void
- */
- public function testValidateSortMultipleWithQuery()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'sort' => 'created',
- 'direction' => 'desc',
- 'order' => [
- 'author_id' => 'asc',
- 'title' => 'asc',
- ],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = [
- 'model.created' => 'desc',
- 'model.author_id' => 'asc',
- 'model.title' => 'asc',
- ];
- $this->assertEquals($expected, $result['order']);
- $options = [
- 'sort' => 'title',
- 'direction' => 'desc',
- 'order' => [
- 'author_id' => 'asc',
- 'title' => 'asc',
- ],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = [
- 'model.title' => 'desc',
- 'model.author_id' => 'asc',
- ];
- $this->assertEquals($expected, $result['order']);
- }
- /**
- * Tests that sort query string and model prefixes default match on assoc merging.
- *
- * @return void
- */
- public function testValidateSortMultipleWithQueryAndAliasedDefault()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'sort' => 'created',
- 'direction' => 'desc',
- 'order' => [
- 'model.created' => 'asc',
- ],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = [
- 'sort' => 'created',
- 'order' => [
- 'model.created' => 'desc',
- ],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that order strings can used by Paginator
- *
- * @return void
- */
- public function testValidateSortWithString()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'order' => 'model.author_id DESC',
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $expected = 'model.author_id DESC';
- $this->assertEquals($expected, $result['order']);
- }
- /**
- * Test that no sort doesn't trigger an error.
- *
- * @return void
- */
- public function testValidateSortNoSort()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = [
- 'direction' => 'asc',
- 'sortWhitelist' => ['title', 'id'],
- ];
- $result = $this->Paginator->validateSort($model, $options);
- $this->assertEquals([], $result['order']);
- }
- /**
- * Test sorting with incorrect aliases on valid fields.
- *
- * @return void
- */
- public function testValidateSortInvalidAlias()
- {
- $model = $this->mockAliasHasFieldModel();
- $options = ['sort' => 'Derp.id'];
- $result = $this->Paginator->validateSort($model, $options);
- $this->assertEquals([], $result['order']);
- }
- /**
- * @return array
- */
- public function checkLimitProvider()
- {
- return [
- 'out of bounds' => [
- ['limit' => 1000000, 'maxLimit' => 100],
- 100,
- ],
- 'limit is nan' => [
- ['limit' => 'sheep!', 'maxLimit' => 100],
- 1,
- ],
- 'negative limit' => [
- ['limit' => '-1', 'maxLimit' => 100],
- 1,
- ],
- 'unset limit' => [
- ['limit' => null, 'maxLimit' => 100],
- 1,
- ],
- 'limit = 0' => [
- ['limit' => 0, 'maxLimit' => 100],
- 1,
- ],
- 'limit = 0 v2' => [
- ['limit' => 0, 'maxLimit' => 0],
- 1,
- ],
- 'limit = null' => [
- ['limit' => null, 'maxLimit' => 0],
- 1,
- ],
- 'bad input, results in 1' => [
- ['limit' => null, 'maxLimit' => null],
- 1,
- ],
- 'bad input, results in 1 v2' => [
- ['limit' => false, 'maxLimit' => false],
- 1,
- ],
- ];
- }
- /**
- * test that maxLimit is respected
- *
- * @dataProvider checkLimitProvider
- * @return void
- */
- public function testCheckLimit($input, $expected)
- {
- $result = $this->Paginator->checkLimit($input);
- $this->assertSame($expected, $result['limit']);
- }
- /**
- * Integration test for checkLimit() being applied inside paginate()
- *
- * @return void
- */
- public function testPaginateMaxLimit()
- {
- $this->loadFixtures('Posts');
- $table = $this->getTableLocator()->get('PaginatorPosts');
- $settings = [
- 'maxLimit' => 100,
- ];
- $params = [
- 'limit' => '1000',
- ];
- $this->Paginator->paginate($table, $params, $settings);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertEquals(100, $pagingParams['PaginatorPosts']['limit']);
- $this->assertEquals(100, $pagingParams['PaginatorPosts']['perPage']);
- $params = [
- 'limit' => '10',
- ];
- $this->Paginator->paginate($table, $params, $settings);
- $pagingParams = $this->Paginator->getPagingParams();
- $this->assertEquals(10, $pagingParams['PaginatorPosts']['limit']);
- $this->assertEquals(10, $pagingParams['PaginatorPosts']['perPage']);
- }
- /**
- * test paginate() and custom finders to ensure the count + find
- * use the custom type.
- *
- * @return void
- */
- public function testPaginateCustomFindCount()
- {
- $settings = [
- 'finder' => 'published',
- 'limit' => 2,
- ];
- $table = $this->_getMockPosts(['query']);
- $query = $this->_getMockFindQuery();
- $table->expects($this->once())
- ->method('query')
- ->will($this->returnValue($query));
- $query->expects($this->once())->method('applyOptions')
- ->with([
- 'limit' => 2,
- 'page' => 1,
- 'order' => [],
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => null,
- ]);
- $this->Paginator->paginate($table, [], $settings);
- }
- /**
- * Tests that it is possible to pass an already made query object to
- * paginate()
- *
- * @return void
- */
- public function testPaginateQuery()
- {
- $params = ['page' => '-1'];
- $settings = [
- 'PaginatorPosts' => [
- 'contain' => ['PaginatorAuthor'],
- 'maxLimit' => 10,
- 'group' => 'PaginatorPosts.published',
- 'order' => ['PaginatorPosts.id' => 'ASC'],
- ],
- ];
- $table = $this->_getMockPosts(['find']);
- $query = $this->_getMockFindQuery($table);
- $table->expects($this->never())->method('find');
- $query->expects($this->once())
- ->method('applyOptions')
- ->with([
- 'contain' => ['PaginatorAuthor'],
- 'group' => 'PaginatorPosts.published',
- 'limit' => 10,
- 'order' => ['PaginatorPosts.id' => 'ASC'],
- 'page' => 1,
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => 'PaginatorPosts.id',
- ]);
- $this->Paginator->paginate($query, $params, $settings);
- }
- /**
- * test paginate() with bind()
- *
- * @return void
- */
- public function testPaginateQueryWithBindValue()
- {
- $config = ConnectionManager::getConfig('test');
- $this->skipIf(strpos($config['driver'], 'Sqlserver') !== false, 'Test temporarily broken in SQLServer');
- $this->loadFixtures('Posts');
- $table = $this->getTableLocator()->get('PaginatorPosts');
- $query = $table->find()
- ->where(['PaginatorPosts.author_id BETWEEN :start AND :end'])
- ->bind(':start', 1)
- ->bind(':end', 2);
- $results = $this->Paginator->paginate($query, []);
- $result = $results->toArray();
- $this->assertCount(2, $result);
- $this->assertEquals('First Post', $result[0]->title);
- $this->assertEquals('Third Post', $result[1]->title);
- }
- /**
- * Tests that passing a query object with a limit clause set will
- * overwrite it with the passed defaults.
- *
- * @return void
- */
- public function testPaginateQueryWithLimit()
- {
- $params = ['page' => '-1'];
- $settings = [
- 'PaginatorPosts' => [
- 'contain' => ['PaginatorAuthor'],
- 'maxLimit' => 10,
- 'limit' => 5,
- 'group' => 'PaginatorPosts.published',
- 'order' => ['PaginatorPosts.id' => 'ASC'],
- ],
- ];
- $table = $this->_getMockPosts(['find']);
- $query = $this->_getMockFindQuery($table);
- $query->limit(2);
- $table->expects($this->never())->method('find');
- $query->expects($this->once())
- ->method('applyOptions')
- ->with([
- 'contain' => ['PaginatorAuthor'],
- 'group' => 'PaginatorPosts.published',
- 'limit' => 5,
- 'order' => ['PaginatorPosts.id' => 'ASC'],
- 'page' => 1,
- 'whitelist' => ['limit', 'sort', 'page', 'direction'],
- 'scope' => null,
- 'sort' => 'PaginatorPosts.id',
- ]);
- $this->Paginator->paginate($query, $params, $settings);
- }
- /**
- * Helper method for making mocks.
- *
- * @param array $methods
- * @return \Cake\ORM\Table|\PHPUnit\Framework\MockObject\MockObject
- */
- protected function _getMockPosts($methods = [])
- {
- return $this->getMockBuilder('TestApp\Model\Table\PaginatorPostsTable')
- ->setMethods($methods)
- ->setConstructorArgs([[
- 'connection' => ConnectionManager::get('test'),
- 'alias' => 'PaginatorPosts',
- 'schema' => [
- 'id' => ['type' => 'integer'],
- 'author_id' => ['type' => 'integer', 'null' => false],
- 'title' => ['type' => 'string', 'null' => false],
- 'body' => 'text',
- 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
- '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
- ],
- ]])
- ->getMock();
- }
- /**
- * Helper method for mocking queries.
- *
- * @param string|null $table
- * @return \Cake\ORM\Query|\PHPUnit\Framework\MockObject\MockObject
- */
- protected function _getMockFindQuery($table = null)
- {
- /** @var \Cake\ORM\Query|\PHPUnit\Framework\MockObject\MockObject $query */
- $query = $this->getMockBuilder('Cake\ORM\Query')
- ->setMethods(['total', 'all', 'count', 'applyOptions'])
- ->disableOriginalConstructor()
- ->getMock();
- $results = $this->getMockBuilder('Cake\ORM\ResultSet')
- ->disableOriginalConstructor()
- ->getMock();
- $query->expects($this->any())
- ->method('count')
- ->will($this->returnValue(2));
- $query->expects($this->any())
- ->method('all')
- ->will($this->returnValue($results));
- if ($table) {
- $query->repository($table);
- }
- return $query;
- }
- }
|