[BUGFIX] Warning in ConfigurationManagerTest
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Configuration / ConfigurationManagerTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Configuration;
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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for class \TYPO3\CMS\Core\Configuration\ConfigurationManager
29 *
30 * @author Christian Kuhn <lolli@schwarzbu.ch>
31 * @package TYPO3
32 * @subpackage t3lib
33 */
34 class ConfigurationManagerTest extends \tx_phpunit_testcase {
35
36 /**
37 * Absolute path to files that must be removed
38 * after a test - handled in tearDown
39 */
40 protected $testFilesToDelete = array();
41
42 /**
43 * Tear down test case
44 */
45 public function tearDown() {
46 foreach ($this->testFilesToDelete as $absoluteFileName) {
47 \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($absoluteFileName);
48 }
49 }
50
51 ///////////////////////
52 // Tests concerning getDefaultConfiguration
53 ///////////////////////
54
55 /**
56 * @test
57 * @expectedException \RuntimeException
58 */
59 public function getDefaultConfigurationExecutesDefinedDefaultConfigurationFile() {
60 $defaultConfigurationFile = 'typo3temp/' . uniqid('defaultConfiguration');
61 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
62 $className = uniqid('ConfigurationManager');
63 eval(
64 'namespace ' . $namespace . ';' .
65 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
66 ' const DEFAULT_CONFIGURATION_FILE = \'' . $defaultConfigurationFile . '\';' .
67 '}'
68 );
69 $className = $namespace . '\\' . $className;
70 file_put_contents(PATH_site . $defaultConfigurationFile, '<?php throw new RuntimeException(\'foo\', 1310203814); ?>');
71 $this->testFilesToDelete[] = PATH_site . $defaultConfigurationFile;
72 $className::getDefaultConfiguration();
73 }
74
75 ///////////////////////
76 // Tests concerning getLocalConfiguration
77 ///////////////////////
78 /**
79 * @test
80 * @expectedException RuntimeException
81 */
82 public function getLocalConfigurationExecutesDefinedConfigurationFile() {
83 $configurationFile = 'typo3temp/' . uniqid('localConfiguration');
84 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
85 $className = uniqid('ConfigurationManager');
86 eval(
87 'namespace ' . $namespace . ';' .
88 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
89 ' const LOCAL_CONFIGURATION_FILE = \'' . $configurationFile . '\';' .
90 '}'
91 );
92 $className = $namespace . '\\' . $className;
93 file_put_contents(PATH_site . $configurationFile, '<?php throw new RuntimeException(\'foo\', 1310203815); ?>');
94 $this->testFilesToDelete[] = PATH_site . $configurationFile;
95 $className::getLocalConfiguration();
96 }
97
98 ///////////////////////
99 // Tests concerning updateLocalConfiguration
100 ///////////////////////
101 /**
102 * @test
103 */
104 public function updateLocalConfigurationWritesNewMergedLocalConfigurationArray() {
105 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
106 $className = uniqid('ConfigurationManager');
107 eval(
108 'namespace ' . $namespace . '; ' .
109 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
110 ' public static function getLocalConfiguration() {' .
111 ' $currentLocalConfiguration = array(' .
112 ' \'notChanged\' => 23,' .
113 ' \'changed\' => \'unChanged\',' .
114 ' );' .
115 ' return $currentLocalConfiguration;' .
116 ' }' .
117 ' public static function writeLocalConfiguration($conf) {' .
118 ' $expectedConfiguration = array(' .
119 ' \'notChanged\' => 23,' .
120 ' \'changed\' => \'changed\',' .
121 ' \'new\' => \'new\',' . ' );' .
122 ' if (!($conf === $expectedConfiguration)) {' .
123 ' throw new Exception(\'broken\');' .
124 ' }' .
125 ' }' .
126 '}'
127 );
128 $className = $namespace . '\\' . $className;
129 $overrideConfiguration = array(
130 'changed' => 'changed',
131 'new' => 'new'
132 );
133 $className::updateLocalConfiguration($overrideConfiguration);
134 }
135
136 ///////////////////////
137 // Tests concerning getDefaultConfigurationValueByPath
138 ///////////////////////
139 /**
140 * @test
141 */
142 public function getDefaultConfigurationValueByPathReturnsCorrectValue() {
143 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
144 $className = uniqid('ConfigurationManager');
145 eval(
146 'namespace ' . $namespace . ';' .
147 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
148 ' public static function getDefaultConfiguration() {' .
149 ' return array(\'path\' => \'value\');' .
150 ' }' .
151 '}'
152 );
153 $className = $namespace . '\\' . $className;
154 $this->assertSame('value', $className::getDefaultConfigurationValueByPath('path'));
155 }
156
157 ///////////////////////
158 // Tests concerning getLocalConfigurationValueByPath
159 ///////////////////////
160 /**
161 * @test
162 */
163 public function getLocalConfigurationValueByPathReturnsCorrectValue() {
164 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
165 $className = uniqid('ConfigurationManager');
166 eval(
167 'namespace ' . $namespace . ';' .
168 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
169 ' public static function getLocalConfiguration() {' .
170 ' return array(\'path\' => \'value\');' .
171 ' }' .
172 '}'
173 );
174 $className = $namespace . '\\' . $className;
175 $this->assertSame('value', $className::getLocalConfigurationValueByPath('path'));
176 }
177
178 ///////////////////////
179 // Tests concerning getConfigurationValueByPath
180 ///////////////////////
181 /**
182 * @test
183 */
184 public function getConfigurationValueByPathReturnsCorrectValue() {
185 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
186 $className = uniqid('ConfigurationManager');
187 eval(
188 'namespace ' . $namespace . ';' .
189 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
190 ' public static function getDefaultConfiguration() {' .
191 ' return array(\'path\' => \'value\');' .
192 ' }' .
193 ' public static function getLocalConfiguration() {' .
194 ' return array(\'path\' => \'valueOverride\');' .
195 ' }' .
196 '}'
197 );
198 $className = $namespace . '\\' . $className;
199 $this->assertSame('valueOverride', $className::getConfigurationValueByPath('path'));
200 }
201
202 ///////////////////////
203 // Tests concerning setLocalConfigurationValueByPath
204 ///////////////////////
205 /**
206 * @test
207 */
208 public function setLocalConfigurationValueByPathReturnFalseIfPathIsNotValid() {
209 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
210 $className = uniqid('ConfigurationManager');
211 eval(
212 'namespace ' . $namespace . ';' .
213 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
214 ' public static function isValidLocalConfigurationPath() {' .
215 ' return FALSE;' .
216 ' }' .
217 '}'
218 );
219 $className = $namespace . '\\' . $className;
220 $this->assertFalse($className::setLocalConfigurationValueByPath('path', 'value'));
221 }
222
223 /**
224 * @test
225 */
226 public function setLocalConfigurationValueByPathUpdatesValueDefinedByPath() {
227 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
228 $className = uniqid('ConfigurationManager');
229 eval(
230 'namespace ' . $namespace . ';' .
231 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
232 ' public static function isValidLocalConfigurationPath() {' .
233 ' return TRUE;' .
234 ' }' .
235 ' public static function getLocalConfiguration() {' .
236 ' $currentLocalConfiguration = array(' .
237 ' \'notChanged\' => 23,' .
238 ' \'toUpdate\' => \'notUpdated\',' .
239 ' );' .
240 ' return $currentLocalConfiguration;' .
241 ' }' .
242 ' public static function writeLocalConfiguration($conf) {' .
243 ' $expectedConfiguration = array(' .
244 ' \'notChanged\' => 23,' .
245 ' \'toUpdate\' => \'updated\',' .
246 ' );' .
247 ' if (!($conf === $expectedConfiguration)) {' .
248 ' throw new Exception(\'broken\');' .
249 ' }' .
250 ' return TRUE;' .
251 ' }' .
252 '}'
253 );
254 $className = $namespace . '\\' . $className;
255 $pathToUpdate = 'toUpdate';
256 $valueToUpdate = 'updated';
257 $this->assertTrue($className::setLocalConfigurationValueByPath($pathToUpdate, $valueToUpdate));
258 }
259
260 ///////////////////////
261 // Tests concerning setLocalConfigurationValuesByPathValuePairs
262 ///////////////////////
263 /**
264 * @test
265 */
266 public function setLocalConfigurationValuesByPathValuePairsSetsPathValuePairs() {
267 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
268 $className = uniqid('ConfigurationManager');
269 eval(
270 'namespace ' . $namespace . ';' .
271 'class ' . $className .
272 ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
273 ' public static function isValidLocalConfigurationPath() {' .
274 ' return TRUE;' .
275 ' }' .
276 ' public static function getLocalConfiguration() {' .
277 ' $currentLocalConfiguration = array(' .
278 ' \'notChanged\' => 23,' .
279 ' \'toUpdate\' => \'notUpdated\',' .
280 ' );' .
281 ' return $currentLocalConfiguration;' .
282 ' }' .
283 ' public static function writeLocalConfiguration($conf) {' .
284 ' $expectedConfiguration = array(' .
285 ' \'notChanged\' => 23,' .
286 ' \'toUpdate\' => \'updated\',' .
287 ' \'new\' => \'new\',' .
288 ' );' .
289 ' if (!($conf === $expectedConfiguration)) {' .
290 ' throw new Exception(\'broken\');' .
291 ' }' .
292 ' return TRUE;' .
293 ' }' .
294 '}'
295 );
296 $className = $namespace . '\\' . $className;
297 $pairs = array(
298 'toUpdate' => 'updated',
299 'new' => 'new'
300 );
301 $this->assertTrue($className::setLocalConfigurationValuesByPathValuePairs($pairs));
302 }
303
304 ///////////////////////
305 // Tests concerning writeLocalConfiguration
306 ///////////////////////
307 /**
308 * @test
309 */
310 public function writeLocalConfigurationWritesSortedContentToConfigurationFile() {
311 $configurationFile = 'typo3temp/' . uniqid('localConfiguration');
312 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
313 $className = uniqid('ConfigurationManager');
314 eval(
315 'namespace ' . $namespace . ';' .
316 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
317 ' const LOCAL_CONFIGURATION_FILE = \'' . $configurationFile . '\';' .
318 ' public static function writeLocalConfiguration($conf) {' .
319 ' return parent::writeLocalConfiguration($conf);' .
320 ' }' .
321 '}'
322 );
323 $className = $namespace . '\\' . $className;
324 $this->testFilesToDelete[] = PATH_site . $configurationFile;
325 $pairs = array(
326 'foo' => 42,
327 'bar' => 23
328 );
329 $expectedContent =
330 '<?php' . LF .
331 'return array(' . LF .
332 TAB . '\'bar\' => 23,' . LF .
333 TAB . '\'foo\' => 42,' . LF .
334 ');' . LF .
335 '?>';
336 $this->assertTrue($className::writeLocalConfiguration($pairs));
337 $this->assertEquals($expectedContent, file_get_contents(PATH_site . $configurationFile));
338 }
339
340 ///////////////////////
341 // Tests concerning isValidLocalConfigurationPath
342 ///////////////////////
343 /**
344 * @test
345 */
346 public function isValidLocalConfigurationPathAcceptsWhitelistedPath() {
347 $namespace = 'TYPO3\\CMS\\Core\\Configuration';
348 $className = uniqid('ConfigurationManager');
349 eval(
350 'namespace ' . $namespace . ';' .
351 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
352 ' protected static $whiteListedLocalConfigurationPaths = array(' .
353 ' \'foo\',' .
354 ' );' .
355 ' public static function isValidLocalConfigurationPath($path) {' .
356 ' return parent::isValidLocalConfigurationPath($path);' .
357 ' }' .
358 '}'
359 );
360 $className = $namespace . '\\' . $className;
361 $this->assertTrue($className::isValidLocalConfigurationPath('foo'));
362 }
363
364 }
365
366 ?>