[BUGFIX] Extension Manager doesn't care about configuration sorting
[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 * Copyright notice
6 *
7 * (c) 2012 Susanne Moog, <susanne.moog@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Tests for ConfigurationItemRepository
29 *
30 */
31 class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
32
33 /**
34 * @var boolean Backup globals
35 */
36 protected $backupGlobals = TRUE;
37
38 /**
39 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
40 */
41 public $configurationItemRepository;
42
43 /**
44 *
45 * @return void
46 */
47 public function setUp() {
48 $this->configurationItemRepository = $this->getAccessibleMock(
49 'TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository',
50 array('dummy',)
51 );
52 $configurationManagerMock = $this->getAccessibleMock(
53 'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager',
54 array('dummy')
55 );
56 $this->configurationItemRepository->injectConfigurationManager($configurationManagerMock);
57 }
58
59 /**
60 * @return void
61 */
62 public function tearDown() {
63 unset($this->configurationItemRepository);
64 }
65
66 /**
67 *
68 * @test
69 * @return void
70 */
71 public function addMetaInformationUnsetsOriginalConfigurationMetaKey() {
72 $configuration = array(
73 '__meta__' => 'metaInformation',
74 'test123' => 'test123'
75 );
76 $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
77 $this->assertEquals(array('test123' => 'test123'), $configuration);
78 }
79
80 /**
81 * @test
82 * @return void
83 */
84 public function addMetaInformationReturnsMetaInformation() {
85 $configuration = array(
86 '__meta__' => 'metaInformation',
87 'test123' => 'test123'
88 );
89 $meta = $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
90 $this->assertEquals('metaInformation', $meta);
91 }
92
93 /**
94 *
95 * @return array
96 */
97 public function extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider() {
98 return array(
99 array(
100 array(
101 'cat' => 'basic',
102 'subcat_name' => 'enable',
103 'subcat' => 'a/enable/z',
104 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
105 'label' => 'Frontend configuration check',
106 'name' => 'checkConfigurationFE',
107 'value' => 0,
108 'default_value' => 0,
109 'comparisonGeneric' => 'EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend'
110 )
111 ),
112 array(
113 array(
114 'cat' => 'basic',
115 'subcat_name' => 'enable',
116 'subcat' => 'a/enable/z',
117 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend]',
118 'label' => 'Backend configuration check',
119 'name' => 'checkConfigurationBE',
120 'value' => 0,
121 'default_value' => 0,
122 'comparisonGeneric' => 'EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend'
123 )
124 ),
125 array(
126 array(
127 'cat' => 'basic',
128 'subcat_name' => 'enable',
129 'subcat' => 'a/enable/z',
130 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE]',
131 '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).',
132 'name' => 'FE.saltedPWHashingMethod',
133 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt',
134 'default_value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt',
135 'comparisonGeneric' => 'EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE'
136 )
137 )
138 );
139 }
140
141 /**
142 *
143 * @test
144 * @dataProvider extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider
145 * @param $configurationOption
146 * @return void
147 */
148 public function extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformation($configurationOption) {
149 $configurationOptionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeUser', $configurationOption);
150 $this->assertEquals('user', $configurationOptionModified['type']);
151 $this->assertEquals($configurationOption['comparisonGeneric'], $configurationOptionModified['generic']);
152 }
153
154 /**
155 *
156 * @test
157 * @return void
158 */
159 public function extractInformationForConfigFieldsOfTypeOptionsAddsGenericTypeAndLabelInformation() {
160 $option = array(
161 'cat' => 'basic',
162 'subcat_name' => 'enable',
163 'subcat' => 'a/enable/100z',
164 '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.)]',
165 'label' => 'Default configuration settings',
166 'name' => 'defaultConfiguration',
167 'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
168 'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
169 'genericComparisonValue' => array(
170 '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.)',
171 '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.)',
172 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)'
173 ),
174 'typeComparisonValue' => 'options'
175 );
176 $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
177 $this->assertArrayHasKey('generic', $optionModified);
178 $this->assertArrayHasKey('type', $optionModified);
179 $this->assertArrayHasKey('label', $optionModified);
180 $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
181 $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
182 }
183
184 /**
185 *
186 * @test
187 * @return void
188 */
189 public function extractInformationForConfigFieldsOfTypeOptionsWithLabelsAndValuesAddsGenericTypeAndLabelInformation() {
190 $option = array(
191 'cat' => 'basic',
192 'subcat_name' => 'enable',
193 'subcat' => 'a/enable/100z',
194 '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]',
195 'label' => 'Default configuration settings',
196 'name' => 'defaultConfiguration',
197 'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
198 'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
199 'genericComparisonValue' => array(
200 'Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.)' => 'MINIMAL',
201 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)' => 'TYPICAL',
202 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'DEMO'
203 ),
204 'typeComparisonValue' => 'options'
205 );
206 $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
207 $this->assertArrayHasKey('generic', $optionModified);
208 $this->assertArrayHasKey('type', $optionModified);
209 $this->assertArrayHasKey('label', $optionModified);
210 $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
211 $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
212 }
213
214 /**
215 *
216 * @test
217 * @return void
218 */
219 public function mergeDefaultConfigurationWithNoCurrentValuesReturnsTheDefaultConfiguration() {
220
221 // @TODO: Possible tests that can be added if ConfigurationManager is not static
222 // and can be mocked:
223 /*
224 // Value is set to null
225 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = NULL;
226 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
227 $this->assertEquals($defaultConfiguration, $configuration);
228
229 // Value is set to integer
230 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 123;
231 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
232 $this->assertEquals($defaultConfiguration, $configuration);
233
234 // valid configuration value - an empty serialized array
235 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 'a:0:{}';
236 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
237 $this->assertEquals($defaultConfiguration, $configuration);
238 */
239
240 $extensionKey = 'some_non_existing_extension';
241 $extension = array(
242 'key' => $extensionKey
243 );
244 $defaultConfiguration = array(
245 'foo' => 'bar'
246 );
247
248 // No value is set
249 $configuration = $this->configurationItemRepository->_callRef('mergeWithExistingConfiguration', $defaultConfiguration, $extension);
250 $this->assertEquals($defaultConfiguration, $configuration);
251 }
252
253 /**
254 *
255 * @test
256 * @return void
257 */
258 public function mergeWithExistingConfigurationOverwritesDefaultKeysWithCurrent() {
259 $configurationManagerMock = $this->getAccessibleMock(
260 'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
261 $configurationManagerMock
262 ->expects($this->once())
263 ->method('getConfigurationValueByPath')
264 ->with('EXT/extConf/testextensionkey')
265 ->will($this->returnValue(
266 serialize(array(
267 'FE.' => array(
268 'enabled' => '1',
269 'saltedPWHashingMethod' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
270 ),
271 'CLI.' => array(
272 'enabled' => '0'
273 )
274 ))
275 ));
276 $this->configurationItemRepository->injectConfigurationManager($configurationManagerMock);
277
278 $defaultConfiguration = array(
279 'FE.enabled' => array(
280 'value' => '0'
281 ),
282 'FE.saltedPWHashingMethod' => array(
283 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
284 ),
285 'BE.enabled' => array(
286 'value' => '1'
287 ),
288 'BE.saltedPWHashingMethod' => array(
289 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
290 )
291 );
292 $expectedResult = array(
293 'FE.enabled' => array(
294 'value' => '1'
295 ),
296 'FE.saltedPWHashingMethod' => array(
297 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
298 ),
299 'BE.enabled' => array(
300 'value' => '1'
301 ),
302 'BE.saltedPWHashingMethod' => array(
303 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
304 ),
305 'CLI.enabled' => array(
306 'value' => '0'
307 )
308 );
309 $result = $this->configurationItemRepository->_call('mergeWithExistingConfiguration', $defaultConfiguration, array('key' => 'testextensionkey'));
310 $this->assertEquals($expectedResult, $result);
311 }
312
313 /**
314 *
315 * @return array
316 */
317 public function createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider() {
318 return array(
319 'demo data from salted passwords' => array(
320 '
321 # cat=basic/enable; type=user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]; label=Frontend configuration check
322 checkConfigurationFE=0
323
324 # cat=advancedBackend; type=boolean; label=Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.
325 BE.forceSalted = 0
326
327 TSConstantEditor.advancedbackend {
328 description = <span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!
329 1=BE.forceSalted
330 }',
331 array(
332 'checkConfigurationFE' => array(
333 'cat' => 'basic',
334 'subcat_name' => 'enable',
335 'subcat' => 'a/enable/z',
336 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
337 'label' => 'Frontend configuration check',
338 'name' => 'checkConfigurationFE',
339 'value' => '0',
340 'default_value' => '0'
341 ),
342 'BE.forceSalted' => array(
343 'cat' => 'advancedbackend',
344 'subcat' => 'x/z',
345 'type' => 'boolean',
346 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
347 'name' => 'BE.forceSalted',
348 'value' => '0',
349 'default_value' => '0'
350 )
351 ),
352 array(
353 'advancedbackend.' => array(
354 'description' => '<span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!',
355 1 => 'BE.forceSalted'
356 )
357 ),
358 array(
359 'checkConfigurationFE' => array(
360 'cat' => 'basic',
361 'subcat_name' => 'enable',
362 'subcat' => 'a/enable/z',
363 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
364 'label' => 'Frontend configuration check',
365 'name' => 'checkConfigurationFE',
366 'value' => '0',
367 'default_value' => '0'
368 ),
369 'BE.forceSalted' => array(
370 'cat' => 'advancedbackend',
371 'subcat' => 'x/z',
372 'type' => 'boolean',
373 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
374 'name' => 'BE.forceSalted',
375 'value' => '0',
376 'default_value' => '0',
377 'highlight' => 1
378 ),
379 '__meta__' => array(
380 'advancedbackend' => array(
381 'highlightText' => '<span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!'
382 )
383 )
384 )
385 )
386 );
387 }
388
389 /**
390 *
391 * @test
392 * @dataProvider createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider
393 * @param $raw
394 * @param $constants
395 * @param $setupTsConstantEditor
396 * @param $expected
397 * @return void
398 */
399 public function createArrayFromConstantsCreatesAnArrayWithMetaInformation($raw, $constants, $setupTsConstantEditor, $expected) {
400 $tsStyleConfig = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
401 $configurationItemRepositoryMock = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository', array('getT3libTsStyleConfig'));
402 $configurationItemRepositoryMock->expects($this->once())->method('getT3libTsStyleConfig')->will($this->returnValue($tsStyleConfig));
403 $tsStyleConfig->expects($this->once())->method('ext_initTSstyleConfig')->with($raw, $this->anything(), $this->anything(), $this->anything())->will($this->returnValue($constants));
404 $tsStyleConfig->setup['constants']['TSConstantEditor.'] = $setupTsConstantEditor;
405 $constantsResult = $configurationItemRepositoryMock->createArrayFromConstants($raw, array());
406 $this->assertEquals($expected, $constantsResult);
407 }
408
409 /**
410 * @test
411 */
412 public function convertRawConfigurationToArrayReturnsSortedHierarchicArray() {
413 $configRaw = '# cat=basic/enable/10; type=string; label=Item 1: This is the first configuration item
414 item1 = one
415
416 # cat=basic/enable/20; type=int+; label=Integer Value: Please insert a positive integer value
417 integerValue = 1
418
419 # cat=advanced/file/10; type=boolean; label=enableJquery: Insert jQuery plugin
420 enableJquery = 1';
421 $extension = array();
422
423 $expectedArray = array(
424 'basic' => array(
425 'enable' => array(
426 'item1' => array(
427 'cat' => 'basic',
428 'subcat_name' => 'enable',
429 'subcat' => 'a/enable/10z',
430 'type' => 'string',
431 'label' => 'Item 1: This is the first configuration item',
432 'name' =>'item1',
433 'value' => 'one',
434 'default_value' => 'one',
435 'labels' => array(
436 0 => 'Item 1',
437 1 => 'This is the first configuration item'
438 )
439 ),
440 'integerValue' => array(
441 'cat' => 'basic',
442 'subcat_name' => 'enable',
443 'subcat' => 'a/enable/20z',
444 'type' => 'int+',
445 'label' => 'Integer Value: Please insert a positive integer value',
446 'name' =>'integerValue',
447 'value' => '1',
448 'default_value' => '1',
449 'labels' => array(
450 0 => 'Integer Value',
451 1 => 'Please insert a positive integer value'
452 )
453 )
454 )
455 ),
456 'advanced' => array(
457 'file' => array(
458 'enableJquery' => array(
459 'cat' => 'advanced',
460 'subcat_name' => 'file',
461 'subcat' => 'c/file/10z',
462 'type' => 'boolean',
463 'label' => 'enableJquery: Insert jQuery plugin',
464 'name' =>'enableJquery',
465 'value' => '1',
466 'default_value' => '1',
467 'labels' => array(
468 0 => 'enableJquery',
469 1 => 'Insert jQuery plugin'
470 )
471 ),
472 )
473 )
474 );
475
476 $this->assertSame($expectedArray, $this->configurationItemRepository->_callRef('convertRawConfigurationToArray', $configRaw, $extension));
477 }
478
479 }
480
481
482 ?>