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