[FEATURE] Integrate preliminary PackageManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Tests / Unit / Utility / ConfigurationUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Oliver Hader <oliver.hader@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 * Configuration utility test
29 *
30 */
31 class ConfigurationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
32
33 /**
34 * @test
35 */
36 public function getCurrentConfigurationReturnsExtensionConfigurationAsValuedConfiguration() {
37 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
38 $configurationUtility = $this->getMock(
39 'TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility',
40 array('getDefaultConfigurationFromExtConfTemplateAsValuedArray')
41 );
42 $configurationUtility
43 ->expects($this->once())
44 ->method('getDefaultConfigurationFromExtConfTemplateAsValuedArray')
45 ->will($this->returnValue(array()));
46 $extensionKey = uniqid('some-extension');
47
48 $currentConfiguration = array(
49 'key1' => 'value1',
50 'key2.' => array(
51 'subkey1' => 'value2'
52 )
53 );
54
55 $expected = array(
56 'key1' => array(
57 'value' => 'value1',
58 ),
59 'key2.subkey1' => array(
60 'value' => 'value2',
61 ),
62 );
63
64 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = serialize($currentConfiguration);
65 $actual = $configurationUtility->getCurrentConfiguration($extensionKey);
66 $this->assertEquals($expected, $actual);
67 }
68
69 /**
70 * @test
71 */
72 public function getDefaultConfigurationFromExtConfTemplateAsValuedArrayReturnsExpectedExampleArray() {
73 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
74 $configurationUtility = $this->getAccessibleMock(
75 'TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility',
76 array('getDefaultConfigurationRawString', 'getExtensionPathInformation')
77 );
78 $configurationUtility
79 ->expects($this->once())
80 ->method('getDefaultConfigurationRawString')
81 ->will($this->returnValue('foo'));
82
83 $configurationUtility
84 ->expects($this->once())
85 ->method('getExtensionPathInformation')
86 ->will($this->returnValue(NULL));
87
88 $tsStyleConfig = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
89
90 $objectManagerMock = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
91 $configurationUtility->_set('objectManager', $objectManagerMock);
92 $objectManagerMock
93 ->expects($this->once())
94 ->method('get')
95 ->with('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm')
96 ->will($this->returnValue($tsStyleConfig));
97
98 $constants = array(
99 'checkConfigurationFE' => array(
100 'cat' => 'basic',
101 'subcat_name' => 'enable',
102 'subcat' => 'a/enable/z',
103 'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
104 'label' => 'Frontend configuration check',
105 'name' => 'checkConfigurationFE',
106 'value' => '0',
107 'default_value' => '0'
108 ),
109 'BE.forceSalted' => array(
110 'cat' => 'advancedbackend',
111 'subcat' => 'x/z',
112 'type' => 'boolean',
113 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
114 'name' => 'BE.forceSalted',
115 'value' => '0',
116 'default_value' => '0'
117 )
118 );
119 $tsStyleConfig
120 ->expects($this->once())
121 ->method('ext_initTSstyleConfig')
122 ->will($this->returnValue($constants));
123
124 $setupTsConstantEditor = array(
125 'advancedbackend.' => array(
126 '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!',
127 1 => 'BE.forceSalted'
128 )
129 );
130 $tsStyleConfig->setup['constants']['TSConstantEditor.'] = $setupTsConstantEditor;
131
132 $expected = array(
133 'checkConfigurationFE' => array(
134 'cat' => 'basic',
135 'subcat_name' => 'enable',
136 'subcat' => 'a/enable/z',
137 'type' => 'user[TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
138 'label' => 'Frontend configuration check',
139 'name' => 'checkConfigurationFE',
140 'value' => '0',
141 'default_value' => '0',
142 'subcat_label' => 'Enable features',
143 ),
144 'BE.forceSalted' => array(
145 'cat' => 'advancedbackend',
146 'subcat' => 'x/z',
147 'type' => 'boolean',
148 'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
149 'name' => 'BE.forceSalted',
150 'value' => '0',
151 'default_value' => '0',
152 'highlight' => 1,
153 ),
154 '__meta__' => array(
155 'advancedbackend' => array(
156 '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!'
157 )
158 )
159 );
160
161 $result = $configurationUtility->getDefaultConfigurationFromExtConfTemplateAsValuedArray(uniqid('some_extension'));
162 $this->assertEquals($expected, $result);
163 }
164
165 /**
166 * Data provider for convertValuedToNestedConfiguration
167 *
168 * @return array
169 */
170 public function convertValuedToNestedConfigurationDataProvider() {
171 return array(
172 'plain array' => array(
173 array(
174 'first' => array(
175 'value' => 'value1'
176 ),
177 'second' => array(
178 'value' => 'value2'
179 )
180 ),
181 array(
182 'first' => 'value1',
183 'second' => 'value2'
184 )
185 ),
186 'nested value with 2 levels' => array(
187 array(
188 'first.firstSub' => array(
189 'value' => 'value1'
190 ),
191 'second.secondSub' => array(
192 'value' => 'value2'
193 )
194 ),
195 array(
196 'first.' => array(
197 'firstSub' => 'value1'
198 ),
199 'second.' => array(
200 'secondSub' => 'value2'
201 )
202 )
203 ),
204 'nested value with 3 levels' => array(
205 array(
206 'first.firstSub.firstSubSub' => array(
207 'value' => 'value1'
208 ),
209 'second.secondSub.secondSubSub' => array(
210 'value' => 'value2'
211 )
212 ),
213 array(
214 'first.' => array(
215 'firstSub.' => array(
216 'firstSubSub' => 'value1'
217 )
218 ),
219 'second.' => array(
220 'secondSub.' => array(
221 'secondSubSub' => 'value2'
222 )
223 )
224 )
225 ),
226 'mixed nested value with 2 levels' => array(
227 array(
228 'first' => array(
229 'value' => 'firstValue'
230 ),
231 'first.firstSub' => array(
232 'value' => 'value1'
233 ),
234 'second.secondSub' => array(
235 'value' => 'value2'
236 )
237 ),
238 array(
239 'first' => 'firstValue',
240 'first.' => array(
241 'firstSub' => 'value1'
242 ),
243 'second.' => array(
244 'secondSub' => 'value2'
245 )
246 )
247 )
248 );
249 }
250
251 /**
252 * @test
253 * @dataProvider convertValuedToNestedConfigurationDataProvider
254 *
255 * @param array $configuration
256 * @param array $expected
257 * @return void
258 */
259 public function convertValuedToNestedConfiguration(array $configuration, array $expected) {
260 /** @var $fixture \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
261 $fixture = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
262 $this->assertEquals($expected, $fixture->convertValuedToNestedConfiguration($configuration));
263 }
264
265 /**
266 * Data provider for convertNestedToValuedConfiguration
267 *
268 * @return array
269 */
270 public function convertNestedToValuedConfigurationDataProvider() {
271 return array(
272 'plain array' => array(
273 array(
274 'first' => 'value1',
275 'second' => 'value2'
276 ),
277 array(
278 'first' => array('value' => 'value1'),
279 'second' => array('value' => 'value2'),
280 )
281 ),
282 'two levels' => array(
283 array(
284 'first.' => array('firstSub' => 'value1'),
285 'second.' => array('firstSub' => 'value2'),
286 ),
287 array(
288 'first.firstSub' => array('value' => 'value1'),
289 'second.firstSub' => array('value' => 'value2'),
290 )
291 ),
292 'three levels' => array(
293 array(
294 'first.' => array('firstSub.' => array('firstSubSub' => 'value1')),
295 'second.' => array('firstSub.' => array('firstSubSub' => 'value2'))
296 ),
297 array(
298 'first.firstSub.firstSubSub' => array('value' => 'value1'),
299 'second.firstSub.firstSubSub' => array('value' => 'value2'),
300 )
301 ),
302 'mixed' => array(
303 array(
304 'first.' => array('firstSub' => 'value1'),
305 'second.' => array('firstSub.' => array('firstSubSub' => 'value2')),
306 'third' => 'value3'
307 ),
308 array(
309 'first.firstSub' => array('value' => 'value1'),
310 'second.firstSub.firstSubSub' => array('value' => 'value2'),
311 'third' => array('value' => 'value3')
312 )
313 )
314 );
315 }
316
317 /**
318 * @test
319 * @dataProvider convertNestedToValuedConfigurationDataProvider
320 *
321 * @param array $configuration
322 * @param array $expected
323 * @return void
324 */
325 public function convertNestedToValuedConfiguration(array $configuration, array $expected) {
326 /** @var $fixture \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
327 $fixture = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
328 $this->assertEquals($expected, $fixture->convertNestedToValuedConfiguration($configuration));
329 }
330 }