FIX / TreeMultiselectField::objectForKey handles list of IDs correctly

This commit is contained in:
Serge Latyntcev 2018-11-22 12:11:18 +13:00
parent 80885fc231
commit 9ce6d91b76
2 changed files with 11 additions and 1 deletions

View File

@ -279,10 +279,14 @@ class TreeMultiselectField extends TreeDropdownField
* of numeric types with string values, issuing a database error.
*
* This fix is not ideal, but supposed to keep backward compatibility for SS4.
* Since SS5 this method should be removed and NULL should be used instead of 'unchanged'.
*
* In 5.0 this method to be removed and NULL should be used instead of 'unchanged' (or an empty array. to be decided).
* In 5.0 this class to be refactored so that $this->value is always an array of values (or null)
*/
if ($this->getKeyField() === 'ID' && $key === 'unchanged') {
$key = null;
} elseif (is_string($key)) {
$key = preg_split('/\s*,\s*/', trim($key));
}
return parent::objectForKey($key);

View File

@ -20,6 +20,9 @@ class TreeMultiselectFieldTest extends SapphireTest
$schemaStateDefaults = $field->getSchemaStateDefaults();
$this->assertArraySubset(['id' => 'TestTree', 'name' => 'TestTree', 'value' => 'unchanged'], $schemaStateDefaults, $strict = true);
$items = $field->getItems();
$this->assertCount(0, $items, $message = 'there must be no items selected');
$html = $field->performReadonlyTransformation()->Field();
$this->assertEquals(
@ -45,6 +48,9 @@ HTML
$schemaStateDefaults = $field->getSchemaStateDefaults();
$this->assertArraySubset(['id' => 'TestTree', 'name' => 'TestTree', 'value' => [$asdf->ID, $subfolderfile1->ID]], $schemaStateDefaults, $strict = true);
$items = $field->getItems();
$this->assertCount(2, $items, $message = 'there must be exactly 2 items selected');
$html = (string)$field->performReadonlyTransformation()->Field();
$this->assertEquals(