[BUGFIX] EXT:form - Cover empty strings for "skipIfValueIsEmpty" 71/54971/2
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Tue, 7 Nov 2017 10:48:00 +0000 (11:48 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Thu, 7 Dec 2017 21:07:48 +0000 (22:07 +0100)
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/54971
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php
typo3/sysext/form/Tests/Unit/Domain/Finishers/SaveToDatabaseFinisherTest.php

index 0373947..6f722be 100644 (file)
@@ -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
index 6533e79..b61c46d 100644 (file)
@@ -90,6 +90,64 @@ class SaveToDatabaseFinisherTest extends \TYPO3\TestingFramework\Core\Unit\UnitT
     }
 
     /**
+     * @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
      */
     public function executeInternalProcessesMultipleTables()