[TASK] Use Time Tracker only
[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_includeLibs',
81 'INTincScript_loadJSCode',
82 'setAbsRefPrefix',
83 'regeneratePageTitle'
84 ), array(), '', false);
85 $tsfe->expects($this->exactly(2))->method('INTincScript_process')->will($this->returnCallback(array($this, 'INTincScript_processCallback')));
86 $tsfe->content = file_get_contents(__DIR__ . '/Fixtures/renderedPage.html');
87 $tsfe->config['INTincScript_ext']['divKey'] = '679b52796e75d474ccbbed486b6837ab';
88 $tsfe->config['INTincScript'] = array('INT_SCRIPT.679b52796e75d474ccbbed486b6837ab' => array());
89 $GLOBALS['TT'] = new TimeTracker(false);
90 return $tsfe;
91 }
92
93 /**
94 * Tests concerning sL
95 */
96
97 /**
98 * @test
99 */
100 public function localizationReturnsUnchangedStringIfNotLocallangLabel()
101 {
102 $string = $this->getUniqueId();
103 $this->assertEquals($string, $this->subject->sL($string));
104 }
105
106 /**
107 * Tests concerning getSysDomainCache
108 */
109
110 /**
111 * @return array
112 */
113 public function getSysDomainCacheDataProvider()
114 {
115 return array(
116 'typo3.org' => array(
117 'typo3.org',
118 ),
119 'foo.bar' => array(
120 'foo.bar',
121 ),
122 'example.com' => array(
123 'example.com',
124 ),
125 );
126 }
127
128 /**
129 * @param string $currentDomain
130 * @test
131 * @dataProvider getSysDomainCacheDataProvider
132 */
133 public function getSysDomainCacheReturnsCurrentDomainRecord($currentDomain)
134 {
135 $_SERVER['HTTP_HOST'] = $currentDomain;
136 $domainRecords = array(
137 'typo3.org' => array(
138 'uid' => '1',
139 'pid' => '1',
140 'domainName' => 'typo3.org',
141 'forced' => 0,
142 ),
143 'foo.bar' => array(
144 'uid' => '2',
145 'pid' => '1',
146 'domainName' => 'foo.bar',
147 'forced' => 0,
148 ),
149 'example.com' => array(
150 'uid' => '3',
151 'pid' => '1',
152 'domainName' => 'example.com',
153 'forced' => 0,
154 ),
155 );
156 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
157 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
158 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
159 $expectedResult = array(
160 $domainRecords[$currentDomain]['pid'] => $domainRecords[$currentDomain],
161 );
162 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
163 }
164
165 /**
166 * @param string $currentDomain
167 * @test
168 * @dataProvider getSysDomainCacheDataProvider
169 */
170 public function getSysDomainCacheReturnsForcedDomainRecord($currentDomain)
171 {
172 $_SERVER['HTTP_HOST'] = $currentDomain;
173 $domainRecords = array(
174 'typo3.org' => array(
175 'uid' => '1',
176 'pid' => '1',
177 'domainName' => 'typo3.org',
178 'forced' => 0,
179 ),
180 'foo.bar' => array(
181 'uid' => '2',
182 'pid' => '1',
183 'domainName' => 'foo.bar',
184 'forced' => 1,
185 ),
186 'example.com' => array(
187 'uid' => '3',
188 'pid' => '1',
189 'domainName' => 'example.com',
190 'forced' => 0,
191 ),
192 );
193 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
194 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
195 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
196 $expectedResult = array(
197 $domainRecords[$currentDomain]['pid'] => $domainRecords['foo.bar'],
198 );
199 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
200 }
201
202 /**
203 * Tests concerning domainNameMatchesCurrentRequest
204 */
205
206 /**
207 * @return array
208 */
209 public function domainNameMatchesCurrentRequestDataProvider()
210 {
211 return array(
212 'same domains' => array(
213 'typo3.org',
214 'typo3.org',
215 '/index.php',
216 true,
217 ),
218 'same domains with subdomain' => array(
219 'www.typo3.org',
220 'www.typo3.org',
221 '/index.php',
222 true,
223 ),
224 'different domains' => array(
225 'foo.bar',
226 'typo3.org',
227 '/index.php',
228 false,
229 ),
230 'domain record with script name' => array(
231 'typo3.org',
232 'typo3.org/foo/bar',
233 '/foo/bar/index.php',
234 true,
235 ),
236 'domain record with wrong script name' => array(
237 'typo3.org',
238 'typo3.org/foo/bar',
239 '/bar/foo/index.php',
240 false,
241 ),
242 );
243 }
244
245 /**
246 * @param string $currentDomain
247 * @param string $domainRecord
248 * @param string $scriptName
249 * @param bool $expectedResult
250 * @test
251 * @dataProvider domainNameMatchesCurrentRequestDataProvider
252 */
253 public function domainNameMatchesCurrentRequest($currentDomain, $domainRecord, $scriptName, $expectedResult)
254 {
255 $_SERVER['HTTP_HOST'] = $currentDomain;
256 $_SERVER['SCRIPT_NAME'] = $scriptName;
257 $this->assertEquals($expectedResult, $this->subject->domainNameMatchesCurrentRequest($domainRecord));
258 }
259 }