Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix submit filters #6877

Merged
merged 16 commits into from
Feb 22, 2021
Merged
3 changes: 2 additions & 1 deletion src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
// NEXT_MAJOR: Uncomment next line.
// use Sonata\AdminBundle\Util\Instantiator;
use Sonata\AdminBundle\Util\ParametersManipulator;
use Sonata\Form\Validator\Constraints\InlineConstraint;
use Sonata\Form\Validator\ErrorElement;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
Expand Down Expand Up @@ -711,7 +712,7 @@ public function getFilterParameters()
}
}

$parameters = array_replace_recursive($parameters, $filters);
$parameters = ParametersManipulator::merge($parameters, $filters);

// always force the parent value
if ($this->isChild() && $this->getParentAssociationMapping()) {
Expand Down
36 changes: 36 additions & 0 deletions src/Util/ParametersManipulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Util;

/**
* @author Willem Verspyck <[email protected]>
*/
final class ParametersManipulator
{
/**
* Merge parameters, but replace them when it's a subarray.
*/
public static function merge(array $parameters, array $newParameters): array
{
foreach (array_intersect_key($parameters, $newParameters) as $key => $parameter) {
if (\is_array($parameter)) {
$parameters[$key] = array_replace($parameter, $newParameters[$key]);
} else {
$parameters[$key] = $newParameters[$key];
}
}

return array_merge($parameters, array_diff_key($newParameters, $parameters));
}
}
152 changes: 152 additions & 0 deletions tests/Util/ParametersManipulatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\Util;

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Util\ParametersManipulator;

/**
* @author Willem Verspyck <[email protected]>
*/
class ParametersManipulatorTest extends TestCase
{
public function provideMergeParameters(): iterable
{
return [
[
[
'_sort_order' => 'DESC',
'_sort_by' => 'id',
'status' => [
'type' => '1',
'value' => 'foo',
],
],
[
'status' => [
'type' => '2',
'value' => 'foo',
],
],
[
'_sort_order' => 'DESC',
'_sort_by' => 'id',
'status' => [
'type' => '2',
'value' => 'foo',
],
],
],
[
[
'status' => [
'type' => '1',
],
],
[
'status' => [
'value' => 'foo',
],
],
[
'status' => [
'type' => '1',
'value' => 'foo',
],
],
],
[
[
'status' => [
'type' => '1',
'value' => 'foo',
],
],
[
'status' => [
'type' => '2',
],
'_page' => 2,
'_per_page' => 25,
],
[
'status' => [
'type' => '2',
'value' => 'foo',
],
'_page' => 2,
'_per_page' => 25,
],
],
[
[
'status' => [
'type' => '1',
'value' => [
'foo',
'bar',
],
],
],
[
'status' => [
'value' => [
'foo',
],
],
],
[
'status' => [
'type' => '1',
'value' => [
'foo',
],
],
],
],
[
[
'status' => [
'value' => [
'foo',
'bar',
],
],
],
[
'status' => [
'value' => [
'baz',
],
],
],
[
'status' => [
'value' => [
'baz',
],
],
],
],
];
}

/**
* @dataProvider provideMergeParameters
*/
public function testMergeParameters(array $parameters, array $newParameters, array $result): void
{
$this->assertSame($result, ParametersManipulator::merge($parameters, $newParameters));
}
}