[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 * 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 /**
18 * Test case
19 *
20 * @author Christian Kuhn <lolli@schwarzbu.ch>
21 */
22 class ConfigurationManagerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * Absolute path to files that must be removed
26 * after a test - handled in tearDown
27 */
28 protected $testFilesToDelete = array();
29
30 /**
31 * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject
32 */
33 protected $fixture;
34
35 public function setUp() {
36 $this->createFixtureWithMockedMethods(
37 array(
38 'getDefaultConfigurationFileLocation',
39 'getLocalConfigurationFileLocation',
40 )
41 );
42 }
43
44 /**
45 * Tear down test case
46 */
47 public function tearDown() {
48 foreach ($this->testFilesToDelete as $absoluteFileName) {
49 \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($absoluteFileName);
50 }
51 parent::tearDown();
52 }
53
54 /**
55 * @param array $methods
56 */
57 protected function createFixtureWithMockedMethods(array $methods) {
58 $this->fixture = $this->getMock(
59 'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager',
60 $methods
61 );
62 }
63
64 /**
65 * @test
66 * @expectedException \RuntimeException
67 */
68 public function getDefaultConfigurationExecutesDefinedDefaultConfigurationFile() {
69 $defaultConfigurationFile = PATH_site . 'typo3temp/' . uniqid('defaultConfiguration');
70 file_put_contents(
71 $defaultConfigurationFile,
72 '<?php throw new \RuntimeException(\'foo\', 1310203814); ?>'
73 );
74 $this->testFilesToDelete[] = $defaultConfigurationFile;
75
76 $this->fixture
77 ->expects($this->once())
78 ->method('getDefaultConfigurationFileLocation')
79 ->will($this->returnValue($defaultConfigurationFile));
80 $this->fixture->getDefaultConfiguration();
81 }
82
83 /**
84 * @test
85 * @expectedException \RuntimeException
86 */
87 public function getLocalConfigurationExecutesDefinedConfigurationFile() {
88 $configurationFile = PATH_site . 'typo3temp/' . uniqid('localConfiguration');
89 file_put_contents(
90 $configurationFile,
91 '<?php throw new \RuntimeException(\'foo\', 1310203815); ?>'
92 );
93 $this->testFilesToDelete[] = $configurationFile;
94
95 $this->fixture
96 ->expects($this->once())
97 ->method('getLocalConfigurationFileLocation')
98 ->will($this->returnValue($configurationFile));
99 $this->fixture->getLocalConfiguration();
100 }
101
102 /**
103 * @test
104 */
105 public function updateLocalConfigurationWritesNewMergedLocalConfigurationArray() {
106 $currentLocalConfiguration = array(
107 'notChanged' => 23,
108 'changed' => 'unChanged',
109 );
110 $overrideConfiguration = array(
111 'changed' => 'changed',
112 'new' => 'new'
113 );
114 $expectedConfiguration = array(
115 'notChanged' => 23,
116 'changed' => 'changed',
117 'new' => 'new',
118 );
119
120 $this->createFixtureWithMockedMethods(
121 array(
122 'getLocalConfiguration',
123 'writeLocalConfiguration',
124 )
125 );
126 $this->fixture->expects($this->once())
127 ->method('getLocalConfiguration')
128 ->will($this->returnValue($currentLocalConfiguration));
129 $this->fixture->expects($this->once())
130 ->method('writeLocalConfiguration')
131 ->with($expectedConfiguration);
132
133 $this->fixture->updateLocalConfiguration($overrideConfiguration);
134 }
135
136 /**
137 * @test
138 */
139 public function getDefaultConfigurationValueByPathReturnsCorrectValue() {
140 $this->createFixtureWithMockedMethods(
141 array(
142 'getDefaultConfiguration',
143 )
144 );
145 $this->fixture->expects($this->once())
146 ->method('getDefaultConfiguration')
147 ->will($this->returnValue(array(
148 'path' => 'value',
149 )
150 ));
151
152 $this->assertSame('value', $this->fixture->getDefaultConfigurationValueByPath('path'));
153 }
154
155 /**
156 * @test
157 */
158 public function getLocalConfigurationValueByPathReturnsCorrectValue() {
159 $this->createFixtureWithMockedMethods(
160 array(
161 'getLocalConfiguration',
162 )
163 );
164 $this->fixture->expects($this->once())
165 ->method('getLocalConfiguration')
166 ->will($this->returnValue(array(
167 'path' => 'value',
168 )
169 ));
170
171 $this->assertSame('value', $this->fixture->getLocalConfigurationValueByPath('path'));
172 }
173
174 /**
175 * @test
176 */
177 public function getConfigurationValueByPathReturnsCorrectValue() {
178 $this->createFixtureWithMockedMethods(
179 array(
180 'getDefaultConfiguration',
181 'getLocalConfiguration',
182 )
183 );
184 $this->fixture->expects($this->once())
185 ->method('getDefaultConfiguration')
186 ->will($this->returnValue(array(
187 'path' => 'value',
188 )
189 ));
190 $this->fixture->expects($this->once())
191 ->method('getLocalConfiguration')
192 ->will($this->returnValue(array(
193 'path' => 'valueOverride',
194 )
195 ));
196
197 $this->assertSame('valueOverride', $this->fixture->getConfigurationValueByPath('path'));
198 }
199
200 /**
201 * @test
202 */
203 public function setLocalConfigurationValueByPathReturnFalseIfPathIsNotValid() {
204 $this->createFixtureWithMockedMethods(array(
205 'isValidLocalConfigurationPath',
206 ));
207 $this->fixture->expects($this->once())
208 ->method('isValidLocalConfigurationPath')
209 ->will($this->returnValue(FALSE));
210
211 $this->assertFalse($this->fixture->setLocalConfigurationValueByPath('path', 'value'));
212 }
213
214 /**
215 * @test
216 */
217 public function setLocalConfigurationValueByPathUpdatesValueDefinedByPath() {
218 $currentLocalConfiguration = array(
219 'notChanged' => 23,
220 'toUpdate' => 'notUpdated',
221 );
222 $expectedConfiguration = array(
223 'notChanged' => 23,
224 'toUpdate' => 'updated',
225 );
226
227 $this->createFixtureWithMockedMethods(
228 array(
229 'isValidLocalConfigurationPath',
230 'getLocalConfiguration',
231 'writeLocalConfiguration',
232 )
233 );
234 $this->fixture->expects($this->once())
235 ->method('isValidLocalConfigurationPath')
236 ->will($this->returnValue(TRUE));
237 $this->fixture->expects($this->once())
238 ->method('getLocalConfiguration')
239 ->will($this->returnValue($currentLocalConfiguration));
240 $this->fixture->expects($this->once())
241 ->method('writeLocalConfiguration')
242 ->with($expectedConfiguration);
243
244 $this->fixture->setLocalConfigurationValueByPath('toUpdate', 'updated');
245 }
246
247 /**
248 * @test
249 */
250 public function setLocalConfigurationValuesByPathValuePairsSetsPathValuePairs() {
251 $currentLocalConfiguration = array(
252 'notChanged' => 23,
253 'toUpdate' => 'notUpdated',
254 );
255 $expectedConfiguration = array(
256 'notChanged' => 23,
257 'toUpdate' => 'updated',
258 'new' => 'new',
259 );
260
261 $this->createFixtureWithMockedMethods(
262 array(
263 'isValidLocalConfigurationPath',
264 'getLocalConfiguration',
265 'writeLocalConfiguration',
266 )
267 );
268 $this->fixture->expects($this->any())
269 ->method('isValidLocalConfigurationPath')
270 ->will($this->returnValue(TRUE));
271 $this->fixture->expects($this->once())
272 ->method('getLocalConfiguration')
273 ->will($this->returnValue($currentLocalConfiguration));
274 $this->fixture->expects($this->once())
275 ->method('writeLocalConfiguration')
276 ->with($expectedConfiguration);
277
278 $pairs = array(
279 'toUpdate' => 'updated',
280 'new' => 'new'
281 );
282 $this->fixture->setLocalConfigurationValuesByPathValuePairs($pairs);
283 }
284
285 /**
286 * @test
287 */
288 public function removeLocalConfigurationKeysByPathRemovesGivenPathsFromConfigurationAndReturnsTrue() {
289 $currentLocalConfiguration = array(
290 'toRemove1' => 'foo',
291 'notChanged' => 23,
292 'toRemove2' => 'bar',
293 );
294 $expectedConfiguration = array(
295 'notChanged' => 23,
296 );
297
298 $this->createFixtureWithMockedMethods(
299 array(
300 'getLocalConfiguration',
301 'writeLocalConfiguration',
302 )
303 );
304 $this->fixture->expects($this->once())
305 ->method('getLocalConfiguration')
306 ->will($this->returnValue($currentLocalConfiguration));
307 $this->fixture->expects($this->once())
308 ->method('writeLocalConfiguration')
309 ->with($expectedConfiguration);
310
311 $removePaths = array(
312 'toRemove1',
313 'toRemove2',
314 );
315 $this->assertTrue($this->fixture->removeLocalConfigurationKeysByPath($removePaths));
316 }
317
318 /**
319 * @test
320 */
321 public function removeLocalConfigurationKeysByPathReturnsFalseIfNothingIsRemoved() {
322 $currentLocalConfiguration = array(
323 'notChanged' => 23,
324 );
325 $this->createFixtureWithMockedMethods(
326 array(
327 'getLocalConfiguration',
328 'writeLocalConfiguration',
329 )
330 );
331 $this->fixture->expects($this->once())
332 ->method('getLocalConfiguration')
333 ->will($this->returnValue($currentLocalConfiguration));
334 $this->fixture->expects($this->never())
335 ->method('writeLocalConfiguration');
336
337 $removeNothing = array();
338 $this->assertFalse($this->fixture->removeLocalConfigurationKeysByPath($removeNothing));
339 }
340
341 /**
342 * @test
343 */
344 public function removeLocalConfigurationKeysByPathReturnsFalseIfSomethingInexistentIsRemoved() {
345 $currentLocalConfiguration = array(
346 'notChanged' => 23,
347 );
348 $this->createFixtureWithMockedMethods(
349 array(
350 'getLocalConfiguration',
351 'writeLocalConfiguration',
352 )
353 );
354 $this->fixture->expects($this->once())
355 ->method('getLocalConfiguration')
356 ->will($this->returnValue($currentLocalConfiguration));
357 $this->fixture->expects($this->never())
358 ->method('writeLocalConfiguration');
359
360 $removeNonExisting = array('notPresent');
361 $this->assertFalse($this->fixture->removeLocalConfigurationKeysByPath($removeNonExisting));
362 }
363
364 /**
365 * @test
366 */
367 public function canWriteConfigurationReturnsFalseIfDirectoryIsNotWritable() {
368 if (function_exists('posix_getegid') && posix_getegid() === 0) {
369 $this->markTestSkipped('Test skipped if run on linux as root');
370 } elseif (TYPO3_OS == 'WIN') {
371 $this->markTestSkipped('Not available on Windows');
372 }
373 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
374 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
375
376 $directory = 'typo3temp/' . uniqid('test_');
377 $absoluteDirectory = PATH_site . $directory;
378 mkdir($absoluteDirectory);
379 chmod($absoluteDirectory, 0544);
380 clearstatcache();
381
382 $fixture->_set('pathTypo3Conf', $directory);
383
384 $result = $fixture->canWriteConfiguration();
385
386 chmod($absoluteDirectory, 0755);
387 rmdir($absoluteDirectory);
388
389 $this->assertFalse($result);
390 }
391
392 /**
393 * @test
394 */
395 public function canWriteConfigurationReturnsFalseIfLocalConfigurationFileIsNotWritable() {
396 if (function_exists('posix_getegid') && posix_getegid() === 0) {
397 $this->markTestSkipped('Test skipped if run on linux as root');
398 } elseif (TYPO3_OS == 'WIN') {
399 $this->markTestSkipped('Not available on Windows');
400 }
401 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
402 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
403
404 $file = 'typo3temp/' . uniqid('test_');
405 $absoluteFile = PATH_site . $file;
406 touch($absoluteFile);
407 $this->testFilesToDelete[] = $absoluteFile;
408 chmod($absoluteFile, 0444);
409 clearstatcache();
410
411 $fixture->_set('localConfigurationFile', $file);
412
413 $result = $fixture->canWriteConfiguration();
414
415 chmod($absoluteFile, 0644);
416
417 $this->assertFalse($result);
418 }
419
420 /**
421 * @test
422 */
423 public function canWriteConfigurationReturnsTrueIfDirectoryAndFilesAreWritable() {
424 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
425 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
426
427 $directory = 'typo3temp/' . uniqid('test_');
428 $absoluteDirectory = PATH_site . $directory;
429 mkdir($absoluteDirectory);
430 $fixture->_set('pathTypo3Conf', $absoluteDirectory);
431
432 $file1 = 'typo3temp/' . uniqid('test_');
433 $absoluteFile1 = PATH_site . $file1;
434 touch($absoluteFile1);
435 $this->testFilesToDelete[] = $absoluteFile1;
436 $fixture->_set('localConfigurationFile', $absoluteFile1);
437
438 $file2 = 'typo3temp/' . uniqid('test_');
439 $absoluteFile2 = PATH_site . $file2;
440 touch($absoluteFile2);
441 $this->testFilesToDelete[] = $absoluteFile2;
442 $fixture->_set('localconfFile', $absoluteFile2);
443
444 clearstatcache();
445
446 $result = $fixture->canWriteConfiguration();
447
448 $this->assertTrue($result);
449 }
450
451 /**
452 * @test
453 */
454 public function writeLocalConfigurationWritesSortedContentToConfigurationFile() {
455 $configurationFile = PATH_site . 'typo3temp/' . uniqid('localConfiguration');
456 if (!is_file($configurationFile)) {
457 if (!$fh = fopen($configurationFile, 'wb')) {
458 $this->markTestSkipped('Can not create file ' . $configurationFile . '. Please check your write permissions.');
459 }
460 fclose($fh);
461 }
462
463 if (!@is_file($configurationFile)) {
464 throw new \RuntimeException('File ' . $configurationFile . ' could not be found. Please check your write permissions', 1346364362);
465 }
466 $this->testFilesToDelete[] = $configurationFile;
467
468 $this->fixture
469 ->expects($this->any())
470 ->method('getLocalConfigurationFileLocation')
471 ->will($this->returnValue($configurationFile));
472
473 $pairs = array(
474 'foo' => 42,
475 'bar' => 23
476 );
477 $expectedContent =
478 '<?php' . LF .
479 'return array(' . LF .
480 TAB . '\'bar\' => 23,' . LF .
481 TAB . '\'foo\' => 42,' . LF .
482 ');' . LF .
483 '?>';
484
485 $this->fixture->writeLocalConfiguration($pairs);
486 $this->assertSame($expectedContent, file_get_contents($configurationFile));
487 }
488
489 /**
490 * @test
491 * @expectedException \RuntimeException
492 */
493 public function createLocalConfigurationFromFactoryConfigurationThrowsExceptionIfFileExists() {
494 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
495 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
496
497 $file = 'typo3temp/' . uniqid('test_');
498 $absoluteFile = PATH_site . $file;
499 touch($absoluteFile);
500 $this->testFilesToDelete[] = $absoluteFile;
501 $fixture->_set('localConfigurationFile', $file);
502
503 $fixture->createLocalConfigurationFromFactoryConfiguration();
504 }
505
506 /**
507 * @test
508 */
509 public function createLocalConfigurationFromFactoryConfigurationWritesContentFromFactoryFile() {
510 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
511 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('writeLocalConfiguration'));
512 $fixture->_set('localConfigurationFile', 'typo3temp/' . uniqid('dummy_'));
513
514 $factoryConfigurationFile = 'typo3temp/' . uniqid('test_') . '.php';
515 $factoryConfigurationAbsoluteFile = PATH_site . $factoryConfigurationFile;
516 $uniqueContentString = uniqid('string_');
517 $validFactoryConfigurationFileContent =
518 '<?php' . LF .
519 'return array(' . LF .
520 $uniqueContentString . ' => foo,' . LF .
521 ');' . LF .
522 '?>';
523 file_put_contents(
524 $factoryConfigurationAbsoluteFile,
525 $validFactoryConfigurationFileContent
526 );
527 $this->testFilesToDelete[] = $factoryConfigurationAbsoluteFile;
528
529 $fixture->_set('factoryConfigurationFile', $factoryConfigurationFile);
530
531 $fixture
532 ->expects($this->once())
533 ->method('writeLocalConfiguration')
534 ->with($this->arrayHasKey($uniqueContentString));
535 $fixture->createLocalConfigurationFromFactoryConfiguration();
536 }
537
538 /**
539 * @test
540 */
541 public function createLocalConfigurationFromFactoryConfigurationMergesConfigurationWithAdditionalFactoryFile() {
542 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
543 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('writeLocalConfiguration'));
544 $fixture->_set('localConfigurationFile', 'typo3temp/' . uniqid('dummy_'));
545
546 $factoryConfigurationFile = 'typo3temp/' . uniqid('test_') . '.php';
547 $factoryConfigurationAbsoluteFile = PATH_site . $factoryConfigurationFile;
548 $validFactoryConfigurationFileContent =
549 '<?php' . LF .
550 'return array();' . LF .
551 '?>';
552 file_put_contents(
553 $factoryConfigurationAbsoluteFile,
554 $validFactoryConfigurationFileContent
555 );
556 $this->testFilesToDelete[] = $factoryConfigurationAbsoluteFile;
557 $fixture->_set('factoryConfigurationFile', $factoryConfigurationFile);
558
559 $additionalFactoryConfigurationFile = 'typo3temp/' . uniqid('test_') . '.php';
560 $additionalFactoryConfigurationAbsoluteFile = PATH_site . $additionalFactoryConfigurationFile;
561 $uniqueContentString = uniqid('string_');
562 $validAdditionalFactoryConfigurationFileContent =
563 '<?php' . LF .
564 'return array(' . LF .
565 $uniqueContentString . ' => foo,' . LF .
566 ');' . LF .
567 '?>';
568 file_put_contents(
569 $additionalFactoryConfigurationAbsoluteFile,
570 $validAdditionalFactoryConfigurationFileContent
571 );
572 $this->testFilesToDelete[] = $additionalFactoryConfigurationAbsoluteFile;
573 $fixture->_set('additionalFactoryConfigurationFile', $additionalFactoryConfigurationFile);
574
575 $fixture
576 ->expects($this->once())
577 ->method('writeLocalConfiguration')
578 ->with($this->arrayHasKey($uniqueContentString));
579 $fixture->createLocalConfigurationFromFactoryConfiguration();
580 }
581
582 /**
583 * @test
584 */
585 public function isValidLocalConfigurationPathAcceptsWhitelistedPath() {
586 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
587 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
588 $fixture->_set('whiteListedLocalConfigurationPaths', array('foo/bar'));
589 $this->assertTrue($fixture->_call('isValidLocalConfigurationPath', 'foo/bar/baz'));
590 }
591
592 /**
593 * @test
594 */
595 public function isValidLocalConfigurationPathDeniesNotWhitelistedPath() {
596 /** @var $fixture \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
597 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
598 $fixture->_set('whiteListedLocalConfigurationPaths', array('foo/bar'));
599 $this->assertFalse($fixture->_call('isValidLocalConfigurationPath', 'bar/baz'));
600 }
601 }