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