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