[TASK] Centralize sys_domain resolving
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / Controller / TypoScriptFrontendControllerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Tests\Unit\Controller;
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
18 use TYPO3\CMS\Core\Context\Context;
19 use TYPO3\CMS\Core\Page\PageRenderer;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
22 use TYPO3\CMS\Frontend\Page\PageRepository;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 /**
26 * Test case
27 */
28 class TypoScriptFrontendControllerTest extends UnitTestCase
29 {
30 /**
31 * @var bool Reset singletons created by subject
32 */
33 protected $resetSingletonInstances = true;
34
35 /**
36 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|TypoScriptFrontendController
37 */
38 protected $subject;
39
40 protected function setUp()
41 {
42 GeneralUtility::flushInternalRuntimeCaches();
43 $this->subject = $this->getAccessibleMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
44 $this->subject->_set('context', new Context());
45 $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '170928423746123078941623042360abceb12341234231';
46
47 $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
48 $this->subject->sys_page = $pageRepository;
49
50 $pageRenderer = $this->getMockBuilder(PageRenderer::class)->getMock();
51 $this->subject->_set('pageRenderer', $pageRenderer);
52 }
53
54 /**
55 * Tests concerning rendering content
56 */
57
58 /**
59 * @test
60 */
61 public function headerAndFooterMarkersAreReplacedDuringIntProcessing()
62 {
63 $GLOBALS['TSFE'] = $this->setupTsfeMockForHeaderFooterReplacementCheck();
64 $GLOBALS['TSFE']->INTincScript();
65 $this->assertContains('headerData', $GLOBALS['TSFE']->content);
66 $this->assertContains('footerData', $GLOBALS['TSFE']->content);
67 }
68
69 /**
70 * This is the callback that mimics a USER_INT extension
71 */
72 public function INTincScript_processCallback()
73 {
74 $GLOBALS['TSFE']->additionalHeaderData[] = 'headerData';
75 $GLOBALS['TSFE']->additionalFooterData[] = 'footerData';
76 }
77
78 /**
79 * Setup a \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController object only for testing the header and footer
80 * replacement during USER_INT rendering
81 *
82 * @return \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController
83 */
84 protected function setupTsfeMockForHeaderFooterReplacementCheck()
85 {
86 /** @var \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController $tsfe */
87 $tsfe = $this->getMockBuilder(TypoScriptFrontendController::class)
88 ->setMethods([
89 'INTincScript_process',
90 'INTincScript_loadJSCode',
91 'setAbsRefPrefix',
92 'regeneratePageTitle'
93 ])->disableOriginalConstructor()
94 ->getMock();
95 $tsfe->expects($this->exactly(2))->method('INTincScript_process')->will($this->returnCallback([$this, 'INTincScript_processCallback']));
96 $tsfe->content = file_get_contents(__DIR__ . '/Fixtures/renderedPage.html');
97 $config = [
98 'INTincScript_ext' => [
99 'divKey' => '679b52796e75d474ccbbed486b6837ab',
100 ],
101 'INTincScript' => [
102 'INT_SCRIPT.679b52796e75d474ccbbed486b6837ab' => [],
103 ]
104 ];
105 $tsfe->config = $config;
106
107 return $tsfe;
108 }
109
110 /**
111 * Tests concerning sL
112 */
113
114 /**
115 * @test
116 */
117 public function localizationReturnsUnchangedStringIfNotLocallangLabel()
118 {
119 $string = $this->getUniqueId();
120 $this->subject->page = [];
121 $this->subject->settingLanguage();
122 $this->assertEquals($string, $this->subject->sL($string));
123 }
124
125 /**
126 * Tests concerning getSysDomainCache
127 */
128
129 /**
130 * @return array
131 */
132 public function getSysDomainCacheDataProvider()
133 {
134 return [
135 'typo3.org' => [
136 'typo3.org',
137 ],
138 'foo.bar' => [
139 'foo.bar',
140 ],
141 'example.com' => [
142 'example.com',
143 ],
144 ];
145 }
146
147 /**
148 * @return array
149 */
150 public function baseUrlWrapHandlesDifferentUrlsDataProvider()
151 {
152 return [
153 'without base url' => [
154 '',
155 'fileadmin/user_uploads/image.jpg',
156 'fileadmin/user_uploads/image.jpg'
157 ],
158 'with base url' => [
159 'http://www.google.com/',
160 'fileadmin/user_uploads/image.jpg',
161 'http://www.google.com/fileadmin/user_uploads/image.jpg'
162 ],
163 'without base url but with url prepended with a forward slash' => [
164 '',
165 '/fileadmin/user_uploads/image.jpg',
166 '/fileadmin/user_uploads/image.jpg',
167 ],
168 'with base url but with url prepended with a forward slash' => [
169 'http://www.google.com/',
170 '/fileadmin/user_uploads/image.jpg',
171 '/fileadmin/user_uploads/image.jpg',
172 ],
173 ];
174 }
175
176 /**
177 * @dataProvider baseUrlWrapHandlesDifferentUrlsDataProvider
178 * @test
179 * @param string $baseUrl
180 * @param string $url
181 * @param string $expected
182 */
183 public function baseUrlWrapHandlesDifferentUrls($baseUrl, $url, $expected)
184 {
185 $this->subject->baseUrl = $baseUrl;
186 $this->assertSame($expected, $this->subject->baseUrlWrap($url));
187 }
188
189 /**
190 * @return array
191 */
192 public function initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider()
193 {
194 return [
195 'one simple search word' => [
196 ['test'],
197 false,
198 'test',
199 ],
200 'one simple search word with standalone words' => [
201 ['test'],
202 true,
203 '[[:space:]]test[[:space:]]',
204 ],
205 'two simple search words' => [
206 ['test', 'test2'],
207 false,
208 'test|test2',
209 ],
210 'two simple search words with standalone words' => [
211 ['test', 'test2'],
212 true,
213 '[[:space:]]test[[:space:]]|[[:space:]]test2[[:space:]]',
214 ],
215 'word with regex chars' => [
216 ['A \\ word with / a bunch of [] regex () chars .*'],
217 false,
218 'A \\\\ word with \\/ a bunch of \\[\\] regex \\(\\) chars \\.\\*',
219 ],
220 ];
221 }
222
223 /**
224 * @test
225 * @dataProvider initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider
226 *
227 * @param array $searchWordGetParameters The values that should be loaded in the sword_list GET parameter.
228 * @param bool $enableStandaloneSearchWords If TRUE the sword_standAlone option will be enabled.
229 * @param string $expectedRegex The expected regex after processing the search words.
230 */
231 public function initializeSearchWordDataInTsfeBuildsCorrectRegex(array $searchWordGetParameters, $enableStandaloneSearchWords, $expectedRegex)
232 {
233 $_GET['sword_list'] = $searchWordGetParameters;
234
235 $this->subject->page = [];
236 if ($enableStandaloneSearchWords) {
237 $this->subject->config = ['config' => ['sword_standAlone' => 1]];
238 }
239
240 $this->subject->preparePageContentGeneration();
241 $this->assertEquals($this->subject->sWordRegEx, $expectedRegex);
242 }
243
244 /**
245 * @test
246 * @dataProvider splitLinkVarsDataProvider
247 *
248 * @param $string
249 * @param $expected
250 */
251 public function splitLinkVarsStringSplitsStringByComma($string, $expected)
252 {
253 $this->assertEquals($expected, $this->subject->_callRef('splitLinkVarsString', $string));
254 }
255
256 /**
257 * @return array
258 */
259 public function splitLinkVarsDataProvider()
260 {
261 return [
262 [
263 'L',
264 ['L']
265 ],
266 [
267 'L,a',
268 [
269 'L',
270 'a'
271 ]
272 ],
273 [
274 'L, a',
275 [
276 'L',
277 'a'
278 ]
279 ],
280 [
281 'L , a',
282 [
283 'L',
284 'a'
285 ]
286 ],
287 [
288 ' L, a ',
289 [
290 'L',
291 'a'
292 ]
293 ],
294 [
295 'L(1)',
296 [
297 'L(1)'
298 ]
299 ],
300 [
301 'L(1),a',
302 [
303 'L(1)',
304 'a'
305 ]
306 ],
307 [
308 'L(1) , a',
309 [
310 'L(1)',
311 'a'
312 ]
313 ],
314 [
315 'a,L(1)',
316 [
317 'a',
318 'L(1)'
319 ]
320 ],
321 [
322 'L(1),a(2-3)',
323 [
324 'L(1)',
325 'a(2-3)'
326 ]
327 ],
328 [
329 'L(1),a((2-3))',
330 [
331 'L(1)',
332 'a((2-3))'
333 ]
334 ],
335 [
336 'L(1),a(a{2,4})',
337 [
338 'L(1)',
339 'a(a{2,4})'
340 ]
341 ],
342 [
343 'L(1),a(/a{2,4}\,()/)',
344 [
345 'L(1)',
346 'a(/a{2,4}\,()/)'
347 ]
348 ],
349 [
350 'L,a , b(c) , dd(/g{1,2}/), eee(, ()f) , 2',
351 [
352 'L',
353 'a',
354 'b(c)',
355 'dd(/g{1,2}/)',
356 'eee(, ()f)',
357 '2'
358 ]
359 ]
360 ];
361 }
362
363 /**
364 * @test
365 * @dataProvider calculateLinkVarsDataProvider
366 * @param string $linkVars
367 * @param array $getVars
368 * @param string $expected
369 */
370 public function calculateLinkVarsConsidersCorrectVariables(string $linkVars, array $getVars, string $expected)
371 {
372 $_GET = $getVars;
373 $this->subject->config['config']['linkVars'] = $linkVars;
374 $this->subject->calculateLinkVars();
375 $this->assertEquals($expected, $this->subject->linkVars);
376 }
377
378 public function calculateLinkVarsDataProvider(): array
379 {
380 return [
381 'simple variable' => [
382 'L',
383 [
384 'L' => 1
385 ],
386 '&L=1'
387 ],
388 'missing variable' => [
389 'L',
390 [
391 ],
392 ''
393 ],
394 'restricted variables' => [
395 'L(1-3),bar(3),foo(array),blub(array)',
396 [
397 'L' => 1,
398 'bar' => 2,
399 'foo' => [ 1, 2, 'f' => [ 4, 5 ] ],
400 'blub' => 123
401 ],
402 '&L=1&foo[0]=1&foo[1]=2&foo[f][0]=4&foo[f][1]=5'
403 ],
404 'nested variables' => [
405 'bar|foo(1-2)',
406 [
407 'bar' => [
408 'foo' => 1,
409 'unused' => 'never'
410 ]
411 ],
412 '&bar[foo]=1'
413 ],
414 ];
415 }
416 }