From 600f8e5b869b4ac4e78e7948977dc31ba4b96c2c Mon Sep 17 00:00:00 2001 From: Brett Tasker Date: Tue, 23 Mar 2021 20:08:37 +1300 Subject: [PATCH] Move hasEmptySchem and emptyString to DataSchema on SingleSelectField --- src/Forms/SingleSelectField.php | 11 +++- tests/php/Forms/DropdownFieldTest.php | 94 +++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/src/Forms/SingleSelectField.php b/src/Forms/SingleSelectField.php index 81cb18fcd07..7d0a218196d 100644 --- a/src/Forms/SingleSelectField.php +++ b/src/Forms/SingleSelectField.php @@ -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; } diff --git a/tests/php/Forms/DropdownFieldTest.php b/tests/php/Forms/DropdownFieldTest.php index 5c7be0a7364..ae650066d29 100644 --- a/tests/php/Forms/DropdownFieldTest.php +++ b/tests/php/Forms/DropdownFieldTest.php @@ -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; @@ -148,6 +149,9 @@ 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), @@ -155,6 +159,96 @@ public function testHasEmptyDefault() ); } + 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'];