[TASK] Doctrine: Migrate DatabaseConnection::getDateTimeFormats() 18/49518/5
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Sat, 20 Aug 2016 13:15:20 +0000 (15:15 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 20 Aug 2016 14:18:04 +0000 (16:18 +0200)
Change-Id: I3ef2494a3347ee04ad3cf03192f8f85f27cc8e9d
Resolves: #77556
Releases: master
Reviewed-on: https://review.typo3.org/49518
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowDateTimeFields.php
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowDateTimeFieldsTest.php
typo3/sysext/core/Classes/DataHandling/DataHandler.php
typo3/sysext/core/Classes/Database/DatabaseConnection.php
typo3/sysext/core/Classes/Database/Query/QueryHelper.php
typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php

index d2bc5ff..4d09920 100644 (file)
@@ -15,7 +15,7 @@ namespace TYPO3\CMS\Backend\Form\FormDataProvider;
  */
 
 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
-use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Database\Query\QueryHelper;
 
 /**
  * Migrate date and datetime db field values to timestamp
@@ -30,7 +30,7 @@ class DatabaseRowDateTimeFields implements FormDataProviderInterface
      */
     public function addData(array $result)
     {
-        $dateTimeFormats = $this->getDatabase()->getDateTimeFormats($result['tableName']);
+        $dateTimeFormats = QueryHelper::getDateTimeFormats();
         foreach ($result['processedTca']['columns'] as $column => $columnConfig) {
             if (isset($columnConfig['config']['dbType'])
                 && ($columnConfig['config']['dbType'] === 'date' || $columnConfig['config']['dbType'] === 'datetime')
@@ -48,12 +48,4 @@ class DatabaseRowDateTimeFields implements FormDataProviderInterface
         }
         return $result;
     }
-
-    /**
-     * @return DatabaseConnection
-     */
-    protected function getDatabase()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
 }
index 4b0420a..cdcb323 100644 (file)
@@ -2238,7 +2238,6 @@ class BackendUtility
             }
         }
         $l = '';
-        $db = static::getDatabaseConnection();
         $lang = static::getLanguageService();
         switch ((string)$theColConf['type']) {
             case 'radio':
@@ -2497,7 +2496,7 @@ class BackendUtility
                     if (GeneralUtility::inList($theColConf['eval'], 'date')) {
                         // Handle native date field
                         if (isset($theColConf['dbType']) && $theColConf['dbType'] === 'date') {
-                            $dateTimeFormats = $db->getDateTimeFormats($table);
+                            $dateTimeFormats = QueryHelper::getDateTimeFormats();
                             $emptyValue = $dateTimeFormats['date']['empty'];
                             $value = $value !== $emptyValue ? strtotime($value) : 0;
                         }
@@ -2532,7 +2531,7 @@ class BackendUtility
                     } elseif (GeneralUtility::inList($theColConf['eval'], 'datetime')) {
                         // Handle native date/time field
                         if (isset($theColConf['dbType']) && $theColConf['dbType'] === 'datetime') {
-                            $dateTimeFormats = $db->getDateTimeFormats($table);
+                            $dateTimeFormats = QueryHelper::getDateTimeFormats();
                             $emptyValue = $dateTimeFormats['datetime']['empty'];
                             $value = $value !== $emptyValue ? strtotime($value) : 0;
                         }
index ed398e6..3b01e99 100644 (file)
@@ -14,10 +14,7 @@ namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
  * The TYPO3 project - inspiring people to share!
  */
 
-use Prophecy\Argument;
-use Prophecy\Prophecy\ObjectProphecy;
 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDateTimeFields;
-use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Tests\UnitTestCase;
 
 /**
@@ -26,53 +23,6 @@ use TYPO3\CMS\Core\Tests\UnitTestCase;
 class DatabaseRowDateTimeFieldsTest extends UnitTestCase
 {
     /**
-     * @var DatabaseRowDateTimeFields
-     */
-    protected $subject;
-
-    /**
-     * @var DatabaseConnection | ObjectProphecy
-     */
-    protected $dbProphecy;
-
-    /**
-     * @var array Date formats
-     */
-    protected $dateFormats = array(
-        'date' => [
-            'empty' => '0000-00-00',
-            'format' => 'Y-m-d'
-        ],
-        'datetime' => [
-            'empty' => '0000-00-00 00:00:00',
-            'format' => 'Y-m-d H:i:s'
-        ],
-    );
-
-    protected function setUp()
-    {
-        $this->subject = new DatabaseRowDateTimeFields();
-        $this->dbProphecy = $this->prophesize(DatabaseConnection::class);
-        $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
-    }
-
-    /**
-     * @test
-     */
-    public function addDataCallsDatabaseConnectionForDateTimeFormats()
-    {
-        $tableName = 'aTable';
-        $input = [
-            'tableName' => $tableName,
-            'processedTca' => [
-                'columns' => [],
-            ],
-        ];
-        $this->dbProphecy->getDateTimeFormats($tableName)->shouldBeCalled();
-        $this->subject->addData($input);
-    }
-
-    /**
      * @test
      */
     public function addDataSetsTimestampZeroForDefaultDateField()
@@ -91,8 +41,7 @@ class DatabaseRowDateTimeFieldsTest extends UnitTestCase
         ];
         $expected = $input;
         $expected['databaseRow']['aField'] = 0;
-        $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
-        $this->assertEquals($expected, $this->subject->addData($input));
+        $this->assertEquals($expected, (new DatabaseRowDateTimeFields())->addData($input));
     }
 
     /**
@@ -114,8 +63,7 @@ class DatabaseRowDateTimeFieldsTest extends UnitTestCase
         ];
         $expected = $input;
         $expected['databaseRow']['aField'] = 0;
-        $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
-        $this->assertEquals($expected, $this->subject->addData($input));
+        $this->assertEquals($expected, (new DatabaseRowDateTimeFields())->addData($input));
     }
 
     /**
@@ -142,8 +90,7 @@ class DatabaseRowDateTimeFieldsTest extends UnitTestCase
         ];
         $expected = $input;
         $expected['databaseRow']['aField'] = 1437955200; // 27.07.2015 0:00 UTC
-        $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
-        $this->assertEquals($expected, $this->subject->addData($input));
+        $this->assertEquals($expected, (new DatabaseRowDateTimeFields())->addData($input));
         date_default_timezone_set($oldTimezone);
     }
 
@@ -171,8 +118,7 @@ class DatabaseRowDateTimeFieldsTest extends UnitTestCase
         ];
         $expected = $input;
         $expected['databaseRow']['aField'] = 1438010732; // 27.07.2015 15:25:32 UTC
-        $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
-        $this->assertEquals($expected, $this->subject->addData($input));
+        $this->assertEquals($expected, (new DatabaseRowDateTimeFields())->addData($input));
         date_default_timezone_set($oldTimezone);
     }
 }
index 24b33db..2ef983d 100644 (file)
@@ -25,6 +25,7 @@ use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
+use TYPO3\CMS\Core\Database\Query\QueryHelper;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
 use TYPO3\CMS\Core\Database\ReferenceIndex;
@@ -995,7 +996,7 @@ class DataHandler
                 $theRealPid = null;
 
                 // Handle native date/time fields
-                $dateTimeFormats = $this->databaseConnection->getDateTimeFormats($table);
+                $dateTimeFormats = QueryHelper::getDateTimeFormats();
                 foreach ($GLOBALS['TCA'][$table]['columns'] as $column => $config) {
                     if (isset($incomingFieldArray[$column])) {
                         if (isset($config['config']['dbType']) && ($config['config']['dbType'] === 'date' || $config['config']['dbType'] === 'datetime')) {
@@ -1763,7 +1764,7 @@ class DataHandler
                 $value = 0;
             } else {
                 $isDateOrDateTimeField = true;
-                $dateTimeFormats = $this->databaseConnection->getDateTimeFormats($table);
+                $dateTimeFormats = QueryHelper::getDateTimeFormats();
                 // Convert the date/time into a timestamp for the sake of the checks
                 $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
                 $format = $dateTimeFormats[$tcaFieldConf['dbType']]['format'];
index cfbd7dc..daa9be7 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Core\Database;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Database\Query\QueryHelper;
 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\StringUtility;
@@ -170,22 +171,6 @@ class DatabaseConnection
     protected $postProcessHookObjects = array();
 
     /**
-     * the date and time formats compatible with the database in general
-     *
-     * @var array
-     */
-    protected static $dateTimeFormats = array(
-        'date' => array(
-            'empty' => '0000-00-00',
-            'format' => 'Y-m-d'
-        ),
-        'datetime' => array(
-            'empty' => '0000-00-00 00:00:00',
-            'format' => 'Y-m-d H:i:s'
-        )
-    );
-
-    /**
      * Initialize the database connection
      *
      * @return void
@@ -988,7 +973,7 @@ class DatabaseConnection
      */
     public function getDateTimeFormats($table)
     {
-        return self::$dateTimeFormats;
+        return QueryHelper::getDateTimeFormats();
     }
 
     /**
index 15fc4c5..291e23b 100644 (file)
@@ -114,4 +114,25 @@ class QueryHelper
     {
         return preg_replace('/^(?:(AND|OR)[[:space:]]*)+/i', '', trim($constraint)) ?: '';
     }
+
+    /**
+     * Returns the date and time formats compatible with the given database.
+     *
+     * This simple method should probably be deprecated and removed later.
+     *
+     * @return array
+     */
+    public static function getDateTimeFormats()
+    {
+        return [
+            'date' => [
+                'empty' => '0000-00-00',
+                'format' => 'Y-m-d'
+            ],
+            'datetime' => [
+                'empty' => '0000-00-00 00:00:00',
+                'format' => 'Y-m-d H:i:s'
+            ]
+        ];
+    }
 }
index a4e9f96..5850b85 100644 (file)
@@ -251,61 +251,6 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     /**
      * @return array
      */
-    public function inputValueCheckCallsGetDateTimeFormatsForDatetimeFieldsDataProvider()
-    {
-        return array(
-            'dbType = date' => array(
-                'date'
-            ),
-            'dbType = datetime' => array(
-                'datetime'
-            )
-        );
-    }
-
-    /**
-     * @test
-     * @dataProvider inputValueCheckCallsGetDateTimeFormatsForDatetimeFieldsDataProvider
-     * @param string $dbType
-     */
-    public function inputValueCheckCallsNotGetDateTimeFormatsForDatetimeFieldsWithEmptyValue($dbType)
-    {
-        $tcaFieldConf = array(
-            'input' => array(),
-            'dbType' => $dbType
-        );
-        $this->mockDatabaseConnection->expects($this->never())->method('getDateTimeFormats');
-        $this->subject->_call('checkValueForInput', '', $tcaFieldConf, '', 0, 0, '');
-    }
-
-    /**
-     * @test
-     * @dataProvider inputValueCheckCallsGetDateTimeFormatsForDatetimeFieldsDataProvider
-     * @param string $dbType
-     */
-    public function inputValueCheckCallsGetDateTimeFormatsForDatetimeFieldsWithNonEmptyValue($dbType)
-    {
-        $dateTimeFormats = [
-            'date' => array(
-                'empty' => '0000-00-00',
-                'format' => 'Y-m-d'
-            ),
-            'datetime' => array(
-                'empty' => '0000-00-00 00:00:00',
-                'format' => 'Y-m-d H:i:s'
-            )
-        ];
-        $tcaFieldConf = array(
-            'input' => array(),
-            'dbType' => $dbType
-        );
-        $this->mockDatabaseConnection->expects($this->once())->method('getDateTimeFormats')->willReturn($dateTimeFormats);
-        $this->subject->_call('checkValueForInput', $dateTimeFormats[$dbType]['empty'], $tcaFieldConf, '', 0, 0, '');
-    }
-
-    /**
-     * @return array
-     */
     public function inputValueCheckDoesNotCallGetDateTimeFormatsForNonDatetimeFieldsDataProvider()
     {
         return array(