From f37abe45a958eaec0a881ad7d01401774b9c2119 Mon Sep 17 00:00:00 2001 From: Morton Jonuschat <m.jonuschat@mojocode.de> Date: Sat, 20 Aug 2016 15:15:20 +0200 Subject: [PATCH] [TASK] Doctrine: Migrate DatabaseConnection::getDateTimeFormats() 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> --- .../DatabaseRowDateTimeFields.php | 12 +--- .../Classes/Utility/BackendUtility.php | 5 +- .../DatabaseRowDateTimeFieldsTest.php | 62 ++----------------- .../core/Classes/DataHandling/DataHandler.php | 5 +- .../Classes/Database/DatabaseConnection.php | 19 +----- .../Classes/Database/Query/QueryHelper.php | 21 +++++++ .../Unit/DataHandling/DataHandlerTest.php | 55 ---------------- 7 files changed, 34 insertions(+), 145 deletions(-) diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowDateTimeFields.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowDateTimeFields.php index d2bc5ffe6fc8..4d09920d2713 100644 --- a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowDateTimeFields.php +++ b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowDateTimeFields.php @@ -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']; - } } diff --git a/typo3/sysext/backend/Classes/Utility/BackendUtility.php b/typo3/sysext/backend/Classes/Utility/BackendUtility.php index 4b0420aed119..cdcb32319147 100644 --- a/typo3/sysext/backend/Classes/Utility/BackendUtility.php +++ b/typo3/sysext/backend/Classes/Utility/BackendUtility.php @@ -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; } diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowDateTimeFieldsTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowDateTimeFieldsTest.php index ed398e6a8384..3b01e99983ec 100644 --- a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowDateTimeFieldsTest.php +++ b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowDateTimeFieldsTest.php @@ -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; /** @@ -25,53 +22,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 */ @@ -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); } } diff --git a/typo3/sysext/core/Classes/DataHandling/DataHandler.php b/typo3/sysext/core/Classes/DataHandling/DataHandler.php index 24b33db6d169..2ef983d62ef5 100644 --- a/typo3/sysext/core/Classes/DataHandling/DataHandler.php +++ b/typo3/sysext/core/Classes/DataHandling/DataHandler.php @@ -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']; diff --git a/typo3/sysext/core/Classes/Database/DatabaseConnection.php b/typo3/sysext/core/Classes/Database/DatabaseConnection.php index cfbd7dcbc93f..daa9be739bba 100644 --- a/typo3/sysext/core/Classes/Database/DatabaseConnection.php +++ b/typo3/sysext/core/Classes/Database/DatabaseConnection.php @@ -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; @@ -169,22 +170,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 * @@ -988,7 +973,7 @@ class DatabaseConnection */ public function getDateTimeFormats($table) { - return self::$dateTimeFormats; + return QueryHelper::getDateTimeFormats(); } /** diff --git a/typo3/sysext/core/Classes/Database/Query/QueryHelper.php b/typo3/sysext/core/Classes/Database/Query/QueryHelper.php index 15fc4c5a2e60..291e23baa07a 100644 --- a/typo3/sysext/core/Classes/Database/Query/QueryHelper.php +++ b/typo3/sysext/core/Classes/Database/Query/QueryHelper.php @@ -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' + ] + ]; + } } diff --git a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php index a4e9f96284cf..5850b8587b30 100644 --- a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php +++ b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php @@ -248,61 +248,6 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase $this->assertSame($returnValue['value'], $expectedReturnValue); } - /** - * @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 */ -- GitLab