‪TYPO3CMS  10.4
ContentObjectRendererTest.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 
24 use TYPO3\CMS\Core\Package\PackageManager;
48 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
49 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
50 
54 class ‪ContentObjectRendererTest extends UnitTestCase
55 {
57 
61  protected ‪$resetSingletonInstances = true;
62 
66  private ‪$subject;
67 
72 
77 
83  private ‪$contentObjectMap = [
84  'TEXT' => TextContentObject::class,
85  'CASE' => CaseContentObject::class,
86  'COBJ_ARRAY' => ContentObjectArrayContentObject::class,
87  'COA' => ContentObjectArrayContentObject::class,
88  'COA_INT' => ContentObjectArrayInternalContentObject::class,
89  'USER' => UserContentObject::class,
90  'USER_INT' => UserInternalContentObject::class,
91  'FILES' => FilesContentObject::class,
92  'IMAGE' => ImageContentObject::class,
93  'IMG_RESOURCE' => ImageResourceContentObject::class,
94  'CONTENT' => ContentContentObject::class,
95  'RECORDS' => RecordsContentObject::class,
96  'HMENU' => HierarchicalMenuContentObject::class,
97  'CASEFUNC' => CaseContentObject::class,
98  'LOAD_REGISTER' => LoadRegisterContentObject::class,
99  'RESTORE_REGISTER' => RestoreRegisterContentObject::class,
100  'FLUIDTEMPLATE' => FluidTemplateContentObject::class,
101  'SVG' => ScalableVectorGraphicsContentObject::class,
102  'EDITPANEL' => EditPanelContentObject::class
103  ];
104 
109 
110  protected ‪$backupEnvironment = true;
111 
115  protected function ‪setUp(): void
116  {
117  parent::setUp();
118 
119  $site = $this->‪createSiteWithLanguage([
120  'base' => '/',
121  'languageId' => 2,
122  'locale' => 'en_UK',
123  'typo3Language' => 'default',
124  ]);
125 
126  ‪$GLOBALS['SIM_ACCESS_TIME'] = 1534278180;
127  $packageManagerMock = $this->getMockBuilder(PackageManager::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130  $this->templateServiceMock =
131  $this->getMockBuilder(TemplateService::class)
132  ->setConstructorArgs([null, $packageManagerMock])
133  ->setMethods(['linkData'])
134  ->getMock();
135  $pageRepositoryMock =
136  $this->getAccessibleMock(PageRepository::class, ['getRawRecord', 'getMountPointInfo']);
137  $this->frontendControllerMock =
138  $this->getAccessibleMock(
139  TypoScriptFrontendController::class,
140  ['sL'],
141  [],
142  '',
143  false
144  );
145  $this->frontendControllerMock->_set('context', GeneralUtility::makeInstance(Context::class));
146  $this->frontendControllerMock->tmpl = ‪$this->templateServiceMock;
147  $this->frontendControllerMock->config = [];
148  $this->frontendControllerMock->page = [];
149  $this->frontendControllerMock->sys_page = $pageRepositoryMock;
150  $this->frontendControllerMock->_set('language', $site->getLanguageById(2));
152 
153  $this->cacheManager = $this->prophesize(CacheManager::class);
154  GeneralUtility::setSingletonInstance(CacheManager::class, $this->cacheManager->reveal());
155 
156  $this->subject = $this->getAccessibleMock(
157  ContentObjectRenderer::class,
158  ['getResourceFactory', 'getEnvironmentVariable'],
159  [$this->frontendControllerMock]
160  );
161 
162  $logger = $this->prophesize(Logger::class);
163  $this->subject->setLogger($logger->reveal());
164  $this->subject->setContentObjectClassMap($this->contentObjectMap);
165  $this->subject->start([], 'tt_content');
166  }
167 
171  public function ‪_parseFuncReturnsCorrectHtmlDataProvider(): array
172  {
173  return [
174  'Text without tag is wrapped with <p> tag' => [
175  'Text without tag',
176  $this->‪getLibParseFunc_RTE(),
177  '<p class="bodytext">Text without tag</p>',
178  ],
179  'Text wrapped with <p> tag remains the same' => [
180  '<p class="myclass">Text with &lt;p&gt; tag</p>',
181  $this->‪getLibParseFunc_RTE(),
182  '<p class="myclass">Text with &lt;p&gt; tag</p>',
183  ],
184  'Text with absolute external link' => [
185  'Text with <link http://example.com/foo/>external link</link>',
186  $this->‪getLibParseFunc_RTE(),
187  '<p class="bodytext">Text with <a href="http://example.com/foo/">external link</a></p>',
188  ],
189  'Empty lines are not duplicated' => [
190  LF,
191  $this->‪getLibParseFunc_RTE(),
192  '<p class="bodytext">&nbsp;</p>',
193  ],
194  'Multiple empty lines with no text' => [
195  LF . LF . LF,
196  $this->‪getLibParseFunc_RTE(),
197  '<p class="bodytext">&nbsp;</p>' . LF . '<p class="bodytext">&nbsp;</p>' . LF . '<p class="bodytext">&nbsp;</p>',
198  ],
199  'Empty lines are not duplicated at the end of content' => [
200  'test' . LF . LF,
201  $this->‪getLibParseFunc_RTE(),
202  '<p class="bodytext">test</p>' . LF . '<p class="bodytext">&nbsp;</p>',
203  ],
204  'Empty lines are not trimmed' => [
205  LF . 'test' . LF,
206  $this->‪getLibParseFunc_RTE(),
207  '<p class="bodytext">&nbsp;</p>' . LF . '<p class="bodytext">test</p>' . LF . '<p class="bodytext">&nbsp;</p>',
208  ],
209  ];
210  }
211 
219  public function ‪stdWrap_parseFuncReturnsParsedHtml($value, $configuration, $expectedResult): void
220  {
221  self::assertEquals($expectedResult, $this->subject->stdWrap_parseFunc($value, $configuration));
222  }
223 
231  {
232  $defaultListItemParseFunc = [
233  'parseFunc' => '',
234  'parseFunc.' => [
235  'tags.' => [
236  'li' => 'TEXT',
237  'li.' => [
238  'wrap' => '<li>LI:|</li>',
239  'current' => '1'
240  ]
241  ],
242  ]
243  ];
244 
245  return [
246  'parent & child tags with same beginning are processed' => [
247  '<div><any data-skip><anyother data-skip>content</anyother></any></div>',
248  [
249  'parseFunc' => '',
250  'parseFunc.' => [
251  'tags.' => [
252  'any' => 'TEXT',
253  'any.' => [
254  'wrap' => '<any data-processed>|</any>',
255  'current' => 1,
256  ],
257  'anyother' => 'TEXT',
258  'anyother.' => [
259  'wrap' => '<anyother data-processed>|</anyother>',
260  'current' => 1,
261  ],
262  ],
263  'htmlSanitize' => true,
264  'htmlSanitize.' => [
265  'build' => TestSanitizerBuilder::class,
266  ],
267  ],
268  ],
269  '<div><any data-processed><anyother data-processed>content</anyother></any></div>',
270  ],
271  'list with empty and filled li' => [
272  '<ul>
273  <li></li>
274  <li>second</li>
275 </ul>',
276  $defaultListItemParseFunc,
277  '<ul>
278  <li>LI:</li>
279  <li>LI:second</li>
280 </ul>',
281  ],
282  'list with filled li wrapped by a div containing text' => [
283  '<div>text<ul><li></li><li>second</li></ul></div>',
284  $defaultListItemParseFunc,
285  '<div>text<ul><li>LI:</li><li>LI:second</li></ul></div>',
286  ],
287  'link list with empty li modification' => [
288  '<ul>
289  <li>
290  <ul>
291  <li></li>
292  </ul>
293  </li>
294 </ul>',
295  $defaultListItemParseFunc,
296  '<ul>
297  <li>LI:
298  <ul>
299  <li>LI:</li>
300  </ul>
301  </li>
302 </ul>',
303  ],
304 
305  'link list with li modifications' => [
306  '<ul>
307  <li>first</li>
308  <li>second
309  <ul>
310  <li>first sub</li>
311  <li>second sub</li>
312  </ul>
313  </li>
314 </ul>',
315  $defaultListItemParseFunc,
316  '<ul>
317  <li>LI:first</li>
318  <li>LI:second
319  <ul>
320  <li>LI:first sub</li>
321  <li>LI:second sub</li>
322  </ul>
323  </li>
324 </ul>'
325  ],
326  'link list with li modifications and no text' => [
327  '<ul>
328  <li>first</li>
329  <li>
330  <ul>
331  <li>first sub</li>
332  <li>second sub</li>
333  </ul>
334  </li>
335 </ul>',
336  $defaultListItemParseFunc,
337  '<ul>
338  <li>LI:first</li>
339  <li>LI:
340  <ul>
341  <li>LI:first sub</li>
342  <li>LI:second sub</li>
343  </ul>
344  </li>
345 </ul>',
346  ],
347  'link list with li modifications on third level' => [
348  '<ul>
349  <li>first</li>
350  <li>second
351  <ul>
352  <li>first sub
353  <ul>
354  <li>first sub sub</li>
355  <li>second sub sub</li>
356  </ul>
357  </li>
358  <li>second sub</li>
359  </ul>
360  </li>
361 </ul>',
362  $defaultListItemParseFunc,
363  '<ul>
364  <li>LI:first</li>
365  <li>LI:second
366  <ul>
367  <li>LI:first sub
368  <ul>
369  <li>LI:first sub sub</li>
370  <li>LI:second sub sub</li>
371  </ul>
372  </li>
373  <li>LI:second sub</li>
374  </ul>
375  </li>
376 </ul>',
377  ],
378  'link list with li modifications on third level no text' => [
379  '<ul>
380  <li>first</li>
381  <li>
382  <ul>
383  <li>
384  <ul>
385  <li>first sub sub</li>
386  <li>first sub sub</li>
387  </ul>
388  </li>
389  <li>second sub</li>
390  </ul>
391  </li>
392 </ul>',
393  $defaultListItemParseFunc,
394  '<ul>
395  <li>LI:first</li>
396  <li>LI:
397  <ul>
398  <li>LI:
399  <ul>
400  <li>LI:first sub sub</li>
401  <li>LI:first sub sub</li>
402  </ul>
403  </li>
404  <li>LI:second sub</li>
405  </ul>
406  </li>
407 </ul>',
408  ],
409  'link list with ul and li modifications' => [
410  '<ul>
411  <li>first</li>
412  <li>second
413  <ul>
414  <li>first sub</li>
415  <li>second sub</li>
416  </ul>
417  </li>
418 </ul>',
419  [
420  'parseFunc' => '',
421  'parseFunc.' => [
422  'tags.' => [
423  'ul' => 'TEXT',
424  'ul.' => [
425  'wrap' => '<ul><li>intro</li>|<li>outro</li></ul>',
426  'current' => '1'
427  ],
428  'li' => 'TEXT',
429  'li.' => [
430  'wrap' => '<li>LI:|</li>',
431  'current' => '1'
432  ]
433  ]
434  ]
435  ],
436  '<ul><li>intro</li>
437  <li>LI:first</li>
438  <li>LI:second
439  <ul><li>intro</li>
440  <li>LI:first sub</li>
441  <li>LI:second sub</li>
442  <li>outro</li></ul>
443  </li>
444 <li>outro</li></ul>',
445  ],
446 
447  'link list with li containing p tag and sub list' => [
448  '<ul>
449  <li>first</li>
450  <li>
451  <ul>
452  <li>
453  <span>
454  <ul>
455  <li>first sub sub</li>
456  <li>first sub sub</li>
457  </ul>
458  </span>
459  </li>
460  <li>second sub</li>
461  </ul>
462  </li>
463 </ul>',
464  $defaultListItemParseFunc,
465  '<ul>
466  <li>LI:first</li>
467  <li>LI:
468  <ul>
469  <li>LI:
470  <span>
471  <ul>
472  <li>LI:first sub sub</li>
473  <li>LI:first sub sub</li>
474  </ul>
475  </span>
476  </li>
477  <li>LI:second sub</li>
478  </ul>
479  </li>
480 </ul>',
481  ]
482  ];
483  }
484 
492  public function ‪parseFuncParsesNestedTagsProperly(string $value, array $configuration, string $expectedResult): void
493  {
494  self::assertEquals($expectedResult, $this->subject->stdWrap_parseFunc($value, $configuration));
495  }
496 }
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ContentObjectRendererTestTrait\createSiteWithLanguage
‪Site createSiteWithLanguage(array $languageConfiguration)
Definition: ContentObjectRendererTestTrait.php:189
‪TYPO3\CMS\Frontend\ContentObject\RestoreRegisterContentObject
Definition: RestoreRegisterContentObject.php:22
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ContentObjectRendererTestTrait\getLibParseFunc_RTE
‪array getLibParseFunc_RTE()
Definition: ContentObjectRendererTestTrait.php:30
‪TYPO3\CMS\Frontend\ContentObject\ImageContentObject
Definition: ImageContentObject.php:29
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\$cacheManager
‪Prophecy Prophecy ObjectProphecy CacheManager $cacheManager
Definition: ContentObjectRendererTest.php:101
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Fixtures\TestSanitizerBuilder
Definition: TestSanitizerBuilder.php:24
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ContentObjectRendererTestTrait
Definition: ContentObjectRendererTestTrait.php:26
‪TYPO3\CMS\Frontend\ContentObject\HierarchicalMenuContentObject
Definition: HierarchicalMenuContentObject.php:26
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\$templateServiceMock
‪PHPUnit Framework MockObject MockObject TemplateService $templateServiceMock
Definition: ContentObjectRendererTest.php:71
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject
Definition: ContentObjectRendererTest.php:18
‪TYPO3\CMS\Frontend\ContentObject\UserInternalContentObject
Definition: UserInternalContentObject.php:22
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\$backupEnvironment
‪$backupEnvironment
Definition: ContentObjectRendererTest.php:103
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ContentObjectRendererTest.php:59
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject
Definition: ScalableVectorGraphicsContentObject.php:25
‪TYPO3\CMS\Frontend\ContentObject\LoadRegisterContentObject
Definition: LoadRegisterContentObject.php:22
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\parseFuncParsesNestedTagsProperly
‪parseFuncParsesNestedTagsProperly(string $value, array $configuration, string $expectedResult)
Definition: ContentObjectRendererTest.php:485
‪TYPO3\CMS\Frontend\ContentObject\UserContentObject
Definition: UserContentObject.php:25
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Frontend\ContentObject\ImageResourceContentObject
Definition: ImageResourceContentObject.php:22
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\$subject
‪PHPUnit Framework MockObject MockObject AccessibleObjectInterface ContentObjectRenderer $subject
Definition: ContentObjectRendererTest.php:63
‪TYPO3\CMS\Frontend\ContentObject\EditPanelContentObject
Definition: EditPanelContentObject.php:22
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectArrayContentObject
Definition: ContentObjectArrayContentObject.php:25
‪TYPO3\CMS\Frontend\ContentObject\CaseContentObject
Definition: CaseContentObject.php:22
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject
Definition: FluidTemplateContentObject.php:31
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\_parseFuncParsesNestedTagsProperlyDataProvider
‪array _parseFuncParsesNestedTagsProperlyDataProvider()
Definition: ContentObjectRendererTest.php:223
‪TYPO3\CMS\Core\TypoScript\TemplateService
Definition: TemplateService.php:46
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\_parseFuncReturnsCorrectHtmlDataProvider
‪array _parseFuncReturnsCorrectHtmlDataProvider()
Definition: ContentObjectRendererTest.php:164
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\$frontendControllerMock
‪PHPUnit Framework MockObject MockObject TypoScriptFrontendController AccessibleObjectInterface $frontendControllerMock
Definition: ContentObjectRendererTest.php:67
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\setUp
‪setUp()
Definition: ContentObjectRendererTest.php:108
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest
Definition: ContentObjectRendererTest.php:55
‪TYPO3\CMS\Frontend\ContentObject\TextContentObject
Definition: TextContentObject.php:22
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject
Definition: RecordsContentObject.php:28
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\$contentObjectMap
‪array $contentObjectMap
Definition: ContentObjectRendererTest.php:77
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject
Definition: FilesContentObject.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectArrayInternalContentObject
Definition: ContentObjectArrayInternalContentObject.php:26
‪TYPO3\CMS\Frontend\ContentObject\ContentContentObject
Definition: ContentContentObject.php:26
‪TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject\ContentObjectRendererTest\stdWrap_parseFuncReturnsParsedHtml
‪stdWrap_parseFuncReturnsParsedHtml($value, $configuration, $expectedResult)
Definition: ContentObjectRendererTest.php:212