[BUGFIX] Prevent repopulating TYPO3_CONF_VARS
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Configuration / ExtensionConfigurationTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Configuration;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
20 use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class ExtensionConfigurationTest extends UnitTestCase
28 {
29 /**
30 * @test
31 */
32 public function getWithEmptyPathReturnsEntireExtensionConfiguration()
33 {
34 $extConf = [
35 'aFeature' => 'iAmEnabled',
36 'aFlagCategory' => [
37 'someFlag' => 'foo',
38 ],
39 ];
40 $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['someExtension'] = $extConf;
41 $this->assertSame((new ExtensionConfiguration())->get('someExtension'), $extConf);
42 }
43
44 /**
45 * @test
46 */
47 public function getWithPathReturnsGivenValue()
48 {
49 $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['someExtension'] = [
50 'aFeature' => 'iAmEnabled',
51 'aFlagCategory' => [
52 'someFlag' => 'foo',
53 ],
54 ];
55 $this->assertSame((new ExtensionConfiguration())->get('someExtension', 'aFeature'), 'iAmEnabled');
56 }
57
58 /**
59 * @test
60 */
61 public function getWithPathReturnsGivenPathSegment()
62 {
63 $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['someExtension'] = [
64 'aFeature' => 'iAmEnabled',
65 'aFlagCategory' => [
66 'someFlag' => 'foo',
67 ],
68 ];
69 $this->assertSame((new ExtensionConfiguration())->get('someExtension', 'aFlagCategory'), ['someFlag' => 'foo']);
70 }
71
72 /**
73 * @test
74 */
75 public function setThrowsExceptionWithEmptyExtension()
76 {
77 $this->expectException(\RuntimeException::class);
78 $this->expectExceptionCode(1509715852);
79 (new ExtensionConfiguration())->set('');
80 }
81
82 /**
83 * @test
84 */
85 public function setRemovesFullExtensionConfiguration()
86 {
87 $configurationManagerProphecy = $this->prophesize(ConfigurationManager::class);
88 GeneralUtility::addInstance(ConfigurationManager::class, $configurationManagerProphecy->reveal());
89 $configurationManagerProphecy->removeLocalConfigurationKeysByPath(['EXTENSIONS/foo'])->shouldBeCalled();
90 (new ExtensionConfiguration())->set('foo');
91 }
92
93 /**
94 * @test
95 */
96 public function setWritesFullExtensionConfig()
97 {
98 $configurationManagerProphecy = $this->prophesize(ConfigurationManager::class);
99 GeneralUtility::addInstance(ConfigurationManager::class, $configurationManagerProphecy->reveal());
100 $configurationManagerProphecy->setLocalConfigurationValueByPath(Argument::cetera())->shouldBeCalled();
101 $configurationManagerProphecy->setLocalConfigurationValueByPath('EXTENSIONS/foo', ['bar' => 'baz'])->shouldBeCalled();
102 (new ExtensionConfiguration())->set('foo', '', ['bar' => 'baz']);
103 }
104
105 /**
106 * @test
107 */
108 public function setUpdatesLegacyExtConfToNewValues()
109 {
110 $configurationManagerProphecy = $this->prophesize(ConfigurationManager::class);
111 GeneralUtility::addInstance(ConfigurationManager::class, $configurationManagerProphecy->reveal());
112 $configurationManagerProphecy->setLocalConfigurationValueByPath(Argument::cetera())->shouldBeCalled();
113 $configurationManagerProphecy->setLocalConfigurationValueByPath('EXT/extConf', ['foo' => serialize(['bar' => 'baz'])])->shouldBeCalled();
114 (new ExtensionConfiguration())->set('foo', '', ['bar' => 'baz']);
115 }
116
117 /**
118 * @test
119 */
120 public function setUpdatesLegacyExtConfWithDottedArrayKeysForNestedConfiguration()
121 {
122 $configurationManagerProphecy = $this->prophesize(ConfigurationManager::class);
123 GeneralUtility::addInstance(ConfigurationManager::class, $configurationManagerProphecy->reveal());
124 $nestedInput = [
125 'FE' => [
126 'forceSalted' => true,
127 ]
128 ];
129 $expectedLegacyExtConf = [
130 'FE.' => [
131 'forceSalted' => true,
132 ]
133 ];
134 $configurationManagerProphecy->setLocalConfigurationValueByPath(Argument::cetera())->shouldBeCalled();
135 $configurationManagerProphecy->setLocalConfigurationValueByPath('EXT/extConf', ['saltedPasswords' => serialize($expectedLegacyExtConf)])->shouldBeCalled();
136 (new ExtensionConfiguration())->set('saltedPasswords', '', $nestedInput);
137 }
138
139 /**
140 * @test
141 */
142 public function setUpdatesLegacyExtConfWithDottedArrayKeysForNestedConfigurationWithMultiNestedArrays()
143 {
144 $configurationManagerProphecy = $this->prophesize(ConfigurationManager::class);
145 GeneralUtility::addInstance(ConfigurationManager::class, $configurationManagerProphecy->reveal());
146 $nestedInput = [
147 'aCategory' => [
148 'aSubCategory' => [
149 'aKey' => 'aValue',
150 ],
151 ],
152 ];
153 $expectedLegacyExtConf = [
154 'aCategory.' => [
155 'aSubCategory.' => [
156 'aKey' => 'aValue',
157 ],
158 ],
159 ];
160 $configurationManagerProphecy->setLocalConfigurationValueByPath(Argument::cetera())->shouldBeCalled();
161 $configurationManagerProphecy->setLocalConfigurationValueByPath('EXT/extConf', ['someExtension' => serialize($expectedLegacyExtConf)])->shouldBeCalled();
162 (new ExtensionConfiguration())->set('someExtension', '', $nestedInput);
163 }
164 }