Skip to content

Commit

Permalink
MAUT-11383 : incorrect date value being calculating when the segment …
Browse files Browse the repository at this point in the history
…filter value is "yesterday" and operator is "gt" for date field (#359)

* Fix custom parameter date issue

* add more test case
  • Loading branch information
dadarya0 authored Oct 10, 2024
1 parent 6ad2360 commit 467455c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Helper/QueryFilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function addCustomFieldValueExpressionFromSegmentFilter(
ContactSegmentFilter $filter,
bool $filterAlreadyNegated = false
): void {
$filterValue = $filter->getParameterValue();
foreach ($unionQueryContainer as $segmentQueryBuilder) {
$valueParameter = $this->randomParameterNameService->generateRandomParameterName();
$expression = $this->getCustomValueValueExpression(
Expand All @@ -94,14 +95,14 @@ public function addCustomFieldValueExpressionFromSegmentFilter(
$filter,
$valueParameter,
$filterAlreadyNegated,
$filter->getParameterValue()
$filterValue
);

$this->addOperatorExpression(
$segmentQueryBuilder,
$expression,
$filter->getOperator(),
$filter->getParameterValue(),
$filterValue,
$valueParameter
);
}
Expand Down
44 changes: 43 additions & 1 deletion Tests/Functional/Helper/QueryFilterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MauticPlugin\CustomObjectsBundle\Tests\Functional\Helper;

use DateTime;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Segment\ContactSegmentFilterFactory;
use Mautic\LeadBundle\Segment\Query\QueryBuilder;
Expand Down Expand Up @@ -244,9 +245,45 @@ public function testGetCustomValueValueExpression(): void
],
]
);

$this->assertMatchWhere(
'test_value.value >= :pard',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_object_product')->getId(),
'object' => 'custom_object',
'type' => 'date',
'operator' => 'gte',
'properties' => [
'filter' => [
'dateTypeMode' => 'absolute',
'absoluteDate' => 'yesterday',
],
],
],
(new DateTime('yesterday'))->format('Y-m-d')
);

$this->assertMatchWhere(
'test_value.value <= :pare',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_object_product')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'lte',
'properties' => [
'filter' => [
'dateTypeMode' => 'absolute',
'absoluteDate' => 'tomorrow',
],
],
],
(new DateTime('tomorrow'))->format('Y-m-d 23:59:59')
);
}

protected function assertMatchWhere(string $expectedWhere, array $filter): void
protected function assertMatchWhere(string $expectedWhere, array $filter, ?string $expectedValue = null): void
{
$unionQueryContainer = new UnionQueryContainer();
$qb = new QueryBuilder($this->em->getConnection());
Expand All @@ -259,7 +296,12 @@ protected function assertMatchWhere(string $expectedWhere, array $filter): void
);

$unionQueryContainer->rewind();

$whereResponse = (string) $unionQueryContainer->current()->getQueryPart('where');

$this->assertSame($expectedWhere, $whereResponse);
if ($expectedValue) {
$this->assertSame($expectedValue, current($unionQueryContainer->current()->getParameters()));
}
}
}

0 comments on commit 467455c

Please sign in to comment.