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