[TASK] Add tests for Extbase UTC date/datetime handling 43/49743/2
authorAndreas Wolf <andreas.wolf@typo3.org>
Sun, 6 Mar 2016 13:03:00 +0000 (14:03 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Fri, 2 Sep 2016 09:43:50 +0000 (11:43 +0200)
The actual bug has been fixed a while ago with commit
827219a1c35b4aca6dbab5855a36e9277b2ec8f4, but the tests I wrote somehow
got lost. This patch adds them back and shows that the problem has
actually been fixed.

Change-Id: Ibca2524d25573d67b3541761c8cf2b662fcd3423
Related: #72878
Releases: master, 7.6
(cherry picked from commit e5f27f56e1420e3d3c2dee7e0782a4c116f39779)
Reviewed-on: https://review.typo3.org/49743
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php
typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Classes/Domain/Model/DateExample.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/tx_blogexample_domain_model_dateexample.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/ext_tables.sql
typo3/sysext/extbase/Tests/Functional/Persistence/Generic/Mapper/DataMapperTest.php

index c3d0a7c..fa7939c 100644 (file)
@@ -333,6 +333,7 @@ class DataMapper implements \TYPO3\CMS\Core\SingletonInterface
             $currentTimeZone = new \DateTimeZone(date_default_timezone_get());
             return $utcDateTime->setTimezone($currentTimeZone);
         } else {
+            // integer timestamps are local server time
             return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($targetType, date('c', $value));
         }
     }
diff --git a/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Classes/Domain/Model/DateExample.php b/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Classes/Domain/Model/DateExample.php
new file mode 100644 (file)
index 0000000..780115b
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+namespace ExtbaseTeam\BlogExample\Domain\Model;
+
+use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
+
+class DateExample extends AbstractEntity
+{
+
+    /**
+     * A datetime stored in a text field
+     *
+     * @var \DateTime
+     */
+    protected $datetimeText;
+
+    /**
+     * A datetime stored in an integer field
+     *
+     * @var \DateTime
+     */
+    protected $datetimeInt;
+
+    /**
+     * A datetime stored in a datetime field
+     *
+     * @var \DateTime
+     */
+    protected $datetimeDatetime;
+
+    /**
+     * @return \DateTime
+     */
+    public function getDatetimeText()
+    {
+        return $this->datetimeText;
+    }
+
+    /**
+     * @param \DateTime $datetimeText
+     */
+    public function setDatetimeText($datetimeText)
+    {
+        $this->datetimeText = $datetimeText;
+    }
+
+    /**
+     * @return \DateTime
+     */
+    public function getDatetimeInt()
+    {
+        return $this->datetimeInt;
+    }
+
+    /**
+     * @param \DateTime $datetimeInt
+     */
+    public function setDatetimeInt($datetimeInt)
+    {
+        $this->datetimeInt = $datetimeInt;
+    }
+
+    /**
+     * @return \DateTime
+     */
+    public function getDatetimeDatetime()
+    {
+        return $this->datetimeDatetime;
+    }
+
+    /**
+     * @param \DateTime $datetimeDatetime
+     */
+    public function setDatetimeDatetime($datetimeDatetime)
+    {
+        $this->datetimeDatetime = $datetimeDatetime;
+    }
+}
diff --git a/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/tx_blogexample_domain_model_dateexample.php b/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/tx_blogexample_domain_model_dateexample.php
new file mode 100644 (file)
index 0000000..3b44e54
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+return [
+    'ctrl' => [
+        'title' => 'LLL:EXT:blog_example/Resources/Private/Language/locallang_db.xml:tx_blogexample_domain_model_dateexample',
+        'label' => 'title',
+        'tstamp' => 'tstamp',
+        'crdate' => 'crdate',
+        'delete' => 'deleted',
+        'enablecolumns' => [
+            'disabled' => 'hidden',
+            'fe_group' => 'fe_group',
+        ],
+        'iconfile' => 'EXT:blog_example/Resources/Public/Icons/icon_tx_blogexample_domain_model_dateexample.gif'
+    ],
+    'interface' => [
+        'showRecordFieldList' => 'title, posts, administrator'
+    ],
+    'columns' => [
+        'datetime_text' => [
+            'exclude' => 1,
+            'label' => 'eval=datetime, db=text',
+            'config' => [
+                'type' => 'input',
+                'eval' => 'datetime',
+            ]
+        ],
+        'datetime_int' => [
+            'exclude' => 1,
+            'label' => 'eval=datetime, db=int',
+            'config' => [
+                'type' => 'input',
+                'eval' => 'datetime',
+            ]
+        ],
+        'datetime_datetime' => [
+            'exclude' => 1,
+            'label' => 'eval=datetime, db=datetime',
+            'config' => [
+                'dbType' => 'datetime',
+                'type' => 'input',
+                'eval' => 'datetime',
+            ]
+        ]
+    ],
+    'types' => [
+        '1' => ['showitem' => 'datetime_text', 'datetime_int', 'datetime_datetime']
+    ],
+    'palettes' => [
+        '1' => ['showitem' => '']
+    ]
+];
index 0323d36..f2d8bf8 100644 (file)
@@ -199,3 +199,24 @@ CREATE TABLE tx_blogexample_post_post_mm (
        KEY uid_local (uid_local),
        KEY uid_foreign (uid_foreign)
 );
+
+#
+# Table structure for table 'tx_blogexample_domain_model_dateexample'
+#
+CREATE TABLE tx_blogexample_domain_model_dateexample (
+       uid int(11) unsigned DEFAULT '0' NOT NULL auto_increment,
+       pid int(11) DEFAULT '0' NOT NULL,
+
+       datetime_int int(11) DEFAULT '0' NOT NULL,
+       datetime_text varchar(255) DEFAULT '' NOT NULL,
+       datetime_datetime datetime,
+
+       tstamp int(11) unsigned DEFAULT '0' NOT NULL,
+       crdate int(11) unsigned DEFAULT '0' NOT NULL,
+       deleted tinyint(4) unsigned DEFAULT '0' NOT NULL,
+       hidden tinyint(4) unsigned DEFAULT '0' NOT NULL,
+       sys_language_uid int(11) DEFAULT '0' NOT NULL,
+
+       PRIMARY KEY (uid),
+       KEY parent (pid)
+);
\ No newline at end of file
index 440a2e0..8d29288 100644 (file)
@@ -2,6 +2,7 @@
 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence\Generic\Mapper;
 
 use ExtbaseTeam\BlogExample\Domain\Model\Comment;
+use ExtbaseTeam\BlogExample\Domain\Model\DateExample;
 use TYPO3\CMS\Core\Tests\FunctionalTestCase;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -58,4 +59,63 @@ class DataMapperTest extends FunctionalTestCase
 
         $this->assertEquals($date->getTimestamp(), $existingComment->getDate()->getTimestamp());
     }
+    /**
+     * @test
+     */
+    public function dateValuesAreStoredInUtcInIntegerDatabaseFields()
+    {
+        $example = new DateExample();
+        $date = new \DateTime('2016-03-06T12:40:00+01:00');
+        $example->setDatetimeInt($date);
+
+        $this->persistenceManager->add($example);
+        $this->persistenceManager->persistAll();
+        $uid = $this->persistenceManager->getIdentifierByObject($example);
+        $this->persistenceManager->clearState();
+
+        /** @var DateExample $example */
+        $example = $this->persistenceManager->getObjectByIdentifier($uid, DateExample::class);
+
+        $this->assertEquals($example->getDatetimeInt()->getTimestamp(), $date->getTimestamp());
+    }
+
+    /**
+     * @test
+     */
+    public function dateValuesAreStoredInUtcInTextDatabaseFields()
+    {
+        $example = new DateExample();
+        $date = new \DateTime('2016-03-06T12:40:00+01:00');
+        $example->setDatetimeText($date);
+
+        $this->persistenceManager->add($example);
+        $this->persistenceManager->persistAll();
+        $uid = $this->persistenceManager->getIdentifierByObject($example);
+        $this->persistenceManager->clearState();
+
+        /** @var DateExample $example */
+        $example = $this->persistenceManager->getObjectByIdentifier($uid, DateExample::class);
+
+        $this->assertEquals($example->getDatetimeText()->getTimestamp(), $date->getTimestamp());
+    }
+
+    /**
+     * @test
+     */
+    public function dateValuesAreStoredInUtcInDatetimeDatabaseFields()
+    {
+        $example = new DateExample();
+        $date = new \DateTime('2016-03-06T12:40:00+01:00');
+        $example->setDatetimeDatetime($date);
+
+        $this->persistenceManager->add($example);
+        $this->persistenceManager->persistAll();
+        $uid = $this->persistenceManager->getIdentifierByObject($example);
+        $this->persistenceManager->clearState();
+
+        /** @var DateExample $example */
+        $example = $this->persistenceManager->getObjectByIdentifier($uid, DateExample::class);
+
+        $this->assertEquals($example->getDatetimeDatetime()->getTimestamp(), $date->getTimestamp());
+    }
 }