‪TYPO3CMS  ‪main
HtmlViewHelperTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 use TYPO3Fluid\Fluid\View\TemplateView;
30 
31 final class ‪HtmlViewHelperTest extends FunctionalTestCase
32 {
34 
38  protected const ‪LANGUAGE_PRESETS = [
39  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
40  ];
41 
42  public static function ‪contentIsRenderedDataProvider(): array
43  {
44  return [
45  'format.html: process lib.parseFunc_RTE by default' => [
46  '<f:format.html>{$project} is a cool CMS</f:format.html>',
47  'TYPO3 is a cool CMS',
48  ],
49  'format.html: process inline notation with undefined variable returns empty string' => [
50  '{undefinedVariable -> f:format.html()}',
51  '',
52  ],
53  'format.html: process specific TS path' => [
54  '<f:format.html parseFuncTSPath="lib.foo">{$foo} is BAR</f:format.html>',
55  'BAR is BAR',
56  ],
57  'format.html: specific TS path with current' => [
58  '<f:format.html parseFuncTSPath="lib.inventor" current="Kasper">Hello</f:format.html>',
59  'Hello Kasper',
60  ],
61  'format.html: specific TS path with data' => [
62  '<f:format.html parseFuncTSPath="lib.record" data="{uid: 1, pid: 12, title: \'foo\'}">Hello</f:format.html>',
63  'Hello foo',
64  ],
65  'format.html: specific TS path with data and currentValueKey' => [
66  '<f:format.html parseFuncTSPath="lib.record" data="{uid: 1, pid: 12, title: \'Bar\'}" currentValueKey="title">Hello</f:format.html>',
67  'Hello Bar',
68  ],
69  'format.html: specific TS path with data, currentValueKey and a constant' => [
70  '<f:format.html parseFuncTSPath="lib.news" data="{uid: 1, pid: 12, title: \'Great news\'}" currentValueKey="title">{$project} news:</f:format.html>',
71  'TYPO3 news: Great news',
72  ],
73  // table attribute is hard to test. It was only used as parent for CONTENT and RECORD cObj.
74  // Further the table will be used in FILES cObj as fallback, if a table was not given in references array.
75  ];
76  }
77 
78  #[DataProvider('contentIsRenderedDataProvider')]
79  #[Test]
80  public function ‪contentIsRendered(string $fluidTemplateSource, string $expected): void
81  {
82  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
84  'test',
85  $this->‪buildSiteConfiguration(1, '/'),
86  [
87  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
88  ]
89  );
90  $this->‪createTypoScriptTemplate($fluidTemplateSource);
91 
92  $response = $this->executeFrontendSubRequest(
93  (new InternalRequest())->withPageId(1)
94  );
95  self::assertStringContainsString($expected, (string)$response->getBody());
96  }
97 
98  public static function ‪invalidInvocationIsDeterminedDataProvider(): array
99  {
100  return [
101  'explicitly empty parseFunc path' => [
102  '<f:format.html parseFuncTSPath="">TYPO3 is a cool CMS</f:format.html>',
103  ],
104  'non-existing parseFunc path' => [
105  '<f:format.html parseFuncTSPath="null.this.does.not.exist">TYPO3 is a cool CMS</f:format.html>',
106  ],
107  ];
108  }
109 
110  #[DataProvider('invalidInvocationIsDeterminedDataProvider')]
111  #[Test]
112  public function ‪invalidInvocationIsDetermined(string $fluidTemplateSource): void
113  {
114  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
116  'test',
117  $this->‪buildSiteConfiguration(1, '/'),
118  [
119  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
120  ]
121  );
122  $this->‪createTypoScriptTemplate($fluidTemplateSource);
123 
124  $this->expectException(\LogicException::class);
125  $this->expectExceptionCode(1641989097);
126  $this->executeFrontendSubRequest(
127  (new InternalRequest())->withPageId(1)
128  );
129  }
130 
131  private function ‪createTypoScriptTemplate(string $fluidTemplateSource): void
132  {
133  (new ‪ConnectionPool())->getConnectionForTable('sys_template')
134  ->insert(
135  'sys_template',
136  [
137  'pid' => 1,
138  'root' => 1,
139  'clear' => 3,
140  'constants' => <<<EOT
141 project = ‪TYPO3
142 foo = BAR
143 EOT,
144  'config' => <<<EOT
145 lib.parseFunc_RTE {
146  htmlSanitize = 1
147 }
148 lib.foo {
149  htmlSanitize = 1
150 }
151 lib.inventor {
152  htmlSanitize = 1
153  plainTextStdWrap.noTrimWrap = || |
154  plainTextStdWrap.dataWrap = |{CURRENT:1}
155 }
156 lib.record {
157  htmlSanitize = 1
158  plainTextStdWrap.noTrimWrap = || |
159  plainTextStdWrap.dataWrap = |{FIELD:title}
160 }
161 lib.news {
162  htmlSanitize = 1
163  plainTextStdWrap.noTrimWrap = || |
164  plainTextStdWrap.dataWrap = |{CURRENT:1}
165 }
166 page = PAGE
167 page.10 = FLUIDTEMPLATE
168 page.10 {
169  template = TEXT
170  template.value = $fluidTemplateSource
171 }
172 EOT
173  ]
174  );
175  }
176 
177  #[Test]
178  public function throwsExceptionIfCalledInBackendContext(): void
179  {
180  $context = $this->get(RenderingContextFactory::class)->create();
181  $request = (new ‪ServerRequest())
182  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
183  $context->setRequest($request);
184  $context->getTemplatePaths()->setTemplateSource('<f:format.html>TYPO3 is a cool CMS</f:format.html>');
185 
186  $this->expectException(\RuntimeException::class);
187  $this->expectExceptionCode(1686813703);
188  $this->expectExceptionMessage('Using f:format.html in backend context is not allowed. Use f:sanitize.html or f:transform.html instead.');
189 
190  (new TemplateView($context))->render();
191  }
192 }
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest\contentIsRenderedDataProvider
‪static contentIsRenderedDataProvider()
Definition: HtmlViewHelperTest.php:41
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest\invalidInvocationIsDetermined
‪invalidInvocationIsDetermined(string $fluidTemplateSource)
Definition: HtmlViewHelperTest.php:111
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest
Definition: HtmlViewHelperTest.php:32
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: HtmlViewHelperTest.php:37
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest\throwsExceptionIfCalledInBackendContext
‪throwsExceptionIfCalledInBackendContext()
Definition: HtmlViewHelperTest.php:177
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format
Definition: BytesViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest\createTypoScriptTemplate
‪createTypoScriptTemplate(string $fluidTemplateSource)
Definition: HtmlViewHelperTest.php:130
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest\invalidInvocationIsDeterminedDataProvider
‪static invalidInvocationIsDeterminedDataProvider()
Definition: HtmlViewHelperTest.php:97
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\HtmlViewHelperTest\contentIsRendered
‪contentIsRendered(string $fluidTemplateSource, string $expected)
Definition: HtmlViewHelperTest.php:79
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46