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