0ce65f1e627ed9145c81e3808aefe5ddc4ca5701
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / TcaFlexPrepareTest.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\TcaFlexPrepare;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Cache\CacheManager;
22 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Test case
27 */
28 class TcaFlexPrepareTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
29 {
30 /**
31 * Subject is not notice free, disable E_NOTICES
32 */
33 protected static $suppressNotices = true;
34
35 /**
36 * @var TcaFlexPrepare
37 */
38 protected $subject;
39
40 /**
41 * @var BackendUserAuthentication|ObjectProphecy
42 */
43 protected $backendUserProphecy;
44
45 /**
46 * @var array A backup of registered singleton instances
47 */
48 protected $singletonInstances = [];
49
50 protected function setUp()
51 {
52 $this->singletonInstances = GeneralUtility::getSingletonInstances();
53
54 // Suppress cache foo in xml helpers of GeneralUtility
55 /** @var CacheManager|ObjectProphecy $cacheManagerProphecy */
56 $cacheManagerProphecy = $this->prophesize(CacheManager::class);
57 GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
58 $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
59 $cacheManagerProphecy->getCache(Argument::cetera())->willReturn($cacheFrontendProphecy->reveal());
60
61 $this->subject = new TcaFlexPrepare();
62 }
63
64 protected function tearDown()
65 {
66 GeneralUtility::purgeInstances();
67 GeneralUtility::resetSingletonInstances($this->singletonInstances);
68 parent::tearDown();
69 }
70
71 /**
72 * @test
73 */
74 public function addDataKeepsExistingDataStructure()
75 {
76 $input = [
77 'systemLanguageRows' => [],
78 'tableName' => 'aTableName',
79 'databaseRow' => [
80 'aField' => [
81 'data' => [],
82 'meta' => [],
83 ],
84 ],
85 'processedTca' => [
86 'columns' => [
87 'aField' => [
88 'config' => [
89 'type' => 'flex',
90 'dataStructureIdentifier' => '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}',
91 'ds' => [
92 'sheets' => [
93 'sDEF' => [
94 'ROOT' => [
95 'type' => 'array',
96 'el' => [
97 'aFlexField' => [
98 'label' => 'aFlexFieldLabel',
99 'config' => [
100 'type' => 'input',
101 ],
102 ],
103 ],
104 ],
105 ],
106 ],
107 'meta' => [],
108 ],
109 ],
110 ],
111 ],
112 ],
113 ];
114 $expected = $input;
115 $this->assertEquals($expected, $this->subject->addData($input));
116 }
117
118 /**
119 * @test
120 */
121 public function addDataSetsParsedDataStructureArray()
122 {
123 $input = [
124 'systemLanguageRows' => [],
125 'tableName' => 'aTableName',
126 'databaseRow' => [
127 'aField' => [
128 'data' => [],
129 'meta' => [],
130 ],
131 ],
132 'processedTca' => [
133 'columns' => [
134 'aField' => [
135 'config' => [
136 'type' => 'flex',
137 'ds' => [
138 'default' => '
139 <T3DataStructure>
140 <ROOT>
141 <type>array</type>
142 <el>
143 <aFlexField>
144 <TCEforms>
145 <label>aFlexFieldLabel</label>
146 <config>
147 <type>input</type>
148 </config>
149 </TCEforms>
150 </aFlexField>
151 </el>
152 </ROOT>
153 </T3DataStructure>
154 ',
155 ],
156 ],
157 ],
158 ],
159 ],
160 ];
161
162 $GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
163
164 $expected = $input;
165 $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
166 = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
167 $expected['processedTca']['columns']['aField']['config']['ds'] = [
168 'sheets' => [
169 'sDEF' => [
170 'ROOT' => [
171 'type' => 'array',
172 'el' => [
173 'aFlexField' => [
174 'label' => 'aFlexFieldLabel',
175 'config' => [
176 'type' => 'input',
177 ],
178 ],
179 ],
180 ],
181 ],
182 ],
183 'meta' => [],
184 ];
185
186 $this->assertEquals($expected, $this->subject->addData($input));
187 }
188
189 /**
190 * @test
191 */
192 public function addDataSetsParsedDataStructureArrayWithSheets()
193 {
194 $input = [
195 'systemLanguageRows' => [],
196 'tableName' => 'aTableName',
197 'databaseRow' => [
198 'aField' => [
199 'data' => [],
200 'meta' => [],
201 ],
202 ],
203 'processedTca' => [
204 'columns' => [
205 'aField' => [
206 'config' => [
207 'type' => 'flex',
208 'ds' => [
209 'default' => '
210 <T3DataStructure>
211 <sheets>
212 <sDEF>
213 <ROOT>
214 <TCEforms>
215 <sheetTitle>aTitle</sheetTitle>
216 </TCEforms>
217 <type>array</type>
218 <el>
219 <aFlexField>
220 <TCEforms>
221 <label>aFlexFieldLabel</label>
222 <config>
223 <type>input</type>
224 </config>
225 </TCEforms>
226 </aFlexField>
227 </el>
228 </ROOT>
229 </sDEF>
230 </sheets>
231 </T3DataStructure>
232 ',
233 ],
234 ],
235 ],
236 ],
237 ],
238 ];
239
240 $GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
241
242 $expected = $input;
243 $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
244 = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
245 $expected['processedTca']['columns']['aField']['config']['ds'] = [
246 'sheets' => [
247 'sDEF' => [
248 'ROOT' => [
249 'type' => 'array',
250 'el' => [
251 'aFlexField' => [
252 'label' => 'aFlexFieldLabel',
253 'config' => [
254 'type' => 'input',
255 ],
256 ],
257 ],
258 'sheetTitle' => 'aTitle',
259 ],
260 ],
261 ],
262 'meta' => [],
263 ];
264
265 $this->assertEquals($expected, $this->subject->addData($input));
266 }
267
268 /**
269 * @test
270 */
271 public function addDataInitializesDatabaseRowValueIfNoDataStringIsGiven()
272 {
273 $input = [
274 'databaseRow' => [],
275 'tableName' => 'aTableName',
276 'systemLanguageRows' => [],
277 'processedTca' => [
278 'columns' => [
279 'aField' => [
280 'config' => [
281 'type' => 'flex',
282 'ds' => [
283 'default' => '
284 <T3DataStructure>
285 <ROOT></ROOT>
286 </T3DataStructure>
287 ',
288 ],
289 ],
290 ],
291 ],
292 ],
293 ];
294
295 $GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
296
297 $expected = $input;
298 $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
299 = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
300 $expected['processedTca']['columns']['aField']['config']['ds'] = [
301 'ROOT' => '',
302 'meta' => [],
303 ];
304 $expected['databaseRow']['aField'] = [
305 'data' => [],
306 'meta' => []
307 ];
308
309 $this->assertEquals($expected, $this->subject->addData($input));
310 }
311 }