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

Between operator for segments with numeric Custom Object fields #346

Merged
merged 12 commits into from
Jun 14, 2024
2 changes: 1 addition & 1 deletion CustomFieldType/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function createValueEntity(CustomField $customField, CustomItem $customIt
public function getOperators(): array
{
$allOperators = parent::getOperators();
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty']);
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty', 'between', '!between']);

return array_intersect_key($allOperators, $allowedOperators);
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/Functional/Helper/QueryFilterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ public function testGetCustomValueValueExpression(): void
],
]
);

$this->assertMatchWhere(
'test_value.value BETWEEN 0 AND 10',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_object_product')->getId(),
'object' => 'custom_object',
'type' => 'int',
'operator' => 'between',
'properties' => [
'filter' => [
'number_from' => 0,
'number_to' => 10,
],
],
]
);
}

protected function assertMatchWhere(string $expectedWhere, array $filter): void
Expand Down
13 changes: 13 additions & 0 deletions Tests/ProjectVersionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\CustomObjectsBundle\Tests;

trait ProjectVersionTrait
{
private function isCloudProject(): bool
{
return str_starts_with((string) getenv('MAUTIC_PROJECT_VERSION'), 'cloud-');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
use MauticPlugin\CustomObjectsBundle\Provider\ConfigProvider;
use MauticPlugin\CustomObjectsBundle\Provider\CustomFieldTypeProvider;
use MauticPlugin\CustomObjectsBundle\Repository\CustomObjectRepository;
use MauticPlugin\CustomObjectsBundle\Tests\ProjectVersionTrait;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\TranslatorInterface;

class SegmentFiltersChoicesGenerateSubscriberTest extends TestCase
{
use ProjectVersionTrait;

/**
* @var CustomObjectRepository|MockObject
*/
Expand Down Expand Up @@ -151,13 +154,11 @@ public function testOnGenerateSegmentFilters(): void
'label' => 'between',
'expr' => 'between',
'negate_expr' => 'notBetween',
'hide' => true,
],
'!between' => [
'label' => 'not between',
'expr' => 'notBetween',
'negate_expr' => 'between',
'hide' => true,
],
'in' => [
'label' => 'including',
Expand Down Expand Up @@ -207,7 +208,12 @@ public function testOnGenerateSegmentFilters(): void
],
];

$fieldOperators = [
$betweenOperators = $this->isCloudProject() ? [
'between' => 'between',
'not between' => '!between',
] : [];

$fieldOperators = array_merge([
'equals' => '=',
'not equal' => '!=',
'greater than' => 'gt',
Expand All @@ -216,7 +222,7 @@ public function testOnGenerateSegmentFilters(): void
'less than or equal' => 'lte',
'empty' => 'empty',
'not empty' => '!empty',
];
], $betweenOperators);

$event = new LeadListFiltersChoicesEvent([], [], $this->translator);

Expand All @@ -233,66 +239,74 @@ public function testOnGenerateSegmentFilters(): void
->with($criteria)
->willReturn(new ArrayCollection([$customObject]));

$translationsKeys = array_filter([
['custom.item.name.label'],
['mautic.lead.list.form.operator.equals'],
['mautic.lead.list.form.operator.notequals'],
['mautic.lead.list.form.operator.isempty'],
['mautic.lead.list.form.operator.isnotempty'],
['mautic.lead.list.form.operator.islike'],
['mautic.lead.list.form.operator.isnotlike'],
['mautic.lead.list.form.operator.regexp'],
['mautic.lead.list.form.operator.notregexp'],
['mautic.core.operator.starts.with'],
['mautic.core.operator.ends.with'],
['mautic.core.operator.contains'],
['mautic.lead.list.form.operator.equals'],
['mautic.lead.list.form.operator.notequals'],
['mautic.lead.list.form.operator.greaterthan'],
['mautic.lead.list.form.operator.greaterthanequals'],
['mautic.lead.list.form.operator.lessthan'],
['mautic.lead.list.form.operator.lessthanequals'],
['mautic.lead.list.form.operator.isempty'],
['mautic.lead.list.form.operator.isnotempty'],
['mautic.lead.list.form.operator.islike'],
['mautic.lead.list.form.operator.isnotlike'],
$this->isCloudProject() ? ['mautic.lead.list.form.operator.between'] : null,
$this->isCloudProject() ? ['mautic.lead.list.form.operator.notbetween'] : null,
['mautic.lead.list.form.operator.regexp'],
['mautic.lead.list.form.operator.notregexp'],
['mautic.core.operator.starts.with'],
['mautic.core.operator.ends.with'],
['mautic.core.operator.contains'],
]);

$translations = array_filter([
'Mobile',
'equals',
'not equal',
'empty',
'not empty',
'like',
'not like',
'regexp',
'not regexp',
'starts with',
'ends with',
'contains',
'equals',
'not equal',
'greater than',
'greater than or equal',
'less than',
'less than or equal',
'empty',
'not empty',
'like',
'not like',
$this->isCloudProject() ? 'between' : null,
$this->isCloudProject() ? 'not between' : null,
'regexp',
'not regexp',
'starts with',
'ends with',
'contains',
]);

$this->translator
->method('trans')
->withConsecutive(
['custom.item.name.label'],
['mautic.lead.list.form.operator.equals'],
['mautic.lead.list.form.operator.notequals'],
['mautic.lead.list.form.operator.isempty'],
['mautic.lead.list.form.operator.isnotempty'],
['mautic.lead.list.form.operator.islike'],
['mautic.lead.list.form.operator.isnotlike'],
['mautic.lead.list.form.operator.regexp'],
['mautic.lead.list.form.operator.notregexp'],
['mautic.core.operator.starts.with'],
['mautic.core.operator.ends.with'],
['mautic.core.operator.contains'],
['mautic.lead.list.form.operator.equals'],
['mautic.lead.list.form.operator.notequals'],
['mautic.lead.list.form.operator.greaterthan'],
['mautic.lead.list.form.operator.greaterthanequals'],
['mautic.lead.list.form.operator.lessthan'],
['mautic.lead.list.form.operator.lessthanequals'],
['mautic.lead.list.form.operator.isempty'],
['mautic.lead.list.form.operator.isnotempty'],
['mautic.lead.list.form.operator.islike'],
['mautic.lead.list.form.operator.isnotlike'],
['mautic.lead.list.form.operator.regexp'],
['mautic.lead.list.form.operator.notregexp'],
['mautic.core.operator.starts.with'],
['mautic.core.operator.ends.with'],
['mautic.core.operator.contains']
)
->willReturn(
'Mobile',
'equals',
'not equal',
'empty',
'not empty',
'like',
'not like',
'regexp',
'not regexp',
'starts with',
'ends with',
'contains',
'equals',
'not equal',
'greater than',
'greater than or equal',
'less than',
'less than or equal',
'empty',
'not empty',
'like',
'not like',
'regexp',
'not regexp',
'starts with',
'ends with',
'contains'
);
->withConsecutive(...$translationsKeys)
->willReturn(...$translations);

$this->filterOperatorProvider->expects($this->once())
->method('getAllOperators')
Expand Down
Loading