[BUGFIX] PHP 7.1 unit tests
[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\Cache\CacheManager;
18 use TYPO3\CMS\Core\Database\DatabaseConnection;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
21 use TYPO3\CMS\Frontend\Page\PageRepository;
22
23 /**
24 * Testcase for TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
25 */
26 class TypoScriptFrontendControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
27 {
28 /**
29 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|TypoScriptFrontendController
30 */
31 protected $subject;
32
33 protected function setUp()
34 {
35 GeneralUtility::flushInternalRuntimeCaches();
36 $this->subject = $this->getAccessibleMock(TypoScriptFrontendController::class, array('dummy'), array(), '', false);
37 $this->subject->TYPO3_CONF_VARS = $GLOBALS['TYPO3_CONF_VARS'];
38 $this->subject->TYPO3_CONF_VARS['SYS']['encryptionKey'] = '170928423746123078941623042360abceb12341234231';
39
40 $pageRepository = $this->getMock(PageRepository::class);
41 $this->subject->sys_page = $pageRepository;
42 }
43
44 /**
45 * Tests concerning rendering content
46 */
47
48 /**
49 * @test
50 */
51 public function headerAndFooterMarkersAreReplacedDuringIntProcessing()
52 {
53 $GLOBALS['TSFE'] = $this->setupTsfeMockForHeaderFooterReplacementCheck();
54 $GLOBALS['TSFE']->INTincScript();
55 $this->assertContains('headerData', $GLOBALS['TSFE']->content);
56 $this->assertContains('footerData', $GLOBALS['TSFE']->content);
57 }
58
59 /**
60 * This is the callback that mimics a USER_INT extension
61 */
62 public function INTincScript_processCallback()
63 {
64 $GLOBALS['TSFE']->additionalHeaderData[] = 'headerData';
65 $GLOBALS['TSFE']->additionalFooterData[] = 'footerData';
66 }
67
68 /**
69 * Setup a \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController object only for testing the header and footer
70 * replacement during USER_INT rendering
71 *
72 * @return \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController
73 */
74 protected function setupTsfeMockForHeaderFooterReplacementCheck()
75 {
76 /** @var \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController $tsfe */
77 $tsfe = $this->getMock(TypoScriptFrontendController::class, array(
78 'INTincScript_process',
79 'INTincScript_includeLibs',
80 'INTincScript_loadJSCode',
81 'setAbsRefPrefix',
82 'regeneratePageTitle'
83 ), array(), '', false);
84 $tsfe->expects($this->exactly(2))->method('INTincScript_process')->will($this->returnCallback(array($this, 'INTincScript_processCallback')));
85 $tsfe->content = file_get_contents(__DIR__ . '/Fixtures/renderedPage.html');
86 $config = [
87 'INTincScript_ext' => [
88 'divKey' => '679b52796e75d474ccbbed486b6837ab',
89 ],
90 'INTincScript' => [
91 'INT_SCRIPT.679b52796e75d474ccbbed486b6837ab' => [],
92 ]
93 ];
94 $tsfe->config = $config;
95 $GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker();
96
97 return $tsfe;
98 }
99
100 /**
101 * Tests concerning sL
102 */
103
104 /**
105 * @test
106 */
107 public function localizationReturnsUnchangedStringIfNotLocallangLabel()
108 {
109 $string = $this->getUniqueId();
110 $this->assertEquals($string, $this->subject->sL($string));
111 }
112
113 /**
114 * Tests concerning getSysDomainCache
115 */
116
117 /**
118 * @return array
119 */
120 public function getSysDomainCacheDataProvider()
121 {
122 return array(
123 'typo3.org' => array(
124 'typo3.org',
125 ),
126 'foo.bar' => array(
127 'foo.bar',
128 ),
129 'example.com' => array(
130 'example.com',
131 ),
132 );
133 }
134
135 /**
136 * @param string $currentDomain
137 * @test
138 * @dataProvider getSysDomainCacheDataProvider
139 */
140 public function getSysDomainCacheReturnsCurrentDomainRecord($currentDomain)
141 {
142 $_SERVER['HTTP_HOST'] = $currentDomain;
143 $domainRecords = array(
144 'typo3.org' => array(
145 'uid' => '1',
146 'pid' => '1',
147 'domainName' => 'typo3.org',
148 'forced' => 0,
149 ),
150 'foo.bar' => array(
151 'uid' => '2',
152 'pid' => '1',
153 'domainName' => 'foo.bar',
154 'forced' => 0,
155 ),
156 'example.com' => array(
157 'uid' => '3',
158 'pid' => '1',
159 'domainName' => 'example.com',
160 'forced' => 0,
161 ),
162 );
163 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
164 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
165 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
166 $expectedResult = array(
167 $domainRecords[$currentDomain]['pid'] => $domainRecords[$currentDomain],
168 );
169 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
170 }
171
172 /**
173 * @param string $currentDomain
174 * @test
175 * @dataProvider getSysDomainCacheDataProvider
176 */
177 public function getSysDomainCacheReturnsForcedDomainRecord($currentDomain)
178 {
179 $_SERVER['HTTP_HOST'] = $currentDomain;
180 $domainRecords = array(
181 'typo3.org' => array(
182 'uid' => '1',
183 'pid' => '1',
184 'domainName' => 'typo3.org',
185 'forced' => 0,
186 ),
187 'foo.bar' => array(
188 'uid' => '2',
189 'pid' => '1',
190 'domainName' => 'foo.bar',
191 'forced' => 1,
192 ),
193 'example.com' => array(
194 'uid' => '3',
195 'pid' => '1',
196 'domainName' => 'example.com',
197 'forced' => 0,
198 ),
199 );
200 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
201 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
202 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
203 $expectedResult = array(
204 $domainRecords[$currentDomain]['pid'] => $domainRecords['foo.bar'],
205 );
206 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
207 }
208
209 /**
210 * Tests concerning domainNameMatchesCurrentRequest
211 */
212
213 /**
214 * @return array
215 */
216 public function domainNameMatchesCurrentRequestDataProvider()
217 {
218 return array(
219 'same domains' => array(
220 'typo3.org',
221 'typo3.org',
222 '/index.php',
223 true,
224 ),
225 'same domains with subdomain' => array(
226 'www.typo3.org',
227 'www.typo3.org',
228 '/index.php',
229 true,
230 ),
231 'different domains' => array(
232 'foo.bar',
233 'typo3.org',
234 '/index.php',
235 false,
236 ),
237 'domain record with script name' => array(
238 'typo3.org',
239 'typo3.org/foo/bar',
240 '/foo/bar/index.php',
241 true,
242 ),
243 'domain record with wrong script name' => array(
244 'typo3.org',
245 'typo3.org/foo/bar',
246 '/bar/foo/index.php',
247 false,
248 ),
249 );
250 }
251
252 /**
253 * @param string $currentDomain
254 * @param string $domainRecord
255 * @param string $scriptName
256 * @param bool $expectedResult
257 * @test
258 * @dataProvider domainNameMatchesCurrentRequestDataProvider
259 */
260 public function domainNameMatchesCurrentRequest($currentDomain, $domainRecord, $scriptName, $expectedResult)
261 {
262 $_SERVER['HTTP_HOST'] = $currentDomain;
263 $_SERVER['SCRIPT_NAME'] = $scriptName;
264 $this->assertEquals($expectedResult, $this->subject->domainNameMatchesCurrentRequest($domainRecord));
265 }
266
267 /**
268 * @return array
269 */
270 public function baseUrlWrapHandlesDifferentUrlsDataProvider()
271 {
272 return [
273 'without base url' => [
274 '',
275 'fileadmin/user_uploads/image.jpg',
276 'fileadmin/user_uploads/image.jpg'
277 ],
278 'with base url' => [
279 'http://www.google.com/',
280 'fileadmin/user_uploads/image.jpg',
281 'http://www.google.com/fileadmin/user_uploads/image.jpg'
282 ],
283 'without base url but with url prepended with a forward slash' => [
284 '',
285 '/fileadmin/user_uploads/image.jpg',
286 '/fileadmin/user_uploads/image.jpg',
287 ],
288 'with base url but with url prepended with a forward slash' => [
289 'http://www.google.com/',
290 '/fileadmin/user_uploads/image.jpg',
291 '/fileadmin/user_uploads/image.jpg',
292 ],
293 ];
294 }
295
296 /**
297 * @dataProvider baseUrlWrapHandlesDifferentUrlsDataProvider
298 * @test
299 * @param string $baseUrl
300 * @param string $url
301 * @param string $expected
302 */
303 public function baseUrlWrapHandlesDifferentUrls($baseUrl, $url, $expected)
304 {
305 $this->subject->baseUrl = $baseUrl;
306 $this->assertSame($expected, $this->subject->baseUrlWrap($url));
307 }
308
309 }