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