Commit f37abe45 authored by Morton Jonuschat's avatar Morton Jonuschat Committed by Christian Kuhn
Browse files

[TASK] Doctrine: Migrate DatabaseConnection::getDateTimeFormats()

Change-Id: I3ef2494a3347ee04ad3cf03192f8f85f27cc8e9d
Resolves: #77556
Releases: master
Reviewed-on: https://review.typo3.org/49518


Tested-by: default avatarBamboo TYPO3com <info@typo3.com>
Reviewed-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
parent d254f834
......@@ -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'];
}
}
......@@ -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;
}
......
......@@ -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);
}
}
......@@ -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'];
......
......@@ -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();
}
/**
......
......@@ -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'
]
];
}
}
......@@ -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
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment