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

Allow array values in api filter paramaters #7933

Merged
merged 4 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions app/bundles/CoreBundle/Entity/CommonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\DBAL\Query\QueryBuilder as DbalQueryBuilder;
use Doctrine\ORM\EntityRepository;
Expand Down Expand Up @@ -1539,12 +1540,19 @@ protected function buildWhereClauseFromArray($query, array $clauses, $expr = nul
break;
case 'in':
case 'notIn':
if (!$isOrm) {
$whereClause = $query->expr()->{$clause['expr']}($column, (array) $clause['val']);
} else {
$param = $this->generateRandomParameterName();

$parsed = str_getcsv(html_entity_decode($clause['val']), ',', '"');

$param = $this->generateRandomParameterName();
$arg = count($parsed) > 1 ? $parsed : array_shift($parsed);

if (is_array($arg)) {
$whereClause = $query->expr()->{$clause['expr']}($column, ':'.$param);
$query->setParameter($param, $clause['val']);
$query->setParameter($param, $arg, Connection::PARAM_STR_ARRAY);
} else {
$expression = 'in' === $clause['expr'] ? 'eq' : 'neq';
$whereClause = $query->expr()->{$expression}($column, ':'.$param);
$query->setParameter($param, $arg);
}
break;
default:
Expand Down
172 changes: 170 additions & 2 deletions app/bundles/CoreBundle/Tests/Unit/Entity/CommonRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Mautic\CoreBundle\Tests\Unit\Entity;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\QueryBuilder;
Expand All @@ -27,6 +29,10 @@ class CommonRepositoryTest extends \PHPUnit\Framework\TestCase
* @var QueryBuilder
*/
private $qb;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|Connection
*/
private $connectionMock;

/**
* Sets up objects used in the tests.
Expand All @@ -42,8 +48,11 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();

$this->repo = new CommonRepository($emMock, $metaMock);
$this->qb = new QueryBuilder($emMock);
$this->repo = new CommonRepository($emMock, $metaMock);
$this->qb = new QueryBuilder($emMock);
$this->connectionMock = $this->createMock(Connection::class);
$this->connectionMock->method('getExpressionBuilder')
->willReturn(new ExpressionBuilder($this->connectionMock));
}

/**
Expand Down Expand Up @@ -156,6 +165,8 @@ public function testValidateOrderByClauseWithMissingCol()
* @param array $args added to the method
*
* @return mixed result of the method
*
* @throws \ReflectionException
*/
private function callProtectedMethod($method, $args)
{
Expand All @@ -165,4 +176,161 @@ private function callProtectedMethod($method, $args)

return $method->invokeArgs($this->repo, $args);
}

public function testArgumentCSVArray()
{
$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.user_id',
'expr' => 'in',
'val' => '"1","2","3","4"',
],
];
$matchArgs = explode(',', $args[0]['val']);
array_walk($matchArgs, function (&$element) { $element = trim($element, '"'); });

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith('l.user_id IN (', (string) $qb->getQueryPart('where'));
$parameters = $qb->getParameters();
$this->assertEquals($matchArgs, array_shift($parameters));

$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.user_id',
'expr' => 'notIn',
'val' => '"1","2","3","4"',
],
];
$matchArgs = explode(',', $args[0]['val']);
array_walk($matchArgs, function (&$element) { $element = trim($element, '"'); });

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith('l.user_id NOT IN (', (string) $qb->getQueryPart('where'));
$parameters = $qb->getParameters();
$this->assertEquals($matchArgs, array_shift($parameters));
}

public function testNoEnquotedArgumentCSVArray()
{
$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.user_id',
'expr' => 'in',
'val' => '1,2,3,4',
],
];
$matchArgs = explode(',', $args[0]['val']);
array_walk($matchArgs, function (&$element) { $element = trim($element, '"'); });

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith('l.user_id IN (', (string) $qb->getQueryPart('where'));

$parameters = $qb->getParameters();
$this->assertEquals($matchArgs, array_shift($parameters));

$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.user_id',
'expr' => 'notIn',
'val' => '1,2,3,4',
],
];
$matchArgs = explode(',', $args[0]['val']);
array_walk($matchArgs, function (&$element) { $element = trim($element, '"'); });

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith('l.user_id NOT IN (', (string) $qb->getQueryPart('where'));

$parameters = $qb->getParameters();
$this->assertEquals($matchArgs, array_shift($parameters));
}

public function testNoEnquotedStringArgumentCSVArray()
{
$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.firstname',
'expr' => 'in',
'val' => 'jan,alan,don,john',
],
];
$matchArgs = explode(',', $args[0]['val']);
array_walk($matchArgs, function (&$element) { $element = trim($element, '"'); });

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith($args[0]['col'].' IN (', (string) $qb->getQueryPart('where'));

$parameters = $qb->getParameters();
$this->assertEquals($matchArgs, array_shift($parameters));

$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.firstname',
'expr' => 'notIn',
'val' => 'jan,alan,don,john',
],
];
$matchArgs = explode(',', $args[0]['val']);
array_walk($matchArgs, function (&$element) { $element = trim($element, '"'); });

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith($args[0]['col'].' NOT IN (', (string) $qb->getQueryPart('where'));

$parameters = $qb->getParameters();
$this->assertEquals($matchArgs, array_shift($parameters));
}

public function testStringArgumentInterpretedAsSingleValueEnquoted()
{
$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.firstname',
'expr' => 'in',
'val' => '"jan,alan,don,john"',
],
];

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith($args[0]['col'].' = ', (string) $qb->getQueryPart('where'));
$parameters = $qb->getParameters();
$this->assertEquals(trim($args[0]['val'], '"'), array_shift($parameters));

$qb = new \Doctrine\DBAL\Query\QueryBuilder($this->connectionMock);
$args = [
[
'col' => 'l.firstname',
'expr' => 'notIn',
'val' => '"jan,alan,don,john"',
],
];

$this->callBuildWhereClauseFromArray($qb, $args);

$this->assertStringStartsWith($args[0]['col'].' <> ', (string) $qb->getQueryPart('where'));
$parameters = $qb->getParameters();
$this->assertEquals(trim($args[0]['val'], '"'), array_shift($parameters));
}

private function callBuildWhereClauseFromArray($qb, $args)
{
$reflection = new \ReflectionClass(CommonRepository::class);
$method = $reflection->getMethod('buildWhereClauseFromArray');
$method->setAccessible(true);

return $method->invokeArgs($this->repo, [$qb, $args]);
}
}