[TASK] Add missing unit tests for SilentConfigurationUpgradeService
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Service / SilentConfigurationUpgradeServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Install\Controller\Exception\RedirectException;
18
19 /**
20 * Test case
21 */
22 class SilentConfigurationUpgradeServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject
26 */
27 protected $configurationManager;
28
29 /**
30 * @param array $methods
31 */
32 protected function createConfigurationManagerWithMockedMethods(array $methods) {
33 $this->configurationManager = $this->getMock(
34 \TYPO3\CMS\Core\Configuration\ConfigurationManager::class,
35 $methods
36 );
37 }
38
39 /**
40 * @test
41 */
42 public function configureSaltedPasswordsWithDefaultConfiguration() {
43 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
44 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
45 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
46 array('dummy'),
47 array(),
48 '',
49 FALSE
50 );
51 $config = 'a:2:{s:3:"BE.";a:3:{s:11:"forceSalted";i:0;s:15:"onlyAuthService";i:0;s:12:"updatePasswd";i:1;}s:3:"FE.";a:4:{s:7:"enabled";i:0;s:11:"forceSalted";i:0;s:15:"onlyAuthService";i:0;s:12:"updatePasswd";i:1;}}';
52 $defaultConfiguration = array();
53 $defaultConfiguration['EXT']['extConf']['saltedpasswords'] = $config;
54
55 $closure = function () {
56 throw new \RuntimeException('Path does not exist in array', 1341397869);
57 };
58
59 $this->createConfigurationManagerWithMockedMethods(
60 array(
61 'getDefaultConfiguration',
62 'getLocalConfigurationValueByPath',
63 'setLocalConfigurationValueByPath',
64 )
65 );
66 $this->configurationManager->expects($this->exactly(1))
67 ->method('getDefaultConfiguration')
68 ->will($this->returnValue($defaultConfiguration));
69 $this->configurationManager->expects($this->exactly(1))
70 ->method('getLocalConfigurationValueByPath')
71 ->will($this->returnCallback($closure));
72 $this->configurationManager->expects($this->once())
73 ->method('setLocalConfigurationValueByPath')
74 ->with($this->equalTo('EXT/extConf/saltedpasswords'), $this->equalTo($config));
75
76 $this->setExpectedException(\TYPO3\CMS\Install\Controller\Exception\RedirectException::class);
77
78 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
79
80 $silentConfigurationUpgradeServiceInstance->_call('configureSaltedPasswords');
81 }
82
83 /**
84 * @test
85 */
86 public function configureSaltedPasswordsWithExtensionConfigurationBeEnabled() {
87 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
88 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
89 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
90 array('dummy'),
91 array(),
92 '',
93 FALSE
94 );
95 $config = 'a:2:{s:3:"BE.";a:1:{s:21:"saltedPWHashingMethod";}s:3:"FE.";a:2:{s:7:"enabled";i:0;s:11:"forceSalted";i:0;}}';
96 $defaultConfiguration = array();
97 $defaultConfiguration['EXT']['extConf']['saltedpasswords'] = $config;
98
99 $currentLocalConfiguration = array(
100 array('EXT/extConf/saltedpasswords', 'a:2:{s:3:"BE.";a:1:{s:7:"enabled";i:1;}s:3:"FE.";a:1:{s:7:"enabled";i:0;}}')
101 );
102 $newConfig = 'a:2:{s:3:"BE.";a:0:{}s:3:"FE.";a:1:{s:7:"enabled";i:0;}}';
103 $this->createConfigurationManagerWithMockedMethods(
104 array(
105 'getDefaultConfiguration',
106 'getLocalConfigurationValueByPath',
107 'setLocalConfigurationValueByPath',
108 )
109 );
110 $this->configurationManager->expects($this->exactly(1))
111 ->method('getDefaultConfiguration')
112 ->will($this->returnValue($defaultConfiguration));
113 $this->configurationManager->expects($this->exactly(1))
114 ->method('getLocalConfigurationValueByPath')
115 ->will($this->returnValueMap($currentLocalConfiguration));
116 $this->configurationManager->expects($this->once())
117 ->method('setLocalConfigurationValueByPath')
118 ->with($this->equalTo('EXT/extConf/saltedpasswords'), $this->equalTo($newConfig));
119
120 $this->setExpectedException(\TYPO3\CMS\Install\Controller\Exception\RedirectException::class);
121
122 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
123
124 $silentConfigurationUpgradeServiceInstance->_call('configureSaltedPasswords');
125 }
126
127 /**
128 * @test
129 */
130 public function configureSaltedPasswordsWithExtensionConfigurationBeNotEnabled() {
131 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
132 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
133 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
134 array('dummy'),
135 array(),
136 '',
137 FALSE
138 );
139 $config = 'a:2:{s:3:"BE.";a:1:{s:15:"onlyAuthService";i:0;}s:3:"FE.";a:2:{s:7:"enabled";i:0;s:11:"forceSalted";i:0;}}';
140 $defaultConfiguration = array();
141 $defaultConfiguration['EXT']['extConf']['saltedpasswords'] = $config;
142
143 $currentLocalConfiguration = array(
144 array('EXT/extConf/saltedpasswords', 'a:2:{s:3:"BE.";a:2:{s:7:"enabled";i:0;s:12:"updatePasswd";i:1;}s:3:"FE.";a:1:{s:7:"enabled";i:0;}}')
145 );
146 $newConfig = 'a:2:{s:3:"BE.";a:1:{s:15:"onlyAuthService";i:0;}s:3:"FE.";a:1:{s:7:"enabled";i:0;}}';
147 $this->createConfigurationManagerWithMockedMethods(
148 array(
149 'getDefaultConfiguration',
150 'getLocalConfigurationValueByPath',
151 'setLocalConfigurationValueByPath',
152 )
153 );
154 $this->configurationManager->expects($this->exactly(1))
155 ->method('getDefaultConfiguration')
156 ->will($this->returnValue($defaultConfiguration));
157 $this->configurationManager->expects($this->exactly(1))
158 ->method('getLocalConfigurationValueByPath')
159 ->will($this->returnValueMap($currentLocalConfiguration));
160 $this->configurationManager->expects($this->once())
161 ->method('setLocalConfigurationValueByPath')
162 ->with($this->equalTo('EXT/extConf/saltedpasswords'), $this->equalTo($newConfig));
163
164 $this->setExpectedException(\TYPO3\CMS\Install\Controller\Exception\RedirectException::class);
165
166 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
167
168 $silentConfigurationUpgradeServiceInstance->_call('configureSaltedPasswords');
169 }
170
171 /**
172 * @test
173 */
174 public function noProxyAuthSchemeSetInLocalConfiguration() {
175 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
176 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
177 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
178 array('dummy'),
179 array(),
180 '',
181 FALSE
182 );
183
184 $closure = function ($param) {
185 throw new \RuntimeException('Path does not exist in array', 1341397869);
186 };
187
188 $this->createConfigurationManagerWithMockedMethods(
189 array(
190 'getLocalConfigurationValueByPath',
191 'removeLocalConfigurationKeysByPath',
192 )
193 );
194 $this->configurationManager->expects($this->exactly(1))
195 ->method('getLocalConfigurationValueByPath')
196 ->will($this->returnCallback($closure));
197 $this->configurationManager->expects($this->never())
198 ->method('removeLocalConfigurationKeysByPath');
199
200 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
201
202 $silentConfigurationUpgradeServiceInstance->_call('setProxyAuthScheme');
203 }
204
205 /**
206 * @test
207 */
208 public function proxyAuthSchemeIsDigest() {
209 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
210 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
211 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
212 array('dummy'),
213 array(),
214 '',
215 FALSE
216 );
217
218 $currentLocalConfiguration = array(
219 array('HTTP/proxy_auth_scheme', 'digest')
220 );
221
222 $this->createConfigurationManagerWithMockedMethods(
223 array(
224 'getLocalConfigurationValueByPath',
225 'removeLocalConfigurationKeysByPath',
226 )
227 );
228 $this->configurationManager->expects($this->exactly(1))
229 ->method('getLocalConfigurationValueByPath')
230 ->will($this->returnValueMap($currentLocalConfiguration));
231 $this->configurationManager->expects($this->never())
232 ->method('removeLocalConfigurationKeysByPath');
233
234 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
235
236 $silentConfigurationUpgradeServiceInstance->_call('setProxyAuthScheme');
237 }
238
239 /**
240 * @test
241 */
242 public function proxyAuthSchemeIsBasic() {
243 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
244 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
245 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
246 array('dummy'),
247 array(),
248 '',
249 FALSE
250 );
251
252 $currentLocalConfiguration = array(
253 array('HTTP/proxy_auth_scheme', 'basic')
254 );
255
256 $this->createConfigurationManagerWithMockedMethods(
257 array(
258 'getLocalConfigurationValueByPath',
259 'removeLocalConfigurationKeysByPath',
260 )
261 );
262 $this->configurationManager->expects($this->exactly(1))
263 ->method('getLocalConfigurationValueByPath')
264 ->will($this->returnValueMap($currentLocalConfiguration));
265 $this->configurationManager->expects($this->once())
266 ->method('removeLocalConfigurationKeysByPath')
267 ->with($this->equalTo(array('HTTP/proxy_auth_scheme')));
268
269 $this->setExpectedException(\TYPO3\CMS\Install\Controller\Exception\RedirectException::class);
270
271 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
272
273 $silentConfigurationUpgradeServiceInstance->_call('setProxyAuthScheme');
274 }
275
276 /**
277 * Dataprovider for transferDeprecatedCurlSettings
278 *
279 * @return array
280 */
281 public function curlProxySettingsToHttpSettingsMapping() {
282 return array(
283 array('http://proxy:3128/', 'proxy', '3128'),
284 array('http://proxy:3128', 'proxy', '3128'),
285 array('proxy:3128', 'proxy', '3128'),
286 array('https://proxy:3128/', 'proxy', '3128'),
287 );
288 }
289
290 /**
291 * @test
292 * @dataProvider curlProxySettingsToHttpSettingsMapping
293 */
294 public function transferDeprecatedCurlSettings($curlProxyServer, $proxyHost, $proxyPort) {
295 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
296 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
297 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
298 array('dummy'),
299 array(),
300 '',
301 FALSE
302 );
303
304 $currentLocalConfiguration = array(
305 array('SYS/curlProxyServer', $curlProxyServer),
306 array('HTTP/proxy_host', ''),
307 array('SYS/curlProxyUserPass', ''),
308 array('HTTP/proxy_user', ''),
309 array('SYS/curlUse', FALSE)
310 );
311 $this->createConfigurationManagerWithMockedMethods(
312 array(
313 'getLocalConfigurationValueByPath',
314 'setLocalConfigurationValueByPath',
315 'getConfigurationValueByPath'
316 )
317 );
318 $this->configurationManager->expects($this->exactly(5))
319 ->method('getLocalConfigurationValueByPath')
320 ->will($this->returnValueMap($currentLocalConfiguration));
321 $this->configurationManager->expects($this->exactly(2))
322 ->method('setLocalConfigurationValueByPath')
323 ->withConsecutive(
324 array('HTTP/proxy_host', $proxyHost),
325 array('HTTP/proxy_port', $proxyPort)
326 );
327
328 $this->setExpectedException(\TYPO3\CMS\Install\Controller\Exception\RedirectException::class);
329
330 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
331
332 $silentConfigurationUpgradeServiceInstance->_call('transferDeprecatedCurlSettings');
333 }
334
335 /**
336 * @test
337 */
338 public function curlProxyServerDoesNotOverwriteHttpSettings() {
339 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
340 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
341 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
342 array('dummy'),
343 array(),
344 '',
345 FALSE
346 );
347
348 $currentLocalConfiguration = array(
349 array('SYS/curlProxyServer', 'http://proxyOld:3128/'),
350 array('SYS/curlProxyUserPass', 'userOld:passOld'),
351 array('HTTP/proxy_host', 'proxyNew'),
352 array('HTTP/proxy_port', '3128'),
353 array('HTTP/proxy_user', 'userNew'),
354 array('HTTP/proxy_pass', 'passNew'),
355 array('SYS/curlUse', FALSE)
356 );
357 $this->createConfigurationManagerWithMockedMethods(
358 array(
359 'getLocalConfigurationValueByPath',
360 'setLocalConfigurationValueByPath',
361 'getConfigurationValueByPath'
362 )
363 );
364 $this->configurationManager->expects($this->exactly(5))
365 ->method('getLocalConfigurationValueByPath')
366 ->will($this->returnValueMap($currentLocalConfiguration));
367 $this->configurationManager->expects($this->never())
368 ->method('setLocalConfigurationValueByPath');
369
370 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
371
372 $silentConfigurationUpgradeServiceInstance->_call('transferDeprecatedCurlSettings');
373 }
374
375 /**
376 * @test
377 */
378 public function curlAdapterUsedIfCurlUse() {
379 /** @var $silentConfigurationUpgradeServiceInstance \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
380 $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
381 \TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService::class,
382 array('dummy'),
383 array(),
384 '',
385 FALSE
386 );
387
388 $currentLocalConfiguration = array(
389 array('SYS/curlProxyServer', ''),
390 array('SYS/curlProxyUserPass', ''),
391 array('HTTP/proxy_host', 'proxyNew'),
392 array('HTTP/proxy_user', 'userNew'),
393 array('SYS/curlUse', TRUE)
394 );
395 $this->createConfigurationManagerWithMockedMethods(
396 array(
397 'getLocalConfigurationValueByPath',
398 'getConfigurationValueByPath',
399 'setLocalConfigurationValueByPath',
400 )
401 );
402 $this->configurationManager->expects($this->exactly(5))
403 ->method('getLocalConfigurationValueByPath')
404 ->will($this->returnValueMap($currentLocalConfiguration));
405 $this->configurationManager->expects($this->once())
406 ->method('setLocalConfigurationValueByPath')
407 ->withConsecutive(
408 array('HTTP/adapter', 'curl')
409 );
410
411 $this->setExpectedException(\TYPO3\CMS\Install\Controller\Exception\RedirectException::class);
412
413 $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
414
415 $silentConfigurationUpgradeServiceInstance->_call('transferDeprecatedCurlSettings');
416 }
417
418 }