‪TYPO3CMS  ‪main
ActionViewHelperTest.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;
33 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
34 
35 final class ‪ActionViewHelperTest extends FunctionalTestCase
36 {
38 
39  protected const ‪LANGUAGE_PRESETS = [
40  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
41  ];
42 
43  protected array ‪$configurationToUseInTestInstance = [
44  'FE' => [
45  'cacheHash' => [
46  'excludedParameters' => [
47  'untrusted',
48  ],
49  ],
50  ],
51  ];
52 
53  #[Test]
54  public function ‪renderThrowsExceptionWithoutARequest(): void
55  {
56  $this->expectException(\RuntimeException::class);
57  $this->expectExceptionCode(1690360598);
58  $view = new ‪StandaloneView();
59  $view->setRequest();
60  $view->setTemplateSource('<f:uri.action />');
61  $view->render();
62  }
63 
64  #[Test]
66  {
67  $request = new ‪ServerRequest();
68  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
69  $request = $request->withAttribute('routing', new ‪PageArguments(1, '0', []));
70  $this->expectException(\RuntimeException::class);
71  $this->expectExceptionCode(1639819692);
72  $view = new ‪StandaloneView();
73  $view->setRequest($request);
74  $view->setTemplateSource('<f:uri.action />');
75  $view->render();
76  }
77 
78  #[Test]
80  {
81  $request = new ‪ServerRequest('http://localhost/typo3/');
82  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
83  $request = $request->withQueryParams(['route' => 'web_layout']);
84  $this->expectException(\RuntimeException::class);
85  $this->expectExceptionCode(1690360598);
86  $view = new ‪StandaloneView();
87  $view->setRequest($request);
88  $view->setTemplateSource('<f:uri.action />');
89  $view->render();
90  }
91 
93  {
94  yield 'link to root page with plugin' => [
95  '<f:uri.action pageUid="1" extensionName="examples" pluginName="haiku" controller="Detail" action="show" />',
96  '/?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=5c6aa07f6ceee30ae2ea8dbf574cf26c',
97  ];
98 
99  yield 'link to root page with plugin and section' => [
100  '<f:uri.action pageUid="1" extensionName="examples" pluginName="haiku" controller="Detail" action="show" section="c13" />',
101  '/?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=5c6aa07f6ceee30ae2ea8dbf574cf26c#c13',
102  ];
103 
104  yield 'link to root page with page type' => [
105  '<f:uri.action pageUid="1" extensionName="examples" pluginName="haiku" controller="Detail" action="show" pageType="1234" />',
106  '/?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bcontroller%5D=Detail&amp;type=1234&amp;cHash=5c6aa07f6ceee30ae2ea8dbf574cf26c',
107  ];
108  }
109 
110  #[DataProvider('renderInFrontendWithCoreContextAndAllNecessaryExtbaseArgumentsDataProvider')]
111  #[Test]
112  public function ‪renderInFrontendWithCoreContextAndAllNecessaryExtbaseArguments(string $template, string $expected): void
113  {
114  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
116  'test',
117  $this->‪buildSiteConfiguration(1, '/'),
118  );
119  $request = new ‪ServerRequest();
120  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
121  $request = $request->withAttribute('routing', new ‪PageArguments(1, '0', ['untrusted' => 123]));
122  $view = new ‪StandaloneView();
123  $view->setRequest($request);
124  $view->setTemplateSource($template);
125  $result = $view->render();
126  self::assertSame($expected, $result);
127  }
128 
129  public static function ‪renderInFrontendWithExtbaseContextDataProvider(): \Generator
130  {
131  // with all extbase arguments provided
132  yield 'link to root page with plugin' => [
133  '<f:uri.action pageUid="1" extensionName="examples" pluginName="haiku" controller="Detail" action="show" />',
134  '/?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=5c6aa07f6ceee30ae2ea8dbf574cf26c',
135  ];
136 
137  yield 'link to root page with plugin and section' => [
138  '<f:uri.action pageUid="1" extensionName="examples" pluginName="haiku" controller="Detail" action="show" section="c13" />',
139  '/?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=5c6aa07f6ceee30ae2ea8dbf574cf26c#c13',
140  ];
141 
142  yield 'link to root page with page type' => [
143  '<f:uri.action pageUid="1" extensionName="examples" pluginName="haiku" controller="Detail" action="show" pageType="1234" />',
144  '/?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bcontroller%5D=Detail&amp;type=1234&amp;cHash=5c6aa07f6ceee30ae2ea8dbf574cf26c',
145  ];
146  // without all extbase arguments provided
147  yield 'renderWillProvideEmptyATagForNonValidLinkTarget' => [
148  '<f:uri.action />',
149  '/?tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=1d5a12de6bf2d5245b654deb866ee9c3',
150  ];
151  yield 'link to root page in extbase context' => [
152  '<f:uri.action pageUid="1" />',
153  '/?tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=1d5a12de6bf2d5245b654deb866ee9c3',
154  ];
155  yield 'link to root page with section' => [
156  '<f:uri.action pageUid="1" section="c13" />',
157  '/?tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=1d5a12de6bf2d5245b654deb866ee9c3#c13',
158  ];
159  yield 'link to root page with page type in extbase context' => [
160  '<f:uri.action pageUid="1" pageType="1234" />',
161  '/?tx_examples_haiku%5Bcontroller%5D=Detail&amp;type=1234&amp;cHash=1d5a12de6bf2d5245b654deb866ee9c3',
162  ];
163  yield 'link to root page with untrusted query arguments' => [
164  '<f:uri.action addQueryString="untrusted" />',
165  '/?tx_examples_haiku%5Bcontroller%5D=Detail&amp;untrusted=123&amp;cHash=1d5a12de6bf2d5245b654deb866ee9c3',
166  ];
167  yield 'link to page sub page' => [
168  '<f:uri.action pageUid="3" />',
169  '/dummy-1-2/dummy-1-2-3?tx_examples_haiku%5Bcontroller%5D=Detail&amp;cHash=d9289022f99f8cbc8080832f61e46509',
170  ];
171  yield 'arguments one level' => [
172  '<f:uri.action pageUid="3" arguments="{foo: \'bar\'}" />',
173  '/dummy-1-2/dummy-1-2-3?tx_examples_haiku%5Bcontroller%5D=Detail&amp;tx_examples_haiku%5Bfoo%5D=bar&amp;cHash=74dd4635cee85b19b67cd9b497ec99e9',
174  ];
175  yield 'additional parameters two levels' => [
176  '<f:uri.action pageUid="3" additionalParams="{tx_examples_haiku: {action: \'show\', haiku: 42}}" />',
177  '/dummy-1-2/dummy-1-2-3?tx_examples_haiku%5Baction%5D=show&amp;tx_examples_haiku%5Bcontroller%5D=Detail&amp;tx_examples_haiku%5Bhaiku%5D=42&amp;cHash=aefc37bc2323ebd8c8e39c222adb7413',
178  ];
179  }
180 
181  #[DataProvider('renderInFrontendWithExtbaseContextDataProvider')]
182  #[Test]
183  public function ‪renderInFrontendWithExtbaseContext(string $template, string $expected): void
184  {
185  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
187  'test',
188  $this->‪buildSiteConfiguration(1, '/'),
189  );
190  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
191  $frontendTypoScript->setSetupArray([]);
192  $frontendTypoScript->setConfigArray([]);
193  $extbaseRequestParameters = new ‪ExtbaseRequestParameters();
194  $extbaseRequestParameters->setControllerExtensionName('Examples');
195  $extbaseRequestParameters->setControllerName('Detail');
196  $extbaseRequestParameters->setControllerActionName('show');
197  $extbaseRequestParameters->setPluginName('Haiku');
198  $request = new ‪ServerRequest();
199  $request = $request->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
200  $request = $request->withAttribute('routing', new ‪PageArguments(1, '0', ['untrusted' => 123]));
201  $request = $request->withAttribute('extbase', $extbaseRequestParameters);
202  $request = $request->withAttribute('currentContentObject', $this->get(ContentObjectRenderer::class));
203  $request = $request->withAttribute('frontend.typoscript', $frontendTypoScript);
204  $pageInformation = new ‪PageInformation();
205  $pageInformation->setId(1);
206  $request = $request->withAttribute('frontend.page.information', $pageInformation);
207  $request = new ‪Request($request);
208  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
209  $view = new ‪StandaloneView();
210  $view->setRequest($request);
211  $view->setTemplateSource($template);
212  $result = $view->render();
213  self::assertSame($expected, $result);
214  }
215 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\renderInFrontendWithCoreContextAndAllNecessaryExtbaseArgumentsDataProvider
‪static renderInFrontendWithCoreContextAndAllNecessaryExtbaseArgumentsDataProvider()
Definition: ActionViewHelperTest.php:91
‪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\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\renderThrowsExceptionWithoutARequest
‪renderThrowsExceptionWithoutARequest()
Definition: ActionViewHelperTest.php:53
‪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\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\renderInFrontendCoreContextThrowsExceptionWithIncompleteArguments
‪renderInFrontendCoreContextThrowsExceptionWithIncompleteArguments()
Definition: ActionViewHelperTest.php:64
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\$configurationToUseInTestInstance
‪array $configurationToUseInTestInstance
Definition: ActionViewHelperTest.php:42
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\renderInFrontendWithExtbaseContextDataProvider
‪static renderInFrontendWithExtbaseContextDataProvider()
Definition: ActionViewHelperTest.php:128
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: ActionViewHelperTest.php:38
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest
Definition: ActionViewHelperTest.php:36
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri
Definition: ActionViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\renderInFrontendWithCoreContextAndAllNecessaryExtbaseArguments
‪renderInFrontendWithCoreContextAndAllNecessaryExtbaseArguments(string $template, string $expected)
Definition: ActionViewHelperTest.php:111
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\renderInBackendCoreContextThrowsExceptionWithIncompleteArguments
‪renderInBackendCoreContextThrowsExceptionWithIncompleteArguments()
Definition: ActionViewHelperTest.php:78
‪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\Fluid\Tests\Functional\ViewHelpers\Uri\ActionViewHelperTest\renderInFrontendWithExtbaseContext
‪renderInFrontendWithExtbaseContext(string $template, string $expected)
Definition: ActionViewHelperTest.php:182
‪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