[TASK] Introduce runtime cache for getIndpEnv()
[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 GeneralUtility::flushInternalRuntimeCaches();
35 $this->subject = $this->getAccessibleMock(TypoScriptFrontendController::class, array('dummy'), array(), '', FALSE);
36 $this->subject->TYPO3_CONF_VARS = $GLOBALS['TYPO3_CONF_VARS'];
37 $this->subject->TYPO3_CONF_VARS['SYS']['encryptionKey'] = '170928423746123078941623042360abceb12341234231';
38
39 $pageRepository = $this->getMock(PageRepository::class);
40 $this->subject->sys_page = $pageRepository;
41 }
42
43 /**
44 * Tests concerning rendering content
45 */
46
47 /**
48 * @test
49 */
50 public function headerAndFooterMarkersAreReplacedDuringIntProcessing() {
51 $GLOBALS['TSFE'] = $this->setupTsfeMockForHeaderFooterReplacementCheck();
52 $GLOBALS['TSFE']->INTincScript();
53 $this->assertContains('headerData', $GLOBALS['TSFE']->content);
54 $this->assertContains('footerData', $GLOBALS['TSFE']->content);
55 }
56
57 /**
58 * This is the callback that mimics a USER_INT extension
59 */
60 public function INTincScript_processCallback() {
61 $GLOBALS['TSFE']->additionalHeaderData[] = 'headerData';
62 $GLOBALS['TSFE']->additionalFooterData[] = 'footerData';
63 }
64
65 /**
66 * Setup a \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController object only for testing the header and footer
67 * replacement during USER_INT rendering
68 *
69 * @return \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController
70 */
71 protected function setupTsfeMockForHeaderFooterReplacementCheck() {
72 /** @var \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController $tsfe */
73 $tsfe = $this->getMock(TypoScriptFrontendController::class, array(
74 'INTincScript_process',
75 'INTincScript_includeLibs',
76 'INTincScript_loadJSCode',
77 'setAbsRefPrefix',
78 'regeneratePageTitle'
79 ), array(), '', FALSE);
80 $tsfe->expects($this->exactly(2))->method('INTincScript_process')->will($this->returnCallback(array($this, 'INTincScript_processCallback')));
81 $tsfe->content = file_get_contents(__DIR__ . '/Fixtures/renderedPage.html');
82 $tsfe->config['INTincScript_ext']['divKey'] = '679b52796e75d474ccbbed486b6837ab';
83 $tsfe->config['INTincScript'] = array('INT_SCRIPT.679b52796e75d474ccbbed486b6837ab' => array());
84 $GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker();
85 return $tsfe;
86 }
87
88 /**
89 * Tests concerning sL
90 */
91
92 /**
93 * @test
94 */
95 public function localizationReturnsUnchangedStringIfNotLocallangLabel() {
96 $string = $this->getUniqueId();
97 $this->assertEquals($string, $this->subject->sL($string));
98 }
99
100 /**
101 * Tests concerning getSysDomainCache
102 */
103
104 /**
105 * @return array
106 */
107 public function getSysDomainCacheDataProvider() {
108 return array(
109 'typo3.org' => array(
110 'typo3.org',
111 ),
112 'foo.bar' => array(
113 'foo.bar',
114 ),
115 'example.com' => array(
116 'example.com',
117 ),
118 );
119 }
120
121 /**
122 * @param string $currentDomain
123 * @test
124 * @dataProvider getSysDomainCacheDataProvider
125 */
126 public function getSysDomainCacheReturnsCurrentDomainRecord($currentDomain) {
127 $_SERVER['HTTP_HOST'] = $currentDomain;
128 $domainRecords = array(
129 'typo3.org' => array(
130 'uid' => '1',
131 'pid' => '1',
132 'domainName' => 'typo3.org',
133 'forced' => 0,
134 ),
135 'foo.bar' => array(
136 'uid' => '2',
137 'pid' => '1',
138 'domainName' => 'foo.bar',
139 'forced' => 0,
140 ),
141 'example.com' => array(
142 'uid' => '3',
143 'pid' => '1',
144 'domainName' => 'example.com',
145 'forced' => 0,
146 ),
147 );
148 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
149 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
150 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
151 $expectedResult = array(
152 $domainRecords[$currentDomain]['pid'] => $domainRecords[$currentDomain],
153 );
154 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
155 }
156
157 /**
158 * @param string $currentDomain
159 * @test
160 * @dataProvider getSysDomainCacheDataProvider
161 */
162 public function getSysDomainCacheReturnsForcedDomainRecord($currentDomain) {
163 $_SERVER['HTTP_HOST'] = $currentDomain;
164 $domainRecords = array(
165 'typo3.org' => array(
166 'uid' => '1',
167 'pid' => '1',
168 'domainName' => 'typo3.org',
169 'forced' => 0,
170 ),
171 'foo.bar' => array(
172 'uid' => '2',
173 'pid' => '1',
174 'domainName' => 'foo.bar',
175 'forced' => 1,
176 ),
177 'example.com' => array(
178 'uid' => '3',
179 'pid' => '1',
180 'domainName' => 'example.com',
181 'forced' => 0,
182 ),
183 );
184 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
185 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
186 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
187 $expectedResult = array(
188 $domainRecords[$currentDomain]['pid'] => $domainRecords['foo.bar'],
189 );
190 $this->assertEquals($expectedResult, $this->subject->_call('getSysDomainCache'));
191 }
192
193 /**
194 * Tests concerning domainNameMatchesCurrentRequest
195 */
196
197 /**
198 * @return array
199 */
200 public function domainNameMatchesCurrentRequestDataProvider() {
201 return array(
202 'same domains' => array(
203 'typo3.org',
204 'typo3.org',
205 '/index.php',
206 TRUE,
207 ),
208 'same domains with subdomain' => array(
209 'www.typo3.org',
210 'www.typo3.org',
211 '/index.php',
212 TRUE,
213 ),
214 'different domains' => array(
215 'foo.bar',
216 'typo3.org',
217 '/index.php',
218 FALSE,
219 ),
220 'domain record with script name' => array(
221 'typo3.org',
222 'typo3.org/foo/bar',
223 '/foo/bar/index.php',
224 TRUE,
225 ),
226 'domain record with wrong script name' => array(
227 'typo3.org',
228 'typo3.org/foo/bar',
229 '/bar/foo/index.php',
230 FALSE,
231 ),
232 );
233 }
234
235 /**
236 * @param string $currentDomain
237 * @param string $domainRecord
238 * @param string $scriptName
239 * @param bool $expectedResult
240 * @test
241 * @dataProvider domainNameMatchesCurrentRequestDataProvider
242 */
243 public function domainNameMatchesCurrentRequest($currentDomain, $domainRecord, $scriptName, $expectedResult) {
244 $_SERVER['HTTP_HOST'] = $currentDomain;
245 $_SERVER['SCRIPT_NAME'] = $scriptName;
246 $this->assertEquals($expectedResult, $this->subject->domainNameMatchesCurrentRequest($domainRecord));
247 }
248
249 }