Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move hasEmptySchema and emptyString to DataSchema on SingleSelectField #9894

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Forms/SingleSelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ public function getSchemaStateDefaults()
{
$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;
}

Expand Down
94 changes: 94 additions & 0 deletions tests/php/Forms/DropdownFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -148,13 +149,106 @@ public function testHasEmptyDefault()
]
);

// Test that an empty option does not comes through in the markup however
$options = $this->findOptionElements($FieldWithoutEmpty->Field());

$this->assertEquals(
1,
count($options),
'As hasEmptyDefault is not provided, then no default option.'
);
}

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'];
Expand Down