Skip to content

Commit

Permalink
MAUT-11616 : Fix operators do not refresh after selecting a different…
Browse files Browse the repository at this point in the history
… type of field in campaign event (#356)

* fix Operators do not refresh after selecting a different type of field

* csfix

* adding test which verify the data-operators attr is available

* fix phpstan

* fix tests

* suggessions
  • Loading branch information
rahuld-dev authored Jul 11, 2024
1 parent 9a9ebd3 commit 089ab33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
24 changes: 11 additions & 13 deletions Form/Type/CampaignConditionFieldValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ public function __construct(
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$fields = $this->customFieldModel->fetchCustomFieldsForObject($options['customObject']);
$choices = [];
$fields = $this->customFieldModel->fetchCustomFieldsForObject($options['customObject']);
$choices = [];
$optionAttr = [];

foreach ($fields as $field) {
$choices[$field->getLabel()] = $field->getId();
$choices[$field->getLabel()] = $field->getId();
$optionAttr[$field->getLabel()] = [
'data-operators' => json_encode($field->getTypeObject()->getOperatorOptions()),
'data-options' => json_encode($field->getChoices()),
'data-field-type' => $field->getType(),
];
}

$builder->add(
Expand All @@ -62,16 +69,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'attr' => [
'class' => 'form-control',
],
'choice_attr' => array_map(
function ($field) {
return [
'data-operators' => json_encode($field->getTypeObject()->getOperatorOptions()),
'data-options' => json_encode($field->getChoices()),
'data-field-type' => $field->getType(),
];
},
$fields
),
'choice_attr' => $optionAttr,
]
);

Expand Down
31 changes: 31 additions & 0 deletions Tests/Functional/EventListener/CampaignConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,35 @@ public function testConditionForm(): void
Assert::assertSame('=', $body['event']['properties']['properties']['operator']);
Assert::assertSame('unicorn', $body['event']['properties']['properties']['value']);
}

public function testVerifyDataOperatorAttrIsAvailableForFields(): void
{
$customObject = $this->createCustomObjectWithAllFields(self::$container, 'Campaign test object');
$crawler = $this->client->request(
Request::METHOD_GET,
's/campaigns/events/new',
[
'campaignId' => 'mautic_041b2a401f680fb0b644654af5ba0892f31f0697',
'type' => "custom_item.{$customObject->getId()}.fieldvalue",
'eventType' => 'condition',
'anchor' => 'leadsource',
'anchorEventType' => 'source',
],
[],
$this->createAjaxHeaders()
);

Assert::assertTrue($this->client->getResponse()->isOk(), $this->client->getResponse()->getContent());

$html = json_decode($this->client->getResponse()->getContent(), true)['newContent'];

$crawler->addHtmlContent($html);

$options = $crawler->filter('#campaignevent_properties_field')->filter('option'); // ->attr('data-operators');

/** @var \DOMElement $option */
foreach ($options as $option) {
Assert::assertNotEmpty($option->getAttribute('data-operators'));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testBuildForm(): void
'class' => 'form-control',
],
'choice_attr' => [
42 => [
'Cheese' => [
'data-operators' => '{"=":"a","!=":"b"}',
'data-options' => '[]',
'data-field-type' => 'int',
Expand Down

0 comments on commit 089ab33

Please sign in to comment.