[BUGFIX] Show correct label for IRRE records
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseRowDateTimeFieldsTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Prophecy\Argument;
18 use Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDateTimeFields;
20 use TYPO3\CMS\Core\Database\DatabaseConnection;
21 use TYPO3\CMS\Core\Tests\UnitTestCase;
22
23 /**
24 * Test case
25 */
26 class DatabaseRowDateTimeFieldsTest extends UnitTestCase
27 {
28 /**
29 * @var DatabaseRowDateTimeFields
30 */
31 protected $subject;
32
33 /**
34 * @var DatabaseConnection | ObjectProphecy
35 */
36 protected $dbProphecy;
37
38 /**
39 * @var array Date formats
40 */
41 protected $dateFormats = array(
42 'date' => [
43 'empty' => '0000-00-00',
44 'format' => 'Y-m-d'
45 ],
46 'datetime' => [
47 'empty' => '0000-00-00 00:00:00',
48 'format' => 'Y-m-d H:i:s'
49 ],
50 );
51
52 protected function setUp()
53 {
54 $this->subject = new DatabaseRowDateTimeFields();
55 $this->dbProphecy = $this->prophesize(DatabaseConnection::class);
56 $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
57 }
58
59 /**
60 * @test
61 */
62 public function addDataCallsDatabaseConnectionForDateTimeFormats()
63 {
64 $tableName = 'aTable';
65 $input = [
66 'tableName' => $tableName,
67 'processedTca' => [
68 'columns' => [],
69 ],
70 ];
71 $this->dbProphecy->getDateTimeFormats($tableName)->shouldBeCalled();
72 $this->subject->addData($input);
73 }
74
75 /**
76 * @test
77 */
78 public function addDataSetsTimestampZeroForDefaultDateField()
79 {
80 $input = [
81 'tableName' => 'aTable',
82 'processedTca' => [
83 'columns' => [
84 'aField' => [
85 'config' => [
86 'dbType' => 'date',
87 ],
88 ],
89 ],
90 ],
91 ];
92 $expected = $input;
93 $expected['databaseRow']['aField'] = 0;
94 $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
95 $this->assertEquals($expected, $this->subject->addData($input));
96 }
97
98 /**
99 * @test
100 */
101 public function addDataSetsTimestampZeroForDefaultDateTimeField()
102 {
103 $input = [
104 'tableName' => 'aTable',
105 'processedTca' => [
106 'columns' => [
107 'aField' => [
108 'config' => [
109 'dbType' => 'datetime',
110 ],
111 ],
112 ],
113 ],
114 ];
115 $expected = $input;
116 $expected['databaseRow']['aField'] = 0;
117 $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
118 $this->assertEquals($expected, $this->subject->addData($input));
119 }
120
121 /**
122 * @test
123 */
124 public function addDataConvertsDateStringToTimestamp()
125 {
126 $oldTimezone = date_default_timezone_get();
127 date_default_timezone_set('UTC');
128 $input = [
129 'tableName' => 'aTable',
130 'processedTca' => [
131 'columns' => [
132 'aField' => [
133 'config' => [
134 'dbType' => 'date',
135 ],
136 ],
137 ],
138 ],
139 'databaseRow' => [
140 'aField' => '2015-07-27',
141 ],
142 ];
143 $expected = $input;
144 $expected['databaseRow']['aField'] = 1437955200; // 27.07.2015 0:00 UTC
145 $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
146 $this->assertEquals($expected, $this->subject->addData($input));
147 date_default_timezone_set($oldTimezone);
148 }
149
150 /**
151 * @test
152 */
153 public function addDataConvertsDateTimeStringToTimestamp()
154 {
155 $oldTimezone = date_default_timezone_get();
156 date_default_timezone_set('UTC');
157 $input = [
158 'tableName' => 'aTable',
159 'processedTca' => [
160 'columns' => [
161 'aField' => [
162 'config' => [
163 'dbType' => 'datetime',
164 ],
165 ],
166 ],
167 ],
168 'databaseRow' => [
169 'aField' => '2015-07-27 15:25:32',
170 ],
171 ];
172 $expected = $input;
173 $expected['databaseRow']['aField'] = 1438010732; // 27.07.2015 15:25:32 UTC
174 $this->dbProphecy->getDateTimeFormats(Argument::cetera())->willReturn($this->dateFormats);
175 $this->assertEquals($expected, $this->subject->addData($input));
176 date_default_timezone_set($oldTimezone);
177 }
178 }