7739bfda58af363b5d400ed0306fb4f53a09200c
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Core / SystemEnvironmentBuilderTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Core;
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
19 *
20 * @author Christia Kuhn <lolli@schwarzbu.ch>
21 */
22 class SystemEnvironmentBuilderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @var \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
26 */
27 protected $fixture = NULL;
28
29 /**
30 * Set up testcase
31 *
32 * @return void
33 */
34 protected function setUp() {
35 $this->fixture = $this->getAccessibleMock(\TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::class, array('dummy'));
36 }
37
38 /**
39 * Data provider for 'fileDenyPatternMatchesPhpExtension' test case.
40 *
41 * @return array
42 */
43 public function fileDenyPatternMatchesPhpExtensionDataProvider() {
44 $fileName = $this->getUniqueId('filename');
45 $data = array();
46 $phpExtensions = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', 'php,php3,php4,php5,php6,phpsh,phtml', TRUE);
47 foreach ($phpExtensions as $extension) {
48 $data[] = array($fileName . '.' . $extension);
49 $data[] = array($fileName . '.' . $extension . '.txt');
50 }
51 return $data;
52 }
53
54 /**
55 * Tests whether an accordant PHP extension is denied.
56 *
57 * @test
58 * @dataProvider fileDenyPatternMatchesPhpExtensionDataProvider
59 * @param string $phpExtension
60 */
61 public function fileDenyPatternMatchesPhpExtension($phpExtension) {
62 $this->assertGreaterThan(0, preg_match('/' . FILE_DENY_PATTERN_DEFAULT . '/', $phpExtension), $phpExtension);
63 }
64
65 /**
66 * @test
67 */
68 public function getPathThisScriptCliReadsLocalPartFromArgv() {
69 $fakedLocalPart = $this->getUniqueId('Test');
70 $GLOBALS['_SERVER']['argv'][0] = $fakedLocalPart;
71 $this->assertStringEndsWith($fakedLocalPart, $this->fixture->_call('getPathThisScriptCli'));
72 }
73
74 /**
75 * @test
76 */
77 public function getPathThisScriptCliReadsLocalPartFromEnv() {
78 $fakedLocalPart = $this->getUniqueId('Test');
79 unset($GLOBALS['_SERVER']['argv']);
80 $GLOBALS['_ENV']['_'] = $fakedLocalPart;
81 $this->assertStringEndsWith($fakedLocalPart, $this->fixture->_call('getPathThisScriptCli'));
82 }
83
84 /**
85 * @test
86 */
87 public function getPathThisScriptCliReadsLocalPartFromServer() {
88 $fakedLocalPart = $this->getUniqueId('Test');
89 unset($GLOBALS['_SERVER']['argv']);
90 unset($GLOBALS['_ENV']['_']);
91 $GLOBALS['_SERVER']['_'] = $fakedLocalPart;
92 $this->assertStringEndsWith($fakedLocalPart, $this->fixture->_call('getPathThisScriptCli'));
93 }
94
95 /**
96 * @test
97 */
98 public function getPathThisScriptCliAddsCurrentWorkingDirectoryFromServerEnvironmentToLocalPathOnUnix() {
99 if (TYPO3_OS === 'WIN') {
100 $this->markTestSkipped('Test not available on Windows OS.');
101 }
102 $GLOBALS['_SERVER']['argv'][0] = 'foo';
103 $fakedAbsolutePart = '/' . $this->getUniqueId('Absolute') . '/';
104 $_SERVER['PWD'] = $fakedAbsolutePart;
105 $this->assertStringStartsWith($fakedAbsolutePart, $this->fixture->_call('getPathThisScriptCli'));
106 }
107
108 /**
109 * @test
110 */
111 public function getUnifiedDirectoryNameWithTrailingSlashReturnsCorrectPathOnUnix() {
112 if (TYPO3_OS === 'WIN') {
113 $this->markTestSkipped('Test not available on Windows OS.');
114 }
115 $input = '/foo/bar/test.php';
116 $expected = '/foo/bar/';
117 $actual = $this->fixture->_call('getUnifiedDirectoryNameWithTrailingSlash', $input);
118 $this->assertEquals($expected, $actual);
119 }
120
121 /**
122 * @test
123 */
124 public function addCorePearPathToIncludePathAddsTypo3ContribPearToPathAsFirstEntry() {
125 $backupPath = get_include_path();
126 $this->fixture->_call('addCorePearPathToIncludePath');
127 $actualValue = get_include_path();
128 set_include_path($backupPath);
129 $this->assertStringStartsWith(PATH_typo3 . 'contrib/pear/' . PATH_SEPARATOR, $actualValue);
130 }
131
132 /**
133 * @test
134 */
135 public function initializeGlobalVariablesUnsetsGlobalErrorArray() {
136 $GLOBALS['error'] = 'foo';
137 $this->fixture->_call('initializeGlobalVariables');
138 $this->assertFalse(isset($GLOBALS['error']));
139 }
140
141 /**
142 * @test
143 */
144 public function initializeGlobalVariablesSetsGlobalClientArray() {
145 unset($GLOBALS['CLIENT']);
146 $this->fixture->_call('initializeGlobalVariables');
147 $this->assertArrayHasKey('CLIENT', $GLOBALS);
148 }
149
150 /**
151 * @test
152 */
153 public function initializeGlobalVariablesSetsGlobalTypo3MiscArray() {
154 unset($GLOBALS['TYPO3_MISC']);
155 $this->fixture->_call('initializeGlobalVariables');
156 $this->assertInternalType('array', $GLOBALS['TYPO3_MISC']);
157 }
158
159 /**
160 * @test
161 */
162 public function initializeGlobalVariablesSetsGlobalT3VarArray() {
163 unset($GLOBALS['T3_VAR']);
164 $this->fixture->_call('initializeGlobalVariables');
165 $this->assertInternalType('array', $GLOBALS['T3_VAR']);
166 }
167
168 /**
169 * @test
170 */
171 public function initializeGlobalVariablesSetsGlobalT3ServicesArray() {
172 unset($GLOBALS['T3_SERVICES']);
173 $this->fixture->_call('initializeGlobalVariables');
174 $this->assertInternalType('array', $GLOBALS['T3_SERVICES']);
175 }
176
177 /**
178 * Data provider for initializeGlobalTimeTrackingVariablesSetsGlobalVariables
179 *
180 * @return array
181 */
182 public function initializeGlobalTimeTrackingVariablesSetsGlobalVariablesDataProvider() {
183 return array(
184 'PARSETIME_START' => array('PARSETIME_START'),
185 'EXEC_TIME' => array('EXEC_TIME'),
186 'ACCESS_TIME' => array('ACCESS_TIME'),
187 'SIM_EXEC_TIME' => array('SIM_EXEC_TIME'),
188 'SIM_ACCESS_TIME' => array('SIM_ACCESS_TIME')
189 );
190 }
191
192 /**
193 * @test
194 * @dataProvider initializeGlobalTimeTrackingVariablesSetsGlobalVariablesDataProvider
195 * @param string $variable Variable to check for in $GLOBALS
196 */
197 public function initializeGlobalTimeTrackingVariablesSetsGlobalVariables($variable) {
198 unset($GLOBALS[$variable]);
199 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
200 $this->assertTrue(isset($GLOBALS[$variable]));
201 }
202
203 /**
204 * @test
205 */
206 public function initializeGlobalTimeTrackingVariablesSetsGlobalTypo3MiscMicrotimeStart() {
207 unset($GLOBALS['TYPO3_MISC']['microtime_start']);
208 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
209 $this->assertTrue(isset($GLOBALS['TYPO3_MISC']['microtime_start']));
210 }
211
212 /**
213 * @test
214 */
215 public function initializeGlobalTimeTrackingVariablesRoundsAccessTimeToSixtySeconds() {
216 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
217 $this->assertEquals(0, $GLOBALS['ACCESS_TIME'] % 60);
218 }
219
220 /**
221 * @test
222 */
223 public function initializeGlobalTimeTrackingVariablesRoundsSimAccessTimeToSixtySeconds() {
224 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
225 $this->assertEquals(0, $GLOBALS['SIM_ACCESS_TIME'] % 60);
226 }
227
228 /**
229 * @test
230 */
231 public function initializeBasicErrorReportingExcludesStrict() {
232 $backupReporting = error_reporting();
233 $this->fixture->_call('initializeBasicErrorReporting');
234 $actualReporting = error_reporting();
235 error_reporting($backupReporting);
236 $this->assertEquals(0, $actualReporting & E_STRICT);
237 }
238
239 /**
240 * @test
241 */
242 public function initializeBasicErrorReportingExcludesNotice() {
243 $backupReporting = error_reporting();
244 $this->fixture->_call('initializeBasicErrorReporting');
245 $actualReporting = error_reporting();
246 error_reporting($backupReporting);
247 $this->assertEquals(0, $actualReporting & E_NOTICE);
248 }
249
250 /**
251 * @test
252 */
253 public function initializeBasicErrorReportingExcludesDeprecated() {
254 $backupReporting = error_reporting();
255 $this->fixture->_call('initializeBasicErrorReporting');
256 $actualReporting = error_reporting();
257 error_reporting($backupReporting);
258 $this->assertEquals(0, $actualReporting & E_DEPRECATED);
259 }
260
261 }