[TASK] Make Tests/Unit/TypoScript/ notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / TypoScript / TypoScriptServiceTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\TypoScript;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\TypoScript\TypoScriptService;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class TypoScriptServiceTest extends UnitTestCase
26 {
27 /**
28 * data provider for convertTypoScriptArrayToPlainArray
29 * @return array
30 */
31 public function convertTypoScriptArrayToPlainArrayTestdata(): array
32 {
33 return [
34 'simple typoscript array' => [
35 'typoScriptSettings' => [
36 '10.' => [
37 'value' => 'Hello World!',
38 'foo.' => [
39 'bar' => 5
40 ]
41 ],
42 '10' => 'TEXT'
43 ],
44 'expectedSettings' => [
45 '10' => [
46 'value' => 'Hello World!',
47 'foo' => [
48 'bar' => 5
49 ],
50 '_typoScriptNodeValue' => 'TEXT'
51 ]
52 ]
53 ],
54 'typoscript with intermediate dots' => [
55 'typoScriptSettings' => [
56 '10.' => [
57 'value' => 'Hello World!',
58 'foo.' => [
59 'bar' => 5
60 ]
61 ],
62 '10' => 'TEXT'
63 ],
64 'expectedSettings' => [
65 '10' => [
66 'value' => 'Hello World!',
67 'foo' => [
68 'bar' => 5
69 ],
70 '_typoScriptNodeValue' => 'TEXT'
71 ]
72 ]
73 ],
74 'typoscript array with changed order' => [
75 'typoScriptSettings' => [
76 '10' => 'TEXT',
77 '10.' => [
78 'value' => 'Hello World!',
79 'foo.' => [
80 'bar' => 5
81 ]
82 ]
83 ],
84 'expectedSettings' => [
85 '10' => [
86 'value' => 'Hello World!',
87 'foo' => [
88 'bar' => 5
89 ],
90 '_typoScriptNodeValue' => 'TEXT'
91 ]
92 ]
93 ],
94 'nested typoscript array' => [
95 'typoScriptSettings' => [
96 '10' => 'COA',
97 '10.' => [
98 '10' => 'TEXT',
99 '10.' => [
100 'value' => 'Hello World!',
101 'foo.' => [
102 'bar' => 5
103 ]
104 ],
105 '20' => 'COA',
106 '20.' => [
107 '10' => 'TEXT',
108 '10.' => [
109 'value' => 'Test',
110 'wrap' => '[|]'
111 ],
112 '20' => 'TEXT',
113 '20.' => [
114 'value' => 'Test',
115 'wrap' => '[|]'
116 ]
117 ],
118 '30' => 'custom'
119 ]
120 ],
121 'expectedSettings' => [
122 '10' => [
123 '10' => [
124 'value' => 'Hello World!',
125 'foo' => [
126 'bar' => 5
127 ],
128 '_typoScriptNodeValue' => 'TEXT'
129 ],
130 '20' => [
131 '10' => [
132 'value' => 'Test',
133 'wrap' => '[|]',
134 '_typoScriptNodeValue' => 'TEXT'
135 ],
136 '20' => [
137 'value' => 'Test',
138 'wrap' => '[|]',
139 '_typoScriptNodeValue' => 'TEXT'
140 ],
141 '_typoScriptNodeValue' => 'COA'
142 ],
143 '30' => 'custom',
144 '_typoScriptNodeValue' => 'COA'
145 ]
146 ]
147 ],
148 ];
149 }
150
151 /**
152 * @test
153 * @dataProvider convertTypoScriptArrayToPlainArrayTestdata
154 * @param mixed $typoScriptSettings
155 * @param mixed $expectedSettings
156 */
157 public function convertTypoScriptArrayToPlainArrayRemovesTrailingDotsWithChangedOrderInTheTypoScriptArray(
158 $typoScriptSettings,
159 $expectedSettings
160 ): void {
161 $typoScriptService = new TypoScriptService();
162 $processedSettings = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptSettings);
163 $this->assertEquals($expectedSettings, $processedSettings);
164 }
165
166 /**
167 * Dataprovider for testcase "convertPlainArrayToTypoScriptArray"
168 *
169 * @return array
170 */
171 public function convertPlainArrayToTypoScriptArrayTestdata(): array
172 {
173 return [
174 'simple typoscript' => [
175 'extbaseTS' => [
176 '10' => [
177 'value' => 'Hallo',
178 '_typoScriptNodeValue' => 'TEXT'
179 ]
180 ],
181 'classic' => [
182 '10' => 'TEXT',
183 '10.' => [
184 'value' => 'Hallo'
185 ]
186 ]
187 ],
188 'typoscript with null value' => [
189 'extbaseTS' => [
190 '10' => [
191 'value' => 'Hallo',
192 '_typoScriptNodeValue' => 'TEXT'
193 ],
194 '20' => null
195 ],
196 'classic' => [
197 '10' => 'TEXT',
198 '10.' => [
199 'value' => 'Hallo'
200 ],
201 '20' => ''
202 ]
203 ],
204 'ts with dots in key' => [
205 'extbaseTS' => [
206 '1.0' => [
207 'value' => 'Hallo',
208 '_typoScriptNodeValue' => 'TEXT'
209 ]
210 ],
211 'classic' => [
212 '1.0' => 'TEXT',
213 '1.0.' => [
214 'value' => 'Hallo'
215 ]
216 ]
217 ],
218 'ts with backslashes in key' => [
219 'extbaseTS' => [
220 '1\\0\\' => [
221 'value' => 'Hallo',
222 '_typoScriptNodeValue' => 'TEXT'
223 ]
224 ],
225 'classic' => [
226 '1\\0\\' => 'TEXT',
227 '1\\0\\.' => [
228 'value' => 'Hallo'
229 ]
230 ]
231 ],
232 'bigger typoscript' => [
233 'extbaseTS' => [
234 '10' => [
235 '10' => [
236 'value' => 'Hello World!',
237 'foo' => [
238 'bar' => 5
239 ],
240 '_typoScriptNodeValue' => 'TEXT'
241 ],
242 '20' => [
243 '10' => [
244 'value' => 'Test',
245 'wrap' => '[|]',
246 '_typoScriptNodeValue' => 'TEXT'
247 ],
248 '20' => [
249 'value' => 'Test',
250 'wrap' => '[|]',
251 '_typoScriptNodeValue' => 'TEXT'
252 ],
253 '_typoScriptNodeValue' => 'COA'
254 ],
255 '_typoScriptNodeValue' => 'COA'
256 ]
257 ],
258 'classic' => [
259 '10' => 'COA',
260 '10.' => [
261 '10' => 'TEXT',
262 '10.' => [
263 'value' => 'Hello World!',
264 'foo.' => [
265 'bar' => 5
266 ]
267 ],
268 '20' => 'COA',
269 '20.' => [
270 '10' => 'TEXT',
271 '10.' => [
272 'value' => 'Test',
273 'wrap' => '[|]'
274 ],
275 '20' => 'TEXT',
276 '20.' => [
277 'value' => 'Test',
278 'wrap' => '[|]'
279 ]
280 ]
281 ]
282 ]
283 ],
284 ];
285 }
286
287 /**
288 * @test
289 * @dataProvider convertPlainArrayToTypoScriptArrayTestdata
290 * @param mixed $extbaseTS
291 * @param mixed $classic
292 */
293 public function convertPlainArrayToTypoScriptArray($extbaseTS, $classic): void
294 {
295 $typoScriptService = new TypoScriptService();
296 $converted = $typoScriptService->convertPlainArrayToTypoScriptArray($extbaseTS);
297 $this->assertEquals($converted, $classic);
298 }
299
300 /**
301 * @return array
302 */
303 public function explodeConfigurationForOptionSplitProvider(): array
304 {
305 return [
306 [
307 ['splitConfiguration' => 'a'],
308 3,
309 [
310 0 => ['splitConfiguration' => 'a'],
311 1 => ['splitConfiguration' => 'a'],
312 2 => ['splitConfiguration' => 'a']
313 ]
314 ],
315 [
316 ['splitConfiguration' => 'a || b || c'],
317 5,
318 [
319 0 => ['splitConfiguration' => 'a'],
320 1 => ['splitConfiguration' => 'b'],
321 2 => ['splitConfiguration' => 'c'],
322 3 => ['splitConfiguration' => 'c'],
323 4 => ['splitConfiguration' => 'c']
324 ]
325 ],
326 [
327 ['splitConfiguration' => 'a || b |*| c'],
328 5,
329 [
330 0 => ['splitConfiguration' => 'a'],
331 1 => ['splitConfiguration' => 'b'],
332 2 => ['splitConfiguration' => 'c'],
333 3 => ['splitConfiguration' => 'c'],
334 4 => ['splitConfiguration' => 'c']
335 ]
336 ],
337 [
338 ['splitConfiguration' => 'a || b |*| c |*| d || e'],
339 7,
340 [
341 0 => ['splitConfiguration' => 'a'],
342 1 => ['splitConfiguration' => 'b'],
343 2 => ['splitConfiguration' => 'c'],
344 3 => ['splitConfiguration' => 'c'],
345 4 => ['splitConfiguration' => 'c'],
346 5 => ['splitConfiguration' => 'd'],
347 6 => ['splitConfiguration' => 'e']
348 ]
349 ],
350 [
351 ['splitConfiguration' => 'a || b |*| c |*| d || e'],
352 4,
353 [
354 0 => ['splitConfiguration' => 'a'],
355 1 => ['splitConfiguration' => 'b'],
356 2 => ['splitConfiguration' => 'd'],
357 3 => ['splitConfiguration' => 'e']
358 ]
359 ],
360 [
361 ['splitConfiguration' => 'a || b |*| c |*| d || e'],
362 3,
363 [
364 0 => ['splitConfiguration' => 'a'],
365 1 => ['splitConfiguration' => 'd'],
366 2 => ['splitConfiguration' => 'e']
367 ]
368 ],
369 [
370 ['splitConfiguration' => 'a || b |*||*| c || d'],
371 7,
372 [
373 0 => ['splitConfiguration' => 'a'],
374 1 => ['splitConfiguration' => 'b'],
375 2 => ['splitConfiguration' => 'b'],
376 3 => ['splitConfiguration' => 'b'],
377 4 => ['splitConfiguration' => 'b'],
378 5 => ['splitConfiguration' => 'c'],
379 6 => ['splitConfiguration' => 'd']
380 ]
381 ],
382 [
383 ['splitConfiguration' => '|*||*| a || b'],
384 7,
385 [
386 0 => ['splitConfiguration' => 'a'],
387 1 => ['splitConfiguration' => 'a'],
388 2 => ['splitConfiguration' => 'a'],
389 3 => ['splitConfiguration' => 'a'],
390 4 => ['splitConfiguration' => 'a'],
391 5 => ['splitConfiguration' => 'a'],
392 6 => ['splitConfiguration' => 'b']
393 ]
394 ],
395 [
396 ['splitConfiguration' => 'a |*| b || c |*|'],
397 7,
398 [
399 0 => ['splitConfiguration' => 'a'],
400 1 => ['splitConfiguration' => 'b'],
401 2 => ['splitConfiguration' => 'c'],
402 3 => ['splitConfiguration' => 'b'],
403 4 => ['splitConfiguration' => 'c'],
404 5 => ['splitConfiguration' => 'b'],
405 6 => ['splitConfiguration' => 'c']
406 ]
407 ],
408 ];
409 }
410
411 /**
412 * @test
413 * @dataProvider explodeConfigurationForOptionSplitProvider
414 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ObjectsAndProperties/Index.html#objects-optionsplit
415 */
416 public function explodeConfigurationForOptionSplitTest($configuration, $splitCount, $expected): void
417 {
418 $serviceObject = new TypoScriptService();
419 $actual = $serviceObject->explodeConfigurationForOptionSplit($configuration, $splitCount);
420 $this->assertSame($expected, $actual);
421 }
422 }