‪TYPO3CMS  10.4
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 
23 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
24 
25 class ‪TypolinkViewHelperTest extends FunctionalTestCase
26 {
28 
29  private const ‪TEMPLATE_BASE_PATH = 'typo3/sysext/fluid/Tests/Functional/ViewHelpers/Fixtures/';
30 
34  protected const ‪LANGUAGE_PRESETS = [
35  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
36  ];
37 
39  'FE' => [
40  'encryptionKey' => '12345',
41  'cacheHash' => [
42  'enforceValidation' => false,
43  ],
44  ],
45  ];
46 
50  protected ‪$testExtensionsToLoad = ['typo3/sysext/fluid/Tests/Functional/Fixtures/Extensions/fluid_test'];
51 
55  protected ‪$coreExtensionsToLoad = ['fluid'];
56 
57  protected function ‪setUp(): void
58  {
59  parent::setUp();
60 
61  $this->importDataSet('typo3/sysext/fluid/Tests/Functional/ViewHelpers/Fixtures/pages.xml');
63  'test',
64  $this->‪buildSiteConfiguration(1, '/'),
65  [
66  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
67  ],
68  [
69  $this->‪buildErrorHandlingConfiguration('Fluid', [404])
70  ]
71  );
72  $_GET = [
73  'foo' => 'bar',
74  'temp' => 'test',
75  ];
76  $_SERVER['HTTP_HOST'] = 'example.com';
77  $_SERVER['REQUEST_URI'] = '/en/';
78  GeneralUtility::flushInternalRuntimeCaches();
79  }
80 
92  public function ‪renderCreatesCorrectLink(
93  $parameter,
94  bool $addQueryString,
95  string $addQueryStringMethod,
96  string $addQueryStringExclude,
97  string $expected,
98  string $template
99  ) {
100  $view = new ‪StandaloneView();
101  $view->setTemplatePathAndFilename('typo3/sysext/fluid/Tests/Functional/ViewHelpers/Fixtures/' . $template . '.html');
102  $view->assignMultiple([
103  'parameter' => $parameter,
104  'uid' => 1,
105  'addQueryString' => $addQueryString,
106  'addQueryStringMethod' => $addQueryStringMethod,
107  'addQueryStringExclude' => $addQueryStringExclude,
108  ]);
109  self::assertEquals($expected, trim(preg_replace('/\s+/', ' ', $view->render())));
110  }
111 
115  public function ‪renderCreatesCorrectLinkProvider(): array
116  {
117  return [
118  'link: default' => [
119  'parameter' => 1,
120  'addQueryString' => false,
121  'addQueryStringMethod' => 'GET',
122  'addQueryStringExclude' => '',
123  'expected' => '<a href="/en/">This is a testlink</a> <a href="/en/">This is a testlink</a>',
124  'template' => 'link_typolink_viewhelper',
125  ],
126  'link: with add query string' => [
127  'parameter' => 1,
128  'addQueryString' => true,
129  'addQueryStringMethod' => 'GET',
130  'addQueryStringExclude' => '',
131  'expected' => '<a href="/en/?foo=bar&amp;temp=test&amp;cHash=286759dfcd3f566fa21091a0d77e9831">This is a testlink</a> <a href="/en/">This is a testlink</a>',
132  'template' => 'link_typolink_viewhelper',
133  ],
134  'link: with add query string and exclude' => [
135  'parameter' => 1,
136  'addQueryString' => true,
137  'addQueryStringMethod' => 'GET',
138  'addQueryStringExclude' => 'temp',
139  'expected' => '<a href="/en/?foo=bar&amp;cHash=afa4b37588ab917af3cfe2cd4464029d">This is a testlink</a> <a href="/en/">This is a testlink</a>',
140  'template' => 'link_typolink_viewhelper',
141  ],
142  'uri: default' => [
143  'parameter' => 1,
144  'addQueryString' => false,
145  'addQueryStringMethod' => 'GET',
146  'addQueryStringExclude' => '',
147  'expected' => '/en/ /en/',
148  'template' => 'uri_typolink_viewhelper',
149  ],
150  'uri: with add query string' => [
151  'parameter' => 1,
152  'addQueryString' => true,
153  'addQueryStringMethod' => 'GET',
154  'addQueryStringExclude' => '',
155  'expected' => '/en/?foo=bar&amp;temp=test&amp;cHash=286759dfcd3f566fa21091a0d77e9831 /en/',
156  'template' => 'uri_typolink_viewhelper',
157  ],
158  'uri: with add query string and exclude' => [
159  'parameter' => 1,
160  'addQueryString' => true,
161  'addQueryStringMethod' => 'GET',
162  'addQueryStringExclude' => 'temp',
163  'expected' => '/en/?foo=bar&amp;cHash=afa4b37588ab917af3cfe2cd4464029d /en/',
164  'template' => 'uri_typolink_viewhelper',
165  ],
166  't3://url link: default' => [
167  'parameter' => 't3://url?url=https://example.org?param=1&other=dude',
168  'addQueryString' => false,
169  'addQueryStringMethod' => 'GET',
170  'addQueryStringExclude' => '',
171  'expected' => '<a href="https://example.org?param=1">This is a testlink</a> <a href="https://example.org?param=1">This is a testlink</a>',
172  'template' => 'link_typolink_viewhelper',
173  ],
174  't3://url link: with add query string' => [
175  'parameter' => 't3://url?url=https://example.org?param=1&other=dude',
176  'addQueryString' => true,
177  'addQueryStringMethod' => 'GET',
178  'addQueryStringExclude' => '',
179  'expected' => '<a href="https://example.org?param=1">This is a testlink</a> <a href="https://example.org?param=1">This is a testlink</a>',
180  'template' => 'link_typolink_viewhelper',
181  ],
182  't3://url link: with add query string and exclude' => [
183  'parameter' => 't3://url?url=https://example.org?param=1&other=dude',
184  'addQueryString' => true,
185  'addQueryStringMethod' => 'GET',
186  'addQueryStringExclude' => 'temp',
187  'expected' => '<a href="https://example.org?param=1">This is a testlink</a> <a href="https://example.org?param=1">This is a testlink</a>',
188  'template' => 'link_typolink_viewhelper',
189  ],
190  't3://url uri: default' => [
191  'parameter' => 't3://url?url=https://example.org?param=1&other=dude',
192  'addQueryString' => false,
193  'addQueryStringMethod' => 'GET',
194  'addQueryStringExclude' => '',
195  'expected' => 'https://example.org?param=1 https://example.org?param=1',
196  'template' => 'uri_typolink_viewhelper',
197  ],
198  't3://url uri: with add query string' => [
199  'parameter' => 't3://url?url=https://example.org?param=1&other=dude',
200  'addQueryString' => true,
201  'addQueryStringMethod' => 'GET',
202  'addQueryStringExclude' => '',
203  'expected' => 'https://example.org?param=1 https://example.org?param=1',
204  'template' => 'uri_typolink_viewhelper',
205  ],
206  't3://url uri: with add query string and exclude' => [
207  'parameter' => 't3://url?url=https://example.org?param=1&other=dude',
208  'addQueryString' => true,
209  'addQueryStringMethod' => 'GET',
210  'addQueryStringExclude' => 'temp',
211  'expected' => 'https://example.org?param=1 https://example.org?param=1',
212  'template' => 'uri_typolink_viewhelper',
213  ],
214  'mailto: link: default' => [
215  'parameter' => 'mailto:foo@typo3.org',
216  'addQueryString' => false,
217  'addQueryStringMethod' => 'GET',
218  'addQueryStringExclude' => '',
219  'expected' => '<a href="mailto:foo@typo3.org">This is a testlink</a> <a href="mailto:foo@typo3.org">This is a testlink</a>',
220  'template' => 'link_typolink_viewhelper',
221  ],
222  'mailto: link: with add query string' => [
223  'parameter' => 'mailto:foo@typo3.org',
224  'addQueryString' => true,
225  'addQueryStringMethod' => 'GET',
226  'addQueryStringExclude' => '',
227  'expected' => '<a href="mailto:foo@typo3.org">This is a testlink</a> <a href="mailto:foo@typo3.org">This is a testlink</a>',
228  'template' => 'link_typolink_viewhelper',
229  ],
230  'mailto: link: with add query string and exclude' => [
231  'parameter' => 'mailto:foo@typo3.org',
232  'addQueryString' => true,
233  'addQueryStringMethod' => 'GET',
234  'addQueryStringExclude' => 'temp',
235  'expected' => '<a href="mailto:foo@typo3.org">This is a testlink</a> <a href="mailto:foo@typo3.org">This is a testlink</a>',
236  'template' => 'link_typolink_viewhelper',
237  ],
238  'mailto: uri: default' => [
239  'parameter' => 'mailto:foo@typo3.org',
240  'addQueryString' => false,
241  'addQueryStringMethod' => 'GET',
242  'addQueryStringExclude' => '',
243  'expected' => 'mailto:foo@typo3.org mailto:foo@typo3.org',
244  'template' => 'uri_typolink_viewhelper',
245  ],
246  'mailto: uri: with add query string' => [
247  'parameter' => 'mailto:foo@typo3.org',
248  'addQueryString' => true,
249  'addQueryStringMethod' => 'GET',
250  'addQueryStringExclude' => '',
251  'expected' => 'mailto:foo@typo3.org mailto:foo@typo3.org',
252  'template' => 'uri_typolink_viewhelper',
253  ],
254  'mailto: uri: with add query string and exclude' => [
255  'parameter' => 'mailto:foo@typo3.org',
256  'addQueryString' => true,
257  'addQueryStringMethod' => 'GET',
258  'addQueryStringExclude' => 'temp',
259  'expected' => 'mailto:foo@typo3.org mailto:foo@typo3.org',
260  'template' => 'uri_typolink_viewhelper',
261  ],
262  'http://: link: default' => [
263  'parameter' => 'http://typo3.org/foo/?foo=bar',
264  'addQueryString' => false,
265  'addQueryStringMethod' => 'GET',
266  'addQueryStringExclude' => '',
267  'expected' => '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a> <a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
268  'template' => 'link_typolink_viewhelper',
269  ],
270  'http://: link: with add query string' => [
271  'parameter' => 'http://typo3.org/foo/?foo=bar',
272  'addQueryString' => true,
273  'addQueryStringMethod' => 'GET',
274  'addQueryStringExclude' => '',
275  'expected' => '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a> <a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
276  'template' => 'link_typolink_viewhelper',
277  ],
278  'http://: link: with add query string and exclude' => [
279  'parameter' => 'http://typo3.org/foo/?foo=bar',
280  'addQueryString' => true,
281  'addQueryStringMethod' => 'GET',
282  'addQueryStringExclude' => 'temp',
283  'expected' => '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a> <a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
284  'template' => 'link_typolink_viewhelper',
285  ],
286  'http://: uri: default' => [
287  'parameter' => 'http://typo3.org/foo/?foo=bar',
288  'addQueryString' => false,
289  'addQueryStringMethod' => 'GET',
290  'addQueryStringExclude' => '',
291  'expected' => 'http://typo3.org/foo/?foo=bar http://typo3.org/foo/?foo=bar',
292  'template' => 'uri_typolink_viewhelper',
293  ],
294  'http://: uri: with add query string' => [
295  'parameter' => 'http://typo3.org/foo/?foo=bar',
296  'addQueryString' => true,
297  'addQueryStringMethod' => 'GET',
298  'addQueryStringExclude' => '',
299  'expected' => 'http://typo3.org/foo/?foo=bar http://typo3.org/foo/?foo=bar',
300  'template' => 'uri_typolink_viewhelper',
301  ],
302  'http://: uri: with add query string and exclude' => [
303  'parameter' => 'http://typo3.org/foo/?foo=bar',
304  'addQueryString' => true,
305  'addQueryStringMethod' => 'GET',
306  'addQueryStringExclude' => 'temp',
307  'expected' => 'http://typo3.org/foo/?foo=bar http://typo3.org/foo/?foo=bar',
308  'template' => 'uri_typolink_viewhelper',
309  ],
310 
311  'page with complex title and extended parameters' => [
312  'parameter' => '1 - - "a \\"link\\" title with \\\\" &x=y',
313  'addQueryString' => false,
314  'addQueryStringMethod' => '',
315  'addQueryStringExclude' => '',
316  'expected' => '<a href="/en/?x=y&amp;cHash=fcdb7fbded8dc9d683ea83aee9909d99" title="a &quot;link&quot; title with \">This is a testlink</a> <a href="/en/?x=y&amp;cHash=fcdb7fbded8dc9d683ea83aee9909d99" title="a &quot;link&quot; title with \">This is a testlink</a>',
317  'template' => 'link_typolink_viewhelper',
318  ],
319  'full parameter usage' => [
320  '1 _blank css-class "testtitle with whitespace" &X=y',
321  'addQueryString' => false,
322  'addQueryStringMethod' => '',
323  'addQueryStringExclude' => '',
324  'expected' => '<a href="/en/?X=y&amp;cHash=b8582914879e1ee43c72a4d26e4a4d98" title="testtitle with whitespace" target="_blank" class="css-class">This is a testlink</a> <a href="/en/?X=y&amp;cHash=b8582914879e1ee43c72a4d26e4a4d98" title="testtitle with whitespace" target="_blank" class="css-class">This is a testlink</a>',
325  'template' => 'link_typolink_viewhelper',
326  ],
327  't3:// with extended class' => [
328  't3://url?url=https://example.org?param=1&other=dude - css-class',
329  'addQueryString' => false,
330  'addQueryStringMethod' => '',
331  'addQueryStringExclude' => '',
332  'expected' => '<a href="https://example.org?param=1" class="css-class">This is a testlink</a> <a href="https://example.org?param=1" class="css-class">This is a testlink</a>',
333  'template' => 'link_typolink_viewhelper',
334  ],
335  't3:// with complex title and extended parameters' => [
336  't3://url?url=https://example.org?param=1&other=dude - - "a \\"link\\" title with \\\\" &x=y',
337  'addQueryString' => false,
338  'addQueryStringMethod' => '',
339  'addQueryStringExclude' => '',
340  'expected' => '<a href="https://example.org?param=1" title="a &quot;link&quot; title with \">This is a testlink</a> <a href="https://example.org?param=1" title="a &quot;link&quot; title with \">This is a testlink</a>',
341  'template' => 'link_typolink_viewhelper',
342  ],
343  't3:// with complex title and extended parameters & correctly encoded other parameter' => [
344  't3://url?url=https://example.org?param=1%26other=dude - - "a \\"link\\" title with \\\\" &x=y',
345  'addQueryString' => false,
346  'addQueryStringMethod' => '',
347  'addQueryStringExclude' => '',
348  'expected' => '<a href="https://example.org?param=1&amp;other=dude" title="a &quot;link&quot; title with \">This is a testlink</a> <a href="https://example.org?param=1&amp;other=dude" title="a &quot;link&quot; title with \">This is a testlink</a>',
349  'template' => 'link_typolink_viewhelper',
350  ],
351  ];
352  }
353 
354  public function ‪typoLinkPartsAreRenderedDataProvider(): array
355  {
356  return [
357  [
358  'http://typo3.org/ "_self" "<CSS>" "<Title>"',
359  '<a href="http://typo3.org/" title="&lt;Title&gt;" target="_self" class="&lt;CSS&gt;">Individual _self &lt;CSS&gt; &lt;Title&gt;</a>',
360  ],
361  [
362  'http://typo3.org/ "<Target>" "<CSS>" "<Title>"', // target does not point to "self", adds noreferrer relationship
363  '<a href="http://typo3.org/" title="&lt;Title&gt;" target="&lt;Target&gt;" class="&lt;CSS&gt;" rel="noreferrer">Individual &lt;Target&gt; &lt;CSS&gt; &lt;Title&gt;</a>',
364  ],
365  ];
366  }
367 
375  public function ‪typoLinkPartsAreRendered(string $parameter, string $expectation): void
376  {
377  $view = new ‪StandaloneView();
378  $view->setTemplatePathAndFilename('typo3/sysext/fluid/Tests/Functional/ViewHelpers/Fixtures/link_typolink_parts.html');
379  $view->assignMultiple(['parameter' => $parameter]);
380  self::assertSame($expectation, trim($view->render()));
381  }
382 
384  {
385  return [
386  [
387  [
388  'parameter' => 'http://typo3.org/ "_self" "<CSS>" "<Title>"',
389  'additionalAttributes' => [
390  'data-html' => '<div data-template="template">'
391  . '<img src="logo.png" alt="&quot;&lt;ALT&gt;&quot;"></div>',
392  'data-other' => '\'\'',
393  ],
394  ],
395  '<a href="http://typo3.org/" title="&lt;Title&gt;" target="_self"'
396  . ' class="&lt;CSS&gt;" data-html="&lt;div data-template=&quot;template&quot;&gt;'
397  . '&lt;img src=&quot;logo.png&quot; alt=&quot;&amp;quot;&amp;lt;ALT&amp;gt;&amp;quot;&quot;&gt;&lt;/div&gt;"'
398  . ' data-other="\'\'">Link Text</a>'
399  ]
400  ];
401  }
402 
410  public function ‪typoLinkAdditionalAttributesAreRendered(array $configuration, string $expectation): void
411  {
412  $view = new ‪StandaloneView();
413  $view->setTemplatePathAndFilename(
414  self::TEMPLATE_BASE_PATH . 'link_typolink_additionalAttributes.html'
415  );
416  $view->assignMultiple($configuration);
417  self::assertSame($expectation, trim($view->render()));
418  }
419 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildErrorHandlingConfiguration
‪array buildErrorHandlingConfiguration(string $handler, array $codes)
Definition: SiteBasedTestTrait.php:184
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:36
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:58
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:124
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:109
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46