[!!!][TASK] Make TimeTracker a singleton
[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\TimeTracker\TimeTracker;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
22 use TYPO3\CMS\Frontend\Page\PageRepository;
23
24 /**
25 * Testcase for TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
26 */
27 class TypoScriptFrontendControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
28 {
29 /**
30 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|TypoScriptFrontendController
31 */
32 protected $subject;
33
34 protected function setUp()
35 {
36 GeneralUtility::flushInternalRuntimeCaches();
37 $this->subject = $this->getAccessibleMock(TypoScriptFrontendController::class, array('dummy'), array(), '', false);
38 $this->subject->TYPO3_CONF_VARS = $GLOBALS['TYPO3_CONF_VARS'];
39 $this->subject->TYPO3_CONF_VARS['SYS']['encryptionKey'] = '170928423746123078941623042360abceb12341234231';
40
41 $pageRepository = $this->getMock(PageRepository::class);
42 $this->subject->sys_page = $pageRepository;
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->getMock(TypoScriptFrontendController::class, array(
79 'INTincScript_process',
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 $tsfe->config['INTincScript_ext']['divKey'] = '679b52796e75d474ccbbed486b6837ab';
87 $tsfe->config['INTincScript'] = array('INT_SCRIPT.679b52796e75d474ccbbed486b6837ab' => array());
88
89 return $tsfe;
90 }
91
92 /**
93 * Tests concerning sL
94 */
95
96 /**
97 * @test
98 */
99 public function localizationReturnsUnchangedStringIfNotLocallangLabel()
100 {
101 $string = $this->getUniqueId();
102 $this->assertEquals($string, $this->subject->sL($string));
103 }
104
105 /**
106 * Tests concerning getSysDomainCache
107 */
108
109 /**
110 * @return array
111 */
112 public function getSysDomainCacheDataProvider()
113 {
114 return array(
115 'typo3.org' => array(
116 'typo3.org',
117 ),
118 'foo.bar' => array(
119 'foo.bar',
120 ),
121 'example.com' => array(
122 'example.com',
123 ),
124 );
125 }
126
127 /**
128 * @param string $currentDomain
129 * @test
130 * @dataProvider getSysDomainCacheDataProvider
131 */
132 public function getSysDomainCacheReturnsCurrentDomainRecord($currentDomain)
133 {
134 $_SERVER['HTTP_HOST'] = $currentDomain;
135 $domainRecords = array(
136 'typo3.org' => array(
137 'uid' => '1',
138 'pid' => '1',
139 'domainName' => 'typo3.org',
140 'forced' => 0,
141 ),
142 'foo.bar' => array(
143 'uid' => '2',
144 'pid' => '1',
145 'domainName' => 'foo.bar',
146 'forced' => 0,
147 ),
148 'example.com' => array(
149 'uid' => '3',
150 'pid' => '1',
151 'domainName' => 'example.com',
152 'forced' => 0,
153 ),
154 );
155 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
156 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
157 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
158 $expectedResult = array(
159 $domainRecords[$currentDomain]['pid'] => $domainRecords[$currentDomain],
160 );
161 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
162 }
163
164 /**
165 * @param string $currentDomain
166 * @test
167 * @dataProvider getSysDomainCacheDataProvider
168 */
169 public function getSysDomainCacheReturnsForcedDomainRecord($currentDomain)
170 {
171 $_SERVER['HTTP_HOST'] = $currentDomain;
172 $domainRecords = array(
173 'typo3.org' => array(
174 'uid' => '1',
175 'pid' => '1',
176 'domainName' => 'typo3.org',
177 'forced' => 0,
178 ),
179 'foo.bar' => array(
180 'uid' => '2',
181 'pid' => '1',
182 'domainName' => 'foo.bar',
183 'forced' => 1,
184 ),
185 'example.com' => array(
186 'uid' => '3',
187 'pid' => '1',
188 'domainName' => 'example.com',
189 'forced' => 0,
190 ),
191 );
192 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
193 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
194 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
195 $expectedResult = array(
196 $domainRecords[$currentDomain]['pid'] => $domainRecords['foo.bar'],
197 );
198 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
199 }
200
201 /**
202 * Tests concerning domainNameMatchesCurrentRequest
203 */
204
205 /**
206 * @return array
207 */
208 public function domainNameMatchesCurrentRequestDataProvider()
209 {
210 return array(
211 'same domains' => array(
212 'typo3.org',
213 'typo3.org',
214 '/index.php',
215 true,
216 ),
217 'same domains with subdomain' => array(
218 'www.typo3.org',
219 'www.typo3.org',
220 '/index.php',
221 true,
222 ),
223 'different domains' => array(
224 'foo.bar',
225 'typo3.org',
226 '/index.php',
227 false,
228 ),
229 'domain record with script name' => array(
230 'typo3.org',
231 'typo3.org/foo/bar',
232 '/foo/bar/index.php',
233 true,
234 ),
235 'domain record with wrong script name' => array(
236 'typo3.org',
237 'typo3.org/foo/bar',
238 '/bar/foo/index.php',
239 false,
240 ),
241 );
242 }
243
244 /**
245 * @param string $currentDomain
246 * @param string $domainRecord
247 * @param string $scriptName
248 * @param bool $expectedResult
249 * @test
250 * @dataProvider domainNameMatchesCurrentRequestDataProvider
251 */
252 public function domainNameMatchesCurrentRequest($currentDomain, $domainRecord, $scriptName, $expectedResult)
253 {
254 $_SERVER['HTTP_HOST'] = $currentDomain;
255 $_SERVER['SCRIPT_NAME'] = $scriptName;
256 $this->assertEquals($expectedResult, $this->subject->domainNameMatchesCurrentRequest($domainRecord));
257 }
258 }