[BUGFIX] Respect DateTimeImmutable in Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / Generic / Mapper / DataMapperTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence\Generic\Mapper;
3
4 use ExtbaseTeam\BlogExample\Domain\Model\Comment;
5 use ExtbaseTeam\BlogExample\Domain\Model\DateExample;
6 use ExtbaseTeam\BlogExample\Domain\Model\DateTimeImmutableExample;
7 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
8 use TYPO3\CMS\Core\Utility\GeneralUtility;
9
10 class DataMapperTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
11 {
12 /**
13 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
14 */
15 protected $persistenceManager;
16
17 /**
18 * @var array
19 */
20 protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
21
22 /**
23 * @var array
24 */
25 protected $coreExtensionsToLoad = ['extbase', 'fluid'];
26
27 /**
28 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
29 */
30 protected $objectManager;
31
32 /**
33 * Sets up this test suite.
34 */
35 protected function setUp()
36 {
37 parent::setUp();
38
39 $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
40 $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
41
42 $GLOBALS['BE_USER'] = new BackendUserAuthentication();
43 }
44
45 /**
46 * @test
47 */
48 public function datetimeObjectsCanBePersistedToDatetimeDatabaseFields()
49 {
50 $date = new \DateTime('2016-03-06T12:40:00+01:00');
51 $comment = new Comment();
52 $comment->setDate($date);
53
54 $this->persistenceManager->add($comment);
55 $this->persistenceManager->persistAll();
56 $uid = $this->persistenceManager->getIdentifierByObject($comment);
57 $this->persistenceManager->clearState();
58
59 /** @var Comment $existingComment */
60 $existingComment = $this->persistenceManager->getObjectByIdentifier($uid, Comment::class);
61
62 $this->assertEquals($date->getTimestamp(), $existingComment->getDate()->getTimestamp());
63 }
64
65 /**
66 * @test
67 */
68 public function dateValuesAreStoredInUtcInIntegerDatabaseFields()
69 {
70 $example = new DateExample();
71 $date = new \DateTime('2016-03-06T12:40:00+01:00');
72 $example->setDatetimeInt($date);
73
74 $this->persistenceManager->add($example);
75 $this->persistenceManager->persistAll();
76 $uid = $this->persistenceManager->getIdentifierByObject($example);
77 $this->persistenceManager->clearState();
78
79 /** @var DateExample $example */
80 $example = $this->persistenceManager->getObjectByIdentifier($uid, DateExample::class);
81
82 $this->assertEquals($example->getDatetimeInt()->getTimestamp(), $date->getTimestamp());
83 }
84
85 /**
86 * @test
87 */
88 public function dateValuesAreStoredInUtcInTextDatabaseFields()
89 {
90 $example = new DateExample();
91 $date = new \DateTime('2016-03-06T12:40:00+01:00');
92 $example->setDatetimeText($date);
93
94 $this->persistenceManager->add($example);
95 $this->persistenceManager->persistAll();
96 $uid = $this->persistenceManager->getIdentifierByObject($example);
97 $this->persistenceManager->clearState();
98
99 /** @var DateExample $example */
100 $example = $this->persistenceManager->getObjectByIdentifier($uid, DateExample::class);
101
102 $this->assertEquals($example->getDatetimeText()->getTimestamp(), $date->getTimestamp());
103 }
104
105 /**
106 * @test
107 */
108 public function dateValuesAreStoredInUtcInDatetimeDatabaseFields()
109 {
110 $example = new DateExample();
111 $date = new \DateTime('2016-03-06T12:40:00+01:00');
112 $example->setDatetimeDatetime($date);
113
114 $this->persistenceManager->add($example);
115 $this->persistenceManager->persistAll();
116 $uid = $this->persistenceManager->getIdentifierByObject($example);
117 $this->persistenceManager->clearState();
118
119 /** @var DateExample $example */
120 $example = $this->persistenceManager->getObjectByIdentifier($uid, DateExample::class);
121
122 $this->assertEquals($example->getDatetimeDatetime()->getTimestamp(), $date->getTimestamp());
123 }
124
125 /**
126 * @test
127 */
128 public function dateTimeImmutableIntIsHandledAsDateTime()
129 {
130 $subject = new DateTimeImmutableExample();
131 $date = new \DateTimeImmutable('2018-07-24T20:40:00');
132 $subject->setDatetimeImmutableInt($date);
133
134 $this->persistenceManager->add($subject);
135 $this->persistenceManager->persistAll();
136 $uid = $this->persistenceManager->getIdentifierByObject($subject);
137 $this->persistenceManager->clearState();
138
139 /** @var DateTimeImmutableExample $subject */
140 $subject = $this->persistenceManager->getObjectByIdentifier($uid, DateTimeImmutableExample::class);
141
142 $this->assertEquals($date, $subject->getDatetimeImmutableInt());
143 }
144
145 /**
146 * @test
147 */
148 public function dateTimeImmutableTextIsHandledAsDateTime()
149 {
150 $subject = new DateTimeImmutableExample();
151 $date = new \DateTimeImmutable('2018-07-24T20:40:00');
152 $subject->setDatetimeImmutableText($date);
153
154 $this->persistenceManager->add($subject);
155 $this->persistenceManager->persistAll();
156 $uid = $this->persistenceManager->getIdentifierByObject($subject);
157 $this->persistenceManager->clearState();
158
159 /** @var DateTimeImmutableExample $subject */
160 $subject = $this->persistenceManager->getObjectByIdentifier($uid, DateTimeImmutableExample::class);
161
162 $this->assertEquals($date, $subject->getDatetimeImmutableText());
163 }
164
165 /**
166 * @test
167 */
168 public function dateTimeImmutableDateTimeIsHandledAsDateTime()
169 {
170 $subject = new DateTimeImmutableExample();
171 $date = new \DateTimeImmutable('2018-07-24T20:40:00');
172 $subject->setDatetimeImmutableDatetime($date);
173
174 $this->persistenceManager->add($subject);
175 $this->persistenceManager->persistAll();
176 $uid = $this->persistenceManager->getIdentifierByObject($subject);
177 $this->persistenceManager->clearState();
178
179 /** @var DateTimeImmutableExample $subject */
180 $subject = $this->persistenceManager->getObjectByIdentifier($uid, DateTimeImmutableExample::class);
181
182 $this->assertSame($date->getTimestamp(), $subject->getDatetimeImmutableDatetime()->getTimestamp());
183 }
184 }