[BUGFIX] Apply default values in DataHandler functional tests 98/42598/5
authorOliver Hader <oliver@typo3.org>
Thu, 13 Aug 2015 17:30:42 +0000 (19:30 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Mon, 17 Aug 2015 11:36:29 +0000 (13:36 +0200)
If MySQL is used in strict mode, setting up the functional
testing scenario requires proper field values for those
fields not having a default value defined in the schema.
For some field types, like BLOB or TEXT this is not possible
and thus has to be determined from the accordant TCA field
configurations.

Resolves: #69030
Releases: master
Change-Id: Ie915fdaca6c90440259e01896677a2d2395417f4
Reviewed-on: http://review.typo3.org/42598
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
typo3/sysext/core/Tests/Functional/DataHandling/AbstractDataHandlerActionTestCase.php
typo3/sysext/core/Tests/Functional/DataHandling/Framework/DataSet.php

index 5e77d63..372c7c5 100644 (file)
@@ -105,7 +105,7 @@ abstract class AbstractDataHandlerActionTestCase extends \TYPO3\CMS\Core\Tests\F
                $fileName = rtrim($this->scenarioDataSetDirectory, '/') . '/' . $dataSetName . '.csv';
                $fileName = GeneralUtility::getFileAbsFileName($fileName);
 
-               $dataSet = DataSet::read($fileName);
+               $dataSet = DataSet::read($fileName, TRUE);
 
                foreach ($dataSet->getTableNames() as $tableName) {
                        foreach ($dataSet->getElements($tableName) as $element) {
index 0b28f07..34888f5 100644 (file)
@@ -28,11 +28,16 @@ class DataSet {
 
        /**
         * @param string $fileName
+        * @param bool $applyDefaultValues
         * @return DataSet
         */
-       static public function read($fileName) {
+       static public function read($fileName, $applyDefaultValues = FALSE) {
                $data = self::parseData(self::readData($fileName));
 
+               if ($applyDefaultValues) {
+                       $data = self::applyDefaultValues($data);
+               }
+
                return GeneralUtility::makeInstance(
                        \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\DataSet::class,
                        $data
@@ -128,6 +133,44 @@ class DataSet {
        }
 
        /**
+        * Applies TCA default values to missing fields on the imported scenario data-set.
+        * This is basically required for running the functional tests in a SQL strict mode environment.
+        *
+        * @param array $data
+        * @return array
+        */
+       static protected function applyDefaultValues(array $data) {
+               foreach ($data as $tableName => $sections) {
+                       if (empty($GLOBALS['TCA'][$tableName]['columns'])) {
+                               continue;
+                       }
+
+                       $fields = $sections['fields'];
+
+                       foreach ($GLOBALS['TCA'][$tableName]['columns'] as $tcaFieldName => $tcaFieldConfiguration) {
+                               // Skip if field was already imported
+                               if (in_array($tcaFieldName, $fields)) {
+                                       continue;
+                               }
+                               // Skip if field is an enable-column (it's expected that those fields have proper DBMS defaults)
+                               if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns']) && in_array($tcaFieldName, $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'])) {
+                                       continue;
+                               }
+                               // Skip if no default value is defined in the accordant TCA definition (NULL values might occur as well)
+                               if (empty($tcaFieldConfiguration['config']) || !array_key_exists('default', $tcaFieldConfiguration['config'])) {
+                                       continue;
+                               }
+
+                               $data[$tableName]['fields'][] = $tcaFieldName;
+                               foreach ($data[$tableName]['elements'] as &$element) {
+                                       $element[$tcaFieldName] = $tcaFieldConfiguration['config']['default'];
+                               }
+                       }
+               }
+               return $data;
+       }
+
+       /**
         * @param array $data
         */
        public function __construct(array $data) {