[TASK] Make frontend/Tests/Unit/Controller notice free
[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 * Tests concerning domainNameMatchesCurrentRequest
149 */
150
151 /**
152 * @return array
153 */
154 public function domainNameMatchesCurrentRequestDataProvider()
155 {
156 return [
157 'same domains' => [
158 'typo3.org',
159 'typo3.org',
160 '/index.php',
161 true,
162 ],
163 'same domains with subdomain' => [
164 'www.typo3.org',
165 'www.typo3.org',
166 '/index.php',
167 true,
168 ],
169 'different domains' => [
170 'foo.bar',
171 'typo3.org',
172 '/index.php',
173 false,
174 ],
175 'domain record with script name' => [
176 'typo3.org',
177 'typo3.org/foo/bar',
178 '/foo/bar/index.php',
179 true,
180 ],
181 'domain record with wrong script name' => [
182 'typo3.org',
183 'typo3.org/foo/bar',
184 '/bar/foo/index.php',
185 false,
186 ],
187 ];
188 }
189
190 /**
191 * @param string $currentDomain
192 * @param string $domainRecord
193 * @param string $scriptName
194 * @param bool $expectedResult
195 * @test
196 * @dataProvider domainNameMatchesCurrentRequestDataProvider
197 */
198 public function domainNameMatchesCurrentRequest($currentDomain, $domainRecord, $scriptName, $expectedResult)
199 {
200 $_SERVER['HTTP_HOST'] = $currentDomain;
201 $_SERVER['SCRIPT_NAME'] = $scriptName;
202 $this->assertEquals($expectedResult, $this->subject->domainNameMatchesCurrentRequest($domainRecord));
203 }
204
205 /**
206 * @return array
207 */
208 public function baseUrlWrapHandlesDifferentUrlsDataProvider()
209 {
210 return [
211 'without base url' => [
212 '',
213 'fileadmin/user_uploads/image.jpg',
214 'fileadmin/user_uploads/image.jpg'
215 ],
216 'with base url' => [
217 'http://www.google.com/',
218 'fileadmin/user_uploads/image.jpg',
219 'http://www.google.com/fileadmin/user_uploads/image.jpg'
220 ],
221 'without base url but with url prepended with a forward slash' => [
222 '',
223 '/fileadmin/user_uploads/image.jpg',
224 '/fileadmin/user_uploads/image.jpg',
225 ],
226 'with base url but with url prepended with a forward slash' => [
227 'http://www.google.com/',
228 '/fileadmin/user_uploads/image.jpg',
229 '/fileadmin/user_uploads/image.jpg',
230 ],
231 ];
232 }
233
234 /**
235 * @dataProvider baseUrlWrapHandlesDifferentUrlsDataProvider
236 * @test
237 * @param string $baseUrl
238 * @param string $url
239 * @param string $expected
240 */
241 public function baseUrlWrapHandlesDifferentUrls($baseUrl, $url, $expected)
242 {
243 $this->subject->baseUrl = $baseUrl;
244 $this->assertSame($expected, $this->subject->baseUrlWrap($url));
245 }
246
247 /**
248 * @return array
249 */
250 public function initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider()
251 {
252 return [
253 'one simple search word' => [
254 ['test'],
255 false,
256 'test',
257 ],
258 'one simple search word with standalone words' => [
259 ['test'],
260 true,
261 '[[:space:]]test[[:space:]]',
262 ],
263 'two simple search words' => [
264 ['test', 'test2'],
265 false,
266 'test|test2',
267 ],
268 'two simple search words with standalone words' => [
269 ['test', 'test2'],
270 true,
271 '[[:space:]]test[[:space:]]|[[:space:]]test2[[:space:]]',
272 ],
273 'word with regex chars' => [
274 ['A \\ word with / a bunch of [] regex () chars .*'],
275 false,
276 'A \\\\ word with \\/ a bunch of \\[\\] regex \\(\\) chars \\.\\*',
277 ],
278 ];
279 }
280
281 /**
282 * @test
283 * @dataProvider initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider
284 *
285 * @param array $searchWordGetParameters The values that should be loaded in the sword_list GET parameter.
286 * @param bool $enableStandaloneSearchWords If TRUE the sword_standAlone option will be enabled.
287 * @param string $expectedRegex The expected regex after processing the search words.
288 */
289 public function initializeSearchWordDataInTsfeBuildsCorrectRegex(array $searchWordGetParameters, $enableStandaloneSearchWords, $expectedRegex)
290 {
291 $_GET['sword_list'] = $searchWordGetParameters;
292
293 $this->subject->page = [];
294 if ($enableStandaloneSearchWords) {
295 $this->subject->config = ['config' => ['sword_standAlone' => 1]];
296 }
297
298 $this->subject->preparePageContentGeneration();
299 $this->assertEquals($this->subject->sWordRegEx, $expectedRegex);
300 }
301
302 /**
303 * @test
304 * @dataProvider splitLinkVarsDataProvider
305 *
306 * @param $string
307 * @param $expected
308 */
309 public function splitLinkVarsStringSplitsStringByComma($string, $expected)
310 {
311 $this->assertEquals($expected, $this->subject->_callRef('splitLinkVarsString', $string));
312 }
313
314 /**
315 * @return array
316 */
317 public function splitLinkVarsDataProvider()
318 {
319 return [
320 [
321 'L',
322 ['L']
323 ],
324 [
325 'L,a',
326 [
327 'L',
328 'a'
329 ]
330 ],
331 [
332 'L, a',
333 [
334 'L',
335 'a'
336 ]
337 ],
338 [
339 'L , a',
340 [
341 'L',
342 'a'
343 ]
344 ],
345 [
346 ' L, a ',
347 [
348 'L',
349 'a'
350 ]
351 ],
352 [
353 'L(1)',
354 [
355 'L(1)'
356 ]
357 ],
358 [
359 'L(1),a',
360 [
361 'L(1)',
362 'a'
363 ]
364 ],
365 [
366 'L(1) , a',
367 [
368 'L(1)',
369 'a'
370 ]
371 ],
372 [
373 'a,L(1)',
374 [
375 'a',
376 'L(1)'
377 ]
378 ],
379 [
380 'L(1),a(2-3)',
381 [
382 'L(1)',
383 'a(2-3)'
384 ]
385 ],
386 [
387 'L(1),a((2-3))',
388 [
389 'L(1)',
390 'a((2-3))'
391 ]
392 ],
393 [
394 'L(1),a(a{2,4})',
395 [
396 'L(1)',
397 'a(a{2,4})'
398 ]
399 ],
400 [
401 'L(1),a(/a{2,4}\,()/)',
402 [
403 'L(1)',
404 'a(/a{2,4}\,()/)'
405 ]
406 ],
407 [
408 'L,a , b(c) , dd(/g{1,2}/), eee(, ()f) , 2',
409 [
410 'L',
411 'a',
412 'b(c)',
413 'dd(/g{1,2}/)',
414 'eee(, ()f)',
415 '2'
416 ]
417 ]
418 ];
419 }
420
421 /**
422 * @test
423 * @dataProvider calculateLinkVarsDataProvider
424 * @param string $linkVars
425 * @param array $getVars
426 * @param string $expected
427 */
428 public function calculateLinkVarsConsidersCorrectVariables(string $linkVars, array $getVars, string $expected)
429 {
430 $_GET = $getVars;
431 $this->subject->config['config']['linkVars'] = $linkVars;
432 $this->subject->calculateLinkVars();
433 $this->assertEquals($expected, $this->subject->linkVars);
434 }
435
436 public function calculateLinkVarsDataProvider(): array
437 {
438 return [
439 'simple variable' => [
440 'L',
441 [
442 'L' => 1
443 ],
444 '&L=1'
445 ],
446 'missing variable' => [
447 'L',
448 [
449 ],
450 ''
451 ],
452 'restricted variables' => [
453 'L(1-3),bar(3),foo(array),blub(array)',
454 [
455 'L' => 1,
456 'bar' => 2,
457 'foo' => [ 1, 2, 'f' => [ 4, 5 ] ],
458 'blub' => 123
459 ],
460 '&L=1&foo[0]=1&foo[1]=2&foo[f][0]=4&foo[f][1]=5'
461 ],
462 'nested variables' => [
463 'bar|foo(1-2)',
464 [
465 'bar' => [
466 'foo' => 1,
467 'unused' => 'never'
468 ]
469 ],
470 '&bar[foo]=1'
471 ],
472 ];
473 }
474 }