‪TYPO3CMS  10.4
MarkerBasedTemplateServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Argument;
21 use Prophecy\Prophecy\ObjectProphecy;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪MarkerBasedTemplateServiceTest extends UnitTestCase
32 {
36  protected ‪$templateService;
37 
41  protected ‪$resetSingletonInstances = true;
42 
46  protected function ‪setUp(): void
47  {
48  parent::setUp();
50  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
51  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
52  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
53  $cacheManagerProphecy->getCache(Argument::cetera())->willReturn($cacheFrontendProphecy->reveal());
54  $this->templateService = new ‪MarkerBasedTemplateService();
55  }
56 
62  public function ‪getSubpartDataProvider(): array
63  {
64  return [
65  'No start marker' => [
66  '<body>text</body>',
67  '###SUBPART###',
68  ''
69  ],
70  'No stop marker' => [
71  '<body>
72 <!-- ###SUBPART### Start -->
73 text
74 </body>',
75  '###SUBPART###',
76  ''
77  ],
78  'Start and stop marker in HTML comment' => [
79  '<body>
80 <!-- ###SUBPART### Start -->
81 text
82 <!-- ###SUBPART### End -->
83 </body>',
84  '###SUBPART###',
85  '
86 text
87 '
88  ],
89  'Stop marker in HTML comment' => [
90  '<body>
91 ###SUBPART###
92 text
93 <!-- ###SUBPART### End -->
94 </body>',
95  '###SUBPART###',
96  '
97 text
98 '
99  ],
100  'Start marker in HTML comment' => [
101  '<body>
102 <!-- ###SUBPART### Start -->
103 text
104 ###SUBPART###
105 </body>',
106  '###SUBPART###',
107  '
108 text
109 '
110  ],
111  'Start and stop marker direct' => [
112  '<body>
113 ###SUBPART###
114 text
115 ###SUBPART###
116 </body>',
117  '###SUBPART###',
118  '
119 text
120 '
121  ],
122  ];
123  }
124 
132  public function ‪getSubpart(string $content, string $marker, string $expected): void
133  {
134  self::assertSame($expected, $this->templateService->getSubpart($content, $marker));
135  }
136 
142  public function ‪substituteSubpartDataProvider(): array
143  {
144  return [
145  'No start marker' => [
146  '<body>text</body>',
147  '###SUBPART###',
148  'hello',
149  false,
150  false,
151  '<body>text</body>'
152  ],
153  'No stop marker' => [
154  '<body>
155 <!-- ###SUBPART### Start -->
156 text
157 </body>',
158  '###SUBPART###',
159  'hello',
160  false,
161  false,
162  '<body>
163 <!-- ###SUBPART### Start -->
164 text
165 </body>',
166  ],
167  'Start and stop marker in HTML comment' => [
168  '<body>
169 <!-- ###SUBPART### Start -->
170 text
171 <!-- ###SUBPART### End -->
172 </body>',
173  '###SUBPART###',
174  'hello',
175  false,
176  false,
177  '<body>
178 hello
179 </body>'
180  ],
181  'Recursive subpart' => [
182  '<body>
183 <!-- ###SUBPART### Start -->text1<!-- ###SUBPART### End -->
184 <!-- ###SUBPART### Start -->text2<!-- ###SUBPART### End -->
185 </body>',
186  '###SUBPART###',
187  'hello',
188  true,
189  false,
190  '<body>
191 hello
192 hello
193 </body>'
194  ],
195  'Keep HTML marker' => [
196  '<body>
197 <!-- ###SUBPART### Start -->text<!-- ###SUBPART### End -->
198 </body>',
199  '###SUBPART###',
200  'hello',
201  false,
202  true,
203  '<body>
204 <!-- ###SUBPART### Start -->hello<!-- ###SUBPART### End -->
205 </body>'
206  ],
207  'Keep HTML begin marker' => [
208  '<body>
209 <!-- ###SUBPART### Start -->text###SUBPART###
210 </body>',
211  '###SUBPART###',
212  'hello',
213  false,
214  true,
215  '<body>
216 <!-- ###SUBPART### Start -->hello###SUBPART###
217 </body>'
218  ],
219  'Keep HTML end marker' => [
220  '<body>
221 ###SUBPART###text<!-- ###SUBPART### End -->
222 </body>',
223  '###SUBPART###',
224  'hello',
225  false,
226  true,
227  '<body>
228 ###SUBPART###hello<!-- ###SUBPART### End -->
229 </body>'
230  ],
231  'Keep plain marker' => [
232  '<body>
233 ###SUBPART###text###SUBPART###
234 </body>',
235  '###SUBPART###',
236  'hello',
237  false,
238  true,
239  '<body>
240 ###SUBPART###hello###SUBPART###
241 </body>'
242  ],
243  'Wrap around' => [
244  '<body>
245 ###SUBPART###text###SUBPART###
246 </body>',
247  '###SUBPART###',
248  ['before-', '-after'],
249  false,
250  true,
251  '<body>
252 ###SUBPART###before-text-after###SUBPART###
253 </body>'
254  ],
255  ];
256  }
257 
268  public function ‪substituteSubpart(
269  string $content,
270  string $marker,
271  $subpartContent,
272  bool $recursive,
273  bool $keepMarker,
274  string $expected
275  ): void {
276  self::assertSame(
277  $expected,
278  $this->templateService->substituteSubpart($content, $marker, $subpartContent, $recursive, $keepMarker)
279  );
280  }
281 
285  public function ‪substituteMarkerArrayDataProvider(): array
286  {
287  return [
288  'Upper case marker' => [
289  'This is ###MARKER1### and this is ###MARKER2###',
290  [
291  '###MARKER1###' => 'marker 1',
292  '###MARKER2###' => 'marker 2'
293  ],
294  '',
295  false,
296  false,
297  'This is marker 1 and this is marker 2'
298  ],
299  'Lower case marker' => [
300  'This is ###MARKER1### and this is ###MARKER2###',
301  [
302  '###marker1###' => 'marker 1',
303  '###marker2###' => 'marker 2'
304  ],
305  '',
306  true,
307  false,
308  'This is marker 1 and this is marker 2'
309  ],
310  'Upper case marker without hash mark' => [
311  'This is ###MARKER1### and this is ###MARKER2###',
312  [
313  'MARKER1' => 'marker 1',
314  'MARKER2' => 'marker 2'
315  ],
316  '###|###',
317  false,
318  false,
319  'This is marker 1 and this is marker 2'
320  ],
321  'Upper case marker with another hash mark' => [
322  'This is *MARKER1* and this is *MARKER2*',
323  [
324  'MARKER1' => 'marker 1',
325  'MARKER2' => 'marker 2'
326  ],
327  '*|*',
328  false,
329  false,
330  'This is marker 1 and this is marker 2'
331  ],
332  'Upper case marker with unused marker' => [
333  'This is ###MARKER1### and this is ###MARKER2### ###UNUSED###',
334  [
335  '###MARKER1###' => 'marker 1',
336  '###MARKER2###' => 'marker 2'
337  ],
338  '',
339  false,
340  false,
341  'This is marker 1 and this is marker 2 ###UNUSED###'
342  ],
343  'Upper case marker with unused marker deleted' => [
344  'This is ###MARKER1### and this is ###MARKER2### ###UNUSED###',
345  [
346  '###MARKER1###' => 'marker 1',
347  '###MARKER2###' => 'marker 2'
348  ],
349  '',
350  false,
351  true,
352  'This is marker 1 and this is marker 2 '
353  ],
354  ];
355  }
356 
367  public function ‪substituteMarkerArray(
368  string $content,
369  array $markContentArray,
370  string $wrap,
371  bool $uppercase,
372  bool $deleteUnused,
373  string $expected
374  ): void {
375  self::assertSame(
376  $expected,
377  $this->templateService->substituteMarkerArray($content, $markContentArray, $wrap, $uppercase, $deleteUnused)
378  );
379  }
380 
384  public function ‪substituteMarkerDataProvider(): array
385  {
386  return [
387  'Single marker' => [
388  'This is a ###SAMPLE### text',
389  '###SAMPLE###',
390  'simple',
391  'This is a simple text'
392  ],
393  'Double marker' => [
394  'This is a ###SAMPLE### text with a ###SAMPLE### content',
395  '###SAMPLE###',
396  'simple',
397  'This is a simple text with a simple content'
398  ],
399  ];
400  }
401 
409  public function ‪substituteMarker(string $content, string $marker, $markContent, string $expected): void
410  {
411  self::assertSame($expected, $this->templateService->substituteMarker($content, $marker, $markContent));
412  }
413 
419  public function ‪substituteSubpartArrayDataProvider(): array
420  {
421  return [
422  'Substitute multiple subparts at once with plain marker' => [
423  '<body>
424 ###SUBPART1###text1###SUBPART1###
425 ###SUBPART2###text2###SUBPART2###
426 </body>',
427  [
428  '###SUBPART1###' => 'hello',
429  '###SUBPART2###' => 'world'
430  ],
431  '<body>
432 hello
433 world
434 </body>'
435  ],
436  ];
437  }
438 
446  public function ‪substituteSubpartArray(string $content, array $subpartsContent, string $expected): void
447  {
448  self::assertSame($expected, $this->templateService->substituteSubpartArray($content, $subpartsContent));
449  }
450 
457  {
458  $template = '###SINGLEMARKER1###
459 <!-- ###FOO### begin -->
460 <!-- ###BAR### begin -->
461 ###SINGLEMARKER2###
462 <!-- ###BAR### end -->
463 <!-- ###FOOTER### begin -->
464 ###SINGLEMARKER3###
465 <!-- ###FOOTER### end -->
466 <!-- ###FOO### end -->';
467 
468  $expected = 'Value 1
469 
470 
471 Value 2.1
472 
473 Value 2.2
474 
475 
476 Value 3.1
477 
478 Value 3.2
479 
480 ';
481 
482  return [
483  'Single marker' => [
484  '###SINGLEMARKER###',
485  [
486  '###SINGLEMARKER###' => 'Value 1'
487  ],
488  '',
489  false,
490  false,
491  'Value 1'
492  ],
493  'Subpart marker' => [
494  $template,
495  [
496  '###SINGLEMARKER1###' => 'Value 1',
497  '###FOO###' => [
498  [
499  '###BAR###' => [
500  [
501  '###SINGLEMARKER2###' => 'Value 2.1'
502  ],
503  [
504  '###SINGLEMARKER2###' => 'Value 2.2'
505  ]
506  ],
507  '###FOOTER###' => [
508  [
509  '###SINGLEMARKER3###' => 'Value 3.1'
510  ],
511  [
512  '###SINGLEMARKER3###' => 'Value 3.2'
513  ]
514  ]
515  ]
516  ]
517  ],
518  '',
519  false,
520  false,
521  $expected
522  ],
523  'Subpart marker with wrap' => [
524  $template,
525  [
526  'SINGLEMARKER1' => 'Value 1',
527  'FOO' => [
528  [
529  'BAR' => [
530  [
531  'SINGLEMARKER2' => 'Value 2.1'
532  ],
533  [
534  'SINGLEMARKER2' => 'Value 2.2'
535  ]
536  ],
537  'FOOTER' => [
538  [
539  'SINGLEMARKER3' => 'Value 3.1'
540  ],
541  [
542  'SINGLEMARKER3' => 'Value 3.2'
543  ]
544  ]
545  ]
546  ]
547  ],
548  '###|###',
549  false,
550  false,
551  $expected
552  ],
553  'Subpart marker with lower marker array keys' => [
554  $template,
555  [
556  '###singlemarker1###' => 'Value 1',
557  '###foo###' => [
558  [
559  '###bar###' => [
560  [
561  '###singlemarker2###' => 'Value 2.1'
562  ],
563  [
564  '###singlemarker2###' => 'Value 2.2'
565  ]
566  ],
567  '###footer###' => [
568  [
569  '###singlemarker3###' => 'Value 3.1'
570  ],
571  [
572  '###singlemarker3###' => 'Value 3.2'
573  ]
574  ]
575  ]
576  ]
577  ],
578  '',
579  true,
580  false,
581  $expected
582  ],
583  'Subpart marker with unused markers' => [
584  $template,
585  [
586  '###FOO###' => [
587  [
588  '###BAR###' => [
589  [
590  '###SINGLEMARKER2###' => 'Value 2.1'
591  ]
592  ],
593  '###FOOTER###' => [
594  [
595  '###SINGLEMARKER3###' => 'Value 3.1'
596  ]
597  ]
598  ]
599  ]
600  ],
601  '',
602  false,
603  true,
604  '
605 
606 
607 Value 2.1
608 
609 
610 Value 3.1
611 
612 '
613  ],
614  'Subpart marker with empty subpart' => [
615  $template,
616  [
617  '###SINGLEMARKER1###' => 'Value 1',
618  '###FOO###' => [
619  [
620  '###BAR###' => [
621  [
622  '###SINGLEMARKER2###' => 'Value 2.1'
623  ],
624  [
625  '###SINGLEMARKER2###' => 'Value 2.2'
626  ]
627  ],
628  '###FOOTER###' => []
629  ]
630  ]
631  ],
632  '',
633  false,
634  false,
635  'Value 1
636 
637 
638 Value 2.1
639 
640 Value 2.2
641 
642 
643 '
644  ]
645  ];
646  }
647 
659  string $template,
660  array $markersAndSubparts,
661  string $wrap,
662  bool $uppercase,
663  bool $deleteUnused,
664  string $expected
665  ): void {
666  self::assertSame(
667  $expected,
668  $this->templateService->substituteMarkerAndSubpartArrayRecursive(
669  $template,
670  $markersAndSubparts,
671  $wrap,
672  $uppercase,
673  $deleteUnused
674  )
675  );
676  }
677 
682  {
683  return [
684  'no markers defined' => [
685  'dummy content with ###UNREPLACED### marker',
686  [],
687  [],
688  [],
689  'dummy content with ###UNREPLACED### marker',
690  ],
691  'no markers used' => [
692  'dummy content with no marker',
693  [
694  '###REPLACED###' => '_replaced_'
695  ],
696  [],
697  [],
698  'dummy content with no marker',
699  ],
700  'one marker' => [
701  'dummy content with ###REPLACED### marker',
702  [
703  '###REPLACED###' => '_replaced_'
704  ],
705  [],
706  [],
707  'dummy content with _replaced_ marker'
708  ],
709  'one marker with lots of chars' => [
710  'dummy content with ###RE.:##-=_()LACED### marker',
711  [
712  '###RE.:##-=_()LACED###' => '_replaced_'
713  ],
714  [],
715  [],
716  'dummy content with _replaced_ marker'
717  ],
718  'markers which are special' => [
719  'dummy ###aa##.#######A### ######',
720  [
721  '###aa##.###' => 'content ',
722  '###A###' => 'is',
723  '######' => '-is not considered-'
724  ],
725  [],
726  [],
727  'dummy content #is ######'
728  ],
729  'two markers in content, but more defined' => [
730  'dummy ###CONTENT### with ###REPLACED### marker',
731  [
732  '###REPLACED###' => '_replaced_',
733  '###CONTENT###' => 'content',
734  '###NEVERUSED###' => 'bar'
735  ],
736  [],
737  [],
738  'dummy content with _replaced_ marker'
739  ],
740  'one subpart' => [
741  'dummy content with ###ASUBPART### around some text###ASUBPART###.',
742  [],
743  [
744  '###ASUBPART###' => 'some other text'
745  ],
746  [],
747  'dummy content with some other text.'
748  ],
749  'one wrapped subpart' => [
750  'dummy content with ###AWRAPPEDSUBPART### around some text###AWRAPPEDSUBPART###.',
751  [],
752  [],
753  [
754  '###AWRAPPEDSUBPART###' => [
755  'more content',
756  'content'
757  ]
758  ],
759  'dummy content with more content around some textcontent.'
760  ],
761  'one subpart with markers, not replaced recursively' => [
762  'dummy ###CONTENT### with ###ASUBPART### around ###SOME### text###ASUBPART###.',
763  [
764  '###CONTENT###' => 'content',
765  '###SOME###' => '-this should never make it into output-',
766  '###OTHER_NOT_REPLACED###' => '-this should never make it into output-'
767  ],
768  [
769  '###ASUBPART###' => 'some ###OTHER_NOT_REPLACED### text'
770  ],
771  [],
772  'dummy content with some ###OTHER_NOT_REPLACED### text.'
773  ],
774  ];
775  }
776 
788  string $content,
789  array $markContentArray,
790  array $subpartContentArray,
791  array $wrappedSubpartContentArray,
792  string $expectedContent
793  ): void {
794  $resultContent = $this->templateService->substituteMarkerArrayCached(
795  $content,
796  $markContentArray,
797  $subpartContentArray,
798  $wrappedSubpartContentArray
799  );
800  self::assertSame($expectedContent, $resultContent);
801  }
802 }
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArrayDataProvider
‪array substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArrayDataProvider()
Definition: MarkerBasedTemplateServiceTest.php:454
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarkerArray
‪substituteMarkerArray(string $content, array $markContentArray, string $wrap, bool $uppercase, bool $deleteUnused, string $expected)
Definition: MarkerBasedTemplateServiceTest.php:365
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\$templateService
‪MarkerBasedTemplateService $templateService
Definition: MarkerBasedTemplateServiceTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray
‪substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray(string $template, array $markersAndSubparts, string $wrap, bool $uppercase, bool $deleteUnused, string $expected)
Definition: MarkerBasedTemplateServiceTest.php:656
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\setUp
‪setUp()
Definition: MarkerBasedTemplateServiceTest.php:44
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarkerArrayDataProvider
‪substituteMarkerArrayDataProvider()
Definition: MarkerBasedTemplateServiceTest.php:283
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarkerDataProvider
‪substituteMarkerDataProvider()
Definition: MarkerBasedTemplateServiceTest.php:382
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteSubpartDataProvider
‪array substituteSubpartDataProvider()
Definition: MarkerBasedTemplateServiceTest.php:140
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\getSubpartDataProvider
‪array getSubpartDataProvider()
Definition: MarkerBasedTemplateServiceTest.php:60
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest
Definition: MarkerBasedTemplateServiceTest.php:32
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteSubpartArrayDataProvider
‪array substituteSubpartArrayDataProvider()
Definition: MarkerBasedTemplateServiceTest.php:417
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarkerArrayCachedReturnsExpectedContentDataProvider
‪array substituteMarkerArrayCachedReturnsExpectedContentDataProvider()
Definition: MarkerBasedTemplateServiceTest.php:679
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\getSubpart
‪getSubpart(string $content, string $marker, string $expected)
Definition: MarkerBasedTemplateServiceTest.php:130
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteSubpart
‪substituteSubpart(string $content, string $marker, $subpartContent, bool $recursive, bool $keepMarker, string $expected)
Definition: MarkerBasedTemplateServiceTest.php:266
‪TYPO3\CMS\Core\Service\MarkerBasedTemplateService
Definition: MarkerBasedTemplateService.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarkerArrayCachedReturnsExpectedContent
‪substituteMarkerArrayCachedReturnsExpectedContent(string $content, array $markContentArray, array $subpartContentArray, array $wrappedSubpartContentArray, string $expectedContent)
Definition: MarkerBasedTemplateServiceTest.php:785
‪TYPO3\CMS\Core\Tests\Unit\Service
Definition: DependencyOrderingServiceTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteSubpartArray
‪substituteSubpartArray(string $content, array $subpartsContent, string $expected)
Definition: MarkerBasedTemplateServiceTest.php:444
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: MarkerBasedTemplateServiceTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Service\MarkerBasedTemplateServiceTest\substituteMarker
‪substituteMarker(string $content, string $marker, $markContent, string $expected)
Definition: MarkerBasedTemplateServiceTest.php:407