diff --git a/typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php b/typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php index 03739473a65d9ed0aee497b4beff68decb863602..6f722be7e569b6da4f0ba18aaf78713eca86a655 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 6533e792bd615383301fc0b180cd20db3bf3182b..b61c46db294f92019a55c2d10b02dec475fa8516 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 */