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