Skip to content

Commit

Permalink
Prevent URL escaping when listing pagination URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
cnizzardini committed Mar 25, 2024
1 parent e56742b commit 20b9f3c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
27 changes: 27 additions & 0 deletions plugins/collection-view/src/View/Helper/PagninatorHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace MixerApi\CollectionView\View\Helper;

use Cake\View\Helper\PaginatorHelper;

class PagninatorHelper extends PaginatorHelper
{
/**
* Overwrite base method to never escape URLs.
*
* @inheritdoc
*/
public function generateUrl(
array $options = [],
array $url = [],
array $urlOptions = []
): string {
$urlOptions += [
'escape' => false,
'fullBase' => false,
];

return $this->Url->build($this->generateUrlParams($options, $url), $urlOptions);
}
}
2 changes: 2 additions & 0 deletions plugins/collection-view/src/View/JsonCollectionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Core\Configure;
use Cake\View\JsonView;
use MixerApi\CollectionView\Serializer;
use MixerApi\CollectionView\View\Helper\PagninatorHelper;

class JsonCollectionView extends JsonView
{
Expand Down Expand Up @@ -35,6 +36,7 @@ public function initialize(): void
parent::initialize();
$this->loadHelper('Paginator', [
'templates' => 'MixerApi/CollectionView.paginator-template',
'className' => PagninatorHelper::class
]);
}

Expand Down
2 changes: 2 additions & 0 deletions plugins/collection-view/src/View/XmlCollectionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Core\Configure;
use Cake\View\SerializedView;
use MixerApi\CollectionView\Serializer;
use MixerApi\CollectionView\View\Helper\PagninatorHelper;

class XmlCollectionView extends SerializedView
{
Expand Down Expand Up @@ -65,6 +66,7 @@ public function initialize(): void
parent::initialize();
$this->loadHelper('Paginator', [
'templates' => 'MixerApi/CollectionView.paginator-template',
'className' => PagninatorHelper::class
]);
}

Expand Down
3 changes: 2 additions & 1 deletion plugins/collection-view/tests/TestCase/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public function setUp(): void

public function test_json(): void
{
$this->get('/actors.json');
$this->get('/actors.json?limit=1');
$body = (string)$this->_response->getBody();
$object = json_decode($body);

$this->assertResponseOk();
$this->assertTrue(isset($object->collection->url));
$this->assertStringNotContainsString( '&amp;', $object->collection->next);
$this->assertNotEmpty($object->data);
}

Expand Down

0 comments on commit 20b9f3c

Please sign in to comment.