Skip to content

Commit

Permalink
Re-reverted knp subscriber changes and fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
pmishev committed Oct 22, 2019
1 parent eca5880 commit 3a5a04b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Subscriber/KnpPaginateQuerySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,22 @@ protected function getSorting(ItemsEvent $event)
$sortField = null;
$sortDirection = 'asc';

if (isset($_GET[$event->options['sortFieldParameterName']])) {
$sortField = $_GET[$event->options['sortFieldParameterName']];
$sortDirection = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'desc' ? 'desc' : 'asc';
$request = $this->requestStack->getCurrentRequest();
if ($request instanceof Request) {
$sortField = $request->get($event->options['sortFieldParameterName']);
$sortDirection = $request->get($event->options['sortDirectionParameterName'], 'desc');
$sortDirection = strtolower($sortDirection) === 'desc' ? 'desc' : 'asc';

// check if the requested sort field is in the sort whitelist
if (isset($event->options['sortFieldWhitelist']) && !in_array($sortField, $event->options['sortFieldWhitelist'])) {
throw new \UnexpectedValueException(sprintf('Cannot sort by [%s] as it is not in the whitelist', $sortField));
if ($sortField) {
// check if the requested sort field is in the sort whitelist
if (isset($event->options['sortFieldWhitelist']) && !in_array($sortField, $event->options['sortFieldWhitelist'])) {
throw new \UnexpectedValueException(
sprintf('Cannot sort by [%s] as it is not in the whitelist', $sortField)
);
}
}
}

return [$sortField, $sortDirection];
}
}
5 changes: 5 additions & 0 deletions Tests/Functional/Finder/Adapter/KnpPaginatorAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sineflow\ElasticsearchBundle\Finder\Adapter\KnpPaginatorAdapter;
use Sineflow\ElasticsearchBundle\Tests\AbstractElasticsearchTestCase;
use Sineflow\ElasticsearchBundle\Tests\App\fixture\Acme\BarBundle\Document\Product;
use Symfony\Component\HttpFoundation\Request;

class KnpPaginatorAdapterTest extends AbstractElasticsearchTestCase
{
Expand Down Expand Up @@ -124,6 +125,10 @@ public function testPagination()

public function testPaginationSorting()
{
// Create an empty request to get around a bug in KNP paginator that assumes there is always a Request
// https://github.com/KnpLabs/knp-components/issues/239
$this->getContainer()->get('request_stack')->push(new Request());

/** @var Repository $repo */
$repo = $this->getIndexManager('bar')->getRepository();
$paginator = $this->getContainer()->get('knp_paginator');
Expand Down

0 comments on commit 3a5a04b

Please sign in to comment.