Merge pull request #5488 from robbieaverill/bugfix/4835-array-to-string

Fix for array to string conversion in DropdownField
This commit is contained in:
Daniel Hensby 2016-05-13 13:54:04 +01:00
commit 8ebdedf330
2 changed files with 32 additions and 1 deletions

View File

@ -172,7 +172,12 @@ class DropdownField extends FormField {
if($value) {
$selected = ($value == $this->value);
} else {
$selected = ($value === $this->value) || (((string) $value) === ((string) $this->value));
// 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;

View File

@ -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