TYPO3 CMS  TYPO3_8-7
SilentConfigurationUpgradeServiceTest.php
Go to the documentation of this file.
1 <?php
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 
22 
26 class SilentConfigurationUpgradeServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
32 
37 
41  protected function setUp()
42  {
44  }
45 
49  protected function tearDown()
50  {
52  parent::tearDown();
53  }
54 
58  protected function createConfigurationManagerWithMockedMethods(array $methods)
59  {
60  $this->configurationManager = $this->getMockBuilder(ConfigurationManager::class)
61  ->setMethods($methods)
62  ->getMock();
63  }
64 
71  {
72  return [
73  ['', 'rsa', true, false],
74  ['normal', 'rsa', true, true],
75  ['rsa', 'normal', false, true],
76  ];
77  }
78 
87  public function configureBackendLoginSecurity($current, $setting, $isPackageActive, $hasLocalConfig)
88  {
90  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
91  SilentConfigurationUpgradeService::class,
92  ['dummy'],
93  [],
94  '',
95  false
96  );
97 
99  $packageManager = $this->createMock(PackageManager::class);
100  $packageManager->expects($this->any())
101  ->method('isPackageActive')
102  ->will($this->returnValue($isPackageActive));
104 
105  $currentLocalConfiguration = [
106  ['BE/loginSecurityLevel', $current]
107  ];
108 
110  [
111  'getLocalConfigurationValueByPath',
112  'setLocalConfigurationValueByPath',
113  ]
114  );
115  if ($hasLocalConfig) {
116  $this->configurationManager->expects($this->once())
117  ->method('getLocalConfigurationValueByPath')
118  ->will($this->returnValueMap($currentLocalConfiguration));
119  } else {
120  $this->configurationManager->expects($this->once())
121  ->method('getLocalConfigurationValueByPath')
122  ->willThrowException(new \RuntimeException('testing', 1544278754));
123  }
124  $this->configurationManager->expects($this->once())
125  ->method('setLocalConfigurationValueByPath')
126  ->with($this->equalTo('BE/loginSecurityLevel'), $this->equalTo($setting));
127 
128  $this->expectException(RedirectException::class);
129 
130  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
131 
132  $silentConfigurationUpgradeServiceInstance->_call('configureBackendLoginSecurity');
133  }
134 
141  {
142  return [
143  ['', 'rsa', true, false],
144  ['normal', 'rsa', true, true],
145  ['rsa', 'normal', false, true],
146  ];
147  }
148 
157  public function configureFrontendLoginSecurity($current, $setting, $isPackageActive, $hasLocalConfig)
158  {
160  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
161  SilentConfigurationUpgradeService::class,
162  ['dummy'],
163  [],
164  '',
165  false
166  );
167 
169  $packageManager = $this->createMock(PackageManager::class);
170  $packageManager->expects($this->any())
171  ->method('isPackageActive')
172  ->will($this->returnValue($isPackageActive));
174 
175  $currentLocalConfiguration = [
176  ['FE/loginSecurityLevel', $current]
177  ];
178 
180  [
181  'getLocalConfigurationValueByPath',
182  'setLocalConfigurationValueByPath',
183  ]
184  );
185  if ($hasLocalConfig) {
186  $this->configurationManager->expects($this->once())
187  ->method('getLocalConfigurationValueByPath')
188  ->will($this->returnValueMap($currentLocalConfiguration));
189  } else {
190  $this->configurationManager->expects($this->once())
191  ->method('getLocalConfigurationValueByPath')
192  ->willThrowException(new \RuntimeException('testing', 1544278778));
193  }
194  if (!$isPackageActive) {
195  $this->configurationManager->expects($this->once())
196  ->method('setLocalConfigurationValueByPath')
197  ->with($this->equalTo('FE/loginSecurityLevel'), $this->equalTo($setting));
198  $this->expectException(RedirectException::class);
199  }
200 
201  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
202 
203  $silentConfigurationUpgradeServiceInstance->_call('configureFrontendLoginSecurity');
204  }
205 
209  public function removeObsoleteLocalConfigurationSettingsIfThereAreOldSettings()
210  {
212  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
213  SilentConfigurationUpgradeService::class,
214  ['dummy'],
215  [],
216  '',
217  false
218  );
219 
220  $obsoleteLocalConfigurationSettings = [
221  'SYS/form_enctype',
222  ];
223 
224  $currentLocalConfiguration = [
225  [$obsoleteLocalConfigurationSettings, true]
226  ];
228  [
229  'removeLocalConfigurationKeysByPath',
230  ]
231  );
232  $this->configurationManager->expects($this->exactly(1))
233  ->method('removeLocalConfigurationKeysByPath')
234  ->will($this->returnValueMap($currentLocalConfiguration));
235 
236  $this->expectException(RedirectException::class);
237 
238  $silentConfigurationUpgradeServiceInstance->_set('obsoleteLocalConfigurationSettings', $obsoleteLocalConfigurationSettings);
239  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
240 
241  $silentConfigurationUpgradeServiceInstance->_call('removeObsoleteLocalConfigurationSettings');
242  }
243 
247  public function doNotRemoveObsoleteLocalConfigurationSettingsIfThereAreNoOldSettings()
248  {
250  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
251  SilentConfigurationUpgradeService::class,
252  ['dummy'],
253  [],
254  '',
255  false
256  );
257 
258  $obsoleteLocalConfigurationSettings = [
259  'SYS/form_enctype',
260  ];
261 
262  $currentLocalConfiguration = [
263  [$obsoleteLocalConfigurationSettings, false]
264  ];
266  [
267  'removeLocalConfigurationKeysByPath',
268  ]
269  );
270  $this->configurationManager->expects($this->exactly(1))
271  ->method('removeLocalConfigurationKeysByPath')
272  ->will($this->returnValueMap($currentLocalConfiguration));
273 
274  $silentConfigurationUpgradeServiceInstance->_set('obsoleteLocalConfigurationSettings', $obsoleteLocalConfigurationSettings);
275  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
276 
277  $silentConfigurationUpgradeServiceInstance->_call('removeObsoleteLocalConfigurationSettings');
278  }
279 
283  public function doNotGenerateEncryptionKeyIfExists()
284  {
286  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
287  SilentConfigurationUpgradeService::class,
288  ['dummy'],
289  [],
290  '',
291  false
292  );
293 
294  $currentLocalConfiguration = [
295  ['SYS/encryptionKey', 'EnCrYpTiOnKeY']
296  ];
297 
299  [
300  'getLocalConfigurationValueByPath',
301  'setLocalConfigurationValueByPath',
302  ]
303  );
304  $this->configurationManager->expects($this->exactly(1))
305  ->method('getLocalConfigurationValueByPath')
306  ->will($this->returnValueMap($currentLocalConfiguration));
307  $this->configurationManager->expects($this->never())
308  ->method('setLocalConfigurationValueByPath');
309 
310  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
311 
312  $silentConfigurationUpgradeServiceInstance->_call('generateEncryptionKeyIfNeeded');
313  }
314 
318  public function generateEncryptionKeyIfNotExists()
319  {
321  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
322  SilentConfigurationUpgradeService::class,
323  ['dummy'],
324  [],
325  '',
326  false
327  );
328 
329  $closure = function () {
330  throw new \RuntimeException('Path does not exist in array', 1476109266);
331  };
332 
334  [
335  'getLocalConfigurationValueByPath',
336  'setLocalConfigurationValueByPath',
337  ]
338  );
339  $this->configurationManager->expects($this->exactly(1))
340  ->method('getLocalConfigurationValueByPath')
341  ->will($this->returnCallback($closure));
342  $this->configurationManager->expects($this->once())
343  ->method('setLocalConfigurationValueByPath')
344  ->with($this->equalTo('SYS/encryptionKey'), $this->isType('string'));
345 
346  $this->expectException(RedirectException::class);
347 
348  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
349 
350  $silentConfigurationUpgradeServiceInstance->_call('generateEncryptionKeyIfNeeded');
351  }
352 
359  {
360  return [
361  'No changes overridden in Local Configuration' => [
362  ['timeout' => 100],
363  ['HTTP/timeout' => 100],
364  false
365  ],
366  'Old and unused settings removed' => [
367  ['adapter' => 'curl'],
368  [],
369  true
370  ],
371  'Old and used settings changed' => [
372  ['protocol_version' => '1.1'],
373  ['HTTP/version' => '1.1'],
374  true
375  ],
376 
378  'Redirects moved to default' => [
379  ['follow_redirects' => true],
380  [],
381  true
382  ],
383  'Redirects moved #1' => [
384  ['follow_redirects' => true, 'max_redirects' => 200, 'strict_redirects' => false],
385  ['HTTP/allow_redirects' => ['max' => 200]],
386  true
387  ],
388  'Redirects moved #2' => [
389  ['follow_redirects' => false, 'max_redirects' => 200, 'strict_redirects' => false],
390  ['HTTP/allow_redirects' => false],
391  true
392  ],
393  'Redirects moved #3' => [
394  ['follow_redirects' => true, 'max_redirects' => 400, 'strict_redirects' => 1],
395  ['HTTP/allow_redirects' => ['max' => 400, 'strict' => true]],
396  true
397  ],
398 
400  'Proxy host set' => [
401  ['proxy_host' => 'vpn.myproxy.com'],
402  ['HTTP/proxy' => 'http://vpn.myproxy.com'],
403  true
404  ],
405  'Proxy host set + port' => [
406  ['proxy_host' => 'vpn.myproxy.com', 'proxy_port' => 8080],
407  ['HTTP/proxy' => 'http://vpn.myproxy.com:8080'],
408  true
409  ],
410  'Proxy host set + port + verification' => [
411  ['proxy_host' => 'vpn.myproxy.com', 'proxy_port' => 8080, 'proxy_auth_scheme' => 'basic', 'proxy_user' => 'myuser', 'proxy_password' => 'mysecret'],
412  ['HTTP/proxy' => 'http://myuser:mysecret@vpn.myproxy.com:8080'],
413  true
414  ],
415 
417  'Only ssl_capath set, invalid migration' => [
418  ['ssl_capath' => '/foo/bar/'],
419  [],
420  true
421  ],
422  'Verification activated, but only ssl_capath set, using default' => [
423  ['ssl_verify_peer' => 1, 'ssl_capath' => '/foo/bar/'],
424  [],
425  true
426  ],
427  'Verification activated, with ssl_capath and ssl_cafile set' => [
428  ['ssl_verify_peer' => 1, 'ssl_capath' => '/foo/bar/', 'ssl_cafile' => 'supersecret.crt'],
429  ['HTTP/verify' => '/foo/bar/supersecret.crt'],
430  true
431  ],
432 
434  'SSL key certification' => [
435  ['ssl_local_cert' => '/foo/bar/supersecret.key'],
436  ['HTTP/ssl_key' => '/foo/bar/supersecret.key'],
437  true
438  ],
439  'SSL key certification + passphrase' => [
440  ['ssl_local_cert' => '/foo/bar/supersecret.key', 'ssl_passphrase' => 'donotcopypasteme'],
441  ['HTTP/ssl_key' => ['/foo/bar/supersecret.key', 'donotcopypasteme']],
442  true
443  ],
444  'SSL key passphrase only - no migration' => [
445  ['ssl_passphrase' => 'donotcopypasteme'],
446  [],
447  true
448  ],
449  ];
450  }
451 
459  public function transferHttpSettingsIfSet($currentLocalConfiguration, $newSettings, $localConfigurationNeedsUpdate)
460  {
462  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
463  SilentConfigurationUpgradeService::class,
464  ['dummy'],
465  [],
466  '',
467  false
468  );
469 
471  [
472  'setLocalConfigurationValuesByPathValuePairs',
473  'removeLocalConfigurationKeysByPath',
474  'getLocalConfiguration'
475  ]
476  );
477 
478  $this->configurationManager->expects($this->any())
479  ->method('getLocalConfiguration')
480  ->willReturn(['HTTP' => $currentLocalConfiguration]);
481  if ($localConfigurationNeedsUpdate) {
482  if (!empty($newSettings)) {
483  $this->configurationManager->expects($this->once())
484  ->method('setLocalConfigurationValuesByPathValuePairs')
485  ->with($newSettings);
486  }
487  $this->configurationManager->expects($this->atMost(1))->method('removeLocalConfigurationKeysByPath');
488  }
489 
490  if ($localConfigurationNeedsUpdate) {
491  $this->expectException(RedirectException::class);
492  }
493 
494  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
495 
496  $silentConfigurationUpgradeServiceInstance->_call('transferHttpSettings');
497  }
498 
502  public function disableImageMagickDetailSettingsIfImageMagickIsDisabled()
503  {
505  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
506  SilentConfigurationUpgradeService::class,
507  ['dummy'],
508  [],
509  '',
510  false
511  );
512 
513  $currentLocalConfiguration = [
514  ['GFX/im', 0],
515  ['GFX/im_path', ''],
516  ['GFX/im_path_lzw', ''],
517  ['GFX/imagefile_ext', 'gif,jpg,png'],
518  ['GFX/thumbnails', 0]
519  ];
521  [
522  'getLocalConfigurationValueByPath',
523  'getDefaultConfigurationValueByPath',
524  'setLocalConfigurationValuesByPathValuePairs',
525  ]
526  );
527  $this->configurationManager->expects($this->exactly(5))
528  ->method('getLocalConfigurationValueByPath')
529  ->will($this->returnValueMap($currentLocalConfiguration));
530  $this->configurationManager->expects($this->never())
531  ->method('getDefaultConfigurationValueByPath');
532  $this->configurationManager->expects($this->once())
533  ->method('setLocalConfigurationValuesByPathValuePairs')
534  ->withConsecutive(
535  [['GFX/imagefile_ext' => 'gif,jpg,jpeg,png']]
536  );
537 
538  $this->expectException(RedirectException::class);
539 
540  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
541 
542  $silentConfigurationUpgradeServiceInstance->_call('disableImageMagickDetailSettingsIfImageMagickIsDisabled');
543  }
544 
548  public function doNotDisableImageMagickDetailSettingsIfImageMagickIsEnabled()
549  {
551  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
552  SilentConfigurationUpgradeService::class,
553  ['dummy'],
554  [],
555  '',
556  false
557  );
558 
559  $currentLocalConfiguration = [
560  ['GFX/im', 1],
561  ['GFX/im_path', ''],
562  ['GFX/im_path_lzw', ''],
563  ['GFX/imagefile_ext', 'gif,jpg,jpeg,png'],
564  ['GFX/thumbnails', 0]
565  ];
567  [
568  'getLocalConfigurationValueByPath',
569  'getDefaultConfigurationValueByPath',
570  'setLocalConfigurationValuesByPathValuePairs',
571  ]
572  );
573  $this->configurationManager->expects($this->exactly(5))
574  ->method('getLocalConfigurationValueByPath')
575  ->will($this->returnValueMap($currentLocalConfiguration));
576  $this->configurationManager->expects($this->never())
577  ->method('getDefaultConfigurationValueByPath');
578  $this->configurationManager->expects($this->never())
579  ->method('setLocalConfigurationValuesByPathValuePairs');
580 
581  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
582 
583  $silentConfigurationUpgradeServiceInstance->_call('disableImageMagickDetailSettingsIfImageMagickIsDisabled');
584  }
585 
589  public function setImageMagickDetailSettings()
590  {
592  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
593  SilentConfigurationUpgradeService::class,
594  ['dummy'],
595  [],
596  '',
597  false
598  );
599 
600  $currentLocalConfiguration = [
601  ['GFX/processor', 'GraphicsMagick'],
602  ['GFX/processor_allowTemporaryMasksAsPng', 1],
603  ];
605  [
606  'getLocalConfigurationValueByPath',
607  'getDefaultConfigurationValueByPath',
608  'setLocalConfigurationValuesByPathValuePairs',
609  ]
610  );
611  $this->configurationManager->expects($this->exactly(2))
612  ->method('getLocalConfigurationValueByPath')
613  ->will($this->returnValueMap($currentLocalConfiguration));
614  $this->configurationManager->expects($this->never())
615  ->method('getDefaultConfigurationValueByPath');
616  $this->configurationManager->expects($this->once())
617  ->method('setLocalConfigurationValuesByPathValuePairs')
618  ->withConsecutive(
619  [['GFX/processor_allowTemporaryMasksAsPng' => 0]]
620  );
621 
622  $this->expectException(RedirectException::class);
623 
624  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
625 
626  $silentConfigurationUpgradeServiceInstance->_call('setImageMagickDetailSettings');
627  }
628 
632  public function doNotSetImageMagickDetailSettings()
633  {
635  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
636  SilentConfigurationUpgradeService::class,
637  ['dummy'],
638  [],
639  '',
640  false
641  );
642 
643  $currentLocalConfiguration = [
644  ['GFX/processor', ''],
645  ['GFX/processor_allowTemporaryMasksAsPng', 0],
646  ];
648  [
649  'getLocalConfigurationValueByPath',
650  'getDefaultConfigurationValueByPath',
651  'setLocalConfigurationValuesByPathValuePairs',
652  ]
653  );
654  $this->configurationManager->expects($this->exactly(2))
655  ->method('getLocalConfigurationValueByPath')
656  ->will($this->returnValueMap($currentLocalConfiguration));
657  $this->configurationManager->expects($this->never())
658  ->method('getDefaultConfigurationValueByPath');
659  $this->configurationManager->expects($this->never())
660  ->method('setLocalConfigurationValuesByPathValuePairs');
661 
662  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
663 
664  $silentConfigurationUpgradeServiceInstance->_call('setImageMagickDetailSettings');
665  }
666 
670  public function migrateNonExistingLangDebug()
671  {
673  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
674  SilentConfigurationUpgradeService::class,
675  ['dummy'],
676  [],
677  '',
678  false
679  );
680 
681  $currentLocalConfiguration = [
682  ];
684  [
685  'getLocalConfigurationValueByPath',
686  'setLocalConfigurationValueByPath',
687  ]
688  );
689 
690  $this->configurationManager->expects($this->exactly(1))
691  ->method('getLocalConfigurationValueByPath')
692  ->will($this->returnValueMap($currentLocalConfiguration));
693  $this->configurationManager->expects($this->never())
694  ->method('setLocalConfigurationValueByPath');
695 
696  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
697 
698  $silentConfigurationUpgradeServiceInstance->_call('migrateLangDebug');
699  }
700 
704  public function migrateExistingLangDebug()
705  {
707  $silentConfigurationUpgradeServiceInstance = $this->getAccessibleMock(
708  SilentConfigurationUpgradeService::class,
709  ['dummy'],
710  [],
711  '',
712  false
713  );
714 
715  $currentLocalConfiguration = [
716  ['BE/lang/debug', false]
717  ];
719  [
720  'getLocalConfigurationValueByPath',
721  'setLocalConfigurationValueByPath',
722  ]
723  );
724 
725  $this->configurationManager->expects($this->exactly(1))
726  ->method('getLocalConfigurationValueByPath')
727  ->will($this->returnValueMap($currentLocalConfiguration));
728  $this->configurationManager->expects($this->once())
729  ->method('setLocalConfigurationValueByPath')
730  ->with($this->equalTo('BE/languageDebug'), false);
731 
732  $silentConfigurationUpgradeServiceInstance->_set('configurationManager', $this->configurationManager);
733 
734  $silentConfigurationUpgradeServiceInstance->_call('migrateLangDebug');
735  }
736 }
static setPackageManager(PackageManager $packageManager)