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