mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Move hasEmptySchem and emptyString to DataSchema on SingleSelectField
This commit is contained in:
parent
47855e3e49
commit
600f8e5b86
@ -34,12 +34,19 @@ abstract class SingleSelectField extends SelectField
|
||||
{
|
||||
$data = parent::getSchemaStateDefaults();
|
||||
|
||||
$data['value'] = $this->getDefaultValue();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getSchemaDataDefaults()
|
||||
{
|
||||
$data = parent::getSchemaDataDefaults();
|
||||
|
||||
// Add options to 'data'
|
||||
$data['data']['hasEmptyDefault'] = $this->getHasEmptyDefault();
|
||||
$data['data']['emptyString'] = $this->getHasEmptyDefault() ? $this->getEmptyString() : null;
|
||||
|
||||
$data['value'] = $this->getDefaultValue();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ use SilverStripe\Dev\CSSContentParser;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\DropdownField;
|
||||
use SilverStripe\Forms\RequiredFields;
|
||||
use SilverStripe\Forms\FormTemplateHelper;
|
||||
use SilverStripe\Forms\FieldList;
|
||||
use SilverStripe\Forms\Form;
|
||||
use SilverStripe\View\ArrayData;
|
||||
@ -148,6 +149,9 @@ class DropdownFieldTest extends SapphireTest
|
||||
]
|
||||
);
|
||||
|
||||
// Test that an empty option does not comes through in the markup however
|
||||
$options = $this->findOptionElements($FieldWithoutEmpty->Field());
|
||||
|
||||
$this->assertEquals(
|
||||
1,
|
||||
count($options),
|
||||
@ -155,6 +159,96 @@ class DropdownFieldTest extends SapphireTest
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmpty()
|
||||
{
|
||||
$fieldName = 'TestField';
|
||||
$formName = 'testForm';
|
||||
// Create mock form
|
||||
$form = $this->createMock(Form::class);
|
||||
$form->method('getTemplateHelper')
|
||||
->willReturn(FormTemplateHelper::singleton());
|
||||
|
||||
$form->method('getHTMLID')
|
||||
->willReturn($formName);
|
||||
|
||||
$source = [
|
||||
'first' => 'value',
|
||||
0 => 'otherValue'
|
||||
];
|
||||
$field = new DropdownField($fieldName, 'Test Field', $source);
|
||||
$field->setForm($form);
|
||||
|
||||
$fieldId = $field->ID();
|
||||
$this->assertEquals($fieldId, sprintf('%s_%s', $formName, $fieldName));
|
||||
|
||||
// Check state for default value
|
||||
$schemaStateDefaults = $field->getSchemaStateDefaults();
|
||||
$this->assertArraySubset(
|
||||
[
|
||||
'id' => $fieldId,
|
||||
'name' => $fieldName,
|
||||
'value' => 'first'
|
||||
],
|
||||
$schemaStateDefaults,
|
||||
true
|
||||
);
|
||||
|
||||
// Check data for empty defaults
|
||||
$schemaDataDefaults = $field->getSchemaDataDefaults();
|
||||
$this->assertArraySubset(
|
||||
[
|
||||
'id' => $fieldId,
|
||||
'name' => $fieldName,
|
||||
'type' => 'text',
|
||||
'schemaType' => 'SingleSelect',
|
||||
'holderId' => sprintf('%s_Holder', $fieldId),
|
||||
'title' => 'Test Field',
|
||||
'extraClass' => 'dropdown',
|
||||
'data' => [
|
||||
'emptyString' => null,
|
||||
'hasEmptyDefault' => false
|
||||
]
|
||||
],
|
||||
$schemaDataDefaults,
|
||||
true
|
||||
);
|
||||
|
||||
// Set an empty string of field
|
||||
$field->setEmptyString('(Any)');
|
||||
|
||||
// Check state for default value
|
||||
$schemaStateDefaults = $field->getSchemaStateDefaults();
|
||||
$this->assertArraySubset(
|
||||
[
|
||||
'id' => $fieldId,
|
||||
'name' => $fieldName,
|
||||
'value' => ''
|
||||
],
|
||||
$schemaStateDefaults,
|
||||
true
|
||||
);
|
||||
|
||||
// Check data for empty defaults
|
||||
$schemaDataDefaults = $field->getSchemaDataDefaults();
|
||||
$this->assertArraySubset(
|
||||
[
|
||||
'id' => $fieldId,
|
||||
'name' => $fieldName,
|
||||
'type' => 'text',
|
||||
'schemaType' => 'SingleSelect',
|
||||
'holderId' => sprintf('%s_Holder', $fieldId),
|
||||
'title' => 'Test Field',
|
||||
'extraClass' => 'dropdown',
|
||||
'data' => [
|
||||
'emptyString' => '(Any)',
|
||||
'hasEmptyDefault' => true
|
||||
]
|
||||
],
|
||||
$schemaDataDefaults,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public function testZeroArraySourceNotOverwrittenByEmptyString()
|
||||
{
|
||||
$source = [0=>'zero'];
|
||||
|
Loading…
Reference in New Issue
Block a user