Skip to content

Commit

Permalink
CollectionTest: Add colliding element names test
Browse files Browse the repository at this point in the history
  • Loading branch information
Timm Ortloff committed Feb 9, 2023
1 parent a1c0595 commit ba1b146
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,86 @@ public function testPopulatingCollection(): void

$this->assertHtml($expected, $this->form);
}

public function testCollidingElementNames(): void
{
$addElement = new SelectElement('add_element', ['options' => ['key1' => 'value1', 'key2' => 'value2']]);

$firstCollection = (new Collection('first_collection'))->setAddElement(clone $addElement);
$secondCollection = (new Collection('second_collection')) ->setAddElement(clone $addElement);

$firstCollection->onAssembleGroup(function ($group, $addElement) {
$group->addElement($addElement);
});

$secondCollection->onAssembleGroup(function ($group, $addElement) {
$group->addElement($addElement);
});

$this->form
->registerElement($firstCollection)
->addHtml($firstCollection)
->registerElement($secondCollection)
->addHtml($secondCollection)
->addElement(new SubmitButtonElement('add_element'))
->populate([
'first_collection' => [
[
'add_element' => 'key1'
]
],
'second_collection' => [
[
'add_element' => 'key2'
],
[
'add_element' => 'key1'
]
]
]);

$expected = <<<'HTML'
<form method="POST">
<fieldset class="collection" name="first_collection">
<fieldset class="form-element-collection" name="first_collection[0]">
<select name="first_collection[0][add_element]">
<option value="key1" selected="selected">value1</option>
<option value="key2">value2</option>
</select>
</fieldset>
<fieldset class="form-element-collection" name="first_collection[1]">
<select name="first_collection[1][add_element]">
<option value="key1">value1</option>
<option value="key2">value2</option>
</select>
</fieldset>
</fieldset>
<fieldset class="collection" name="second_collection">
<fieldset class="form-element-collection" name="second_collection[0]">
<select name="second_collection[0][add_element]">
<option value="key1">value1</option>
<option value="key2" selected="selected">value2</option>
</select>
</fieldset>
<fieldset class="form-element-collection" name="second_collection[1]">
<select name="second_collection[1][add_element]">
<option value="key1" selected="selected">value1</option>
<option value="key2">value2</option>
</select>
</fieldset>
<fieldset class="form-element-collection" name="second_collection[2]">
<select name="second_collection[2][add_element]">
<option value="key1">value1</option>
<option value="key2">value2</option>
</select>
</fieldset>
</fieldset>
<div class="simple-decorator">
<button name="add_element" type="submit" value="y"/>
</div>
</form>
HTML;

$this->assertHtml($expected, $this->form);
}
}

0 comments on commit ba1b146

Please sign in to comment.