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