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