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