TYPO3 CMS  TYPO3_8-7
ConfigurationItemRepositoryTest.php
Go to the documentation of this file.
1 <?php
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 
20 class ConfigurationItemRepositoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
26 
31 
35  protected function setUp()
36  {
37  // Mock system under test to make protected methods accessible
38  $this->configurationItemRepository = $this->getAccessibleMock(
39  \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository::class,
40  ['dummy']
41  );
42 
43  $this->injectedObjectManagerMock = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
44  $this->configurationItemRepository->_set(
45  'objectManager',
46  $this->injectedObjectManagerMock
47  );
48  }
49 
54  {
55  $flatConfigurationItemArray = [
56  'item1' => [
57  'cat' => 'basic',
58  'subcat_name' => 'enable',
59  'subcat' => 'a/enable/10z',
60  'type' => 'string',
61  'label' => 'Item 1: This is the first configuration item',
62  'name' =>'item1',
63  'value' => 'one',
64  'default_value' => 'one',
65  'subcat_label' => 'Enable features',
66  ],
67  'integerValue' => [
68  'cat' => 'basic',
69  'subcat_name' => 'enable',
70  'subcat' => 'a/enable/20z',
71  'type' => 'int+',
72  'label' => 'Integer Value: Please insert a positive integer value',
73  'name' =>'integerValue',
74  'value' => '1',
75  'default_value' => '1',
76  'subcat_label' => 'Enable features',
77  ],
78  'enableJquery' => [
79  'cat' => 'advanced',
80  'subcat_name' => 'file',
81  'subcat' => 'c/file/10z',
82  'type' => 'boolean',
83  'label' => 'enableJquery: Insert jQuery plugin',
84  'name' =>'enableJquery',
85  'value' => '1',
86  'default_value' => '1',
87  'subcat_label' => 'Files',
88  ],
89  ];
90 
91  $configurationItemRepository = $this->getAccessibleMock(
92  \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository::class,
93  ['mergeWithExistingConfiguration', 'translate']
94  );
96  'objectManager',
97  $this->injectedObjectManagerMock
98  );
99  $configurationUtilityMock = $this->createMock(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
100  $configurationUtilityMock
101  ->expects($this->once())
102  ->method('getDefaultConfigurationFromExtConfTemplateAsValuedArray')
103  ->will($this->returnValue($flatConfigurationItemArray));
104  $this->injectedObjectManagerMock
105  ->expects($this->any())
106  ->method('get')
107  ->will($this->returnValue($configurationUtilityMock));
109  ->expects($this->any())
110  ->method('mergeWithExistingConfiguration')
111  ->will($this->returnValue($flatConfigurationItemArray));
112 
113  $expectedArray = [
114  'basic' => [
115  'enable' => [
116  'item1' => [
117  'cat' => 'basic',
118  'subcat_name' => 'enable',
119  'subcat' => 'a/enable/10z',
120  'type' => 'string',
121  'label' => 'Item 1: This is the first configuration item',
122  'name' =>'item1',
123  'value' => 'one',
124  'default_value' => 'one',
125  'subcat_label' => 'Enable features',
126  'labels' => [
127  0 => 'Item 1',
128  1 => 'This is the first configuration item'
129  ]
130  ],
131  'integerValue' => [
132  'cat' => 'basic',
133  'subcat_name' => 'enable',
134  'subcat' => 'a/enable/20z',
135  'type' => 'int+',
136  'label' => 'Integer Value: Please insert a positive integer value',
137  'name' =>'integerValue',
138  'value' => '1',
139  'default_value' => '1',
140  'subcat_label' => 'Enable features',
141  'labels' => [
142  0 => 'Integer Value',
143  1 => 'Please insert a positive integer value'
144  ]
145  ]
146  ]
147  ],
148  'advanced' => [
149  'file' => [
150  'enableJquery' => [
151  'cat' => 'advanced',
152  'subcat_name' => 'file',
153  'subcat' => 'c/file/10z',
154  'type' => 'boolean',
155  'label' => 'enableJquery: Insert jQuery plugin',
156  'name' =>'enableJquery',
157  'value' => '1',
158  'default_value' => '1',
159  'subcat_label' => 'Files',
160  'labels' => [
161  0 => 'enableJquery',
162  1 => 'Insert jQuery plugin'
163  ]
164  ],
165  ]
166  ]
167  ];
168 
169  $this->assertSame(
170  $expectedArray,
171  $configurationItemRepository->_call('getConfigurationArrayFromExtensionKey', $this->getUniqueId('some_extension'))
172  );
173  }
174 
179  {
180  $configuration = [
181  '__meta__' => 'metaInformation',
182  'test123' => 'test123'
183  ];
184  $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
185  $this->assertEquals(['test123' => 'test123'], $configuration);
186  }
187 
192  {
193  $configuration = [
194  '__meta__' => 'metaInformation',
195  'test123' => 'test123'
196  ];
197  $meta = $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
198  $this->assertEquals('metaInformation', $meta);
199  }
200 
205  {
206  return [
207  [
208  [
209  'cat' => 'basic',
210  'subcat_name' => 'enable',
211  'subcat' => 'a/enable/z',
212  'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
213  'label' => 'Frontend configuration check',
214  'name' => 'checkConfigurationFE',
215  'value' => 0,
216  'default_value' => 0,
217  'comparisonGeneric' => 'TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend'
218  ]
219  ],
220  [
221  [
222  'cat' => 'basic',
223  'subcat_name' => 'enable',
224  'subcat' => 'a/enable/z',
225  'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend]',
226  'label' => 'Backend configuration check',
227  'name' => 'checkConfigurationBE',
228  'value' => 0,
229  'default_value' => 0,
230  'comparisonGeneric' => 'TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend'
231  ]
232  ],
233  [
234  [
235  'cat' => 'basic',
236  'subcat_name' => 'enable',
237  'subcat' => 'a/enable/z',
238  'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE]',
239  '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).',
240  'name' => 'FE.saltedPWHashingMethod',
241  'value' => \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class,
242  'default_value' => \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class,
243  'comparisonGeneric' => 'TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE'
244  ]
245  ]
246  ];
247  }
248 
255  {
256  $configurationOptionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeUser', $configurationOption);
257  $this->assertEquals('user', $configurationOptionModified['type']);
258  $this->assertEquals($configurationOption['comparisonGeneric'], $configurationOptionModified['generic']);
259  }
260 
265  {
266  $option = [
267  'cat' => 'basic',
268  'subcat_name' => 'enable',
269  'subcat' => 'a/enable/100z',
270  '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.)]',
271  'label' => 'Default configuration settings',
272  'name' => 'defaultConfiguration',
273  'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
274  'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
275  'genericComparisonValue' => [
276  '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.)',
277  '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.)',
278  'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)'
279  ],
280  'typeComparisonValue' => 'options'
281  ];
282  $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
283  $this->assertArrayHasKey('generic', $optionModified);
284  $this->assertArrayHasKey('type', $optionModified);
285  $this->assertArrayHasKey('label', $optionModified);
286  $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
287  $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
288  }
289 
294  {
295  $option = [
296  'cat' => 'basic',
297  'subcat_name' => 'enable',
298  'subcat' => 'a/enable/100z',
299  '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]',
300  'label' => 'Default configuration settings',
301  'name' => 'defaultConfiguration',
302  'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
303  'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
304  'genericComparisonValue' => [
305  'Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.)' => 'MINIMAL',
306  'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)' => 'TYPICAL',
307  'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'DEMO'
308  ],
309  'typeComparisonValue' => 'options'
310  ];
311  $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
312  $this->assertArrayHasKey('generic', $optionModified);
313  $this->assertArrayHasKey('type', $optionModified);
314  $this->assertArrayHasKey('label', $optionModified);
315  $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
316  $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
317  }
318 
323  {
324  $exception = $this->getMockBuilder('RuntimeException')->getMock();
325  $configurationManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->getMock();
326  $configurationManagerMock
327  ->expects($this->once())
328  ->method('getConfigurationValueByPath')
329  ->will($this->throwException($exception));
330  $this->injectedObjectManagerMock
331  ->expects($this->any())
332  ->method('get')
333  ->will($this->returnValue($configurationManagerMock));
334 
335  $this->configurationItemRepository->_call(
336  'mergeWithExistingConfiguration',
337  [],
338  $this->getUniqueId('not_existing_extension')
339  );
340  }
341 
346  {
347  $exception = $this->getMockBuilder('RuntimeException')->getMock();
348  $configurationManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->getMock();
349  $configurationManagerMock
350  ->expects($this->once())
351  ->method('getConfigurationValueByPath')
352  ->will($this->throwException($exception));
353  $this->injectedObjectManagerMock
354  ->expects($this->any())
355  ->method('get')
356  ->will($this->returnValue($configurationManagerMock));
357  $defaultConfiguration = [
358  'foo' => 'bar'
359  ];
360  $configuration = $this->configurationItemRepository->_call(
361  'mergeWithExistingConfiguration',
362  $defaultConfiguration,
363  $this->getUniqueId('not_existing_extension')
364  );
365  $this->assertEquals($defaultConfiguration, $configuration);
366  }
367 
372  {
373  $localConfiguration = serialize([
374  'FE.' => [
375  'enabled' => '1',
376  'saltedPWHashingMethod' => \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface_sha1::class
377  ],
378  'CLI.' => [
379  'enabled' => '0'
380  ]
381  ]);
382 
383  $configurationManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->getMock();
384  $configurationManagerMock
385  ->expects($this->once())
386  ->method('getConfigurationValueByPath')
387  ->with('EXT/extConf/testextensionkey')
388  ->will($this->returnValue($localConfiguration));
389 
390  $this->injectedObjectManagerMock
391  ->expects($this->any())
392  ->method('get')
393  ->will($this->returnValue($configurationManagerMock));
394 
395  $defaultConfiguration = [
396  'FE.enabled' => [
397  'value' => '0'
398  ],
399  'FE.saltedPWHashingMethod' => [
400  'value' => \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class
401  ],
402  'BE.enabled' => [
403  'value' => '1'
404  ],
405  'BE.saltedPWHashingMethod' => [
406  'value' => \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class
407  ]
408  ];
409 
410  $expectedResult = [
411  'FE.enabled' => [
412  'value' => '1'
413  ],
414  'FE.saltedPWHashingMethod' => [
415  'value' => \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface_sha1::class
416  ],
417  'BE.enabled' => [
418  'value' => '1'
419  ],
420  'BE.saltedPWHashingMethod' => [
421  'value' => \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class
422  ],
423  'CLI.enabled' => [
424  'value' => '0'
425  ]
426  ];
427 
428  $actualResult = $this->configurationItemRepository->_call(
429  'mergeWithExistingConfiguration',
430  $defaultConfiguration,
431  'testextensionkey'
432  );
433 
434  $this->assertEquals($expectedResult, $actualResult);
435  }
436 }