TYPO3 CMS  TYPO3_7-6
MarkerBasedTemplateServiceTest.php
Go to the documentation of this file.
1 <?php
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 
19 
24 {
28  protected $templateService;
29 
30  protected function setUp()
31  {
32  $this->templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
33  }
34 
40  public function getSubpartDataProvider()
41  {
42  return [
43  'No start marker' => [
44  '<body>text</body>',
45  '###SUBPART###',
46  ''
47  ],
48  'No stop marker' => [
49  '<body>
50 <!-- ###SUBPART### Start -->
51 text
52 </body>',
53  '###SUBPART###',
54  ''
55  ],
56  'Start and stop marker in HTML comment' => [
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' => [
68  '<body>
69 ###SUBPART###
70 text
71 <!-- ###SUBPART### End -->
72 </body>',
73  '###SUBPART###',
74  '
75 text
76 '
77  ],
78  'Start marker in HTML comment' => [
79  '<body>
80 <!-- ###SUBPART### Start -->
81 text
82 ###SUBPART###
83 </body>',
84  '###SUBPART###',
85  '
86 text
87 '
88  ],
89  'Start and stop marker direct' => [
90  '<body>
91 ###SUBPART###
92 text
93 ###SUBPART###
94 </body>',
95  '###SUBPART###',
96  '
97 text
98 '
99  ],
100  ];
101  }
102 
110  public function getSubpart($content, $marker, $expected)
111  {
112  $this->assertSame($expected, $this->templateService->getSubpart($content, $marker));
113  }
114 
121  {
122  return [
123  'No start marker' => [
124  '<body>text</body>',
125  '###SUBPART###',
126  'hello',
127  false,
128  false,
129  '<body>text</body>'
130  ],
131  'No stop marker' => [
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' => [
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' => [
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' => [
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' => [
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' => [
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' => [
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' => [
222  '<body>
223 ###SUBPART###text###SUBPART###
224 </body>',
225  '###SUBPART###',
226  ['before-', '-after'],
227  false,
228  true,
229  '<body>
230 ###SUBPART###before-text-after###SUBPART###
231 </body>'
232  ],
233  ];
234  }
235 
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 
255  {
256  return [
257  'Upper case marker' => [
258  'This is ###MARKER1### and this is ###MARKER2###',
259  ['###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' => [
267  'This is ###MARKER1### and this is ###MARKER2###',
268  ['###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' => [
276  'This is ###MARKER1### and this is ###MARKER2###',
277  ['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' => [
285  'This is *MARKER1* and this is *MARKER2*',
286  ['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' => [
294  'This is ###MARKER1### and this is ###MARKER2### ###UNUSED###',
295  ['###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' => [
303  'This is ###MARKER1### and this is ###MARKER2### ###UNUSED###',
304  ['###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 
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 
333  {
334  return [
335  'Single marker' => [
336  'This is a ###SAMPLE### text',
337  '###SAMPLE###',
338  'simple',
339  'This is a simple text'
340  ],
341  'Double marker' => [
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 
357  public function substituteMarker($content, $marker, $markContent, $expected)
358  {
359  $this->assertSame($expected, $this->templateService->substituteMarker($content, $marker, $markContent));
360  }
361 
368  {
369  return [
370  'Substitute multiple subparts at once with plain marker' => [
371  '<body>
372 ###SUBPART1###text1###SUBPART1###
373 ###SUBPART2###text2###SUBPART2###
374 </body>',
375  ['###SUBPART1###' => 'hello',
376  '###SUBPART2###' => 'world'],
377  '<body>
378 hello
379 world
380 </body>'
381  ],
382  ];
383  }
384 
392  public function substituteSubpartArray($content, array $subpartsContent, $expected)
393  {
394  $this->assertSame($expected, $this->templateService->substituteSubpartArray($content, $subpartsContent));
395  }
396 
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 [
429  'Single marker' => [
430  '###SINGLEMARKER###',
431  [
432  '###SINGLEMARKER###' => 'Value 1'
433  ],
434  '',
435  false,
436  false,
437  'Value 1'
438  ],
439  'Subpart marker' => [
440  $template,
441  [
442  '###SINGLEMARKER1###' => 'Value 1',
443  '###FOO###' => [
444  [
445  '###BAR###' => [
446  [
447  '###SINGLEMARKER2###' => 'Value 2.1'
448  ],
449  [
450  '###SINGLEMARKER2###' => 'Value 2.2'
451  ]
452  ],
453  '###FOOTER###' => [
454  [
455  '###SINGLEMARKER3###' => 'Value 3.1'
456  ],
457  [
458  '###SINGLEMARKER3###' => 'Value 3.2'
459  ]
460  ]
461  ]
462  ]
463  ],
464  '',
465  false,
466  false,
467  $expected
468  ],
469  'Subpart marker with wrap' => [
470  $template,
471  [
472  'SINGLEMARKER1' => 'Value 1',
473  'FOO' => [
474  [
475  'BAR' => [
476  [
477  'SINGLEMARKER2' => 'Value 2.1'
478  ],
479  [
480  'SINGLEMARKER2' => 'Value 2.2'
481  ]
482  ],
483  'FOOTER' => [
484  [
485  'SINGLEMARKER3' => 'Value 3.1'
486  ],
487  [
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' => [
500  $template,
501  [
502  '###singlemarker1###' => 'Value 1',
503  '###foo###' => [
504  [
505  '###bar###' => [
506  [
507  '###singlemarker2###' => 'Value 2.1'
508  ],
509  [
510  '###singlemarker2###' => 'Value 2.2'
511  ]
512  ],
513  '###footer###' => [
514  [
515  '###singlemarker3###' => 'Value 3.1'
516  ],
517  [
518  '###singlemarker3###' => 'Value 3.2'
519  ]
520  ]
521  ]
522  ]
523  ],
524  '',
525  true,
526  false,
527  $expected
528  ],
529  'Subpart marker with unused markers' => [
530  $template,
531  [
532  '###FOO###' => [
533  [
534  '###BAR###' => [
535  [
536  '###SINGLEMARKER2###' => 'Value 2.1'
537  ]
538  ],
539  '###FOOTER###' => [
540  [
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' => [
561  $template,
562  [
563  '###SINGLEMARKER1###' => 'Value 1',
564  '###FOO###' => [
565  [
566  '###BAR###' => [
567  [
568  '###SINGLEMARKER2###' => 'Value 2.1'
569  ],
570  [
571  '###SINGLEMARKER2###' => 'Value 2.2'
572  ]
573  ],
574  '###FOOTER###' => []
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 
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 }
substituteMarkerArray($content, $markContentArray, $wrap, $uppercase, $deleteUnused, $expected)
substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused, $expected)
substituteSubpart($content, $marker, $subpartContent, $recursive, $keepMarker, $expected)