[BUGFIX] EM 6.0 parse configuration type 'options' correctly
[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 * @package Extension Manager
31 * @subpackage Tests
32 */
33 class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
34
35 /**
36 * @var boolean Backup globals
37 */
38 protected $backupGlobals = TRUE;
39
40 /**
41 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
42 */
43 public $configurationItemRepository;
44
45 /**
46 *
47 * @return void
48 */
49 public function setUp() {
50 $this->configurationItemRepository = $this->getAccessibleMock(
51 'TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository',
52 array('dummy',)
53 );
54 $configurationManagerMock = $this->getAccessibleMock(
55 'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager',
56 array('dummy')
57 );
58 $this->configurationItemRepository->injectConfigurationManager($configurationManagerMock);
59 }
60
61 /**
62 *
63 * @test
64 * @return void
65 */
66 public function addMetaInformationUnsetsOriginalConfigurationMetaKey() {
67 $configuration = array(
68 '__meta__' => 'metaInformation',
69 'test123' => 'test123'
70 );
71 $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
72 $this->assertEquals(array('test123' => 'test123'), $configuration);
73 }
74
75 /**
76 * @test
77 * @return void
78 */
79 public function addMetaInformationReturnsMetaInformation() {
80 $configuration = array(
81 '__meta__' => 'metaInformation',
82 'test123' => 'test123'
83 );
84 $meta = $this->configurationItemRepository->_callRef('addMetaInformation', $configuration);
85 $this->assertEquals('metaInformation', $meta);
86 }
87
88 /**
89 *
90 * @return array
91 */
92 public function extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider() {
93 return array(
94 array(
95 array(
96 'cat' => 'basic',
97 'subcat_name' => 'enable',
98 'subcat' => 'a/enable/z',
99 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
100 'label' => 'Frontend configuration check',
101 'name' => 'checkConfigurationFE',
102 'value' => 0,
103 'default_value' => 0,
104 'comparisonGeneric' => 'EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend'
105 )
106 ),
107 array(
108 array(
109 'cat' => 'basic',
110 'subcat_name' => 'enable',
111 'subcat' => 'a/enable/z',
112 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend]',
113 'label' => 'Backend configuration check',
114 'name' => 'checkConfigurationBE',
115 'value' => 0,
116 'default_value' => 0,
117 'comparisonGeneric' => 'EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationBackend'
118 )
119 ),
120 array(
121 array(
122 'cat' => 'basic',
123 'subcat_name' => 'enable',
124 'subcat' => 'a/enable/z',
125 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE]',
126 '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).',
127 'name' => 'FE.saltedPWHashingMethod',
128 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt',
129 'default_value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt',
130 'comparisonGeneric' => 'EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->buildHashMethodSelectorFE'
131 )
132 )
133 );
134 }
135
136 /**
137 *
138 * @test
139 * @dataProvider extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider
140 * @param $configurationOption
141 * @return void
142 */
143 public function extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformation($configurationOption) {
144 $configurationOptionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeUser', $configurationOption);
145 $this->assertEquals('user', $configurationOptionModified['type']);
146 $this->assertEquals($configurationOption['comparisonGeneric'], $configurationOptionModified['generic']);
147 }
148
149 /**
150 *
151 * @test
152 * @return void
153 */
154 public function extractInformationForConfigFieldsOfTypeOptionsAddsGenericTypeAndLabelInformation() {
155 $option = array(
156 'cat' => 'basic',
157 'subcat_name' => 'enable',
158 'subcat' => 'a/enable/100z',
159 '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.)]',
160 'label' => 'Default configuration settings',
161 'name' => 'defaultConfiguration',
162 'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
163 'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
164 'genericComparisonValue' => array(
165 '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.)',
166 '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.)',
167 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)'
168 ),
169 'typeComparisonValue' => 'options'
170 );
171 $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
172 $this->assertArrayHasKey('generic', $optionModified);
173 $this->assertArrayHasKey('type', $optionModified);
174 $this->assertArrayHasKey('label', $optionModified);
175 $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
176 $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
177 }
178
179 /**
180 *
181 * @test
182 * @return void
183 */
184 public function extractInformationForConfigFieldsOfTypeOptionsWithLabelsAndValuesAddsGenericTypeAndLabelInformation() {
185 $option = array(
186 'cat' => 'basic',
187 'subcat_name' => 'enable',
188 'subcat' => 'a/enable/100z',
189 '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]',
190 'label' => 'Default configuration settings',
191 'name' => 'defaultConfiguration',
192 'value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
193 'default_value' => 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)',
194 'genericComparisonValue' => array(
195 'Minimal (Most features disabled. Administrator needs to enable them using TypoScript. For advanced administrators only.)' => 'MINIMAL',
196 'Typical (Most commonly used features are enabled. Select this option if you are unsure which one to use.)' => 'TYPICAL',
197 'Demo (Show-off configuration. Includes pre-configured styles. Not for production environments.)' => 'DEMO'
198 ),
199 'typeComparisonValue' => 'options'
200 );
201 $optionModified = $this->configurationItemRepository->_callRef('extractInformationForConfigFieldsOfTypeOptions', $option);
202 $this->assertArrayHasKey('generic', $optionModified);
203 $this->assertArrayHasKey('type', $optionModified);
204 $this->assertArrayHasKey('label', $optionModified);
205 $this->assertEquals($option['genericComparisonValue'], $optionModified['generic']);
206 $this->assertEquals($option['typeComparisonValue'], $optionModified['type']);
207 }
208
209 /**
210 *
211 * @test
212 * @return void
213 */
214 public function mergeDefaultConfigurationWithNoCurrentValuesReturnsTheDefaultConfiguration() {
215
216 // @TODO: Possible tests that can be added if ConfigurationManager is not static
217 // and can be mocked:
218 /*
219 // Value is set to null
220 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = NULL;
221 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
222 $this->assertEquals($defaultConfiguration, $configuration);
223
224 // Value is set to integer
225 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 123;
226 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
227 $this->assertEquals($defaultConfiguration, $configuration);
228
229 // valid configuration value - an empty serialized array
230 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 'a:0:{}';
231 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
232 $this->assertEquals($defaultConfiguration, $configuration);
233 */
234
235 $extensionKey = 'some_non_existing_extension';
236 $extension = array(
237 'key' => $extensionKey
238 );
239 $defaultConfiguration = array(
240 'foo' => 'bar'
241 );
242
243 // No value is set
244 $configuration = $this->configurationItemRepository->_callRef('mergeWithExistingConfiguration', $defaultConfiguration, $extension);
245 $this->assertEquals($defaultConfiguration, $configuration);
246 }
247
248 /**
249 *
250 * @test
251 * @return void
252 */
253 public function mergeWithExistingConfigurationOverwritesDefaultKeysWithCurrent() {
254 $configurationManagerMock = $this->getAccessibleMock(
255 'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
256 $configurationManagerMock
257 ->expects($this->once())
258 ->method('getConfigurationValueByPath')
259 ->with('EXT/extConf/testextensionkey')
260 ->will($this->returnValue(
261 serialize(array(
262 'FE.' => array(
263 'enabled' => '1',
264 'saltedPWHashingMethod' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
265 ),
266 'CLI.' => array(
267 'enabled' => '0'
268 )
269 ))
270 ));
271 $this->configurationItemRepository->injectConfigurationManager($configurationManagerMock);
272
273 $defaultConfiguration = array(
274 'FE.enabled' => array(
275 'value' => '0'
276 ),
277 'FE.saltedPWHashingMethod' => array(
278 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
279 ),
280 'BE.enabled' => array(
281 'value' => '1'
282 ),
283 'BE.saltedPWHashingMethod' => array(
284 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
285 )
286 );
287 $expectedResult = array(
288 'FE.enabled' => array(
289 'value' => '1'
290 ),
291 'FE.saltedPWHashingMethod' => array(
292 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
293 ),
294 'BE.enabled' => array(
295 'value' => '1'
296 ),
297 'BE.saltedPWHashingMethod' => array(
298 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
299 ),
300 'CLI.enabled' => array(
301 'value' => '0'
302 )
303 );
304 $result = $this->configurationItemRepository->_call('mergeWithExistingConfiguration', $defaultConfiguration, array('key' => 'testextensionkey'));
305 $this->assertEquals($expectedResult, $result);
306 }
307
308 /**
309 *
310 * @return array
311 */
312 public function createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider() {
313 return array(
314 'demo data from salted passwords' => array(
315 '
316 # cat=basic/enable; type=user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]; label=Frontend configuration check
317 checkConfigurationFE=0
318
319 # cat=advancedBackend; type=boolean; label=Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.
320 BE.forceSalted = 0
321
322 TSConstantEditor.advancedbackend {
323 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!
324 1=BE.forceSalted
325 }',
326 array(
327 'checkConfigurationFE' => array(
328 'cat' => 'basic',
329 'subcat_name' => 'enable',
330 'subcat' => 'a/enable/z',
331 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
332 'label' => 'Frontend configuration check',
333 'name' => 'checkConfigurationFE',
334 'value' => '0',
335 'default_value' => '0'
336 ),
337 'BE.forceSalted' => array(
338 'cat' => 'advancedbackend',
339 'subcat' => 'x/z',
340 'type' => 'boolean',
341 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
342 'name' => 'BE.forceSalted',
343 'value' => '0',
344 'default_value' => '0'
345 )
346 ),
347 array(
348 'advancedbackend.' => array(
349 '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!',
350 1 => 'BE.forceSalted'
351 )
352 ),
353 array(
354 'checkConfigurationFE' => array(
355 'cat' => 'basic',
356 'subcat_name' => 'enable',
357 'subcat' => 'a/enable/z',
358 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
359 'label' => 'Frontend configuration check',
360 'name' => 'checkConfigurationFE',
361 'value' => '0',
362 'default_value' => '0'
363 ),
364 'BE.forceSalted' => array(
365 'cat' => 'advancedbackend',
366 'subcat' => 'x/z',
367 'type' => 'boolean',
368 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
369 'name' => 'BE.forceSalted',
370 'value' => '0',
371 'default_value' => '0',
372 'highlight' => 1
373 ),
374 '__meta__' => array(
375 'advancedbackend' => array(
376 '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!'
377 )
378 )
379 )
380 )
381 );
382 }
383
384 /**
385 *
386 * @test
387 * @dataProvider createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider
388 * @param $raw
389 * @param $constants
390 * @param $setupTsConstantEditor
391 * @param $expected
392 * @return void
393 */
394 public function createArrayFromConstantsCreatesAnArrayWithMetaInformation($raw, $constants, $setupTsConstantEditor, $expected) {
395 $tsStyleConfig = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
396 $configurationItemRepositoryMock = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository', array('getT3libTsStyleConfig'));
397 $configurationItemRepositoryMock->expects($this->once())->method('getT3libTsStyleConfig')->will($this->returnValue($tsStyleConfig));
398 $tsStyleConfig->expects($this->once())->method('ext_initTSstyleConfig')->with($raw, $this->anything(), $this->anything(), $this->anything())->will($this->returnValue($constants));
399 $tsStyleConfig->setup['constants']['TSConstantEditor.'] = $setupTsConstantEditor;
400 $constantsResult = $configurationItemRepositoryMock->createArrayFromConstants($raw, array());
401 $this->assertEquals($expected, $constantsResult);
402 }
403
404 }
405
406
407 ?>