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:
Robbie Averill 2016-05-10 10:27:36 +12:00
parent 457931d664
commit 7d7800e5e7
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 {
// 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