‪TYPO3CMS  ‪main
TypolinkViewHelperTest.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;
29 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
30 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
31 use TYPO3Fluid\Fluid\View\TemplateView;
32 
33 final class ‪TypolinkViewHelperTest extends FunctionalTestCase
34 {
36 
40  protected const ‪LANGUAGE_PRESETS = [
41  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
42  ];
43 
44  protected array ‪$configurationToUseInTestInstance = [
45  'FE' => [
46  'cacheHash' => [
47  'enforceValidation' => false,
48  ],
49  ],
50  ];
51 
52  protected function ‪setUp(): void
53  {
54  parent::setUp();
55  $request = new ‪ServerRequest('http://localhost/');
56  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
57  $request = $request->withAttribute('routing', new ‪PageArguments(1, '0', []));
58  $request = $request->withAttribute('site', new ‪Site(
59  'site',
60  1,
61  [
62  'base' => 'http://localhost/',
63  'languages' => [],
64  ]
65  ));
66  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
67  }
68 
69  public static function ‪renderDataProvider(): array
70  {
71  return [
72  'link: default' => [
73  '<f:link.typolink parameter="1">This is a testlink</f:link.typolink>',
74  '<a href="/en/">This is a testlink</a>',
75  ],
76  'link: with add query string' => [
77  '<f:link.typolink parameter="1" addQueryString="untrusted">This is a testlink</f:link.typolink>',
78  '<a href="/en/?foo=bar&amp;temp=test&amp;cHash=286759dfcd3f566fa21091a0d77e9831">This is a testlink</a>',
79  ],
80  'link: with add query string and exclude' => [
81  '<f:link.typolink parameter="1" addQueryString="untrusted" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
82  '<a href="/en/?foo=bar&amp;cHash=afa4b37588ab917af3cfe2cd4464029d">This is a testlink</a>',
83  ],
84  't3://url link: default' => [
85  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude">This is a testlink</f:link.typolink>',
86  '<a href="https://example.org?param=1">This is a testlink</a>',
87  ],
88  't3://url link: with add query string' => [
89  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude" addQueryString="untrusted">This is a testlink</f:link.typolink>',
90  '<a href="https://example.org?param=1">This is a testlink</a>',
91  ],
92  't3://url link: with add query string and exclude' => [
93  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude" addQueryString="untrusted" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
94  '<a href="https://example.org?param=1">This is a testlink</a>',
95  ],
96  'mailto: link: default' => [
97  '<f:link.typolink parameter="mailto:foo@typo3.org">This is a testlink</f:link.typolink>',
98  '<a href="mailto:foo@typo3.org">This is a testlink</a>',
99  ],
100  'mailto: link: with add query string' => [
101  '<f:link.typolink parameter="mailto:foo@typo3.org" addQueryString="untrusted">This is a testlink</f:link.typolink>',
102  '<a href="mailto:foo@typo3.org">This is a testlink</a>',
103  ],
104  'mailto: link: with add query string and exclude' => [
105  '<f:link.typolink parameter="mailto:foo@typo3.org" addQueryString="untrusted" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
106  '<a href="mailto:foo@typo3.org">This is a testlink</a>',
107  ],
108  'http://: link: default' => [
109  '<f:link.typolink parameter="http://typo3.org/foo/?foo=bar">This is a testlink</f:link.typolink>',
110  '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
111  ],
112  'http://: link: with add query string' => [
113  '<f:link.typolink parameter="http://typo3.org/foo/?foo=bar" addQueryString="untrusted">This is a testlink</f:link.typolink>',
114  '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
115  ],
116  'http://: link: with add query string and exclude' => [
117  '<f:link.typolink parameter="http://typo3.org/foo/?foo=bar" addQueryString="untrusted" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
118  '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
119  ],
120  'page with complex title and extended parameters' => [
121  '<f:link.typolink parameter="1 - - \"a \\\"link\\\" title with \\\\\ \" &x=y">This is a testlink</f:link.typolink>',
122  '<a href="/en/?x=y&amp;cHash=fcdb7fbded8dc9d683ea83aee9909d99" title="a &quot;link&quot; title with \">This is a testlink</a>',
123  ],
124  'full parameter usage' => [
125  '<f:link.typolink parameter="1 _blank css-class \"testtitle with whitespace\" &X=y">This is a testlink</f:link.typolink>',
126  '<a href="/en/?X=y&amp;cHash=b8582914879e1ee43c72a4d26e4a4d98" target="_blank" title="testtitle with whitespace" class="css-class">This is a testlink</a>',
127  ],
128  't3:// with extended class' => [
129  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude - css-class">This is a testlink</f:link.typolink>',
130  '<a href="https://example.org?param=1" class="css-class">This is a testlink</a>',
131  ],
132  't3:// with complex title and extended parameters' => [
133  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude - - \"a \\\"link\\\" title with \\\\\ \" &x=y">This is a testlink</f:link.typolink>',
134  '<a href="https://example.org?param=1" title="a &quot;link&quot; title with \">This is a testlink</a>',
135  ],
136  't3:// with complex title and extended parameters & correctly encoded other parameter' => [
137  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1%26other=dude - - \"a \\\"link\\\" title with \\\\\ \" &x=y">This is a testlink</f:link.typolink>',
138  '<a href="https://example.org?param=1&amp;other=dude" title="a &quot;link&quot; title with \">This is a testlink</a>',
139  ],
140  ];
141  }
142 
143  #[DataProvider('renderDataProvider')]
144  #[Test]
145  public function ‪render(string $template, string $expected): void
146  {
147  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
149  'test',
150  $this->‪buildSiteConfiguration(1, '/'),
151  [
152  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
153  ]
154  );
155  (new ‪ConnectionPool())->getConnectionForTable('sys_template')->insert('sys_template', [
156  'pid' => 1,
157  'root' => 1,
158  'clear' => 1,
159  'config' => <<<EOT
160 page = PAGE
161 page.10 = FLUIDTEMPLATE
162 page.10 {
163  template = TEXT
164  template.value = $template
165 }
166 EOT
167  ]);
168  $response = $this->executeFrontendSubRequest(
169  (new InternalRequest())
170  ->withPageId(1)
171  ->withQueryParameter('foo', 'bar')
172  ->withQueryParameter('temp', 'test')
173  );
174  self::assertStringContainsString($expected, (string)$response->getBody());
175  }
176 
177  public static function renderWithAssignedParametersDataProvider(): array
178  {
179  return [
180  'target _self' => [
181  '<f:link.typolink parameter="{parameter}" parts-as="typoLinkParts">Individual {typoLinkParts.target} {typoLinkParts.class} {typoLinkParts.title}</f:link.typolink>',
182  [
183  'parameter' => 'http://typo3.org/ "_self" "<CSS>" "<Title>"',
184  ],
185  '<a href="http://typo3.org/" target="_self" title="&lt;Title&gt;" class="&lt;CSS&gt;">Individual _self &lt;CSS&gt; &lt;Title&gt;</a>',
186  ],
187  'target does not point to "self", adds noreferrer relationship' => [
188  '<f:link.typolink parameter="{parameter}" parts-as="typoLinkParts">Individual {typoLinkParts.target} {typoLinkParts.class} {typoLinkParts.title}</f:link.typolink>',
189  [
190  'parameter' => 'http://typo3.org/ "<Target>" "<CSS>" "<Title>"',
191  ],
192  '<a href="http://typo3.org/" target="&lt;Target&gt;" rel="noreferrer" title="&lt;Title&gt;" class="&lt;CSS&gt;">Individual &lt;Target&gt; &lt;CSS&gt; &lt;Title&gt;</a>',
193  ],
194  'typoLinkAdditionalAttributesAreRendered' => [
195  '<f:link.typolink parameter="{parameter}" additionalAttributes="{additionalAttributes}">Link Text</f:link.typolink>',
196  [
197  'parameter' => 'http://typo3.org/ "_self" "<CSS>" "<Title>"',
198  'additionalAttributes' => [
199  'data-bs-html' => '<div data-template="template">'
200  . '<img src="logo.png" alt="&quot;&lt;ALT&gt;&quot;"></div>',
201  'data-other' => '\'\'',
202  ],
203  ],
204  '<a href="http://typo3.org/" target="_self"'
205  . ' data-bs-html="&lt;div data-template=&quot;template&quot;&gt;'
206  . '&lt;img src=&quot;logo.png&quot; alt=&quot;&amp;quot;&amp;lt;ALT&amp;gt;&amp;quot;&quot;&gt;&lt;/div&gt;"'
207  . ' data-other="&#039;&#039;" title="&lt;Title&gt;" class="&lt;CSS&gt;">Link Text</a>',
208  ],
209  'parts-as is local variable' => [
210  '<f:link.typolink parameter="{parameter}" parts-as="typoLinkParts">{typoLinkParts.target}</f:link.typolink>{typoLinkParts.target}',
211  [
212  'parameter' => 'http://typo3.org/ "_self"',
213  ],
214  '<a href="http://typo3.org/" target="_self">_self</a>',
215  ],
216  ];
217  }
218 
219  #[DataProvider('renderWithAssignedParametersDataProvider')]
220  #[Test]
221  public function renderWithAssignedParameters(string $template, array $assigns, string $expected): void
222  {
223  $context = $this->get(RenderingContextFactory::class)->create();
224  $context->getTemplatePaths()->setTemplateSource($template);
225  $view = new TemplateView($context);
226  $view->assignMultiple($assigns);
227  self::assertSame($expected, trim($view->render()));
228  }
229 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪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\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪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\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_FE
‪const REQUESTTYPE_FE
Definition: SystemEnvironmentBuilder.php:43