Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 42 additions & 2 deletions EventListener/DynamicContentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ public static function getSubscribedEvents(): array

public function evaluateFilters(ContactFiltersEvaluateEvent $event): void
{
// 2. Normalize only Custom Object filters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment

$filters = $this->normalizeCustomObjectFilters($event->getFilters());
if ($event->isEvaluated()
|| !$this->configProvider->pluginIsEnabled()
|| !$this->hasCustomObjectFilters($event->getFilters())
|| !$this->hasCustomObjectFilters($filters)
) {
return;
}

$event->setIsEvaluated(true);
$event->stopPropagation();
$event->setIsMatched($this->contactFilterMatcher->match(
$event->getFilters(),
$filters,
$event->getContact()->getProfileFields()
));
}
Expand All @@ -71,4 +73,42 @@ private function hasCustomObjectFilters(array $filters): bool

return false;
}

/**
* @param array<int, array<string, mixed>> $filters
*
* @return array<int, array<string, mixed>>
*/
private function normalizeCustomObjectFilters(array $filters): array
{
foreach ($filters as &$filter) {
if (
!isset($filter['properties']['options']) ||
!is_array($filter['properties']['options'])
) {
continue;
}

$label = $filter['filter_value'] ?? null;
if (!$label) {
continue;
}

$options = $filter['properties']['options'];

// Case A: Standard format (label => value)
if (isset($options[$label])) {
$filter['filter_value'] = $options[$label];
continue;
}

// Case B: Reverse format (value => label)
$reversed = array_flip($options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$value = array_search($label, $options, true);
if ($value !== false) {
$filter['filter_value'] = $value;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array_flip is expensive operation

if (isset($reversed[$label])) {
$filter['filter_value'] = $reversed[$label];
}
}

return $filters;
}
}
120 changes: 120 additions & 0 deletions Tests/Functional/Helper/DynamicContentSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\CustomObjectsBundle\Tests\EventListener;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\DynamicContentBundle\Event\ContactFiltersEvaluateEvent;
use Mautic\LeadBundle\Entity\Lead;
use MauticPlugin\CustomObjectsBundle\Entity\CustomField;
use MauticPlugin\CustomObjectsBundle\Entity\CustomObject;
use MauticPlugin\CustomObjectsBundle\EventListener\DynamicContentSubscriber;
use MauticPlugin\CustomObjectsBundle\Helper\ContactFilterMatcher;
use MauticPlugin\CustomObjectsBundle\Provider\ConfigProvider;
use MauticPlugin\CustomObjectsBundle\Segment\Query\Filter\QueryFilterFactory;

class DynamicContentSubscriberTest extends MauticMysqlTestCase
{
private CustomObject $object;
private CustomField $field;

protected function setUp(): void
{
parent::setUp();

// --- Create Custom Object ---
$this->object = new CustomObject();
$this->object->setNameSingular('TestObject');
$this->object->setNamePlural('TestObjects');
$this->object->setAlias('testobject');
$this->object->setIsPublished(true);
$this->em->persist($this->object);

// --- Create Custom Field ---
$this->field = new CustomField();
$this->field->setLabel('Country');
$this->field->setAlias('country');
$this->field->setCustomObject($this->object);
$this->field->setType('select'); // important
$this->field->setIsPublished(true);

// Create Doctrine Collection of option entities
$options = new \Doctrine\Common\Collections\ArrayCollection();

// Albania => AL
$opt1 = new \MauticPlugin\CustomObjectsBundle\Entity\CustomFieldOption();
$opt1->setLabel('Albania');
$opt1->setValue('AL');
$opt1->setCustomField($this->field);
$options->add($opt1);

// France => FR
$opt2 = new \MauticPlugin\CustomObjectsBundle\Entity\CustomFieldOption();
$opt2->setLabel('France');
$opt2->setValue('FR');
$opt2->setCustomField($this->field);
$options->add($opt2);

// Assign options
$this->field->setOptions($options);

// Persist all
$this->em->persist($this->field);
$this->em->persist($opt1);
$this->em->persist($opt2);
$this->em->flush();
}

public function testCustomObjectLabelIsConvertedToValue(): void
{
// --- Build filter using the custom field we created ---
$filters = [[
'object' => 'custom_object',
'field' => $this->field->getId(),
'filter_type' => 'text',
'filter_value' => 'Albania',
'properties' => [
'options' => [
'Albania' => 'AL',
'France' => 'FR',
],
],
]];

// --- Mock dependencies ---
$configProvider = $this->createMock(ConfigProvider::class);
$configProvider->method('pluginIsEnabled')->willReturn(true);

$queryFilterFactory = static::getContainer()->get(QueryFilterFactory::class);
$matcher = static::getContainer()->get(ContactFilterMatcher::class);

$subscriber = new DynamicContentSubscriber(
$queryFilterFactory,
$configProvider,
$matcher
);

// Dummy contact
$contact = new Lead();

// Build event
$event = new ContactFiltersEvaluateEvent($filters, $contact);

// Execute subscriber
$subscriber->evaluateFilters($event);

// --- Expect the filter to be normalized ---
// Use reflection to call normalizeCustomObjectFilters() directly
$method = new \ReflectionMethod(DynamicContentSubscriber::class, 'normalizeCustomObjectFilters');
$method->setAccessible(true);

$normalized = $method->invoke($subscriber, $filters);

$this->assertSame(
'AL',
$normalized[0]['filter_value'],
'Label "Albania" was not normalized to "AL"'
);
}
}