‪TYPO3CMS  ‪main
PageViewHelperTest.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;
34 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
35 
36 final class ‪PageViewHelperTest extends FunctionalTestCase
37 {
39 
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  'excludedParameters' => [
48  'untrusted',
49  ],
50  ],
51  ],
52  ];
53 
54  #[Test]
55  public function ‪renderThrowsExceptionWithoutRequest(): void
56  {
57  $this->expectException(\RuntimeException::class);
58  $this->expectExceptionCode(1639819269);
59  $view = new ‪StandaloneView();
60  $view->setRequest();
61  $view->setTemplateSource('<f:link.page>foo</f:link.page>');
62  $view->render();
63  }
64 
65  #[Test]
67  {
68  $request = new ‪ServerRequest();
69  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
70  $view = new ‪StandaloneView();
71  $view->setRequest($request);
72  $view->setTemplateSource('<f:link.page>foo</f:link.page>');
73  $result = $view->render();
74  self::assertSame('foo', $result);
75  }
76 
77  #[Test]
79  {
80  $request = new ‪ServerRequest();
81  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
82  $request = $request->withQueryParams(['route' => 'web_layout']);
83  $view = new ‪StandaloneView();
84  $view->setRequest($request);
85  $view->setTemplateSource('<f:link.page addQueryString="1" pageUid="42">foo</f:link.page>');
86  $result = $view->render();
87  self::assertSame('<a href="/typo3/module/web/layout?token=dummyToken&amp;id=42">foo</a>', $result);
88  }
89 
90  #[Test]
92  {
93  $request = new ‪ServerRequest();
94  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
95  $view = new ‪StandaloneView();
96  $view->setRequest($request);
97  $view->setTemplateSource('<f:link.page additionalParams="{\'route\': \'web_layout\'}" pageUid="42">foo</f:link.page>');
98  $result = $view->render();
99  self::assertSame('<a href="/typo3/module/web/layout?token=dummyToken&amp;id=42">foo</a>', $result);
100  }
101 
102  #[Test]
104  {
105  $request = new ‪ServerRequest();
106  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
107  $request = $request->withAttribute('route', new ‪Route('dummy', ['_identifier' => 'web_layout']));
108  $view = new ‪StandaloneView();
109  $view->setRequest($request);
110  $view->setTemplateSource('<f:link.page pageUid="42">foo</f:link.page>');
111  $result = $view->render();
112  self::assertSame('<a href="/typo3/module/web/layout?token=dummyToken&amp;id=42">foo</a>', $result);
113  }
114 
115  #[Test]
116  public function ‪renderInBackendCoreContextAddsSection(): void
117  {
118  $request = new ‪ServerRequest('http://localhost/typo3/');
119  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
120  $request = $request->withAttribute('route', new ‪Route('dummy', ['_identifier' => 'web_layout']));
121  $view = new ‪StandaloneView();
122  $view->setRequest($request);
123  $view->setTemplateSource('<f:link.page pageUid="42" section="mySection">foo</f:link.page>');
124  $result = $view->render();
125  self::assertSame('<a href="/typo3/module/web/layout?token=dummyToken&amp;id=42#mySection">foo</a>', $result);
126  }
127 
128  #[Test]
130  {
131  $request = new ‪ServerRequest('http://localhost/typo3', null, 'php://input', [], ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => '/index.php']);
132  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
133  $request = $request->withAttribute('route', new ‪Route('dummy', ['_identifier' => 'web_layout']));
134  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
135  $view = new ‪StandaloneView();
136  $view->setRequest($request);
137  $view->setTemplateSource('<f:link.page pageUid="42" absolute="1">foo</f:link.page>');
138  $result = $view->render();
139  self::assertSame('<a href="http://localhost/typo3/module/web/layout?token=dummyToken&amp;id=42">foo</a>', $result);
140  }
141 
142  #[Test]
144  {
145  $request = new ‪ServerRequest('http://localhost/typo3/');
146  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
147  $request = $request->withAttribute('route', new ‪Route('module/web/layout', ['_identifier' => 'web_layout']));
148  $request = $request->withQueryParams(['id' => 42]);
149  $request = $request->withAttribute('extbase', new ‪ExtbaseRequestParameters());
150  $request = new ‪Request($request);
151  $view = new ‪StandaloneView();
152  $view->setRequest($request);
153  $view->setTemplateSource('<f:link.page>foo</f:link.page>');
154  $result = $view->render();
155  self::assertSame('<a href="/typo3/module/web/layout?token=dummyToken&amp;id=42">foo</a>', $result);
156  }
157 
158  #[Test]
160  {
161  $request = new ‪ServerRequest('http://localhost/typo3/', null, 'php://input', [], ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => 'typo3/index.php']);
162  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
163  $request = $request->withAttribute('route', new ‪Route('module/web/layout', ['_identifier' => 'web_layout']));
164  $request = $request->withQueryParams(['id' => 42]);
165  $request = $request->withAttribute('extbase', new ‪ExtbaseRequestParameters());
166  $request = new ‪Request($request);
167  $view = new ‪StandaloneView();
168  $view->setRequest($request);
169  $view->setTemplateSource('<f:link.page absolute="1">foo</f:link.page>');
170  $result = $view->render();
171  self::assertSame('<a href="http://localhost/typo3/module/web/layout?token=dummyToken&amp;id=42">foo</a>', $result);
172  }
173 
174  public static function ‪renderDataProvider(): array
175  {
176  return [
177  'renderProvidesATagForValidLinkTarget' => [
178  '<f:link.page>index.php</f:link.page>',
179  '<a href="/">index.php</a>',
180  ],
181  'renderWillProvideEmptyATagForNonValidLinkTarget' => [
182  '<f:link.page></f:link.page>',
183  '<a href="/"></a>',
184  ],
185  'link to root page' => [
186  '<f:link.page pageUid="1">linkMe</f:link.page>',
187  '<a href="/">linkMe</a>',
188  ],
189  'link to root page with section' => [
190  '<f:link.page pageUid="1" section="c13">linkMe</f:link.page>',
191  '<a href="/#c13">linkMe</a>',
192  ],
193  'link to root page with page type' => [
194  '<f:link.page pageUid="1" pageType="1234">linkMe</f:link.page>',
195  '<a href="/?type=1234">linkMe</a>',
196  ],
197  'link to root page with untrusted query arguments' => [
198  '<f:link.page addQueryString="untrusted"></f:link.page>',
199  '<a href="/?untrusted=123"></a>',
200  ],
201  'link to page sub page' => [
202  '<f:link.page pageUid="3">linkMe</f:link.page>',
203  '<a href="/dummy-1-2/dummy-1-2-3">linkMe</a>',
204  ],
205  'additional parameters one level' => [
206  '<f:link.page pageUid="3" additionalParams="{tx_examples_haiku: \'foo\'}">haiku title</f:link.page>',
207  '<a href="/dummy-1-2/dummy-1-2-3?tx_examples_haiku=foo&amp;cHash=3ed8716f46e97ba37335fa4b28ce2d8a">haiku title</a>',
208  ],
209  'additional parameters two levels' => [
210  '<f:link.page pageUid="3" additionalParams="{tx_examples_haiku: {action: \'show\', haiku: 42}}">haiku title</f:link.page>',
211  '<a href="/dummy-1-2/dummy-1-2-3?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bhaiku%5D=42&amp;cHash=1e0eb1e54d6bacf0138a50107c6ae29a">haiku title</a>',
212  ],
213  // see: https://forge.typo3.org/issues/101432
214  'link with target renders the correct target attribute if intTarget is configured' => [
215  '<f:link.page pageUid="3" target="home">link me</f:link.page>',
216  '<a target="home" href="/dummy-1-2/dummy-1-2-3">link me</a>',
217  [
218  'config.' => [
219  'intTarget' => '_self',
220  ],
221  ],
222  ],
223  // see: https://forge.typo3.org/issues/101432
224  'link skips configured intTarget if no target viewhelper attribute is provided' => [
225  '<f:link.page pageUid="3">link me</f:link.page>',
226  '<a href="/dummy-1-2/dummy-1-2-3">link me</a>',
227  [
228  'config' => [
229  'intTarget' => '_self',
230  ],
231  ],
232  ],
233  ];
234  }
235 
236  #[DataProvider('renderDataProvider')]
237  #[Test]
238  public function ‪renderInFrontendWithCoreContext(string $template, string $expected, array $frontendTypoScriptConfigArray = []): void
239  {
240  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
242  'test',
243  $this->‪buildSiteConfiguration(1, '/'),
244  );
245  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
246  $frontendTypoScript->setSetupArray([]);
247  $frontendTypoScript->setConfigArray($frontendTypoScriptConfigArray);
248  $request = new ‪ServerRequest('http://localhost/typo3/');
249  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
250  $request = $request->withAttribute('routing', new ‪PageArguments(1, '0', ['untrusted' => 123]));
251  $request = $request->withAttribute('currentContentObject', $this->get(ContentObjectRenderer::class));
252  $request = $request->withAttribute('frontend.typoscript', $frontendTypoScript);
253  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
254  $view = new ‪StandaloneView();
255  $view->setRequest($request);
256  $view->setTemplateSource($template);
257  $result = $view->render();
258  self::assertSame($expected, $result);
259  }
260 
261  #[DataProvider('renderDataProvider')]
262  #[Test]
263  public function ‪renderInFrontendWithExtbaseContext(string $template, string $expected, array $frontendTypoScriptSetupArray = [], array $tsfeConfigArray = []): void
264  {
265  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
267  'test',
268  $this->‪buildSiteConfiguration(1, '/'),
269  );
270  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
271  $frontendTypoScript->setSetupArray($frontendTypoScriptSetupArray);
272  $frontendTypoScript->setConfigArray([]);
273  $request = new ‪ServerRequest();
274  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
275  $request = $request->withAttribute('routing', new ‪PageArguments(1, '0', ['untrusted' => 123]));
276  $request = $request->withAttribute('extbase', new ‪ExtbaseRequestParameters());
277  $request = $request->withAttribute('currentContentObject', $this->get(ContentObjectRenderer::class));
278  $request = $request->withAttribute('frontend.typoscript', $frontendTypoScript);
279  $pageInformation = new ‪PageInformation();
280  $pageInformation->setId(1);
281  $request = $request->withAttribute('frontend.page.information', $pageInformation);
282  $request = new ‪Request($request);
283  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
284  $view = new ‪StandaloneView();
285  $view->setRequest($request);
286  $view->setTemplateSource($template);
287  $result = $view->render();
288  self::assertSame($expected, $result);
289  }
290 }
‪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\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\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters
Definition: ExtbaseRequestParameters.php:35
‪TYPO3\CMS\Core\TypoScript\FrontendTypoScript
Definition: FrontendTypoScript.php:30
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_FE
‪const REQUESTTYPE_FE
Definition: SystemEnvironmentBuilder.php:43
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:35
‪TYPO3\CMS\Frontend\Page\PageInformation
Definition: PageInformation.php:28