From ef60ae1c07c291801612e6e26c7d963b508340ac Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Thu, 4 Oct 2018 15:00:19 +0200 Subject: [PATCH 1/2] UI: Only accept known options in Select Field --- .../Component/Input/Field/Select.php | 4 +-- tests/UI/Component/Input/Field/SelectTest.php | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/UI/Component/Input/Field/SelectTest.php diff --git a/src/UI/Implementation/Component/Input/Field/Select.php b/src/UI/Implementation/Component/Input/Field/Select.php index fce63121dd5c..1b1ec95f9721 100644 --- a/src/UI/Implementation/Component/Input/Field/Select.php +++ b/src/UI/Implementation/Component/Input/Field/Select.php @@ -52,7 +52,7 @@ public function getOptions() * @inheritdoc */ protected function isClientSideValueOk($value) { - return is_string($value); + return in_array($value, array_keys($this->options)); } /** @@ -61,4 +61,4 @@ protected function isClientSideValueOk($value) { protected function getConstraintForRequirement() { return $this->validation_factory->hasMinLength(1); } -} \ No newline at end of file +} diff --git a/tests/UI/Component/Input/Field/SelectTest.php b/tests/UI/Component/Input/Field/SelectTest.php new file mode 100644 index 000000000000..d9a2fb05df99 --- /dev/null +++ b/tests/UI/Component/Input/Field/SelectTest.php @@ -0,0 +1,30 @@ + Extended GPL, see docs/LICENSE */ + +require_once(__DIR__ . "/../../../Base.php"); + +class SelectForTest extends ILIAS\UI\Implementation\Component\Input\Field\Select { + public function _isClientSideValueOk($value) { + return $this->isClientSideValueOk($value); + } +} + +class SelectInputTest extends ILIAS_UI_TestBase { + public function testOnlyValuesFromOptionsAreAcceptableClientSideValues() { + $options = ["one" => "Eins", "two" => "Zwei", "three" => "Drei"]; + $select = new SelectForTest( + $this->createMock(ILIAS\Data\Factory::class), + $this->createMock(ILIAS\Validation\Factory::class), + $this->createMock(ILIAS\Transformation\Factory::class), + "", + $options, + "" + ); + + $this->assertTrue($select->_isClientSideValueOk("one")); + $this->assertTrue($select->_isClientSideValueOk("two")); + $this->assertTrue($select->_isClientSideValueOk("three")); + $this->assertFalse($select->_isClientSideValueOk("four")); + } +} From 7673af158b7c097e12727db7e98efcc45b0dc1ec Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Thu, 4 Oct 2018 15:11:38 +0200 Subject: [PATCH 2/2] UI: respect isRequired in Selects client side values If a select is not required, also an empty string is a valid option. --- .../Component/Input/Field/Select.php | 4 ++- tests/UI/Component/Input/Field/SelectTest.php | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/UI/Implementation/Component/Input/Field/Select.php b/src/UI/Implementation/Component/Input/Field/Select.php index 1b1ec95f9721..2ee8a935df4e 100644 --- a/src/UI/Implementation/Component/Input/Field/Select.php +++ b/src/UI/Implementation/Component/Input/Field/Select.php @@ -52,7 +52,9 @@ public function getOptions() * @inheritdoc */ protected function isClientSideValueOk($value) { - return in_array($value, array_keys($this->options)); + return + in_array($value, array_keys($this->options)) + || (!$this->isRequired() && $value == ""); } /** diff --git a/tests/UI/Component/Input/Field/SelectTest.php b/tests/UI/Component/Input/Field/SelectTest.php index d9a2fb05df99..31b4ffed3438 100644 --- a/tests/UI/Component/Input/Field/SelectTest.php +++ b/tests/UI/Component/Input/Field/SelectTest.php @@ -27,4 +27,32 @@ public function testOnlyValuesFromOptionsAreAcceptableClientSideValues() { $this->assertTrue($select->_isClientSideValueOk("three")); $this->assertFalse($select->_isClientSideValueOk("four")); } + + public function testEmptyStringIsAcceptableClientSideValueIfSelectIsNotRequired() { + $options = []; + $select = new SelectForTest( + $this->createMock(ILIAS\Data\Factory::class), + $this->createMock(ILIAS\Validation\Factory::class), + $this->createMock(ILIAS\Transformation\Factory::class), + "", + $options, + "" + ); + + $this->assertTrue($select->_isClientSideValueOk("")); + } + + public function testEmptyStringIsNoAcceptableClientSideValueIfSelectIsRequired() { + $options = []; + $select = (new SelectForTest( + $this->createMock(ILIAS\Data\Factory::class), + $this->createMock(ILIAS\Validation\Factory::class), + $this->createMock(ILIAS\Transformation\Factory::class), + "", + $options, + "" + ))->withRequired(true); + + $this->assertFalse($select->_isClientSideValueOk("")); + } }