[TASK] Namespace SystemEnvironmentBuilderTest
[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 * Copyright notice
6 *
7 * (c) 2012 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Testcase
31 *
32 * @author Christia Kuhn <lolli@schwarzbu.ch>
33 * @package TYPO3
34 * @subpackage tests
35 */
36 class SystemEnvironmentBuilderTest extends \tx_phpunit_testcase {
37
38 /**
39 * Enable backup of global and system variables
40 *
41 * @var boolean
42 */
43 protected $backupGlobals = TRUE;
44
45 /**
46 * Exclude TYPO3_DB from backup/ restore of $GLOBALS because of included ressource
47 *
48 * @var array
49 */
50 protected $backupGlobalsBlacklist = array('TYPO3_DB');
51
52 /**
53 * @var \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder|\Tx_Phpunit_Interface_AccessibleObject
54 */
55 protected $fixture = NULL;
56
57 /**
58 * Set up testcase
59 *
60 * @return void
61 */
62 public function setUp() {
63 $this->fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Core\\SystemEnvironmentBuilder', array('dummy'));
64 }
65
66 /**
67 * Tear down
68 *
69 * @return void
70 */
71 public function tearDown() {
72 unset($this->fixture);
73 }
74
75 /**
76 * Data provider for 'fileDenyPatternMatchesPhpExtension' test case.
77 *
78 * @return array
79 */
80 public function fileDenyPatternMatchesPhpExtensionDataProvider() {
81 $fileName = uniqid('filename');
82 $data = array();
83 $phpExtensions = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', 'php,php3,php4,php5,php6,phpsh,phtml', TRUE);
84 foreach ($phpExtensions as $extension) {
85 $data[] = array($fileName . '.' . $extension);
86 $data[] = array($fileName . '.' . $extension . '.txt');
87 }
88 return $data;
89 }
90
91 /**
92 * Tests whether an accordant PHP extension is denied.
93 *
94 * @test
95 * @dataProvider fileDenyPatternMatchesPhpExtensionDataProvider
96 * @param string $phpExtension
97 */
98 public function fileDenyPatternMatchesPhpExtension($phpExtension) {
99 $this->assertGreaterThan(0, preg_match(('/' . FILE_DENY_PATTERN_DEFAULT) . '/', $phpExtension), $phpExtension);
100 }
101
102 /**
103 * @test
104 */
105 public function getPathThisScriptCliReadsLocalPartFromArgv() {
106 $fakedLocalPart = uniqid('Test');
107 $GLOBALS['_SERVER']['argv'][0] = $fakedLocalPart;
108 $this->assertStringEndsWith($fakedLocalPart, $this->fixture->_call('getPathThisScriptCli'));
109 }
110
111 /**
112 * @test
113 */
114 public function getPathThisScriptCliReadsLocalPartFromEnv() {
115 $fakedLocalPart = uniqid('Test');
116 unset($GLOBALS['_SERVER']['argv']);
117 $GLOBALS['_ENV']['_'] = $fakedLocalPart;
118 $this->assertStringEndsWith($fakedLocalPart, $this->fixture->_call('getPathThisScriptCli'));
119 }
120
121 /**
122 * @test
123 */
124 public function getPathThisScriptCliReadsLocalPartFromServer() {
125 $fakedLocalPart = uniqid('Test');
126 unset($GLOBALS['_SERVER']['argv']);
127 unset($GLOBALS['_ENV']['_']);
128 $GLOBALS['_SERVER']['_'] = $fakedLocalPart;
129 $this->assertStringEndsWith($fakedLocalPart, $this->fixture->_call('getPathThisScriptCli'));
130 }
131
132 /**
133 * @test
134 */
135 public function getPathThisScriptCliAddsCurrentWorkingDirectoryFromServerEnvironmentToLocalPathOnUnix() {
136 if (TYPO3_OS === 'WIN') {
137 $this->markTestSkipped('Test not available on Windows OS.');
138 }
139 $GLOBALS['_SERVER']['argv'][0] = 'foo';
140 $fakedAbsolutePart = ('/' . uniqid('Absolute')) . '/';
141 $_SERVER['PWD'] = $fakedAbsolutePart;
142 $this->assertStringStartsWith($fakedAbsolutePart, $this->fixture->_call('getPathThisScriptCli'));
143 }
144
145 /**
146 * @test
147 */
148 public function getUnifiedDirectoryNameWithTrailingSlashReturnsCorrectPathOnUnix() {
149 if (TYPO3_OS === 'WIN') {
150 $this->markTestSkipped('Test not available on Windows OS.');
151 }
152 $input = '/foo/bar/test.php';
153 $expected = '/foo/bar/';
154 $actual = $this->fixture->_call('getUnifiedDirectoryNameWithTrailingSlash', $input);
155 $this->assertEquals($expected, $actual);
156 }
157
158 /**
159 * @test
160 */
161 public function addCorePearPathToIncludePathAddsTypo3ContribPearToPathAsFirstEntry() {
162 $backupPath = get_include_path();
163 $this->fixture->_call('addCorePearPathToIncludePath');
164 $actualValue = get_include_path();
165 set_include_path($backupPath);
166 $this->assertStringStartsWith((PATH_typo3 . 'contrib/pear/') . PATH_SEPARATOR, $actualValue);
167 }
168
169 /**
170 * @test
171 */
172 public function initializeGlobalVariablesUnsetsGlobalErrorArray() {
173 $GLOBALS['error'] = 'foo';
174 $this->fixture->_call('initializeGlobalVariables');
175 $this->assertFalse(isset($GLOBALS['error']));
176 }
177
178 /**
179 * @test
180 */
181 public function initializeGlobalVariablesSetsGlobalClientArray() {
182 unset($GLOBALS['CLIENT']);
183 $this->fixture->_call('initializeGlobalVariables');
184 $this->assertArrayHasKey('CLIENT', $GLOBALS);
185 }
186
187 /**
188 * @test
189 */
190 public function initializeGlobalVariablesSetsGlobalTypo3MiscArray() {
191 unset($GLOBALS['TYPO3_MISC']);
192 $this->fixture->_call('initializeGlobalVariables');
193 $this->assertInternalType('array', $GLOBALS['TYPO3_MISC']);
194 }
195
196 /**
197 * @test
198 */
199 public function initializeGlobalVariablesSetsGlobalT3VarArray() {
200 unset($GLOBALS['T3_VAR']);
201 $this->fixture->_call('initializeGlobalVariables');
202 $this->assertInternalType('array', $GLOBALS['T3_VAR']);
203 }
204
205 /**
206 * @test
207 */
208 public function initializeGlobalVariablesSetsGlobalT3ServicesArray() {
209 unset($GLOBALS['T3_SERVICES']);
210 $this->fixture->_call('initializeGlobalVariables');
211 $this->assertInternalType('array', $GLOBALS['T3_SERVICES']);
212 }
213
214 /**
215 * @test
216 */
217 public function loadDefaultConfigurationPopulatesTypo3ConfVarsArray() {
218 unset($GLOBALS['TYPO3_CONF_VARS']);
219 $this->fixture->_call('loadDefaultConfiguration');
220 $this->assertInternalType('array', $GLOBALS['TYPO3_CONF_VARS']);
221 }
222
223 /**
224 * Data provider for initializeGlobalTimeTrackingVariablesSetsGlobalVariables
225 *
226 * @return array
227 */
228 public function initializeGlobalTimeTrackingVariablesSetsGlobalVariablesDataProvider() {
229 return array(
230 'PARSETIME_START' => array('PARSETIME_START'),
231 'EXEC_TIME' => array('EXEC_TIME'),
232 'ACCESS_TIME' => array('ACCESS_TIME'),
233 'SIM_EXEC_TIME' => array('SIM_EXEC_TIME'),
234 'SIM_ACCESS_TIME' => array('SIM_ACCESS_TIME')
235 );
236 }
237
238 /**
239 * @test
240 * @dataProvider initializeGlobalTimeTrackingVariablesSetsGlobalVariablesDataProvider
241 * @param string $variable Variable to check for in $GLOBALS
242 */
243 public function initializeGlobalTimeTrackingVariablesSetsGlobalVariables($variable) {
244 unset($GLOBALS[$variable]);
245 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
246 $this->assertTrue(isset($GLOBALS[$variable]));
247 }
248
249 /**
250 * @test
251 */
252 public function initializeGlobalTimeTrackingVariablesSetsGlobalTypo3MiscMicrotimeStart() {
253 unset($GLOBALS['TYPO3_MISC']['microtime_start']);
254 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
255 $this->assertTrue(isset($GLOBALS['TYPO3_MISC']['microtime_start']));
256 }
257
258 /**
259 * @test
260 */
261 public function initializeGlobalTimeTrackingVariablesRoundsAccessTimeToSixtySeconds() {
262 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
263 $this->assertEquals(0, $GLOBALS['ACCESS_TIME'] % 60);
264 }
265
266 /**
267 * @test
268 */
269 public function initializeGlobalTimeTrackingVariablesRoundsSimAccessTimeToSixtySeconds() {
270 $this->fixture->_call('initializeGlobalTimeTrackingVariables');
271 $this->assertEquals(0, $GLOBALS['SIM_ACCESS_TIME'] % 60);
272 }
273
274 /**
275 * @test
276 */
277 public function initializeBasicErrorReportingExcludesStrict() {
278 $backupReporting = error_reporting();
279 $this->fixture->_call('initializeBasicErrorReporting');
280 $actualReporting = error_reporting();
281 error_reporting($backupReporting);
282 $this->assertEquals(0, $actualReporting & E_STRICT);
283 }
284
285 /**
286 * @test
287 */
288 public function initializeBasicErrorReportingExcludesNotice() {
289 $backupReporting = error_reporting();
290 $this->fixture->_call('initializeBasicErrorReporting');
291 $actualReporting = error_reporting();
292 error_reporting($backupReporting);
293 $this->assertEquals(0, $actualReporting & E_NOTICE);
294 }
295
296 /**
297 * @test
298 */
299 public function initializeBasicErrorReportingExcludesDeprecated() {
300 $backupReporting = error_reporting();
301 $this->fixture->_call('initializeBasicErrorReporting');
302 $actualReporting = error_reporting();
303 error_reporting($backupReporting);
304 $this->assertEquals(0, $actualReporting & E_DEPRECATED);
305 }
306
307 }
308
309 ?>