[TASK] Avoid ObjectManager in ext:form TranslationService
authorChristian Kuhn <lolli@schwarzbu.ch>
Thu, 10 Jun 2021 16:17:53 +0000 (18:17 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 10 Jun 2021 19:25:23 +0000 (21:25 +0200)
Extbase ConfigurationManager is already configured to
be injectable. Use this in TranslationService.

Resolves: #94303
Related: #90803
Releases: master
Change-Id: I6fa2a2e05a2c7b348b411142e8a44c2dad20c3f7
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69454
Tested-by: core-ci <typo3@b13.com>
Tested-by: Jochen <rothjochen@gmail.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Jochen <rothjochen@gmail.com>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/form/Classes/Service/TranslationService.php
typo3/sysext/form/Tests/Unit/Controller/FormEditorControllerTest.php
typo3/sysext/form/Tests/Unit/Controller/FormManagerControllerTest.php
typo3/sysext/form/Tests/Unit/Domain/Finishers/RedirectFinisherTest.php
typo3/sysext/form/Tests/Unit/Service/TranslationServiceTest.php

index 49cf758..adaa960 100644 (file)
@@ -28,7 +28,6 @@ use TYPO3\CMS\Core\Utility\ArrayUtility;
 use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
-use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
@@ -75,20 +74,24 @@ class TranslationService implements SingletonInterface
      */
     protected $alternativeLanguageKeys = [];
 
-    /**
-     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
-     */
-    protected $configurationManager;
+    protected ConfigurationManagerInterface $configurationManager;
+
+    public function __construct(ConfigurationManagerInterface $configurationManager)
+    {
+        $this->configurationManager = $configurationManager;
+    }
 
     /**
      * Return TranslationService as singleton
      *
-     * @return TranslationService
+     * @return self
      * @internal
      */
-    public static function getInstance()
+    public static function getInstance(): self
     {
-        return GeneralUtility::makeInstance(ObjectManager::class)->get(self::class);
+        /** @var self $instance */
+        $instance = GeneralUtility::makeInstance(self::class);
+        return $instance;
     }
 
     /**
@@ -615,7 +618,7 @@ class TranslationService implements SingletonInterface
      */
     protected function loadTypoScriptLabels()
     {
-        $frameworkConfiguration = $this->getConfigurationManager()
+        $frameworkConfiguration = $this->configurationManager
             ->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, 'form');
 
         if (!is_array($frameworkConfiguration['_LOCAL_LANG'] ?? null)) {
@@ -687,22 +690,6 @@ class TranslationService implements SingletonInterface
         return $array;
     }
 
-    /**
-     * Returns instance of the configuration manager
-     *
-     * @return ConfigurationManagerInterface
-     */
-    protected function getConfigurationManager(): ConfigurationManagerInterface
-    {
-        if ($this->configurationManager !== null) {
-            return $this->configurationManager;
-        }
-
-        $this->configurationManager = GeneralUtility::makeInstance(ObjectManager::class)
-            ->get(ConfigurationManagerInterface::class);
-        return $this->configurationManager;
-    }
-
     /**
      * Returns the currently configured "site language" if a site is configured (= resolved) in the current request.
      *
index 69fce6e..610cf59 100644 (file)
@@ -18,7 +18,6 @@ declare(strict_types=1);
 namespace TYPO3\CMS\Form\Tests\Unit\Controller;
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Form\Controller\FormEditorController;
 use TYPO3\CMS\Form\Domain\Exception\RenderingException;
 use TYPO3\CMS\Form\Service\TranslationService;
@@ -48,27 +47,15 @@ class FormEditorControllerTest extends UnitTestCase
      */
     public function getInsertRenderablesPanelConfigurationReturnsGroupedAndSortedConfiguration(): void
     {
-        $mockController = $this->getAccessibleMock(FormEditorController::class, [
-            'dummy'
-        ], [], '', false);
-
-        $objectManagerProphecy = $this->prophesize(ObjectManager::class);
-        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
-
-        $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
-            'translate'
-        ], [], '', false);
-
+        $mockTranslationService = $this->getAccessibleMock(TranslationService::class, ['translate'], [], '', false);
+        GeneralUtility::setSingletonInstance(TranslationService::class, $mockTranslationService);
         $mockTranslationService
             ->expects(self::any())
             ->method('translate')
             ->willReturnArgument(4);
 
-        $objectManagerProphecy
-            ->get(TranslationService::class)
-            ->willReturn($mockTranslationService);
-
-        $mockController->_set('prototypeConfiguration', [
+        $subject = $this->getAccessibleMock(FormEditorController::class, ['dummy'], [], '', false);
+        $subject->_set('prototypeConfiguration', [
             'formEditor' => [
                 'formElementGroups' => [
                     'input' => [
@@ -138,7 +125,7 @@ class FormEditorControllerTest extends UnitTestCase
             ],
         ];
 
-        self::assertSame($expected, $mockController->_call('getInsertRenderablesPanelConfiguration', $input));
+        self::assertSame($expected, $subject->_call('getInsertRenderablesPanelConfiguration', $input));
     }
 
     /**
@@ -146,27 +133,15 @@ class FormEditorControllerTest extends UnitTestCase
      */
     public function getFormEditorDefinitionsReturnReducedConfiguration(): void
     {
-        $mockController = $this->getAccessibleMock(FormEditorController::class, [
-            'dummy'
-        ], [], '', false);
-
-        $objectManagerProphecy = $this->prophesize(ObjectManager::class);
-        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
-
-        $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
-            'translateValuesRecursive'
-        ], [], '', false);
-
+        $mockTranslationService = $this->getAccessibleMock(TranslationService::class, ['translateValuesRecursive'], [], '', false);
+        GeneralUtility::setSingletonInstance(TranslationService::class, $mockTranslationService);
         $mockTranslationService
             ->expects(self::any())
             ->method('translateValuesRecursive')
             ->willReturnArgument(0);
 
-        $objectManagerProphecy
-            ->get(TranslationService::class)
-            ->willReturn($mockTranslationService);
-
-        $mockController->_set('prototypeConfiguration', [
+        $subject = $this->getAccessibleMock(FormEditorController::class, ['dummy'], [], '', false);
+        $subject->_set('prototypeConfiguration', [
             'formEditor' => [
                 'someOtherValues' => [
                     'horst' => [
@@ -272,7 +247,7 @@ class FormEditorControllerTest extends UnitTestCase
             ],
         ];
 
-        self::assertSame($expected, $mockController->_call('getFormEditorDefinitions'));
+        self::assertSame($expected, $subject->_call('getFormEditorDefinitions'));
     }
 
     /**
index d728547..9f33d36 100644 (file)
@@ -24,7 +24,6 @@ use TYPO3\CMS\Core\Resource\Folder;
 use TYPO3\CMS\Core\Resource\ResourceStorage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
-use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Form\Controller\FormManagerController;
 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
 use TYPO3\CMS\Form\Service\DatabaseService;
@@ -103,31 +102,19 @@ class FormManagerControllerTest extends UnitTestCase
      */
     public function getFormManagerAppInitialDataReturnsProcessedArray(): void
     {
-        $objectManagerProphecy = $this->prophesize(ObjectManager::class);
-        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
-
-        $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
-            'translateValuesRecursive'
-        ], [], '', false);
-
+        $mockTranslationService = $this->getAccessibleMock(TranslationService::class, ['translateValuesRecursive'], [], '', false);
+        GeneralUtility::setSingletonInstance(TranslationService::class, $mockTranslationService);
         $mockTranslationService
             ->expects(self::any())
             ->method('translateValuesRecursive')
             ->willReturnArgument(0);
 
-        $objectManagerProphecy
-            ->get(TranslationService::class)
-            ->willReturn($mockTranslationService);
-
-        $mockController = $this->getAccessibleMock(FormManagerController::class, [
-            'getAccessibleFormStorageFolders'
-        ], [], '', false);
+        $subject = $this->getAccessibleMock(FormManagerController::class, ['getAccessibleFormStorageFolders'], [], '', false);
 
         $mockUriBuilder = $this->createMock(UriBuilder::class);
 
-        $mockController->_set('uriBuilder', $mockUriBuilder);
-
-        $mockController->_set('formSettings', [
+        $subject->_set('uriBuilder', $mockUriBuilder);
+        $subject->_set('formSettings', [
             'formManager' => [
                 'selectablePrototypesConfiguration' => [],
             ],
@@ -137,7 +124,7 @@ class FormManagerControllerTest extends UnitTestCase
             '/typo3/index.php?some=param'
         );
 
-        $mockController
+        $subject
             ->expects(self::any())
             ->method('getAccessibleFormStorageFolders')
             ->willReturn([
@@ -163,7 +150,7 @@ class FormManagerControllerTest extends UnitTestCase
             ],
         ];
 
-        self::assertSame(json_encode($expected), $mockController->_call('getFormManagerAppInitialData'));
+        self::assertSame(json_encode($expected), $subject->_call('getFormManagerAppInitialData'));
     }
 
     /**
index 7b14ed9..a9f71d9 100644 (file)
@@ -73,11 +73,11 @@ class RedirectFinisherTest extends UnitTestCase
         $finisherContextProphecy->cancel()->shouldBeCalledOnce();
 
         $translationServiceProphecy = $this->prophesize(TranslationService::class);
+        GeneralUtility::setSingletonInstance(TranslationService::class, $translationServiceProphecy->reveal());
         $translationServiceProphecy->translateFinisherOption(Argument::cetera())->willReturnArgument(3);
 
         $objectManagerProphecy = $this->prophesize(ObjectManager::class);
         $objectManagerProphecy->get(UriBuilder::class)->willReturn(new UriBuilder());
-        $objectManagerProphecy->get(TranslationService::class)->willReturn($translationServiceProphecy->reveal());
         GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
         $redirectFinisherMock->injectObjectManager($objectManagerProphecy->reveal());
 
index 7c82015..7e72863 100644 (file)
@@ -64,7 +64,6 @@ class TranslationServiceTest extends UnitTestCase
     public function setUp(): void
     {
         parent::setUp();
-        $this->resetSingletonInstances = true;
         $cacheManagerProphecy = $this->prophesize(CacheManager::class);
         GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
         $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
@@ -94,9 +93,7 @@ class TranslationServiceTest extends UnitTestCase
 
         GeneralUtility::setSingletonInstance(LocalizationFactory::class, $localizationFactory->reveal());
 
-        $this->mockConfigurationManager = $this->getAccessibleMock(ConfigurationManager::class, [
-            'getConfiguration'
-        ], [], '', false);
+        $this->mockConfigurationManager = $this->getAccessibleMock(ConfigurationManager::class, ['getConfiguration'], [], '', false);
 
         $this->mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
             'getConfigurationManager',
@@ -109,10 +106,7 @@ class TranslationServiceTest extends UnitTestCase
             ->method('getLanguageService')
             ->willReturn($languageService);
 
-        $this->mockTranslationService
-            ->expects(self::any())
-            ->method('getConfigurationManager')
-            ->willReturn($this->mockConfigurationManager);
+        $this->mockTranslationService->_set('configurationManager', $this->mockConfigurationManager);
 
         $this->store = GeneralUtility::makeInstance(LanguageStore::class);
         $this->store->initialize();