Skip to content

Commit

Permalink
Add missing test case
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed May 28, 2024
1 parent 94f1ff6 commit cc3d9c6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Manager/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ protected function updateFields(array $data, Form $form, string $locale): void
++$counter;
$fieldType = self::getValue($fieldData, 'type');
$fieldKey = self::getValue($fieldData, 'key');

$field = $form->getField($fieldKey);
$uniqueKey = $this->getUniqueKey($fieldType, $reservedKeys);

Expand All @@ -347,10 +348,12 @@ protected function updateFields(array $data, Form $form, string $locale): void
if (!$field) {
$field = new FormField();
$field->setKey($uniqueKey);
$reservedKeys[] = $uniqueKey;
} elseif ($field->getType() !== $fieldType || !$field->getKey()) {
$field->setKey($uniqueKey);
$reservedKeys[] = $uniqueKey;
}

if (!\in_array($field->getKey(), $reservedKeys)) {
$reservedKeys[] = $field->getKey();
}

$field->setOrder($counter);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Application/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_ENV=test
DATABASE_URL=mysql://[email protected]:3306/su_form_test?serverVersion=5.7
DATABASE_URL=mysql://root:ChangeMe@127.0.0.1:3306/su_form_test?serverVersion=5.7
DATABASE_CHARSET=utf8mb4
DATABASE_COLLATE=utf8mb4_unicode_ci
73 changes: 73 additions & 0 deletions Tests/Functional/Controller/FormControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,79 @@ public function testPutFull(): void
$this->assertFullForm($response);
}

public function testPutDuplicatedField(): void
{
$form = $this->createFullForm();
$this->client->request(
'PUT',
'/admin/api/forms/' . $form->getId(),
[
'locale' => 'en',
'title' => 'Title',
'toEmail' => '[email protected]',
'fromEmail' => '[email protected]',
'fields' => [
[
'key' => 'email',
'type' => 'email',
'title' => 'Title',
'shortTitle' => 'Short Title',
'placeholder' => 'Placeholder',
'defaultValue' => 'Default Value',
'width' => 'full',
'required' => true,
],
[
'key' => 'email', // we are testing here if second email field is correctly interpreted as email1
'type' => 'email',
'title' => 'Title',
'shortTitle' => 'Short Title',
'placeholder' => 'Placeholder',
'defaultValue' => 'Default Value',
'width' => 'full',
'required' => true,
],
[
'key' => 'email', // we are testing here if second email field is correctly created as email2
'type' => 'email',
'title' => 'Title',
'shortTitle' => 'Short Title',
'placeholder' => 'Placeholder',
'defaultValue' => 'Default Value',
'width' => 'full',
'required' => true,
],
],
]
);

$this->assertHttpStatusCode(200, $this->client->getResponse());
$response = \json_decode($this->client->getResponse()->getContent(), true);

$respondedFields = [];
foreach ($response['fields'] ?? [] as $field) {
$respondedFields[] = [
'key' => $field['key'],
'type' => $field['type'],
];
}

$this->assertSame([
[
'key' => 'email',
'type' => 'email',
],
[
'key' => 'email1',
'type' => 'email',
],
[
'key' => 'email2',
'type' => 'email',
],
], $respondedFields);
}

public function testPutNotExist(): void
{
$this->client->request(
Expand Down

0 comments on commit cc3d9c6

Please sign in to comment.