[FEATURE] Store extension configuration as plain array
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Tests / Unit / Domain / Repository / ConfigurationItemRepositoryTest.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Domain\Repository;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Object\ObjectManager;
18
19 /**
20 * Tests for ConfigurationItemRepository
21 */
22 class ConfigurationItemRepositoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject
26 */
27 protected $configurationItemRepository;
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
31 */
32 protected $injectedObjectManagerMock;
33
34 /**
35 * Set up
36 */
37 protected function setUp()
38 {
39 // Mock system under test to make protected methods accessible
40 $this->configurationItemRepository = $this->getAccessibleMock(
41 \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository::class,
42 ['dummy']
43 );
44
45 $this->injectedObjectManagerMock = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
46 $this->configurationItemRepository->_set(
47 'objectManager',
48 $this->injectedObjectManagerMock
49 );
50 }
51
52 /**
53 * @test
54 */
55 public function getConfigurationArrayFromExtensionKeyReturnsSortedHierarchicArray()
56 {
57 $flatConfigurationItemArray = [
58 'item1' => [
59 'cat' => 'basic',
60 'subcat_name' => 'enable',
61 'subcat' => 'a/enable/10z',
62 'type' => 'string',
63 'label' => 'Item 1: This is the first configuration item',
64 'name' =>'item1',
65 'value' => 'one',
66 'default_value' => 'one',
67 'subcat_label' => 'Enable features',
68 ],
69 'integerValue' => [
70 'cat' => 'basic',
71 'subcat_name' => 'enable',
72 'subcat' => 'a/enable/20z',
73 'type' => 'int+',
74 'label' => 'Integer Value: Please insert a positive integer value',
75 'name' =>'integerValue',
76 'value' => '1',
77 'default_value' => '1',
78 'subcat_label' => 'Enable features',
79 ],
80 'enableJquery' => [
81 'cat' => 'advanced',
82 'subcat_name' => 'file',
83 'subcat' => 'c/file/10z',
84 'type' => 'boolean',
85 'label' => 'enableJquery: Insert jQuery plugin',
86 'name' =>'enableJquery',
87 'value' => '1',
88 'default_value' => '1',
89 'subcat_label' => 'Files',
90 ],
91 ];
92
93 $configurationItemRepository = $this->getAccessibleMock(
94 \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository::class,
95 ['translate']
96 );
97 $configurationItemRepository->_set(
98 'objectManager',
99 $this->injectedObjectManagerMock
100 );
101 $configurationUtilityMock = $this->createMock(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
102 $configurationUtilityMock
103 ->expects($this->once())
104 ->method('getCurrentConfiguration')
105 ->will($this->returnValue($flatConfigurationItemArray));
106
107 $this->injectedObjectManagerMock
108 ->expects($this->any())
109 ->method('get')
110 ->will($this->returnValue($configurationUtilityMock));
111
112 $expectedArray = [
113 'basic' => [
114 'enable' => [
115 'item1' => [
116 'cat' => 'basic',
117 'subcat_name' => 'enable',
118 'subcat' => 'a/enable/10z',
119 'type' => 'string',
120 'label' => 'Item 1: This is the first configuration item',
121 'name' =>'item1',
122 'value' => 'one',
123 'default_value' => 'one',
124 'subcat_label' => 'Enable features',
125 'labels' => [
126 0 => 'Item 1',
127 1 => 'This is the first configuration item'
128 ]
129 ],
130 'integerValue' => [
131 'cat' => 'basic',
132 'subcat_name' => 'enable',
133 'subcat' => 'a/enable/20z',
134 'type' => 'int+',
135 'label' => 'Integer Value: Please insert a positive integer value',
136 'name' =>'integerValue',
137 'value' => '1',
138 'default_value' => '1',
139 'subcat_label' => 'Enable features',
140 'labels' => [
141 0 => 'Integer Value',
142 1 => 'Please insert a positive integer value'
143 ]
144 ]
145 ]
146 ],
147 'advanced' => [
148 'file' => [
149 'enableJquery' => [
150 'cat' => 'advanced',
151 'subcat_name' => 'file',
152 'subcat' => 'c/file/10z',
153 'type' => 'boolean',
154 'label' => 'enableJquery: Insert jQuery plugin',
155 'name' =>'enableJquery',
156 'value' => '1',
157 'default_value' => '1',
158 'subcat_label' => 'Files',
159 'labels' => [
160 0 => 'enableJquery',
161 1 => 'Insert jQuery plugin'
162 ]
163 ],
164 ]
165 ]
166 ];
167
168 $this->assertSame(
169 $expectedArray,
170 $configurationItemRepository->_call('getConfigurationArrayFromExtensionKey', $this->getUniqueId('some_extension'))
171 );
172 }
173
174 /**
175 * @test
176 */
177 public function addMetaInformationUnsetsOriginalConfigurationMetaKey()
178 {
179 $configuration = [
180 '__meta__' => 'metaInformation',
181 'test123' => 'test123'
182 ];
183 $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
184 $this->assertEquals(['test123' => 'test123'], $configuration);
185 }
186
187 /**
188 * @test
189 */
190 public function addMetaInformationReturnsMetaInformation()
191 {
192 $configuration = [
193 '__meta__' => 'metaInformation',
194 'test123' => 'test123'
195 ];
196 $meta = $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
197 $this->assertEquals('metaInformation', $meta);
198 }
199
200 /**
201 * @return array
202 */
203 public function extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider()
204 {
205 return [
206 [
207 [
208 'cat' => 'basic',
209 'subcat_name' => 'enable',
210 'subcat' => 'a/enable/z',
211 'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
212 'label' => 'Frontend configuration check',
213 'name' => 'checkConfigurationFE',
214 'value' => 0,
215 'default_value' => 0,
216 'comparisonGeneric' => 'TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend'
217 ]
218 ],
219 [
220 [
221 'cat' => 'basic',
222 'subcat_name' => 'enable',
223 'subcat' => 'a/enable/z',
224 'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend]',
225 'label' => 'Backend configuration check',
226 'name' => 'checkConfigurationBE',
227 'value' => 0,
228 'default_value' => 0,
229 'comparisonGeneric' => 'TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend'
230 ]
231 ],
232 [
233 [
234 'cat' => 'basic',
235 'subcat_name' => 'enable',
236 'subcat' => 'a/enable/z',
237 'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE]',
238 'label' => 'Hashing method for the frontend: Defines salted hashing method to use. Choose "Portable PHP password hashing" to stay compatible with other CMS (e.g. Drupal, Wordpress). Choose "MD5 salted hashing" to reuse TYPO3 passwords for OS level authentication (other servers could use TYPO3 passwords). Choose "Blowfish salted hashing" for advanced security to reuse passwords on OS level (Blowfish might not be supported on your system TODO).',
239 'name' => 'FE.saltedPWHashingMethod',
240 'value' => \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class,
241 'default_value' => \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class,
242 'comparisonGeneric' => 'TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE'
243 ]
244 ]
245 ];
246 }
247
248 /**
249 * @test
250 * @dataProvider extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider
251 * @param $configurationOption
252 */
253 public function extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformation($configurationOption)
254 {
255 $configurationOptionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeUser', $configurationOption);
256 $this->assertEquals('user', $configurationOptionModified['type']);
257 $this->assertEquals($configurationOption['comparisonGeneric'], $configurationOptionModified['generic']);
258 }
259
260 /**
261 * @test
262 */
263 public function extractInformationForConfigFieldsOfTypeOptionsAddsGenericTypeAndLabelInformation()
264 {
265 $option = [
266 'cat' => 'basic',
267 'subcat_name' => 'enable',
268 'subcat' => 'a/enable/100z',
269 'type' => 'options[Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.),Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.),Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)]',
270 'label' => 'Default configuration settings',
271 'name' => 'defaultConfiguration',
272 'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
273 'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
274 'genericComparisonValue' => [
275 'Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.)' => 'Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.)',
276 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
277 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)'
278 ],
279 'typeComparisonValue' => 'options'
280 ];
281 $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
282 $this->assertArrayHasKey('generic', $optionModified);
283 $this->assertArrayHasKey('type', $optionModified);
284 $this->assertArrayHasKey('label', $optionModified);
285 $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
286 $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
287 }
288
289 /**
290 * @test
291 */
292 public function extractInformationForConfigFieldsOfTypeOptionsWithLabelsAndValuesAddsGenericTypeAndLabelInformation()
293 {
294 $option = [
295 'cat' => 'basic',
296 'subcat_name' => 'enable',
297 'subcat' => 'a/enable/100z',
298 'type' => 'options[Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.)=MINIMAL,Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.) = TYPICAL,Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)=DEMO]',
299 'label' => 'Default configuration settings',
300 'name' => 'defaultConfiguration',
301 'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
302 'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
303 'genericComparisonValue' => [
304 'Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.)' => 'MINIMAL',
305 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)' => 'TYPICAL',
306 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'DEMO'
307 ],
308 'typeComparisonValue' => 'options'
309 ];
310 $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
311 $this->assertArrayHasKey('generic', $optionModified);
312 $this->assertArrayHasKey('type', $optionModified);
313 $this->assertArrayHasKey('label', $optionModified);
314 $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
315 $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
316 }
317 }