Skip to content

Commit

Permalink
MAUT-11515 : Custom object date segment filters should have have same…
Browse files Browse the repository at this point in the history
… operators than date custom field filters (#358)

* added all date/datetime filters to custom oject date fields

* check if getcontext is exist

* Added new function instead changing old one

* change if condition sequence

* increase code coverage

* skip test case if mrunning on mautic 4.4
  • Loading branch information
dadarya0 authored Jul 16, 2024
1 parent 61d2b9f commit 6f2784e
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CustomFieldType/DateOperatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public function getOperators(): array

return array_intersect_key($allOperators, $allowedOperators);
}

public function getOperatorsForSegment(): array
{
return parent::getOperators();
}
}
13 changes: 11 additions & 2 deletions EventListener/SegmentFiltersChoicesGenerateSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,20 @@ function (CustomObject $customObject) use ($event, $fieldTypes): void {

/** @var CustomField $customField */
foreach ($customObject->getCustomFields()->getIterator() as $customField) {
if ($customField->getType() === $fieldTypes[HiddenType::NAME]) { // We don't want to show hidden types in filter list
if ($customField->getType() === $fieldTypes[HiddenType::NAME]) {
// We don't want to show hidden types in filter list
continue;
}

$allowedOperators = $customField->getTypeObject()->getOperators();
if (method_exists($this->typeOperatorProvider, 'getContext') &&
'segment' === $this->typeOperatorProvider->getContext() &&
method_exists($customField->getTypeObject(), 'getOperatorsForSegment')
) {
$allowedOperators = $customField->getTypeObject()->getOperatorsForSegment();
} else {
$allowedOperators = $customField->getTypeObject()->getOperators();
}

$typeOperators = $this->typeOperatorProvider->getOperatorsForFieldType($customField->getType());
$availableOperators = array_flip($typeOperators);
$operators = array_intersect_key($availableOperators, $allowedOperators);
Expand Down
93 changes: 74 additions & 19 deletions Tests/Functional/EventListener/FilterOperatorSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadField;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Entity\ListLead;
use MauticPlugin\CustomObjectsBundle\CustomFieldType\CustomFieldTypeInterface;
use MauticPlugin\CustomObjectsBundle\Entity\CustomField;
use MauticPlugin\CustomObjectsBundle\Entity\CustomItem;
use MauticPlugin\CustomObjectsBundle\Entity\CustomObject;
use MauticPlugin\CustomObjectsBundle\Provider\CustomFieldTypeProvider;
use MauticPlugin\CustomObjectsBundle\Tests\ProjectVersionTrait;
use Symfony\Component\HttpFoundation\Request;

class FilterOperatorSubscriberTest extends MauticMysqlTestCase
{
use ProjectVersionTrait;

public function testIfNewOperatorNotInCustomObjectsAddedinSegmentFilter()
{
// Create a segment
$segment = new LeadList();
$segment->setName('Test Segment A');
$segment->setPublicName('Test Segment A');
$segment->setAlias('test-segment-a');

$this->em->persist($segment);
$this->em->flush();

$crawler = $this->client->request(Request::METHOD_GET, '/s/segments/edit/'.$segment->getId());

$crawler = $this->client->request(Request::METHOD_GET, '/s/segments/new/');
$segment_filters = $crawler->filter('#available_segment_filters')->html();

$this->assertStringContainsString('not in custom objects', $segment_filters);
}

Expand All @@ -44,13 +40,7 @@ public function testIfProperContactsAreAddedinSegmentWithNotInCustomObjectsFilte
$this->em->persist($contact2);

// 2) create custom object "Email List" with "[email protected]" and "[email protected]" as items
$customObject = new CustomObject();
$customObject->setNameSingular('Email List');
$customObject->setNamePlural('Emai List');
$customObject->setAlias('emails');
$customObject->setType(CustomObject::TYPE_MASTER);
$this->em->persist($customObject);

$customObject = $this->createCustomObject('emails');
$customItem1 = new CustomItem($customObject);
$customItem1->setName('[email protected]');
$this->em->persist($customItem1);
Expand Down Expand Up @@ -94,4 +84,69 @@ public function testIfProperContactsAreAddedinSegmentWithNotInCustomObjectsFilte
$this->assertSame($contact2->getLastname(), $leads[0]->getLead()->getLastname());
$this->assertSame($contact2->getEmail(), $leads[0]->getLead()->getEmail());
}

public function testCustomObjectSegmentFilterOperatorForDateField(): void
{
if (!$this->isCloudProject()) {
$this->markTestSkipped('As context is not available for segment only in 4.4');
}

$leadField = $this->createField('date_field', 'date');

$fieldTypeProvider = self::$container->get('custom_field.type.provider');
\assert($fieldTypeProvider instanceof CustomFieldTypeProvider);
$objectType = $fieldTypeProvider->getType('date');
$dateField = $this->createCustomField('co_date_field', $objectType);

$this->createCustomObject('obj', $dateField);

$this->em->flush();
$crawler = $this->client->request(Request::METHOD_GET, '/s/segments/new/');

$coDateFilterOperators = $crawler
->filter('#available_segment_filters option[id="available_custom_object_cmf_'.$dateField->getId().'"]')
->attr('data-field-operators');

$leadDateFilterOperators = $crawler
->filter('#available_segment_filters option[id="available_lead_'.$leadField->getAlias().'"]')
->attr('data-field-operators');

$this->assertSame($coDateFilterOperators, $leadDateFilterOperators);
}

private function createField(string $alias, string $type): LeadField
{
$field = new LeadField();
$field->setName($alias);
$field->setAlias($alias);
$field->setType($type);
$this->em->persist($field);

return $field;
}

private function createCustomObject(string $alias, ?CustomField $dateField = null): CustomObject
{
$customObject = new CustomObject();
$customObject->setNameSingular($alias);
$customObject->setNamePlural($alias.'s');
$customObject->setAlias($alias);
if ($dateField) {
$customObject->addCustomField($dateField);
}
$this->em->persist($customObject);

return $customObject;
}

private function createCustomField(string $alias, CustomFieldTypeInterface $objectType): CustomField
{
$dateField = new CustomField();
$dateField->setTypeObject($objectType);
$dateField->setType($objectType->getKey());
$dateField->setLabel($alias);
$dateField->setAlias($alias);

return $dateField;
}
}
70 changes: 70 additions & 0 deletions Tests/Functional/Helper/QueryFilterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,76 @@ public function testGetCustomValueValueExpression(): void
]
);

$this->assertMatchWhere(
'test_value.value LIKE :par5',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_field1')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'like',
'properties' => [
'filter' => '2024',
],
]
);

$this->assertMatchWhere(
'test_value.value REGEXP :par6',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_field1')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'regexp',
'properties' => [
'filter' => '2024',
],
]
);

$this->assertMatchWhere(
'test_value.value LIKE :par7',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_field1')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'startsWith',
'properties' => [
'filter' => '2024',
],
]
);

$this->assertMatchWhere(
'test_value.value LIKE :par8',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_field1')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'endsWith',
'properties' => [
'filter' => '2024',
],
]
);

$this->assertMatchWhere(
'test_value.value LIKE :par9',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_field1')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'contains',
'properties' => [
'filter' => '2024',
],
]
);

$this->assertMatchWhere(
'test_value.value IS NULL',
[
Expand Down

0 comments on commit 6f2784e

Please sign in to comment.