2 declare(strict_types = 1);
19 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 protected function setUp(): void
37 'getDefaultConfigurationFileLocation',
38 'getLocalConfigurationFileLocation',
48 $this->subject = $this->getMockBuilder(ConfigurationManager::class)
49 ->setMethods($methods)
58 $this->expectException(\RuntimeException::class);
59 $this->expectExceptionCode(1310203814);
63 $defaultConfigurationFile,
64 '<?php throw new \RuntimeException(\'foo\', 1310203814); ?>'
66 $this->testFilesToDelete[] = $defaultConfigurationFile;
69 ->expects($this->once())
70 ->method(
'getDefaultConfigurationFileLocation')
71 ->will($this->returnValue($defaultConfigurationFile));
72 $this->subject->getDefaultConfiguration();
80 $this->expectException(\RuntimeException::class);
85 '<?php throw new \RuntimeException(\'foo\', 1310203815); ?>'
87 $this->testFilesToDelete[] = $configurationFile;
90 ->expects($this->once())
91 ->method(
'getLocalConfigurationFileLocation')
92 ->will($this->returnValue($configurationFile));
93 $this->subject->getLocalConfiguration();
101 $currentLocalConfiguration = [
103 'changed' =>
'unChanged',
105 $overrideConfiguration = [
106 'changed' =>
'changed',
109 $expectedConfiguration = [
111 'changed' =>
'changed',
117 'getLocalConfiguration',
118 'writeLocalConfiguration',
121 $this->subject->expects($this->once())
122 ->method(
'getLocalConfiguration')
123 ->will($this->returnValue($currentLocalConfiguration));
124 $this->subject->expects($this->once())
125 ->method(
'writeLocalConfiguration')
126 ->with($expectedConfiguration);
128 $this->subject->updateLocalConfiguration($overrideConfiguration);
138 'getDefaultConfiguration',
141 $this->subject->expects($this->once())
142 ->method(
'getDefaultConfiguration')
143 ->will($this->returnValue(
149 $this->assertSame(
'value', $this->subject->getDefaultConfigurationValueByPath(
'path'));
159 'getLocalConfiguration',
162 $this->subject->expects($this->once())
163 ->method(
'getLocalConfiguration')
164 ->will($this->returnValue(
170 $this->assertSame(
'value', $this->subject->getLocalConfigurationValueByPath(
'path'));
180 'getDefaultConfiguration',
181 'getLocalConfiguration',
184 $this->subject->expects($this->once())
185 ->method(
'getDefaultConfiguration')
186 ->will($this->returnValue(
191 $this->subject->expects($this->once())
192 ->method(
'getLocalConfiguration')
193 ->will($this->returnValue(
195 'path' =>
'valueOverride',
199 $this->assertSame(
'valueOverride', $this->subject->getConfigurationValueByPath(
'path'));
208 'isValidLocalConfigurationPath',
210 $this->subject->expects($this->once())
211 ->method(
'isValidLocalConfigurationPath')
212 ->will($this->returnValue(
false));
214 $this->assertFalse($this->subject->setLocalConfigurationValueByPath(
'path',
'value'));
222 $currentLocalConfiguration = [
224 'toUpdate' =>
'notUpdated',
226 $expectedConfiguration = [
228 'toUpdate' =>
'updated',
233 'isValidLocalConfigurationPath',
234 'getLocalConfiguration',
235 'writeLocalConfiguration',
238 $this->subject->expects($this->once())
239 ->method(
'isValidLocalConfigurationPath')
240 ->will($this->returnValue(
true));
241 $this->subject->expects($this->once())
242 ->method(
'getLocalConfiguration')
243 ->will($this->returnValue($currentLocalConfiguration));
244 $this->subject->expects($this->once())
245 ->method(
'writeLocalConfiguration')
246 ->with($expectedConfiguration);
248 $this->subject->setLocalConfigurationValueByPath(
'toUpdate',
'updated');
256 $currentLocalConfiguration = [
258 'toUpdate' =>
'notUpdated',
260 $expectedConfiguration = [
262 'toUpdate' =>
'updated',
268 'isValidLocalConfigurationPath',
269 'getLocalConfiguration',
270 'writeLocalConfiguration',
273 $this->subject->expects($this->any())
274 ->method(
'isValidLocalConfigurationPath')
275 ->will($this->returnValue(
true));
276 $this->subject->expects($this->once())
277 ->method(
'getLocalConfiguration')
278 ->will($this->returnValue($currentLocalConfiguration));
279 $this->subject->expects($this->once())
280 ->method(
'writeLocalConfiguration')
281 ->with($expectedConfiguration);
284 'toUpdate' =>
'updated',
287 $this->subject->setLocalConfigurationValuesByPathValuePairs($pairs);
295 $currentLocalConfiguration = [
296 'toRemove1' =>
'foo',
298 'toRemove2' =>
'bar',
300 $expectedConfiguration = [
306 'getLocalConfiguration',
307 'writeLocalConfiguration',
310 $this->subject->expects($this->once())
311 ->method(
'getLocalConfiguration')
312 ->will($this->returnValue($currentLocalConfiguration));
313 $this->subject->expects($this->once())
314 ->method(
'writeLocalConfiguration')
315 ->with($expectedConfiguration);
321 $this->assertTrue($this->subject->removeLocalConfigurationKeysByPath($removePaths));
329 $currentLocalConfiguration = [
334 'getLocalConfiguration',
335 'writeLocalConfiguration',
338 $this->subject->expects($this->once())
339 ->method(
'getLocalConfiguration')
340 ->will($this->returnValue($currentLocalConfiguration));
341 $this->subject->expects($this->never())
342 ->method(
'writeLocalConfiguration');
345 $this->assertFalse($this->subject->removeLocalConfigurationKeysByPath($removeNothing));
353 $currentLocalConfiguration = [
358 'getLocalConfiguration',
359 'writeLocalConfiguration',
362 $this->subject->expects($this->once())
363 ->method(
'getLocalConfiguration')
364 ->will($this->returnValue($currentLocalConfiguration));
365 $this->subject->expects($this->never())
366 ->method(
'writeLocalConfiguration');
368 $removeNonExisting = [
'notPresent'];
369 $this->assertFalse($this->subject->removeLocalConfigurationKeysByPath($removeNonExisting));
377 if (\function_exists(
'posix_getegid') && posix_getegid() === 0) {
378 $this->markTestSkipped(
'Test skipped if run on linux as root');
381 $subject = $this->getAccessibleMock(ConfigurationManager::class, [
'dummy']);
383 $file =
'../typo3temp/var/tests/' . $this->getUniqueId(
'test_');
385 touch($absoluteFile);
386 $this->testFilesToDelete[] = $absoluteFile;
387 chmod($absoluteFile, 0444);
390 $subject->_set(
'localConfigurationFile', $file);
392 $result =
$subject->canWriteConfiguration();
394 chmod($absoluteFile, 0644);
396 $this->assertFalse($result);
405 $subject = $this->getAccessibleMock(ConfigurationManager::class, [
'dummy']);
407 $directory =
'typo3temp/var/tests/' . $this->getUniqueId(
'test_');
409 mkdir($absoluteDirectory);
411 $file =
'typo3temp/var/tests/' . $this->getUniqueId(
'test_');
413 touch($absoluteFile1);
414 $this->testFilesToDelete[] = $absoluteFile1;
415 $subject->_set(
'localConfigurationFile', $absoluteFile1);
419 $result =
$subject->canWriteConfiguration();
421 $this->assertTrue($result);
422 $this->testFilesToDelete[] = $absoluteDirectory;
431 if (!is_file($configurationFile)) {
432 if (!$fh = fopen($configurationFile,
'wb')) {
433 $this->markTestSkipped(
'Can not create file ' . $configurationFile .
'. Please check your write permissions.');
438 if (!@is_file($configurationFile)) {
439 throw new \RuntimeException(
440 'File ' . $configurationFile .
' could not be found. Please check your write permissions',
444 $this->testFilesToDelete[] = $configurationFile;
447 ->expects($this->any())
448 ->method(
'getLocalConfigurationFileLocation')
449 ->will($this->returnValue($configurationFile));
458 ' \'bar\' => 23,' . LF .
459 ' \'foo\' => 42,' . LF .
462 $this->subject->writeLocalConfiguration($pairs);
463 $this->assertSame($expectedContent, file_get_contents($configurationFile));
471 $this->expectException(\RuntimeException::class);
474 $subject = $this->getAccessibleMock(ConfigurationManager::class, [
'dummy']);
476 $file =
'../typo3temp/var/tests/' . $this->getUniqueId(
'test_');
478 touch($absoluteFile);
479 $this->testFilesToDelete[] = $absoluteFile;
480 $subject->_set(
'localConfigurationFile', $file);
482 $subject->createLocalConfigurationFromFactoryConfiguration();
491 $subject = $this->getAccessibleMock(ConfigurationManager::class, [
'writeLocalConfiguration']);
492 $subject->_set(
'localConfigurationFile',
'../../typo3temp/var/tests/' . $this->getUniqueId(
'dummy_'));
494 $factoryConfigurationFile =
'../../typo3temp/var/tests/' . $this->getUniqueId(
'test_') .
'.php';
496 $uniqueContentString = $this->getUniqueId(
'string_');
497 $validFactoryConfigurationFileContent =
500 '\'' . $uniqueContentString .
'\' => \
'foo\',' . LF .
503 $factoryConfigurationAbsoluteFile,
504 $validFactoryConfigurationFileContent
506 $this->testFilesToDelete[] = $factoryConfigurationAbsoluteFile;
508 $subject->_set(
'factoryConfigurationFile', $factoryConfigurationFile);
511 ->expects($this->once())
512 ->method(
'writeLocalConfiguration')
513 ->with($this->arrayHasKey($uniqueContentString));
514 $subject->createLocalConfigurationFromFactoryConfiguration();
523 $subject = $this->getAccessibleMock(ConfigurationManager::class, [
'writeLocalConfiguration']);
524 $subject->_set(
'localConfigurationFile',
'../../typo3temp/var/tests/' . $this->getUniqueId(
'dummy_'));
526 $factoryConfigurationFile =
'../../typo3temp/var/tests/' . $this->getUniqueId(
'test_') .
'.php';
528 $validFactoryConfigurationFileContent =
532 $factoryConfigurationAbsoluteFile,
533 $validFactoryConfigurationFileContent
535 $this->testFilesToDelete[] = $factoryConfigurationAbsoluteFile;
536 $subject->_set(
'factoryConfigurationFile', $factoryConfigurationFile);
538 $additionalFactoryConfigurationFile =
'../typo3temp/var/tests/' . $this->getUniqueId(
'test_') .
'.php';
540 $uniqueContentString = $this->getUniqueId(
'string_');
541 $validAdditionalFactoryConfigurationFileContent =
544 '\'' . $uniqueContentString .
'\' => \
'foo\',' . LF .
547 $additionalFactoryConfigurationAbsoluteFile,
548 $validAdditionalFactoryConfigurationFileContent
550 $this->testFilesToDelete[] = $additionalFactoryConfigurationAbsoluteFile;
551 $subject->_set(
'additionalFactoryConfigurationFile', $additionalFactoryConfigurationFile);
554 ->expects($this->once())
555 ->method(
'writeLocalConfiguration')
556 ->with($this->arrayHasKey($uniqueContentString));
557 $subject->createLocalConfigurationFromFactoryConfiguration();
566 $subject = $this->getAccessibleMock(ConfigurationManager::class, [
'dummy']);
567 $subject->_set(
'whiteListedLocalConfigurationPaths', [
'foo/bar']);
568 $this->assertTrue(
$subject->_call(
'isValidLocalConfigurationPath',
'foo/bar/baz'));
577 $subject = $this->getAccessibleMock(ConfigurationManager::class, [
'dummy']);
578 $subject->_set(
'whiteListedLocalConfigurationPaths', [
'foo/bar']);
579 $this->assertFalse(
$subject->_call(
'isValidLocalConfigurationPath',
'bar/baz'));