e7bb3e1407a7daa465e9cc684849f6f2ecb69795
[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 mergeDefaultConfigurationWithNoCurrentValuesReturnsTheDefaultConfiguration() {
185
186 // @TODO: Possible tests that can be added if ConfigurationManager is not static
187 // and can be mocked:
188 /*
189 // Value is set to null
190 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = NULL;
191 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
192 $this->assertEquals($defaultConfiguration, $configuration);
193
194 // Value is set to integer
195 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 123;
196 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
197 $this->assertEquals($defaultConfiguration, $configuration);
198
199 // valid configuration value - an empty serialized array
200 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 'a:0:{}';
201 $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
202 $this->assertEquals($defaultConfiguration, $configuration);
203 */
204
205 $extensionKey = 'some_non_existing_extension';
206 $extension = array(
207 'key' => $extensionKey
208 );
209 $defaultConfiguration = array(
210 'foo' => 'bar'
211 );
212
213 // No value is set
214 $configuration = $this->configurationItemRepository->_callRef('mergeWithExistingConfiguration', $defaultConfiguration, $extension);
215 $this->assertEquals($defaultConfiguration, $configuration);
216 }
217
218 /**
219 *
220 * @test
221 * @return void
222 */
223 public function mergeWithExistingConfigurationOverwritesDefaultKeysWithCurrent() {
224 $configurationManagerMock = $this->getAccessibleMock(
225 'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
226 $configurationManagerMock
227 ->expects($this->once())
228 ->method('getConfigurationValueByPath')
229 ->with('EXT/extConf/testextensionkey')
230 ->will($this->returnValue(
231 serialize(array(
232 'FE.' => array(
233 'enabled' => '1',
234 'saltedPWHashingMethod' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
235 ),
236 'CLI.' => array(
237 'enabled' => '0'
238 )
239 ))
240 ));
241 $this->configurationItemRepository->injectConfigurationManager($configurationManagerMock);
242
243 $defaultConfiguration = array(
244 'FE.enabled' => array(
245 'value' => '0'
246 ),
247 'FE.saltedPWHashingMethod' => array(
248 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
249 ),
250 'BE.enabled' => array(
251 'value' => '1'
252 ),
253 'BE.saltedPWHashingMethod' => array(
254 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
255 )
256 );
257 $expectedResult = array(
258 'FE.enabled' => array(
259 'value' => '1'
260 ),
261 'FE.saltedPWHashingMethod' => array(
262 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
263 ),
264 'BE.enabled' => array(
265 'value' => '1'
266 ),
267 'BE.saltedPWHashingMethod' => array(
268 'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
269 ),
270 'CLI.enabled' => array(
271 'value' => '0'
272 )
273 );
274 $result = $this->configurationItemRepository->_call('mergeWithExistingConfiguration', $defaultConfiguration, array('key' => 'testextensionkey'));
275 $this->assertEquals($expectedResult, $result);
276 }
277
278 /**
279 *
280 * @return array
281 */
282 public function createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider() {
283 return array(
284 'demo data from salted passwords' => array(
285 '
286 # cat=basic/enable; type=user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]; label=Frontend configuration check
287 checkConfigurationFE=0
288
289 # cat=advancedBackend; type=boolean; label=Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.
290 BE.forceSalted = 0
291
292 TSConstantEditor.advancedbackend {
293 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!
294 1=BE.forceSalted
295 }',
296 array(
297 'checkConfigurationFE' => array(
298 'cat' => 'basic',
299 'subcat_name' => 'enable',
300 'subcat' => 'a/enable/z',
301 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
302 'label' => 'Frontend configuration check',
303 'name' => 'checkConfigurationFE',
304 'value' => '0',
305 'default_value' => '0'
306 ),
307 'BE.forceSalted' => array(
308 'cat' => 'advancedbackend',
309 'subcat' => 'x/z',
310 'type' => 'boolean',
311 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
312 'name' => 'BE.forceSalted',
313 'value' => '0',
314 'default_value' => '0'
315 )
316 ),
317 array(
318 'advancedbackend.' => array(
319 '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!',
320 1 => 'BE.forceSalted'
321 )
322 ),
323 array(
324 'checkConfigurationFE' => array(
325 'cat' => 'basic',
326 'subcat_name' => 'enable',
327 'subcat' => 'a/enable/z',
328 'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
329 'label' => 'Frontend configuration check',
330 'name' => 'checkConfigurationFE',
331 'value' => '0',
332 'default_value' => '0'
333 ),
334 'BE.forceSalted' => array(
335 'cat' => 'advancedbackend',
336 'subcat' => 'x/z',
337 'type' => 'boolean',
338 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
339 'name' => 'BE.forceSalted',
340 'value' => '0',
341 'default_value' => '0',
342 'highlight' => 1
343 ),
344 '__meta__' => array(
345 'advancedbackend' => array(
346 '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!'
347 )
348 )
349 )
350 )
351 );
352 }
353
354 /**
355 *
356 * @test
357 * @dataProvider createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider
358 * @param $raw
359 * @param $constants
360 * @param $setupTsConstantEditor
361 * @param $expected
362 * @return void
363 */
364 public function createArrayFromConstantsCreatesAnArrayWithMetaInformation($raw, $constants, $setupTsConstantEditor, $expected) {
365 $tsStyleConfig = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
366 $configurationItemRepositoryMock = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository', array('getT3libTsStyleConfig'));
367 $configurationItemRepositoryMock->expects($this->once())->method('getT3libTsStyleConfig')->will($this->returnValue($tsStyleConfig));
368 $tsStyleConfig->expects($this->once())->method('ext_initTSstyleConfig')->with($raw, $this->anything(), $this->anything(), $this->anything())->will($this->returnValue($constants));
369 $tsStyleConfig->setup['constants']['TSConstantEditor.'] = $setupTsConstantEditor;
370 $constantsResult = $configurationItemRepositoryMock->createArrayFromConstants($raw, array());
371 $this->assertEquals($expected, $constantsResult);
372 }
373
374 }
375
376
377 ?>