-
Notifications
You must be signed in to change notification settings - Fork 20
#12891: Fixed issue for Cuctom object for country filter #376
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
Open
dhirendramishra10
wants to merge
2
commits into
staging
Choose a base branch
from
MAUT-12891
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,17 +40,19 @@ public static function getSubscribedEvents(): array | |
|
|
||
| public function evaluateFilters(ContactFiltersEvaluateEvent $event): void | ||
| { | ||
| // 2. Normalize only Custom Object filters | ||
| $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() | ||
| )); | ||
| } | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $value = array_search($label, $options, true);
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
120
Tests/Functional/Helper/DynamicContentSubscriberTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"' | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this comment