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

Form field automatically remapping after being unmapped #13744

Merged
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
11 changes: 11 additions & 0 deletions app/bundles/FormBundle/Entity/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ public function getMappedObject(): ?string
public function setMappedObject(?string $mappedObject): void
{
$this->mappedObject = $mappedObject;
$this->resetLeadFieldIfValueIsEmpty($mappedObject);
}

public function getMappedField(): ?string
Expand All @@ -997,5 +998,15 @@ public function getMappedField(): ?string
public function setMappedField(?string $mappedField): void
{
$this->mappedField = $mappedField;
$this->resetLeadFieldIfValueIsEmpty($mappedField);
}

private function resetLeadFieldIfValueIsEmpty(?string $value): void
{
if ($value) {
return;
}

$this->leadField = null;
}
}
2 changes: 1 addition & 1 deletion app/bundles/FormBundle/Form/Type/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'tooltip' => 'mautic.form.field.help.mapped.field',
],
'required' => false,
'data' => $mappedField ?? $this->getDefaultMappedField((string) $type),
'data' => $mappedField ?? (empty($options['data']['id']) ? $this->getDefaultMappedField((string) $type) : ''),
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
<!-- end: "{{ template }}" -->
</div>

{% if ((field.showWhenValueExists is defined and false is same as field.showWhenValueExists) or field.showAfterXSubmissions) is not empty
or (field.alwaysDisplay is defined and true is same as field.alwaysDisplay)
or (field.leadField is defined and field.leadField is not empty)
or (field.conditions is defined and field.conditions is not empty)
{% if ((field.showWhenValueExists is defined and false is same as field.showWhenValueExists) or field.showAfterXSubmissions is not empty)
or (true is same as field.alwaysDisplay|default(null))
or (field.mappedField|default(null) is not empty)
or (field.conditions|default(null) is not empty)
%}
<div class="panel-footer">
{% if field.conditions.expr is defined and field.conditions.expr is not empty %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Mautic\CoreBundle\Helper\LanguageHelper;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\FormBundle\Entity\Field;
use Mautic\FormBundle\Entity\Form;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -136,4 +139,88 @@ public function testLanguageForm(): void

$filesystem->remove($languagePath);
}

public function testMappedFieldIsNotMarkedAsRemappedUponSavingTheForm(): void
{
$form = $this->createForm('Test', 'test');
$field = $this->createFormField([
'label' => 'Email',
'type' => 'email',
])->setForm($form);

// @phpstan-ignore-next-line (using the deprecated method on purpose)
$field->setLeadField('email');
$this->em->persist($field);
$this->em->flush();
$this->em->clear();

$crawler = $this->client->request('GET', sprintf('/s/forms/edit/%d', $form->getId()));
$this->assertTrue($this->client->getResponse()->isOk());

$formElement = $crawler->filterXPath('//form[@name="mauticform"]')->form();
$this->client->submit($formElement);
$response = $this->client->getResponse();
$this->assertTrue($response->isOk());
$this->assertStringNotContainsString('contact: Email', $response->getContent(), 'Email field should not be marked as mapped.');
}

public function testMappedFieldIsNotAutoFilledWhenUpdatingField(): void
{
$form = $this->createForm('Test', 'test');
$field = $this->createFormField([
'label' => 'Email',
'type' => 'email',
])->setForm($form);
$field->setMappedObject(null);
$field->setMappedField(null);
$this->em->persist($field);
$this->em->flush();
$this->em->clear();

$crawler = $this->client->request('GET', sprintf('/s/forms/edit/%d', $form->getId()));
$this->assertTrue($this->client->getResponse()->isOk());

$formElement = $crawler->filterXPath('//form[@name="mauticform"]')->form();
$this->client->submit($formElement);
$this->assertTrue($this->client->getResponse()->isOk());

$this->client->request('GET', sprintf('/s/forms/field/edit/%d?formId=%d', $field->getId(), $form->getId()), [], [], $this->createAjaxHeaders());
$response = $this->client->getResponse();
$this->assertTrue($response->isOk());
$this->assertJson($response->getContent());

$content = json_decode($response->getContent())->newContent;
$crawler = new Crawler($content, $this->client->getInternalRequest()->getUri());
$options = $crawler->filterXPath('//select[@name="formfield[mappedField]"]')->html();
$this->assertStringContainsString('<option value="email">Email</option>', $options, 'Email option should not be pre-selected.');
}

private function createForm(string $name, string $alias): Form
{
$form = new Form();
$form->setName($name);
$form->setAlias($alias);
$form->setPostActionProperty('Success');
$this->em->persist($form);

return $form;
}

/**
* @param array<string,mixed> $data
*/
private function createFormField(array $data = []): Field
{
$field = new Field();
$aliasSlug = strtolower(str_replace(' ', '_', $data['label'] ?? 'Field 1'));
$field->setLabel($data['label'] ?? 'Field 1');
$field->setAlias('field_'.$aliasSlug);
$field->setType($data['type'] ?? 'text');
$field->setMappedObject($data['mappedObject'] ?? '');
$field->setMappedField($data['mappedField'] ?? '');
$field->setConditions($data['conditions'] ?? []);
$this->em->persist($field);

return $field;
}
}
Loading