[TASK] Make TYPO3 Core PSR-2 standard compliant
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Service / MarkerBasedTemplateServiceTest.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\Service\MarkerBasedTemplateService;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Unit test for marker utility
22 */
23 class MarkerBasedTemplateServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
24 {
25 /**
26 * @var MarkerBasedTemplateService
27 */
28 protected $templateService;
29
30 protected function setUp()
31 {
32 $this->templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
33 }
34
35 /**
36 * Data provider for getSubpart
37 *
38 * @return array
39 */
40 public function getSubpartDataProvider()
41 {
42 return array(
43 'No start marker' => array(
44 '<body>text</body>',
45 '###SUBPART###',
46 ''
47 ),
48 'No stop marker' => array(
49 '<body>
50 <!-- ###SUBPART### Start -->
51 text
52 </body>',
53 '###SUBPART###',
54 ''
55 ),
56 'Start and stop marker in HTML comment' => array(
57 '<body>
58 <!-- ###SUBPART### Start -->
59 text
60 <!-- ###SUBPART### End -->
61 </body>',
62 '###SUBPART###',
63 '
64 text
65 '
66 ),
67 'Stop marker in HTML comment' => array(
68 '<body>
69 ###SUBPART###
70 text
71 <!-- ###SUBPART### End -->
72 </body>',
73 '###SUBPART###',
74 '
75 text
76 '
77 ),
78 'Start marker in HTML comment' => array(
79 '<body>
80 <!-- ###SUBPART### Start -->
81 text
82 ###SUBPART###
83 </body>',
84 '###SUBPART###',
85 '
86 text
87 '
88 ),
89 'Start and stop marker direct' => array(
90 '<body>
91 ###SUBPART###
92 text
93 ###SUBPART###
94 </body>',
95 '###SUBPART###',
96 '
97 text
98 '
99 ),
100 );
101 }
102
103 /**
104 * @test
105 * @param string $content
106 * @param string $marker
107 * @param string $expected
108 * @dataProvider getSubpartDataProvider
109 */
110 public function getSubpart($content, $marker, $expected)
111 {
112 $this->assertSame($expected, $this->templateService->getSubpart($content, $marker));
113 }
114
115 /**
116 * Data provider for substituteSubpart
117 *
118 * @return array
119 */
120 public function substituteSubpartDataProvider()
121 {
122 return array(
123 'No start marker' => array(
124 '<body>text</body>',
125 '###SUBPART###',
126 'hello',
127 false,
128 false,
129 '<body>text</body>'
130 ),
131 'No stop marker' => array(
132 '<body>
133 <!-- ###SUBPART### Start -->
134 text
135 </body>',
136 '###SUBPART###',
137 'hello',
138 false,
139 false,
140 '<body>
141 <!-- ###SUBPART### Start -->
142 text
143 </body>',
144 ),
145 'Start and stop marker in HTML comment' => array(
146 '<body>
147 <!-- ###SUBPART### Start -->
148 text
149 <!-- ###SUBPART### End -->
150 </body>',
151 '###SUBPART###',
152 'hello',
153 false,
154 false,
155 '<body>
156 hello
157 </body>'
158 ),
159 'Recursive subpart' => array(
160 '<body>
161 <!-- ###SUBPART### Start -->text1<!-- ###SUBPART### End -->
162 <!-- ###SUBPART### Start -->text2<!-- ###SUBPART### End -->
163 </body>',
164 '###SUBPART###',
165 'hello',
166 true,
167 false,
168 '<body>
169 hello
170 hello
171 </body>'
172 ),
173 'Keep HTML marker' => array(
174 '<body>
175 <!-- ###SUBPART### Start -->text<!-- ###SUBPART### End -->
176 </body>',
177 '###SUBPART###',
178 'hello',
179 false,
180 true,
181 '<body>
182 <!-- ###SUBPART### Start -->hello<!-- ###SUBPART### End -->
183 </body>'
184 ),
185 'Keep HTML begin marker' => array(
186 '<body>
187 <!-- ###SUBPART### Start -->text###SUBPART###
188 </body>',
189 '###SUBPART###',
190 'hello',
191 false,
192 true,
193 '<body>
194 <!-- ###SUBPART### Start -->hello###SUBPART###
195 </body>'
196 ),
197 'Keep HTML end marker' => array(
198 '<body>
199 ###SUBPART###text<!-- ###SUBPART### End -->
200 </body>',
201 '###SUBPART###',
202 'hello',
203 false,
204 true,
205 '<body>
206 ###SUBPART###hello<!-- ###SUBPART### End -->
207 </body>'
208 ),
209 'Keep plain marker' => array(
210 '<body>
211 ###SUBPART###text###SUBPART###
212 </body>',
213 '###SUBPART###',
214 'hello',
215 false,
216 true,
217 '<body>
218 ###SUBPART###hello###SUBPART###
219 </body>'
220 ),
221 'Wrap around' => array(
222 '<body>
223 ###SUBPART###text###SUBPART###
224 </body>',
225 '###SUBPART###',
226 array('before-', '-after'),
227 false,
228 true,
229 '<body>
230 ###SUBPART###before-text-after###SUBPART###
231 </body>'
232 ),
233 );
234 }
235
236 /**
237 * @test
238 * @param string $content
239 * @param string $marker
240 * @param array $subpartContent
241 * @param bool $recursive
242 * @param bool $keepMarker
243 * @param string $expected
244 * @dataProvider substituteSubpartDataProvider
245 */
246 public function substituteSubpart($content, $marker, $subpartContent, $recursive, $keepMarker, $expected)
247 {
248 $this->assertSame($expected, $this->templateService->substituteSubpart($content, $marker, $subpartContent, $recursive, $keepMarker));
249 }
250
251 /**
252 * Data provider for substituteMarkerArray
253 */
254 public function substituteMarkerArrayDataProvider()
255 {
256 return array(
257 'Upper case marker' => array(
258 'This is ###MARKER1### and this is ###MARKER2###',
259 array('###MARKER1###' => 'marker 1',
260 '###MARKER2###' => 'marker 2'),
261 '',
262 false,
263 false,
264 'This is marker 1 and this is marker 2'
265 ),
266 'Lower case marker' => array(
267 'This is ###MARKER1### and this is ###MARKER2###',
268 array('###marker1###' => 'marker 1',
269 '###marker2###' => 'marker 2'),
270 '',
271 true,
272 false,
273 'This is marker 1 and this is marker 2'
274 ),
275 'Upper case marker without hash mark' => array(
276 'This is ###MARKER1### and this is ###MARKER2###',
277 array('MARKER1' => 'marker 1',
278 'MARKER2' => 'marker 2'),
279 '###|###',
280 false,
281 false,
282 'This is marker 1 and this is marker 2'
283 ),
284 'Upper case marker with another hash mark' => array(
285 'This is *MARKER1* and this is *MARKER2*',
286 array('MARKER1' => 'marker 1',
287 'MARKER2' => 'marker 2'),
288 '*|*',
289 false,
290 false,
291 'This is marker 1 and this is marker 2'
292 ),
293 'Upper case marker with unused marker' => array(
294 'This is ###MARKER1### and this is ###MARKER2### ###UNUSED###',
295 array('###MARKER1###' => 'marker 1',
296 '###MARKER2###' => 'marker 2'),
297 '',
298 false,
299 false,
300 'This is marker 1 and this is marker 2 ###UNUSED###'
301 ),
302 'Upper case marker with unused marker deleted' => array(
303 'This is ###MARKER1### and this is ###MARKER2### ###UNUSED###',
304 array('###MARKER1###' => 'marker 1',
305 '###MARKER2###' => 'marker 2'),
306 '',
307 false,
308 true,
309 'This is marker 1 and this is marker 2 '
310 ),
311 );
312 }
313
314 /**
315 * @test
316 * @dataProvider substituteMarkerArrayDataProvider
317 * @param string $content The content stream, typically HTML template content.
318 * @param array $markContentArray The array of key/value pairs being marker/content values used in the substitution. For each element in this array the function will substitute a marker in the content stream with the content.
319 * @param string $wrap A wrap value - [part 1] | [part 2] - for the markers before substitution
320 * @param bool $uppercase If set, all marker string substitution is done with upper-case markers.
321 * @param bool $deleteUnused If set, all unused marker are deleted.
322 * @param string $expected
323 */
324 public function substituteMarkerArray($content, $markContentArray, $wrap, $uppercase, $deleteUnused, $expected)
325 {
326 $this->assertSame($expected, $this->templateService->substituteMarkerArray($content, $markContentArray, $wrap, $uppercase, $deleteUnused));
327 }
328
329 /**
330 * Data provider for substituteMarker
331 */
332 public function substituteMarkerDataProvider()
333 {
334 return array(
335 'Single marker' => array(
336 'This is a ###SAMPLE### text',
337 '###SAMPLE###',
338 'simple',
339 'This is a simple text'
340 ),
341 'Double marker' => array(
342 'This is a ###SAMPLE### text with a ###SAMPLE### content',
343 '###SAMPLE###',
344 'simple',
345 'This is a simple text with a simple content'
346 ),
347 );
348 }
349
350 /**
351 * @dataProvider substituteMarkerDataProvider
352 * @param string $content The content stream, typically HTML template content.
353 * @param string $marker The marker string, typically on the form "###[the marker string]###
354 * @param mixed $markContent The content to insert instead of the marker string found.
355 * @param string $expected The expected result of the substitution
356 */
357 public function substituteMarker($content, $marker, $markContent, $expected)
358 {
359 $this->assertSame($expected, $this->templateService->substituteMarker($content, $marker, $markContent));
360 }
361
362 /**
363 * Data provider for substituteSubpartArray
364 *
365 * @return array
366 */
367 public function substituteSubpartArrayDataProvider()
368 {
369 return array(
370 'Substitute multiple subparts at once with plain marker' => array(
371 '<body>
372 ###SUBPART1###text1###SUBPART1###
373 ###SUBPART2###text2###SUBPART2###
374 </body>',
375 array('###SUBPART1###' => 'hello',
376 '###SUBPART2###' => 'world'),
377 '<body>
378 hello
379 world
380 </body>'
381 ),
382 );
383 }
384
385 /**
386 * @test
387 * @param string $content
388 * @param array $subpartsContent
389 * @param string $expected
390 * @dataProvider substituteSubpartArrayDataProvider
391 */
392 public function substituteSubpartArray($content, array $subpartsContent, $expected)
393 {
394 $this->assertSame($expected, $this->templateService->substituteSubpartArray($content, $subpartsContent));
395 }
396
397 /**
398 * Data provider for substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray
399 *
400 * @return array
401 */
402 public function substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArrayDataProvider()
403 {
404 $template = '###SINGLEMARKER1###
405 <!-- ###FOO### begin -->
406 <!-- ###BAR### begin -->
407 ###SINGLEMARKER2###
408 <!-- ###BAR### end -->
409 <!-- ###FOOTER### begin -->
410 ###SINGLEMARKER3###
411 <!-- ###FOOTER### end -->
412 <!-- ###FOO### end -->';
413
414 $expected ='Value 1
415
416
417 Value 2.1
418
419 Value 2.2
420
421
422 Value 3.1
423
424 Value 3.2
425
426 ';
427
428 return array(
429 'Single marker' => array(
430 '###SINGLEMARKER###',
431 array(
432 '###SINGLEMARKER###' => 'Value 1'
433 ),
434 '',
435 false,
436 false,
437 'Value 1'
438 ),
439 'Subpart marker' => array(
440 $template,
441 array(
442 '###SINGLEMARKER1###' => 'Value 1',
443 '###FOO###' => array(
444 array(
445 '###BAR###' => array(
446 array(
447 '###SINGLEMARKER2###' => 'Value 2.1'
448 ),
449 array(
450 '###SINGLEMARKER2###' => 'Value 2.2'
451 )
452 ),
453 '###FOOTER###' => array(
454 array(
455 '###SINGLEMARKER3###' => 'Value 3.1'
456 ),
457 array(
458 '###SINGLEMARKER3###' => 'Value 3.2'
459 )
460 )
461 )
462 )
463 ),
464 '',
465 false,
466 false,
467 $expected
468 ),
469 'Subpart marker with wrap' => array(
470 $template,
471 array(
472 'SINGLEMARKER1' => 'Value 1',
473 'FOO' => array(
474 array(
475 'BAR' => array(
476 array(
477 'SINGLEMARKER2' => 'Value 2.1'
478 ),
479 array(
480 'SINGLEMARKER2' => 'Value 2.2'
481 )
482 ),
483 'FOOTER' => array(
484 array(
485 'SINGLEMARKER3' => 'Value 3.1'
486 ),
487 array(
488 'SINGLEMARKER3' => 'Value 3.2'
489 )
490 )
491 )
492 )
493 ),
494 '###|###',
495 false,
496 false,
497 $expected
498 ),
499 'Subpart marker with lower marker array keys' => array(
500 $template,
501 array(
502 '###singlemarker1###' => 'Value 1',
503 '###foo###' => array(
504 array(
505 '###bar###' => array(
506 array(
507 '###singlemarker2###' => 'Value 2.1'
508 ),
509 array(
510 '###singlemarker2###' => 'Value 2.2'
511 )
512 ),
513 '###footer###' => array(
514 array(
515 '###singlemarker3###' => 'Value 3.1'
516 ),
517 array(
518 '###singlemarker3###' => 'Value 3.2'
519 )
520 )
521 )
522 )
523 ),
524 '',
525 true,
526 false,
527 $expected
528 ),
529 'Subpart marker with unused markers' => array(
530 $template,
531 array(
532 '###FOO###' => array(
533 array(
534 '###BAR###' => array(
535 array(
536 '###SINGLEMARKER2###' => 'Value 2.1'
537 )
538 ),
539 '###FOOTER###' => array(
540 array(
541 '###SINGLEMARKER3###' => 'Value 3.1'
542 )
543 )
544 )
545 )
546 ),
547 '',
548 false,
549 true,
550 '
551
552
553 Value 2.1
554
555
556 Value 3.1
557
558 '
559 ),
560 'Subpart marker with empty subpart' => array(
561 $template,
562 array(
563 '###SINGLEMARKER1###' => 'Value 1',
564 '###FOO###' => array(
565 array(
566 '###BAR###' => array(
567 array(
568 '###SINGLEMARKER2###' => 'Value 2.1'
569 ),
570 array(
571 '###SINGLEMARKER2###' => 'Value 2.2'
572 )
573 ),
574 '###FOOTER###' => array()
575 )
576 )
577 ),
578 '',
579 false,
580 false,
581 'Value 1
582
583
584 Value 2.1
585
586 Value 2.2
587
588
589 '
590 )
591 );
592 }
593
594 /**
595 * @test
596 * @param string $template
597 * @param array $markersAndSubparts
598 * @param string $wrap
599 * @param bool $uppercase
600 * @param bool $deleteUnused
601 * @param string $expected
602 * @dataProvider substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArrayDataProvider
603 */
604 public function substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused, $expected)
605 {
606 $this->assertSame($expected, $this->templateService->substituteMarkerAndSubpartArrayRecursive($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused));
607 }
608 }