[TASK] Stabilize / Solidify public API of adminPanel
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Tests / Unit / Service / ConfigurationServiceTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Tests\Unit\Service;
5
6 use Prophecy\Argument;
7 use Psr\Http\Message\ServerRequestInterface;
8 use TYPO3\CMS\Adminpanel\Service\ConfigurationService;
9 use TYPO3\CMS\Adminpanel\Tests\Unit\Fixtures\MainModuleFixture;
10 use TYPO3\CMS\Adminpanel\Tests\Unit\Fixtures\SubModuleFixture;
11 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
12 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
13
14 class ConfigurationServiceTest extends UnitTestCase
15 {
16 /**
17 * @var BackendUserAuthentication|\Prophecy\Prophecy\ObjectProphecy
18 */
19 protected $beUserProphecy;
20
21 public function setUp()
22 {
23 parent::setUp();
24 $this->beUserProphecy = $this->prophesize(BackendUserAuthentication::class);
25 $GLOBALS['BE_USER'] = $this->beUserProphecy->reveal();
26 }
27
28 /**
29 * @test
30 */
31 public function getMainConfigurationReturnsTsConfigFromUser(): void
32 {
33 $userTsAdmPanelConfig = [
34 'enable.' => [
35 'all' => '1',
36 ],
37 ];
38 $this->setUpUserTsConfigForAdmPanel($userTsAdmPanelConfig);
39
40 $configurationService = new ConfigurationService();
41 $result = $configurationService->getMainConfiguration();
42
43 self::assertSame($userTsAdmPanelConfig, $result);
44 }
45
46 /**
47 * @test
48 */
49 public function getConfigurationOptionReturnsEmptyStringIfNoConfigurationFound(): void
50 {
51 $configurationService = new ConfigurationService();
52 $result = $configurationService->getConfigurationOption('foo', 'bar');
53 self::assertSame('', $result);
54 }
55
56 /**
57 * @test
58 */
59 public function getConfigurationOptionReturnsOverrideOptionIfSet(): void
60 {
61 $this->setUpUserTsConfigForAdmPanel(
62 [
63 'override.' => [
64 'preview.' => [
65 'showHiddenPages' => '1',
66 ],
67 ],
68 ]
69 );
70
71 $configurationService = new ConfigurationService();
72 $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
73
74 self::assertSame('1', $result);
75 }
76
77 /**
78 * @test
79 */
80 public function getConfigurationOptionCastsResultToString(): void
81 {
82 $this->setUpUserTsConfigForAdmPanel(
83 [
84 'override.' => [
85 'preview.' => [
86 'showHiddenPages' => 1,
87 ],
88 ],
89 ]
90 );
91
92 $configurationService = new ConfigurationService();
93 $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
94
95 self::assertSame('1', $result);
96 }
97
98 public function getConfigurationOptionEmptyArgumentDataProvider(): array
99 {
100 return [
101 'empty identifier' => [
102 '',
103 'foo',
104 ],
105 'empty option' => [
106 'foo',
107 '',
108 ],
109 'both empty' => [
110 '',
111 '',
112 ],
113 ];
114 }
115
116 /**
117 * @test
118 * @dataProvider getConfigurationOptionEmptyArgumentDataProvider
119 * @param $identifier
120 * @param $option
121 */
122 public function getConfigurationOptionThrowsExceptionOnEmptyArgument($identifier, $option): void
123 {
124 $this->expectException(\InvalidArgumentException::class);
125 $this->expectExceptionCode(1532861423);
126
127 $configurationService = new ConfigurationService();
128 $configurationService->getConfigurationOption($identifier, $option);
129 }
130
131 /**
132 * @test
133 */
134 public function getConfigurationOptionReturnsSettingFromUcIfNoOverrideGiven(): void
135 {
136 $this->setUpUserTsConfigForAdmPanel([]);
137 $this->beUserProphecy->uc = [
138 'TSFE_adminConfig' => [
139 'preview_showHiddenPages' => '1',
140 ],
141 ];
142
143 $configurationService = new ConfigurationService();
144 $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
145
146 self::assertSame('1', $result);
147 }
148
149 /**
150 * @test
151 */
152 public function saveConfigurationTriggersOnSubmitOnEnabledModules(): void
153 {
154 $subModuleFixture = $this->prophesize(SubModuleFixture::class);
155 $mainModuleFixture = $this->prophesize(MainModuleFixture::class);
156 $mainModuleFixture->isEnabled()->willReturn(true);
157 $mainModuleFixture->onSubmit(Argument::cetera())->shouldBeCalled()->hasReturnVoid();
158 $mainModuleFixture->getSubModules()->willReturn(
159 [$subModuleFixture->reveal()]
160 );
161 $modules = [
162 $mainModuleFixture->reveal(),
163 ];
164
165 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
166
167 $configurationService = new ConfigurationService();
168 $configurationService->saveConfiguration($modules, $requestProphecy->reveal());
169
170 $mainModuleFixture->onSubmit([], $requestProphecy->reveal())->shouldHaveBeenCalled();
171 $subModuleFixture->onSubmit([], $requestProphecy->reveal())->shouldHaveBeenCalled();
172 }
173
174 /**
175 * @test
176 */
177 public function saveConfigurationSavesMergedExistingAndNewConfiguration(): void
178 {
179 // existing configuration from UC
180 $this->beUserProphecy->uc = [
181 'TSFE_adminConfig' => [
182 'foo' => 'bar',
183 ],
184 ];
185
186 // new configuration to save
187 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
188 $requestProphecy->getParsedBody()->willReturn(
189 [
190 'TSFE_ADMIN_PANEL' => [
191 'baz' => 'bam',
192 ],
193 ]
194 );
195
196 $configurationService = new ConfigurationService();
197 $configurationService->saveConfiguration([], $requestProphecy->reveal());
198
199 $expected = [
200 'TSFE_adminConfig' => [
201 'foo' => 'bar',
202 'baz' => 'bam',
203 ],
204 ];
205 self::assertSame($expected, $this->beUserProphecy->uc);
206 $this->beUserProphecy->writeUC()->shouldHaveBeenCalled();
207 }
208
209 /**
210 * @param $userTsAdmPanelConfig
211 */
212 private function setUpUserTsConfigForAdmPanel($userTsAdmPanelConfig): void
213 {
214 $this->beUserProphecy->getTSConfig('admPanel')->willReturn(
215 ['properties' => $userTsAdmPanelConfig]
216 );
217 }
218 }