‪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\FunctionalTestCase;
28 use TYPO3Fluid\Fluid\View\TemplateView;
29 
30 final class ‪HtmlViewHelperTest extends FunctionalTestCase
31 {
33 
34  protected const ‪LANGUAGE_PRESETS = [
35  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
36  ];
37 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  $this->importCsvDataSet(__DIR__ . '/../../Fixtures/pages.csv');
43  'typo3-localhost',
44  $this->‪buildSiteConfiguration(1, 'https://typo3.localhost/'),
45  [$this->‪buildDefaultLanguageConfiguration('EN', '/')]
46  );
47 
48  // @todo: Update this. There should always be a site object and setting $GLOBALS['TYPO3_REQUEST'] is a hack.
49  $rootPageSite = new ‪NullSite();
50  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest('https://typo3-2.localhost/', 'GET'))
51  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE)
52  ->withAttribute('language', $rootPageSite->getDefaultLanguage());
53  }
54 
55  public static function ‪isTransformedDataProvider(): array
56  {
57  return [
58  'empty string' => [
59  '',
60  '',
61  ],
62  'any HTML tag' => [
63  '<p>value a</p><p>value b</p>',
64  '<p>value a</p><p>value b</p>',
65  ],
66  'unknown HTML tag' => [
67  '<unknown>value</unknown>',
68  '<unknown>value</unknown>',
69  ],
70  'empty' => [
71  '<a href>visit</a>',
72  '<a href>visit</a>',
73  ],
74  'invalid' => [
75  '<a href="#">visit</a>',
76  '<a href="#">visit</a>',
77  ],
78  'tel anchor' => [
79  '<a href="tel:+123456789" class="phone voice">call</a>',
80  '<a href="tel:+123456789" class="phone voice">call</a>',
81  ],
82  'mailto anchor' => [
83  '<a href="mailto:test@typo3.localhost?subject=Test" class="mailto">send mail</a>',
84  '<a href="mailto:test@typo3.localhost?subject=Test" class="mailto">send mail</a>',
85  ],
86  'https anchor' => [
87  '<a href="https://typo3.localhost/path/visit.html" class="page">visit</a>',
88  '<a href="https://typo3.localhost/path/visit.html" class="page">visit</a>',
89  ],
90  'absolute anchor' => [
91  '<a href="/path/visit.html" class="page">visit</a>',
92  '<a href="/path/visit.html" class="page">visit</a>',
93  ],
94  'relative anchor' => [
95  '<a href="path/visit.html" class="page">visit</a>',
96  '<a href="path/visit.html" class="page">visit</a>',
97  ],
98  't3-page anchor' => [
99  '<a href="t3://page?uid=1" class="page">visit</a>',
100  '<a href="https://typo3.localhost/" class="page">visit</a>',
101  ],
102  't3-page without uid anchor' => [
103  '<a href="t3://page">visit</a>',
104  '<a href="https://typo3.localhost/">visit</a>',
105  ],
106  ];
107  }
108 
109  #[DataProvider('isTransformedDataProvider')]
110  #[Test]
111  public function ‪isTransformed(string $payload, string $expectation): void
112  {
113  $context = $this->get(RenderingContextFactory::class)->create();
114  $context->getTemplatePaths()->setTemplateSource(sprintf('<f:transform.html>%s</f:transform.html>', $payload));
115  self::assertSame($expectation, (new TemplateView($context))->render());
116  }
117 
118  public static function ‪isTransformedWithSelectorDataProvider(): array
119  {
120  return [
121  'a.href' => [
122  'a.href',
123  '<a href="t3://page?uid=1" class="page">visit</a>',
124  '<a href="https://typo3.localhost/" class="page">visit</a>',
125  ],
126  '.href' => [
127  '.href',
128  '<a href="t3://page?uid=1" class="page">visit</a>',
129  '<a href="https://typo3.localhost/" class="page">visit</a>',
130  ],
131  'div.data-uri' => [
132  'div.data-uri',
133  '<div data-uri="t3://page?uid=1" class="page">visit</div>',
134  '<div data-uri="https://typo3.localhost/" class="page">visit</div>',
135  ],
136  'a.href,div.data-uri' => [
137  'a.href,div.data-uri',
138  '<a href="t3://page?uid=1">visit</a><div data-uri="t3://page?uid=1">visit</div>',
139  '<a href="https://typo3.localhost/">visit</a><div data-uri="https://typo3.localhost/">visit</div>',
140  ],
141  ];
142  }
143 
144  #[DataProvider('isTransformedWithSelectorDataProvider')]
145  #[Test]
146  public function ‪isTransformedWithSelector(string $selector, string $payload, string $expectation): void
147  {
148  $context = $this->get(RenderingContextFactory::class)->create();
149  $context->getTemplatePaths()->setTemplateSource(sprintf('<f:transform.html selector="%s">%s</f:transform.html>', $selector, $payload));
150  self::assertSame($expectation, (new TemplateView($context))->render());
151  }
152 
153  public static function ‪isTransformedWithOnFailureDataProvider(): array
154  {
155  return [
156  't3-page invalid uid anchor (default)' => [
157  null,
158  '<a href="t3://page?uid=9876">visit</a>',
159  'visit',
160  ],
161  't3-page invalid uid anchor ("removeEnclosure")' => [
162  'removeEnclosure',
163  '<a href="t3://page?uid=9876">visit</a>',
164  'visit',
165  ],
166  't3-page invalid uid anchor ("removeTag")' => [
167  'removeTag',
168  '<a href="t3://page?uid=9876">visit</a>',
169  '',
170  ],
171  't3-page invalid uid anchor ("removeAttr")' => [
172  'removeAttr',
173  '<a href="t3://page?uid=9876">visit</a>',
174  '<a>visit</a>',
175  ],
176  't3-page invalid uid anchor ("null")' => [
177  'null',
178  '<a href="t3://page?uid=9876">visit</a>',
179  '<a href="t3://page?uid=9876">visit</a>',
180  ],
181  't3-page invalid uid anchor ("")' => [
182  '',
183  '<a href="t3://page?uid=9876">visit</a>',
184  '<a href="t3://page?uid=9876">visit</a>',
185  ],
186  ];
187  }
188 
189  #[DataProvider('isTransformedWithOnFailureDataProvider')]
190  #[Test]
191  public function ‪isTransformedWithOnFailure(?string $onFailure, string $payload, string $expectation): void
192  {
193  $context = $this->get(RenderingContextFactory::class)->create();
194  $context->getTemplatePaths()->setTemplateSource(sprintf(
195  '<f:transform.html %s>%s</f:transform.html>',
196  $onFailure !== null ? 'onFailure="' . $onFailure . '"' : '',
197  $payload
198  ));
199  self::assertSame($expectation, (new TemplateView($context))->render());
200  }
201 }
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.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\Core\Site\Entity\NullSite
Definition: NullSite.php:32
‪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\Transform\HtmlViewHelperTest\isTransformedWithOnFailure
‪isTransformedWithOnFailure(?string $onFailure, string $payload, string $expectation)
Definition: HtmlViewHelperTest.php:190
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: HtmlViewHelperTest.php:33
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest\isTransformedDataProvider
‪static isTransformedDataProvider()
Definition: HtmlViewHelperTest.php:54
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest\isTransformedWithSelector
‪isTransformedWithSelector(string $selector, string $payload, string $expectation)
Definition: HtmlViewHelperTest.php:145
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest\isTransformedWithOnFailureDataProvider
‪static isTransformedWithOnFailureDataProvider()
Definition: HtmlViewHelperTest.php:152
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest
Definition: HtmlViewHelperTest.php:31
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest\isTransformed
‪isTransformed(string $payload, string $expectation)
Definition: HtmlViewHelperTest.php:110
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest\setUp
‪setUp()
Definition: HtmlViewHelperTest.php:37
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform
Definition: HtmlViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform\HtmlViewHelperTest\isTransformedWithSelectorDataProvider
‪static isTransformedWithSelectorDataProvider()
Definition: HtmlViewHelperTest.php:117