mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Fix for array to string conversion in DropdownField
* Resolves #4835 * Add unit test to cover array values * Add value assertion
This commit is contained in:
parent
457931d664
commit
7d7800e5e7
@ -172,7 +172,12 @@ class DropdownField extends FormField {
|
||||
if($value) {
|
||||
$selected = ($value == $this->value);
|
||||
} else {
|
||||
// Safety check against casting arrays as strings in PHP>5.4
|
||||
if(!is_array($value) && !is_array($this->value)) {
|
||||
$selected = ($value === $this->value) || (((string) $value) === ((string) $this->value));
|
||||
} else {
|
||||
$selected = ($value === $this->value);
|
||||
}
|
||||
}
|
||||
|
||||
$this->isSelected = $selected;
|
||||
|
@ -223,6 +223,32 @@ class DropdownFieldTest extends SapphireTest {
|
||||
$this->assertEquals(count($disabledOptions), 0, 'There are no disabled options');
|
||||
}
|
||||
|
||||
/**
|
||||
* The Field() method should be able to handle arrays as values in an edge case. If it couldn't handle it then
|
||||
* this test would trigger an array to string conversion PHP notice
|
||||
*
|
||||
* @dataProvider arrayValueProvider
|
||||
*/
|
||||
public function testDropdownWithArrayValues($value) {
|
||||
$field = $this->createDropdownField();
|
||||
$field->setValue($value);
|
||||
$this->assertInstanceOf('HTMLText', $field->Field());
|
||||
$this->assertSame($value, $field->Value());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function arrayValueProvider() {
|
||||
return array(
|
||||
array(array()),
|
||||
array(array(0)),
|
||||
array(array(123)),
|
||||
array(array('string')),
|
||||
array('Regression-ish test.')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a test dropdown field, with the option to
|
||||
* set what source and blank value it should contain
|
||||
|
Loading…
Reference in New Issue
Block a user