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

Fix setValue on checkbox and select fields if they are multiple #526

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
3 changes: 3 additions & 0 deletions src/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public function setValue($value): void
return;
}

if ($this->selector->isMultiple()) {
$this->selector->deselectAll();
}
foreach ((array) $value as $v) {
$this->selector->selectByValue($v);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/DomCrawler/Field/ChoiceFormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ public function testGetValueFromSelectMultipleIfOneIsSelected(callable $clientFa
$this->assertSame(['20'], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
public function testSetValueFromSelectMultipleIfOneIsSelectedAfterAllHaveBeenSelectedBefore(callable $clientFactory): void
{
$crawler = $this->request($clientFactory, '/choice-form-field.html');
$form = $crawler->filter('form')->form();

$field = $form['select_multiple_selected_all'];
$this->assertInstanceOf(ChoiceFormField::class, $field);
$this->assertSame(['10', '20', '30'], $field->getValue());
$field->setValue('20');
$this->assertSame(['20'], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
Expand Down Expand Up @@ -152,6 +167,26 @@ public function testGetValueFromCheckboxIfMultipleAreChecked(callable $clientFac
$this->assertSame(['checked_one', 'checked_two'], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
public function testSetValueFromCheckboxIfOneIsCheckedAfterAllHaveBeenCheckedBefore(callable $clientFactory, string $type): void
{
$crawler = $this->request($clientFactory, '/choice-form-field.html');
$form = $crawler->filter('form')->form();

$field = $form['checkbox_multiple_checked'];

$this->assertInstanceOf(ChoiceFormField::class, $field);
// https://github.com/symfony/symfony/issues/26827
if (PantherClient::class !== $type) {
$this->markTestSkipped('The DomCrawler component doesn\'t support multiple fields with the same name');
}
$this->assertSame(['checked_one', 'checked_two'], $field->getValue());
$field->setValue('checked_two');
$this->assertSame('checked_two', $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/choice-form-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
</select>
</label>

<label class="field">
select_multiple_selected_all
<select name="select_multiple_selected_all" multiple="multiple">
<option value="10" selected>ten</option>
<option value="20" selected>twenty</option>
<option value="30" selected>thirty</option>
</select>
</label>

<label class="field">
select_multiple_selected_multiple
Expand Down