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