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