9b93c7d55003a236cd097ebc245d581b5b6348ff
[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->subject->page = [];
112 $this->subject->settingLanguage();
113 $this->assertEquals($string, $this->subject->sL($string));
114 }
115
116 /**
117 * Tests concerning getSysDomainCache
118 */
119
120 /**
121 * @return array
122 */
123 public function getSysDomainCacheDataProvider()
124 {
125 return [
126 'typo3.org' => [
127 'typo3.org',
128 ],
129 'foo.bar' => [
130 'foo.bar',
131 ],
132 'example.com' => [
133 'example.com',
134 ],
135 ];
136 }
137
138 /**
139 * Tests concerning domainNameMatchesCurrentRequest
140 */
141
142 /**
143 * @return array
144 */
145 public function domainNameMatchesCurrentRequestDataProvider()
146 {
147 return [
148 'same domains' => [
149 'typo3.org',
150 'typo3.org',
151 '/index.php',
152 true,
153 ],
154 'same domains with subdomain' => [
155 'www.typo3.org',
156 'www.typo3.org',
157 '/index.php',
158 true,
159 ],
160 'different domains' => [
161 'foo.bar',
162 'typo3.org',
163 '/index.php',
164 false,
165 ],
166 'domain record with script name' => [
167 'typo3.org',
168 'typo3.org/foo/bar',
169 '/foo/bar/index.php',
170 true,
171 ],
172 'domain record with wrong script name' => [
173 'typo3.org',
174 'typo3.org/foo/bar',
175 '/bar/foo/index.php',
176 false,
177 ],
178 ];
179 }
180
181 /**
182 * @param string $currentDomain
183 * @param string $domainRecord
184 * @param string $scriptName
185 * @param bool $expectedResult
186 * @test
187 * @dataProvider domainNameMatchesCurrentRequestDataProvider
188 */
189 public function domainNameMatchesCurrentRequest($currentDomain, $domainRecord, $scriptName, $expectedResult)
190 {
191 $_SERVER['HTTP_HOST'] = $currentDomain;
192 $_SERVER['SCRIPT_NAME'] = $scriptName;
193 $this->assertEquals($expectedResult, $this->subject->domainNameMatchesCurrentRequest($domainRecord));
194 }
195
196 /**
197 * @return array
198 */
199 public function baseUrlWrapHandlesDifferentUrlsDataProvider()
200 {
201 return [
202 'without base url' => [
203 '',
204 'fileadmin/user_uploads/image.jpg',
205 'fileadmin/user_uploads/image.jpg'
206 ],
207 'with base url' => [
208 'http://www.google.com/',
209 'fileadmin/user_uploads/image.jpg',
210 'http://www.google.com/fileadmin/user_uploads/image.jpg'
211 ],
212 'without base url but with url prepended with a forward slash' => [
213 '',
214 '/fileadmin/user_uploads/image.jpg',
215 '/fileadmin/user_uploads/image.jpg',
216 ],
217 'with base url but with url prepended with a forward slash' => [
218 'http://www.google.com/',
219 '/fileadmin/user_uploads/image.jpg',
220 '/fileadmin/user_uploads/image.jpg',
221 ],
222 ];
223 }
224
225 /**
226 * @dataProvider baseUrlWrapHandlesDifferentUrlsDataProvider
227 * @test
228 * @param string $baseUrl
229 * @param string $url
230 * @param string $expected
231 */
232 public function baseUrlWrapHandlesDifferentUrls($baseUrl, $url, $expected)
233 {
234 $this->subject->baseUrl = $baseUrl;
235 $this->assertSame($expected, $this->subject->baseUrlWrap($url));
236 }
237
238 /**
239 * @return array
240 */
241 public function initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider()
242 {
243 return [
244 'one simple search word' => [
245 ['test'],
246 false,
247 'test',
248 ],
249 'one simple search word with standalone words' => [
250 ['test'],
251 true,
252 '[[:space:]]test[[:space:]]',
253 ],
254 'two simple search words' => [
255 ['test', 'test2'],
256 false,
257 'test|test2',
258 ],
259 'two simple search words with standalone words' => [
260 ['test', 'test2'],
261 true,
262 '[[:space:]]test[[:space:]]|[[:space:]]test2[[:space:]]',
263 ],
264 'word with regex chars' => [
265 ['A \\ word with / a bunch of [] regex () chars .*'],
266 false,
267 'A \\\\ word with \\/ a bunch of \\[\\] regex \\(\\) chars \\.\\*',
268 ],
269 ];
270 }
271
272 /**
273 * @test
274 * @dataProvider initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider
275 *
276 * @param array $searchWordGetParameters The values that should be loaded in the sword_list GET parameter.
277 * @param bool $enableStandaloneSearchWords If TRUE the sword_standAlone option will be enabled.
278 * @param string $expectedRegex The expected regex after processing the search words.
279 */
280 public function initializeSearchWordDataInTsfeBuildsCorrectRegex(array $searchWordGetParameters, $enableStandaloneSearchWords, $expectedRegex)
281 {
282 $_GET['sword_list'] = $searchWordGetParameters;
283
284 $this->subject->page = [];
285 if ($enableStandaloneSearchWords) {
286 $this->subject->config = ['config' => ['sword_standAlone' => 1]];
287 }
288
289 $this->subject->preparePageContentGeneration();
290 $this->assertEquals($this->subject->sWordRegEx, $expectedRegex);
291 }
292
293 /**
294 * @test
295 * @dataProvider splitLinkVarsDataProvider
296 *
297 * @param $string
298 * @param $expected
299 */
300 public function splitLinkVarsStringSplitsStringByComma($string, $expected)
301 {
302 $this->assertEquals($expected, $this->subject->_callRef('splitLinkVarsString', $string));
303 }
304
305 /**
306 * @return array
307 */
308 public function splitLinkVarsDataProvider()
309 {
310 return [
311 [
312 'L',
313 ['L']
314 ],
315 [
316 'L,a',
317 [
318 'L',
319 'a'
320 ]
321 ],
322 [
323 'L, a',
324 [
325 'L',
326 'a'
327 ]
328 ],
329 [
330 'L , a',
331 [
332 'L',
333 'a'
334 ]
335 ],
336 [
337 ' L, a ',
338 [
339 'L',
340 'a'
341 ]
342 ],
343 [
344 'L(1)',
345 [
346 'L(1)'
347 ]
348 ],
349 [
350 'L(1),a',
351 [
352 'L(1)',
353 'a'
354 ]
355 ],
356 [
357 'L(1) , a',
358 [
359 'L(1)',
360 'a'
361 ]
362 ],
363 [
364 'a,L(1)',
365 [
366 'a',
367 'L(1)'
368 ]
369 ],
370 [
371 'L(1),a(2-3)',
372 [
373 'L(1)',
374 'a(2-3)'
375 ]
376 ],
377 [
378 'L(1),a((2-3))',
379 [
380 'L(1)',
381 'a((2-3))'
382 ]
383 ],
384 [
385 'L(1),a(a{2,4})',
386 [
387 'L(1)',
388 'a(a{2,4})'
389 ]
390 ],
391 [
392 'L(1),a(/a{2,4}\,()/)',
393 [
394 'L(1)',
395 'a(/a{2,4}\,()/)'
396 ]
397 ],
398 [
399 'L,a , b(c) , dd(/g{1,2}/), eee(, ()f) , 2',
400 [
401 'L',
402 'a',
403 'b(c)',
404 'dd(/g{1,2}/)',
405 'eee(, ()f)',
406 '2'
407 ]
408 ]
409 ];
410 }
411 }