[TASK] Make Core\Configuration\ConfigurationManager non static
[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 \TYPO3\CMS\Core\Tests\UnitTestCase {
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 * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $fixture;
46
47 public function setUp() {
48 $this->createFixtureWithMockedMethods(
49 array(
50 'getDefaultConfigurationFileResource',
51 'getLocalConfigurationFileResource',
52 )
53 );
54 }
55
56 /**
57 * Tear down test case
58 */
59 public function tearDown() {
60 foreach ($this->testFilesToDelete as $absoluteFileName) {
61 \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($absoluteFileName);
62 }
63 }
64
65 /**
66 * @param array $methods
67 */
68 protected function createFixtureWithMockedMethods(array $methods) {
69 $this->fixture = $this->getMock(
70 'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager',
71 $methods
72 );
73
74 }
75
76 /**
77 * Tests concerning getDefaultConfiguration
78 *
79 */
80 /**
81 * @test
82 * @expectedException \RuntimeException
83 */
84 public function getDefaultConfigurationExecutesDefinedDefaultConfigurationFile() {
85 $defaultConfigurationFile = PATH_site . 'typo3temp/' . uniqid('defaultConfiguration');
86 file_put_contents($defaultConfigurationFile, '<?php throw new \RuntimeException(\'foo\', 1310203814); ?>');
87 $this->testFilesToDelete[] = $defaultConfigurationFile;
88
89 $this->fixture->expects($this->once())->method('getDefaultConfigurationFileResource')->will($this->returnValue($defaultConfigurationFile));
90 $this->fixture->getDefaultConfiguration();
91 }
92
93 /**
94 * Tests concerning getLocalConfiguration
95 */
96 /**
97 * @test
98 * @expectedException \RuntimeException
99 */
100 public function getLocalConfigurationExecutesDefinedConfigurationFile() {
101 $configurationFile = PATH_site . 'typo3temp/' . uniqid('localConfiguration');
102 file_put_contents($configurationFile, '<?php throw new \RuntimeException(\'foo\', 1310203815); ?>');
103 $this->testFilesToDelete[] = $configurationFile;
104
105 $this->fixture->expects($this->once())->method('getLocalConfigurationFileResource')->will($this->returnValue($configurationFile));
106 $this->fixture->getLocalConfiguration();
107 }
108
109 /**
110 * Tests concerning updateLocalConfiguration
111 */
112 /**
113 * @test
114 */
115 public function updateLocalConfigurationWritesNewMergedLocalConfigurationArray() {
116 $currentLocalConfiguration = array(
117 'notChanged' => 23,
118 'changed' => 'unChanged',
119 );
120 $overrideConfiguration = array(
121 'changed' => 'changed',
122 'new' => 'new'
123 );
124 $expectedConfiguration = array(
125 'notChanged' => 23,
126 'changed' => 'changed',
127 'new' => 'new',
128 );
129
130 $this->createFixtureWithMockedMethods(array(
131 'getLocalConfiguration',
132 'writeLocalConfiguration',
133 ));
134 $this->fixture->expects($this->once())
135 ->method('getLocalConfiguration')
136 ->will($this->returnValue($currentLocalConfiguration));
137 $this->fixture->expects($this->once())
138 ->method('writeLocalConfiguration')
139 ->with($expectedConfiguration);
140
141 $this->fixture->updateLocalConfiguration($overrideConfiguration);
142 }
143
144 /**
145 * Tests concerning getDefaultConfigurationValueByPath
146 */
147 /**
148 * @test
149 */
150 public function getDefaultConfigurationValueByPathReturnsCorrectValue() {
151 $this->createFixtureWithMockedMethods(array(
152 'getDefaultConfiguration',
153 ));
154 $this->fixture->expects($this->once())
155 ->method('getDefaultConfiguration')
156 ->will($this->returnValue(array(
157 'path' => 'value',
158 )
159 ));
160
161 $this->assertSame('value', $this->fixture->getDefaultConfigurationValueByPath('path'));
162 }
163
164 /**
165 * Tests concerning getLocalConfigurationValueByPath
166 */
167 /**
168 * @test
169 */
170 public function getLocalConfigurationValueByPathReturnsCorrectValue() {
171 $this->createFixtureWithMockedMethods(array(
172 'getLocalConfiguration',
173 ));
174 $this->fixture->expects($this->once())
175 ->method('getLocalConfiguration')
176 ->will($this->returnValue(array(
177 'path' => 'value',
178 )
179 ));
180
181 $this->assertSame('value', $this->fixture->getLocalConfigurationValueByPath('path'));
182 }
183
184 /**
185 * Tests concerning getConfigurationValueByPath
186 */
187 /**
188 * @test
189 */
190 public function getConfigurationValueByPathReturnsCorrectValue() {
191 $this->createFixtureWithMockedMethods(array(
192 'getDefaultConfiguration',
193 'getLocalConfiguration',
194 ));
195 $this->fixture->expects($this->once())
196 ->method('getDefaultConfiguration')
197 ->will($this->returnValue(array(
198 'path' => 'value',
199 )
200 ));
201 $this->fixture->expects($this->once())
202 ->method('getLocalConfiguration')
203 ->will($this->returnValue(array(
204 'path' => 'valueOverride',
205 )
206 ));
207
208 $this->assertSame('valueOverride', $this->fixture->getConfigurationValueByPath('path'));
209 }
210
211 /**
212 * Tests concerning setLocalConfigurationValueByPath
213 */
214 /**
215 * @test
216 */
217 public function setLocalConfigurationValueByPathReturnFalseIfPathIsNotValid() {
218 $this->createFixtureWithMockedMethods(array(
219 'isValidLocalConfigurationPath',
220 ));
221 $this->fixture->expects($this->once())
222 ->method('isValidLocalConfigurationPath')
223 ->will($this->returnValue(FALSE));
224
225 $this->assertFalse($this->fixture->setLocalConfigurationValueByPath('path', 'value'));
226 }
227
228 /**
229 * @test
230 */
231 public function setLocalConfigurationValueByPathUpdatesValueDefinedByPath() {
232 $currentLocalConfiguration = array(
233 'notChanged' => 23,
234 'toUpdate' => 'notUpdated',
235 );
236 $expectedConfiguration = array(
237 'notChanged' => 23,
238 'toUpdate' => 'updated',
239 );
240
241 $this->createFixtureWithMockedMethods(array(
242 'isValidLocalConfigurationPath',
243 'getLocalConfiguration',
244 'writeLocalConfiguration',
245 ));
246 $this->fixture->expects($this->once())
247 ->method('isValidLocalConfigurationPath')
248 ->will($this->returnValue(TRUE));
249 $this->fixture->expects($this->once())
250 ->method('getLocalConfiguration')
251 ->will($this->returnValue($currentLocalConfiguration));
252 $this->fixture->expects($this->once())
253 ->method('writeLocalConfiguration')
254 ->with($expectedConfiguration);
255
256 $this->fixture->setLocalConfigurationValueByPath('toUpdate', 'updated');
257 }
258
259 /**
260 * Tests concerning setLocalConfigurationValuesByPathValuePairs
261 */
262 /**
263 * @test
264 */
265 public function setLocalConfigurationValuesByPathValuePairsSetsPathValuePairs() {
266 $currentLocalConfiguration = array(
267 'notChanged' => 23,
268 'toUpdate' => 'notUpdated',
269 );
270 $expectedConfiguration = array(
271 'notChanged' => 23,
272 'toUpdate' => 'updated',
273 'new' => 'new',
274 );
275
276 $this->createFixtureWithMockedMethods(array(
277 'isValidLocalConfigurationPath',
278 'getLocalConfiguration',
279 'writeLocalConfiguration',
280 ));
281 $this->fixture->expects($this->any())
282 ->method('isValidLocalConfigurationPath')
283 ->will($this->returnValue(TRUE));
284 $this->fixture->expects($this->once())
285 ->method('getLocalConfiguration')
286 ->will($this->returnValue($currentLocalConfiguration));
287 $this->fixture->expects($this->once())
288 ->method('writeLocalConfiguration')
289 ->with($expectedConfiguration);
290
291 $pairs = array(
292 'toUpdate' => 'updated',
293 'new' => 'new'
294 );
295 $this->fixture->setLocalConfigurationValuesByPathValuePairs($pairs);
296 }
297
298 /**
299 * Tests concerning writeLocalConfiguration
300 */
301 /**
302 * @test
303 * @expectedException \RuntimeException
304 */
305 public function writeLocalConfigurationThrowsExceptionForInvalidFile() {
306 $configurationFile = 'typo3temp/' . uniqid('localConfiguration');
307 $this->fixture->expects($this->once())->method('getLocalConfigurationFileResource')->will($this->returnValue($configurationFile));
308
309 $pairs = array(
310 'foo' => 42,
311 'bar' => 23
312 );
313 $this->fixture->writeLocalConfiguration($pairs);
314 }
315
316 /**
317 * @test
318 */
319 public function writeLocalConfigurationWritesSortedContentToConfigurationFile() {
320 $configurationFile = PATH_site . 'typo3temp/' . uniqid('localConfiguration');
321 if (!is_file($configurationFile)) {
322 if (!$fh = fopen($configurationFile, 'wb')) {
323 $this->markTestSkipped('Can not create file ' . $configurationFile . '. Please check your write permissions.');
324 }
325 fclose($fh);
326 }
327
328 if (!@is_file($configurationFile)) {
329 throw new \RuntimeException('File ' . $configurationFile . ' could not be found. Please check your write permissions', 1346364362);
330 }
331 $this->testFilesToDelete[] = $configurationFile;
332
333 $this->fixture->expects($this->once())->method('getLocalConfigurationFileResource')->will($this->returnValue($configurationFile));
334
335 $pairs = array(
336 'foo' => 42,
337 'bar' => 23
338 );
339 $expectedContent =
340 '<?php' . LF .
341 'return array(' . LF .
342 TAB . '\'bar\' => 23,' . LF .
343 TAB . '\'foo\' => 42,' . LF .
344 ');' . LF .
345 '?>';
346
347 $this->fixture->writeLocalConfiguration($pairs);
348 $this->assertSame($expectedContent, file_get_contents($configurationFile));
349 }
350
351 /**
352 * Tests concerning isValidLocalConfigurationPath
353 */
354 /**
355 * @test
356 */
357 public function isValidLocalConfigurationPathAcceptsWhitelistedPath() {
358 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
359 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
360 $fixture->_set('whiteListedLocalConfigurationPaths', array('foo/bar'));
361 $this->assertTrue($fixture->_call('isValidLocalConfigurationPath', 'foo/bar/baz'));
362 }
363
364 /**
365 * @test
366 */
367 public function isValidLocalConfigurationPathDeniesNotWhitelistedPath() {
368 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
369 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
370 $fixture->_set('whiteListedLocalConfigurationPaths', array('foo/bar'));
371 $this->assertFalse($fixture->_call('isValidLocalConfigurationPath', 'bar/baz'));
372 }
373 }
374 ?>