[BUGFIX] Do not execute getDateTimeFormats for each input 20/35320/4
authorStephan Großberndt <stephan@grossberndt.de>
Wed, 10 Dec 2014 16:58:29 +0000 (17:58 +0100)
committerMarkus Klein <klein.t3@reelworx.at>
Thu, 11 Dec 2014 21:59:37 +0000 (22:59 +0100)
Fetches the dateTimeFormats only for date- and datetime-fields instead
of unconditionally for each input.

Resolves: #63745
Releases: master, 6.2
Change-Id: I18fcdb8a991ebb82891d9da06a4ef2b01fbc104f
Reviewed-on: http://review.typo3.org/35320
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/core/Classes/DataHandling/DataHandler.php
typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php

index 6eff43a..5bf2781 100644 (file)
@@ -1651,8 +1651,10 @@ class DataHandler {
        public function checkValue_input($res, $value, $tcaFieldConf, $PP, $field = '') {
                list($table, $id, $curValue, $status, $realPid, $recFID) = $PP;
                // Handle native date/time fields
-               $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
+               $isDateOrDateTimeField = FALSE;
                if (isset($tcaFieldConf['dbType']) && GeneralUtility::inList('date,datetime', $tcaFieldConf['dbType'])) {
+                       $isDateOrDateTimeField = TRUE;
+                       $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
                        // Convert the date/time into a timestamp for the sake of the checks
                        $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
                        $format = $dateTimeFormats[$tcaFieldConf['dbType']]['format'];
@@ -1688,10 +1690,8 @@ class DataHandler {
                        }
                }
                // Handle native date/time fields
-               if (isset($tcaFieldConf['dbType']) && GeneralUtility::inList('date,datetime', $tcaFieldConf['dbType'])) {
+               if ($isDateOrDateTimeField) {
                        // Convert the timestamp back to a date/time
-                       $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
-                       $format = $dateTimeFormats[$tcaFieldConf['dbType']]['format'];
                        $res['value'] = $res['value'] ? date($format, $res['value']) : $emptyValue;
                }
                return $res;
index 3337b96..20641a9 100644 (file)
@@ -37,10 +37,17 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        protected $backEndUser;
 
+       /**
+        * @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $mockDatabaseConnection;
+
        public function setUp() {
                $GLOBALS['TCA'] = array();
                $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
                $this->backEndUser = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
+               $this->mockDatabaseConnection = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
+               $GLOBALS['TYPO3_DB'] = $this->mockDatabaseConnection;
                $this->subject = new \TYPO3\CMS\Core\DataHandling\DataHandler();
                $this->subject->start(array(), '', $this->backEndUser);
        }
@@ -179,6 +186,64 @@ 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 inputValueCheckCallsGetDateTimeFormatsForDatetimeFields($dbType) {
+               $tcaFieldConf = array(
+                       'input' => array(),
+                       'dbType' => $dbType
+               );
+               $this->mockDatabaseConnection->expects($this->once())->method('getDateTimeFormats');
+               $this->subject->checkValue_input(array(), '', $tcaFieldConf, array());
+       }
+
+       /**
+        * @return array
+        */
+       public function inputValueCheckDoesNotCallGetDateTimeFormatsForNonDatetimeFieldsDataProvider() {
+               return array(
+                       'tca without dbType' => array(
+                               array(
+                                       'input' => array()
+                               )
+                       ),
+                       'tca with dbType != date/datetime' => array(
+                               array(
+                                       'input' => array(),
+                                       'dbType' => 'foo'
+                               )
+                       )
+               );
+       }
+
+       /**
+        * @test
+        * @param array $tcaFieldConf
+        * @dataProvider inputValueCheckDoesNotCallGetDateTimeFormatsForNonDatetimeFieldsDataProvider
+        */
+       public function inputValueCheckDoesNotCallGetDateTimeFormatsForNonDatetimeFields($tcaFieldConf) {
+               $this->mockDatabaseConnection->expects($this->never())->method('getDateTimeFormats');
+               $this->subject->checkValue_input(array(), '', $tcaFieldConf, array());
+       }
+
+
        ///////////////////////////////////////////
        // Tests concerning checkModifyAccessList
        ///////////////////////////////////////////
@@ -187,7 +252,7 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * Tests whether a wrong interface on the 'checkModifyAccessList' hook throws an exception.
         *
         * @test
-        * @expectedException UnexpectedValueException
+        * @expectedException \UnexpectedValueException
         */
        public function doesCheckModifyAccessListThrowExceptionOnWrongHookInterface() {
                $hookClass = uniqid('tx_coretest');