‪TYPO3CMS  11.5
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\Framework\Frontend\InternalRequest;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪TypolinkViewHelperTest extends FunctionalTestCase
27 {
29 
33  protected const ‪LANGUAGE_PRESETS = [
34  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
35  ];
36 
38  'FE' => [
39  'cacheHash' => [
40  'enforceValidation' => false,
41  ],
42  ],
43  ];
44 
45  public function ‪renderDataProvider(): array
46  {
47  return [
48  'link: default' => [
49  '<f:link.typolink parameter="1">This is a testlink</f:link.typolink>',
50  '<a href="/en/">This is a testlink</a>',
51  ],
52  'link: with add query string' => [
53  '<f:link.typolink parameter="1" addQueryString="true">This is a testlink</f:link.typolink>',
54  '<a href="/en/?foo=bar&amp;temp=test&amp;cHash=286759dfcd3f566fa21091a0d77e9831">This is a testlink</a>',
55  ],
56  'link: with add query string and exclude' => [
57  '<f:link.typolink parameter="1" addQueryString="true" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
58  '<a href="/en/?foo=bar&amp;cHash=afa4b37588ab917af3cfe2cd4464029d">This is a testlink</a>',
59  ],
60  't3://url link: default' => [
61  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude">This is a testlink</f:link.typolink>',
62  '<a href="https://example.org?param=1">This is a testlink</a>',
63  ],
64  't3://url link: with add query string' => [
65  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude" addQueryString="true">This is a testlink</f:link.typolink>',
66  '<a href="https://example.org?param=1">This is a testlink</a>',
67  ],
68  't3://url link: with add query string and exclude' => [
69  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude" addQueryString="true" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
70  '<a href="https://example.org?param=1">This is a testlink</a>',
71  ],
72  'mailto: link: default' => [
73  '<f:link.typolink parameter="mailto:foo@typo3.org">This is a testlink</f:link.typolink>',
74  '<a href="mailto:foo@typo3.org">This is a testlink</a>',
75  ],
76  'mailto: link: with add query string' => [
77  '<f:link.typolink parameter="mailto:foo@typo3.org" addQueryString="true">This is a testlink</f:link.typolink>',
78  '<a href="mailto:foo@typo3.org">This is a testlink</a>',
79  ],
80  'mailto: link: with add query string and exclude' => [
81  '<f:link.typolink parameter="mailto:foo@typo3.org" addQueryString="true" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
82  '<a href="mailto:foo@typo3.org">This is a testlink</a>',
83  ],
84  'http://: link: default' => [
85  '<f:link.typolink parameter="http://typo3.org/foo/?foo=bar">This is a testlink</f:link.typolink>',
86  '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
87  ],
88  'http://: link: with add query string' => [
89  '<f:link.typolink parameter="http://typo3.org/foo/?foo=bar" addQueryString="true">This is a testlink</f:link.typolink>',
90  '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
91  ],
92  'http://: link: with add query string and exclude' => [
93  '<f:link.typolink parameter="http://typo3.org/foo/?foo=bar" addQueryString="true" addQueryStringExclude="temp">This is a testlink</f:link.typolink>',
94  '<a href="http://typo3.org/foo/?foo=bar">This is a testlink</a>',
95  ],
96  'page with complex title and extended parameters' => [
97  '<f:link.typolink parameter="1 - - \"a \\\"link\\\" title with \\\\\ \" &x=y">This is a testlink</f:link.typolink>',
98  '<a href="/en/?x=y&amp;cHash=fcdb7fbded8dc9d683ea83aee9909d99" title="a &quot;link&quot; title with \">This is a testlink</a>',
99  ],
100  'full parameter usage' => [
101  '<f:link.typolink parameter="1 _blank css-class \"testtitle with whitespace\" &X=y">This is a testlink</f:link.typolink>',
102  '<a href="/en/?X=y&amp;cHash=b8582914879e1ee43c72a4d26e4a4d98" title="testtitle with whitespace" target="_blank" class="css-class">This is a testlink</a>',
103  ],
104  't3:// with extended class' => [
105  '<f:link.typolink parameter="t3://url?url=https://example.org?param=1&other=dude - css-class">This is a testlink</f:link.typolink>',
106  '<a href="https://example.org?param=1" class="css-class">This is a testlink</a>',
107  ],
108  't3:// with complex title and extended parameters' => [
109  '<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>',
110  '<a href="https://example.org?param=1" title="a &quot;link&quot; title with \">This is a testlink</a>',
111  ],
112  't3:// with complex title and extended parameters & correctly encoded other parameter' => [
113  '<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>',
114  '<a href="https://example.org?param=1&amp;other=dude" title="a &quot;link&quot; title with \">This is a testlink</a>',
115  ],
116  ];
117  }
118 
123  public function ‪render(string $template, string $expected): void
124  {
125  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
127  'test',
128  $this->‪buildSiteConfiguration(1, '/'),
129  [
130  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
131  ]
132  );
133  (new ‪ConnectionPool())->getConnectionForTable('sys_template')->insert('sys_template', [
134  'pid' => 1,
135  'root' => 1,
136  'clear' => 1,
137  'config' => <<<EOT
138 page = PAGE
139 page.10 = FLUIDTEMPLATE
140 page.10 {
141  template = TEXT
142  template.value = $template
143 }
144 EOT
145  ]);
146  $response = $this->executeFrontendSubRequest(
147  (new InternalRequest())
148  ->withPageId(1)
149  ->withQueryParameter('foo', 'bar')
150  ->withQueryParameter('temp', 'test')
151  );
152  self::assertStringContainsString($expected, (string)$response->getBody());
153  }
154 
155  public function renderWithAssignedParametersDataProvider(): array
156  {
157  return [
158  'target _self' => [
159  '<f:link.typolink parameter="{parameter}" parts-as="typoLinkParts">Individual {typoLinkParts.target} {typoLinkParts.class} {typoLinkParts.title}</f:link.typolink>',
160  [
161  'parameter' => 'http://typo3.org/ "_self" "<CSS>" "<Title>"',
162  ],
163  '<a href="http://typo3.org/" title="&lt;Title&gt;" target="_self" class="&lt;CSS&gt;">Individual _self &lt;CSS&gt; &lt;Title&gt;</a>',
164  ],
165  'target does not point to "self", adds noreferrer relationship' => [
166  '<f:link.typolink parameter="{parameter}" parts-as="typoLinkParts">Individual {typoLinkParts.target} {typoLinkParts.class} {typoLinkParts.title}</f:link.typolink>',
167  [
168  'parameter' => 'http://typo3.org/ "<Target>" "<CSS>" "<Title>"',
169  ],
170  '<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>',
171  ],
172  'typoLinkAdditionalAttributesAreRendered' => [
173  '<f:link.typolink parameter="{parameter}" additionalAttributes="{additionalAttributes}">Link Text</f:link.typolink>',
174  [
175  'parameter' => 'http://typo3.org/ "_self" "<CSS>" "<Title>"',
176  'additionalAttributes' => [
177  'data-bs-html' => '<div data-template="template">'
178  . '<img src="logo.png" alt="&quot;&lt;ALT&gt;&quot;"></div>',
179  'data-other' => '\'\'',
180  ],
181  ],
182  // check against correct value regarding php 8.1 change of default argument values of flags for ex. htmlspecialchars()
183  // @todo remove conditional values when php 8.1 is min requirement
184  (PHP_VERSION_ID < 80100
185  // before php 8.1 - remove this for >php8.1 only
186  ? '<a href="http://typo3.org/" title="&lt;Title&gt;" target="_self"'
187  . ' class="&lt;CSS&gt;" data-bs-html="&lt;div data-template=&quot;template&quot;&gt;'
188  . '&lt;img src=&quot;logo.png&quot; alt=&quot;&amp;quot;&amp;lt;ALT&amp;gt;&amp;quot;&quot;&gt;&lt;/div&gt;"'
189  . ' data-other="\'\'">Link Text</a>'
190  // for php 8.1
191  : '<a href="http://typo3.org/" title="&lt;Title&gt;" target="_self"'
192  . ' class="&lt;CSS&gt;" data-bs-html="&lt;div data-template=&quot;template&quot;&gt;'
193  . '&lt;img src=&quot;logo.png&quot; alt=&quot;&amp;quot;&amp;lt;ALT&amp;gt;&amp;quot;&quot;&gt;&lt;/div&gt;"'
194  . ' data-other="&#039;&#039;">Link Text</a>'),
195  ],
196  ];
197  }
198 
203  public function renderWithAssignedParameters(string $template, array $assigns, string $expected): void
204  {
205  $view = new ‪StandaloneView();
206  $view->setTemplateSource($template);
207  $view->assignMultiple($assigns);
208  self::assertSame($expected, trim($view->render()));
209  }
210 }
‪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\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:126
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:111
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46