From 7b179396f3f3a2fe5aa75377aa3c357df2e13b35 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez <a.fernandez@scripting-base.de> Date: Tue, 7 Nov 2017 11:48:00 +0100 Subject: [PATCH] [BUGFIX] EXT:form - Cover empty strings for "skipIfValueIsEmpty" This ensures that e.g. unchecked checkboxes are not tried to be stored in the database as empty strings causing an error on int fields. Resolves: #82938 Releases: master, 8.7 Change-Id: I6f1811b8fdf17de4c0dd1fac4dec8076600924c6 Reviewed-on: https://review.typo3.org/54577 Tested-by: TYPO3com <no-reply@typo3.com> Reviewed-by: Daniel Lorenz <daniel.lorenz@extco.de> Tested-by: Daniel Lorenz <daniel.lorenz@extco.de> Reviewed-by: Susanne Moog <susanne.moog@typo3.org> Tested-by: Susanne Moog <susanne.moog@typo3.org> --- .../Finishers/SaveToDatabaseFinisher.php | 2 +- .../Finishers/SaveToDatabaseFinisherTest.php | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php b/typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php index 03739473a65d..6f722be7e569 100644 --- a/typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php +++ b/typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php @@ -219,7 +219,7 @@ class SaveToDatabaseFinisher extends AbstractFinisher { foreach ($this->getFormValues() as $elementIdentifier => $elementValue) { if ( - $elementValue === null + ($elementValue === null || $elementValue === '') && isset($elementsConfiguration[$elementIdentifier]) && isset($elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty']) && $elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty'] === true diff --git a/typo3/sysext/form/Tests/Unit/Domain/Finishers/SaveToDatabaseFinisherTest.php b/typo3/sysext/form/Tests/Unit/Domain/Finishers/SaveToDatabaseFinisherTest.php index 6533e792bd61..b61c46db294f 100644 --- a/typo3/sysext/form/Tests/Unit/Domain/Finishers/SaveToDatabaseFinisherTest.php +++ b/typo3/sysext/form/Tests/Unit/Domain/Finishers/SaveToDatabaseFinisherTest.php @@ -89,6 +89,64 @@ class SaveToDatabaseFinisherTest extends \TYPO3\TestingFramework\Core\Unit\UnitT $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal()); } + /** + * @return array + */ + public function skipIfValueIsEmptyDataProvider() + { + return [ + 'null value' => [ + 'value' => null, + 'expectedEmpty' => true, + ], + 'empty string' => [ + 'value' => '', + 'expectedEmpty' => true, + ], + 'false value' => [ + 'value' => false, + 'expectedEmpty' => false, + ], + 'space character' => [ + 'value' => ' ', + 'expectedEmpty' => false, + ], + 'zero' => [ + 'value' => 0, + 'expectedEmpty' => false, + ], + 'zero float' => [ + 'value' => 0.0, + 'expectedEmpty' => false, + ], + ]; + } + + /** + * @test + * @dataProvider skipIfValueIsEmptyDataProvider + * @param mixed $value + * @param bool $expectedEmpty + */ + public function skipIfValueIsEmptyDetectsEmptyValues($value, bool $expectedEmpty) + { + $elementsConfiguration = [ + 'foo' => [ + 'mapOnDatabaseColumn' => 'bar', + 'skipIfValueIsEmpty' => true, + ] + ]; + + $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']); + $saveToDatabaseFinisher->method('getFormValues')->willReturn([ + 'foo' => $value + ]); + $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal()); + $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []); + + self:self::assertSame($expectedEmpty, empty($databaseData)); + } + /** * @test */ -- GitLab