‪TYPO3CMS  ‪main
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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use PHPUnit\Framework\MockObject\MockObject;
23 use Psr\Http\Message\ServerRequestInterface;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪ConfigurationServiceTest extends UnitTestCase
31 {
33 
34  public function ‪setUp(): void
35  {
36  parent::setUp();
37  $this->beUser = $this->getMockBuilder(BackendUserAuthentication::class)->disableOriginalConstructor()->getMock();
38  ‪$GLOBALS['BE_USER'] = ‪$this->beUser;
39  }
40 
41  #[Test]
43  {
44  $userTsAdmPanelConfig = [
45  'enable.' => [
46  'all' => '1',
47  ],
48  ];
49  $this->‪setUpUserTsConfigForAdmPanel($userTsAdmPanelConfig);
50 
51  $configurationService = new ‪ConfigurationService();
52  $result = $configurationService->getMainConfiguration();
53 
54  self::assertSame($userTsAdmPanelConfig, $result);
55  }
56 
57  #[Test]
59  {
60  $configurationService = new ‪ConfigurationService();
61  $result = $configurationService->getConfigurationOption('foo', 'bar');
62  self::assertSame('', $result);
63  }
64 
65  #[Test]
67  {
69  [
70  'override.' => [
71  'preview.' => [
72  'showHiddenPages' => '1',
73  ],
74  ],
75  ]
76  );
77 
78  $configurationService = new ‪ConfigurationService();
79  $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
80 
81  self::assertSame('1', $result);
82  }
83 
84  #[Test]
86  {
88  [
89  'override.' => [
90  'preview.' => [
91  'showHiddenPages' => 1,
92  ],
93  ],
94  ]
95  );
96 
97  $configurationService = new ‪ConfigurationService();
98  $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
99 
100  self::assertSame('1', $result);
101  }
102 
104  {
105  return [
106  'empty identifier' => [
107  '',
108  'foo',
109  ],
110  'empty option' => [
111  'foo',
112  '',
113  ],
114  'both empty' => [
115  '',
116  '',
117  ],
118  ];
119  }
120 
121  #[DataProvider('getConfigurationOptionEmptyArgumentDataProvider')]
122  #[Test]
123  public function ‪getConfigurationOptionThrowsExceptionOnEmptyArgument(string ‪$identifier, string $option): void
124  {
125  $this->expectException(\InvalidArgumentException::class);
126  $this->expectExceptionCode(1532861423);
127 
128  $configurationService = new ‪ConfigurationService();
129  $configurationService->getConfigurationOption(‪$identifier, $option);
130  }
131 
132  #[Test]
134  {
136  $this->beUser->uc = [
137  'AdminPanel' => [
138  'preview_showHiddenPages' => '1',
139  ],
140  ];
141 
142  $configurationService = new ‪ConfigurationService();
143  $result = $configurationService->getConfigurationOption('preview', 'showHiddenPages');
144 
145  self::assertSame('1', $result);
146  }
147 
148  #[Test]
150  {
151  $requestMock = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
152 
153  $subModuleFixture = $this->getMockBuilder(SubModuleFixture::class)->getMock();
154  $subModuleFixture->expects(self::atLeastOnce())->method('onSubmit')->with([], $requestMock);
155  $mainModuleFixture = $this->getMockBuilder(MainModuleFixture::class)->getMock();
156  $mainModuleFixture->method('isEnabled')->willReturn(true);
157  $mainModuleFixture->expects(self::atLeastOnce())->method('onSubmit')->with([], $requestMock);
158  $mainModuleFixture->method('getSubModules')->willReturn(
159  [$subModuleFixture]
160  );
161  $modules = [
162  $mainModuleFixture,
163  ];
164 
165  $configurationService = new ‪ConfigurationService();
166  $configurationService->saveConfiguration($modules, $requestMock);
167  }
168 
169  #[Test]
171  {
172  // existing configuration from UC
173  $this->beUser->uc = [
174  'AdminPanel' => [
175  'foo' => 'bar',
176  ],
177  ];
178 
179  $this->beUser->expects(self::atLeastOnce())->method('writeUC');
180 
181  // new configuration to save
182  $requestMock = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
183  $requestMock->method('getParsedBody')->willReturn(
184  [
185  'TSFE_ADMIN_PANEL' => [
186  'baz' => 'bam',
187  ],
188  ]
189  );
190 
191  $configurationService = new ‪ConfigurationService();
192  $configurationService->saveConfiguration([], $requestMock);
193 
194  $expected = [
195  'AdminPanel' => [
196  'foo' => 'bar',
197  'baz' => 'bam',
198  ],
199  ];
200  self::assertSame($expected, $this->beUser->uc);
201  }
202 
203  private function ‪setUpUserTsConfigForAdmPanel(array $userTsAdmPanelConfig): void
204  {
205  $this->beUser->method('getTSConfig')->willReturn(
206  ['admPanel.' => $userTsAdmPanelConfig]
207  );
208  }
209 }
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getMainConfigurationReturnsTsConfigFromUser
‪getMainConfigurationReturnsTsConfigFromUser()
Definition: ConfigurationServiceTest.php:42
‪TYPO3\CMS\Adminpanel\Tests\Unit\Fixtures\MainModuleFixture
Definition: MainModuleFixture.php:40
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionEmptyArgumentDataProvider
‪static getConfigurationOptionEmptyArgumentDataProvider()
Definition: ConfigurationServiceTest.php:103
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionReturnsEmptyStringIfNoConfigurationFound
‪getConfigurationOptionReturnsEmptyStringIfNoConfigurationFound()
Definition: ConfigurationServiceTest.php:58
‪TYPO3\CMS\Adminpanel\Service\ConfigurationService
Definition: ConfigurationService.php:34
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionReturnsSettingFromUcIfNoOverrideGiven
‪getConfigurationOptionReturnsSettingFromUcIfNoOverrideGiven()
Definition: ConfigurationServiceTest.php:133
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\$beUser
‪MockObject &BackendUserAuthentication $beUser
Definition: ConfigurationServiceTest.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\setUp
‪setUp()
Definition: ConfigurationServiceTest.php:34
‪$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:203
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service
Definition: ConfigurationServiceTest.php:18
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionReturnsOverrideOptionIfSet
‪getConfigurationOptionReturnsOverrideOptionIfSet()
Definition: ConfigurationServiceTest.php:66
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\saveConfigurationSavesMergedExistingAndNewConfiguration
‪saveConfigurationSavesMergedExistingAndNewConfiguration()
Definition: ConfigurationServiceTest.php:170
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionThrowsExceptionOnEmptyArgument
‪getConfigurationOptionThrowsExceptionOnEmptyArgument(string $identifier, string $option)
Definition: ConfigurationServiceTest.php:123
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\saveConfigurationTriggersOnSubmitOnEnabledModules
‪saveConfigurationTriggersOnSubmitOnEnabledModules()
Definition: ConfigurationServiceTest.php:149
‪TYPO3\CMS\Adminpanel\Tests\Unit\Fixtures\SubModuleFixture
Definition: SubModuleFixture.php:29
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest\getConfigurationOptionCastsResultToString
‪getConfigurationOptionCastsResultToString()
Definition: ConfigurationServiceTest.php:85
‪TYPO3\CMS\Adminpanel\Tests\Unit\Service\ConfigurationServiceTest
Definition: ConfigurationServiceTest.php:31