‪TYPO3CMS  11.5
ConfigurationServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Prophecy\Argument;
21 use Prophecy\PhpUnit\ProphecyTrait;
22 use Prophecy\Prophecy\ObjectProphecy;
23 use Psr\Http\Message\ServerRequestInterface;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 class ‪ConfigurationServiceTest extends UnitTestCase
31 {
32  use ProphecyTrait;
33 
35  protected ObjectProphecy ‪$beUserProphecy;
36 
38 
39  public function ‪setUp(): void
40  {
41  parent::setUp();
42  $this->beUserProphecy = $this->prophesize(BackendUserAuthentication::class);
43  $this->beUser = $this->beUserProphecy->reveal();
44  ‪$GLOBALS['BE_USER'] = ‪$this->beUser;
45  }
46 
51  {
52  $userTsAdmPanelConfig = [
53  'enable.' => [
54  'all' => '1',
55  ],
56  ];
57  $this->‪setUpUserTsConfigForAdmPanel($userTsAdmPanelConfig);
58 
59  $configurationService = new ‪ConfigurationService();
60  $result = $configurationService->getMainConfiguration();
61 
62  self::assertSame($userTsAdmPanelConfig, $result);
63  }
64 
69  {
70  $configurationService = new ConfigurationService();
71  $result = $configurationService->getConfigurationOption('foo', 'bar');
72  self::assertSame('', $result);
73  }
74 
79  {
81  [
82  'override.' => [
83  'preview.' => [
84  'showHiddenPages' => '1',
85  ],
86  ],
87  ]
88  );
89 
90  $configurationService = new ConfigurationService();
91  $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
92 
93  self::assertSame('1', $result);
94  }
95 
99  public function ‪getConfigurationOptionCastsResultToString(): void
100  {
102  [
103  'override.' => [
104  'preview.' => [
105  'showHiddenPages' => 1,
106  ],
107  ],
108  ]
109  );
110 
111  $configurationService = new ConfigurationService();
112  $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
113 
114  self::assertSame('1', $result);
115  }
116 
118  {
119  return [
120  'empty identifier' => [
121  '',
122  'foo',
123  ],
124  'empty option' => [
125  'foo',
126  '',
127  ],
128  'both empty' => [
129  '',
130  '',
131  ],
132  ];
133  }
134 
141  public function ‪getConfigurationOptionThrowsExceptionOnEmptyArgument(string $identifier, string $option): void
142  {
143  $this->expectException(\InvalidArgumentException::class);
144  $this->expectExceptionCode(1532861423);
145 
146  $configurationService = new ‪ConfigurationService();
147  $configurationService->getConfigurationOption($identifier, $option);
148  }
149 
154  {
156  $this->beUser->uc = [
157  'AdminPanel' => [
158  'preview_showHiddenPages' => '1',
159  ],
160  ];
161 
162  $configurationService = new ConfigurationService();
163  $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
164 
165  self::assertSame('1', $result);
166  }
167 
172  {
173  $subModuleFixture = $this->prophesize(SubModuleFixture::class);
174  $mainModuleFixture = $this->prophesize(MainModuleFixture::class);
175  $mainModuleFixture->isEnabled()->willReturn(true);
176  $mainModuleFixture->onSubmit(Argument::cetera())->shouldBeCalled()->hasReturnVoid();
177  $mainModuleFixture->getSubModules()->willReturn(
178  [$subModuleFixture->reveal()]
179  );
180  $modules = [
181  $mainModuleFixture->reveal(),
182  ];
183 
184  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
185 
186  $configurationService = new ConfigurationService();
187  $configurationService->saveConfiguration($modules, $requestProphecy->reveal());
188 
189  $mainModuleFixture->onSubmit([], $requestProphecy->reveal())->shouldHaveBeenCalled();
190  $subModuleFixture->onSubmit([], $requestProphecy->reveal())->shouldHaveBeenCalled();
191  }
192 
197  {
198  // existing configuration from UC
199  $this->beUser->uc = [
200  'AdminPanel' => [
201  'foo' => 'bar',
202  ],
203  ];
204 
205  // new configuration to save
206  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
207  $requestProphecy->getParsedBody()->willReturn(
208  [
209  'TSFE_ADMIN_PANEL' => [
210  'baz' => 'bam',
211  ],
212  ]
213  );
214 
215  $configurationService = new ConfigurationService();
216  $configurationService->saveConfiguration([], $requestProphecy->reveal());
217 
218  $expected = [
219  'AdminPanel' => [
220  'foo' => 'bar',
221  'baz' => 'bam',
222  ],
223  ];
224  self::assertSame($expected, $this->beUser->uc);
225  $this->beUserProphecy->writeUC()->shouldHaveBeenCalled();
226  }
227 
231  private function ‪setUpUserTsConfigForAdmPanel(array $userTsAdmPanelConfig): void
232  {
233  $this->beUserProphecy->getTSConfig()->willReturn(
234  ['admPanel.' => $userTsAdmPanelConfig]
235  );
236  }
237 }
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getMainConfigurationReturnsTsConfigFromUser
‪getMainConfigurationReturnsTsConfigFromUser()
Definition: ConfigurationServiceTest.php:49
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\$beUserProphecy
‪ObjectProphecy $beUserProphecy
Definition: ConfigurationServiceTest.php:34
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\$beUser
‪BackendUserAuthentication $beUser
Definition: ConfigurationServiceTest.php:36
‪TYPO3\CMS\Adminpanel\Tests\Unit\Fixtures\MainModuleFixture
Definition: MainModuleFixture.php:39
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionReturnsEmptyStringIfNoConfigurationFound
‪getConfigurationOptionReturnsEmptyStringIfNoConfigurationFound()
Definition: ConfigurationServiceTest.php:67
‪TYPO3\CMS\Adminpanel\Service\ConfigurationService
Definition: ConfigurationService.php:34
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionReturnsSettingFromUcIfNoOverrideGiven
‪getConfigurationOptionReturnsSettingFromUcIfNoOverrideGiven()
Definition: ConfigurationServiceTest.php:152
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\setUp
‪setUp()
Definition: ConfigurationServiceTest.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\setUpUserTsConfigForAdmPanel
‪setUpUserTsConfigForAdmPanel(array $userTsAdmPanelConfig)
Definition: ConfigurationServiceTest.php:230
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service
Definition: ConfigurationServiceTest.php:18
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionReturnsOverrideOptionIfSet
‪getConfigurationOptionReturnsOverrideOptionIfSet()
Definition: ConfigurationServiceTest.php:77
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\saveConfigurationSavesMergedExistingAndNewConfiguration
‪saveConfigurationSavesMergedExistingAndNewConfiguration()
Definition: ConfigurationServiceTest.php:195
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionThrowsExceptionOnEmptyArgument
‪getConfigurationOptionThrowsExceptionOnEmptyArgument(string $identifier, string $option)
Definition: ConfigurationServiceTest.php:140
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionEmptyArgumentDataProvider
‪getConfigurationOptionEmptyArgumentDataProvider()
Definition: ConfigurationServiceTest.php:116
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\saveConfigurationTriggersOnSubmitOnEnabledModules
‪saveConfigurationTriggersOnSubmitOnEnabledModules()
Definition: ConfigurationServiceTest.php:170
‪TYPO3\CMS\Adminpanel\Tests\Unit\Fixtures\SubModuleFixture
Definition: SubModuleFixture.php:29
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionCastsResultToString
‪getConfigurationOptionCastsResultToString()
Definition: ConfigurationServiceTest.php:98
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest
Definition: ConfigurationServiceTest.php:31