From d7a765130ac17be0497e1db86126007c9b78a369 Mon Sep 17 00:00:00 2001 From: Christian Kuhn <lolli@schwarzbu.ch> Date: Tue, 11 Oct 2022 15:46:18 +0200 Subject: [PATCH] [TASK] Clean up BackendConfigurationManager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A first code clean up of this class streamlines the properties and removes some rather obvious stuff. The unit tests receive an overhaul. Resolves: #98581 Related: #98578 Releases: main Change-Id: I9a6db5015c976986fde24f721e5c2fd8cc1d3f04 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76090 Tested-by: core-ci <typo3@b13.com> Tested-by: Stefan Bürk <stefan@buerk.tech> Tested-by: Georg Ringer <georg.ringer@gmail.com> Reviewed-by: Stefan Bürk <stefan@buerk.tech> Reviewed-by: Georg Ringer <georg.ringer@gmail.com> --- Build/phpstan/phpstan-baseline.neon | 10 - .../BackendConfigurationManager.php | 89 +--- .../BackendConfigurationManagerTest.php | 25 +- .../BackendConfigurationManagerTest.php | 389 ++++-------------- 4 files changed, 104 insertions(+), 409 deletions(-) diff --git a/Build/phpstan/phpstan-baseline.neon b/Build/phpstan/phpstan-baseline.neon index f23f8a0fc4e5..54e88e98b881 100644 --- a/Build/phpstan/phpstan-baseline.neon +++ b/Build/phpstan/phpstan-baseline.neon @@ -1805,11 +1805,6 @@ parameters: count: 1 path: ../../typo3/sysext/dashboard/Classes/Controller/WidgetAjaxController.php - - - message: "#^Unreachable statement \\- code above always terminates\\.$#" - count: 1 - path: ../../typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php - - message: "#^Unreachable statement \\- code above always terminates\\.$#" count: 1 @@ -2095,11 +2090,6 @@ parameters: count: 1 path: ../../typo3/sysext/extbase/Tests/Functional/Property/TypeConverter/PersistentObjectConverterTest.php - - - message: "#^Property TYPO3\\\\CMS\\\\Extbase\\\\Tests\\\\Unit\\\\Configuration\\\\BackendConfigurationManagerTest\\:\\:\\$mockTypoScriptService \\(PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&TYPO3\\\\CMS\\\\Core\\\\TypoScript\\\\TypoScriptService&TYPO3\\\\TestingFramework\\\\Core\\\\AccessibleObjectInterface\\) does not accept PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&TYPO3\\\\CMS\\\\Core\\\\TypoScript\\\\TypoScriptService\\.$#" - count: 1 - path: ../../typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php - - message: "#^Property TYPO3\\\\CMS\\\\Extbase\\\\Tests\\\\Unit\\\\Configuration\\\\FrontendConfigurationManagerTest\\:\\:\\$mockTypoScriptService \\(PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&TYPO3\\\\CMS\\\\Core\\\\TypoScript\\\\TypoScriptService&TYPO3\\\\TestingFramework\\\\Core\\\\AccessibleObjectInterface\\) does not accept PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&TYPO3\\\\CMS\\\\Core\\\\TypoScript\\\\TypoScriptService\\.$#" count: 1 diff --git a/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php b/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php index 3a8f1561528e..bc5ca9ad505c 100644 --- a/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php +++ b/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php @@ -17,13 +17,11 @@ declare(strict_types=1); namespace TYPO3\CMS\Extbase\Configuration; -use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryHelper; use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction; -use TYPO3\CMS\Core\Http\ApplicationType; use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\Type\Bitmask\Permission; use TYPO3\CMS\Core\TypoScript\TemplateService; @@ -47,53 +45,36 @@ class BackendConfigurationManager implements SingletonInterface /** * Storage of the raw TypoScript configuration - * - * @var array - */ - protected $configuration = []; - - /** - * @var ContentObjectRenderer */ - protected $contentObject; + protected array $configuration = []; - protected TypoScriptService $typoScriptService; + protected ?ContentObjectRenderer $contentObject = null; /** - * name of the extension this Configuration Manager instance belongs to - * - * @var string + * Name of the extension this Configuration Manager instance belongs to */ - protected $extensionName; + protected ?string $extensionName = null; /** - * name of the plugin this Configuration Manager instance belongs to - * - * @var string + * Name of the plugin this Configuration Manager instance belongs to */ - protected $pluginName; + protected ?string $pluginName = null; /** * 1st level configuration cache - * - * @var array */ - protected $configurationCache = []; + protected array $configurationCache = []; - /** - * @var array - */ - protected $typoScriptSetupCache = []; + protected array $typoScriptSetupCache = []; /** - * stores the current page ID - * @var int + * Stores the current page ID */ - protected $currentPageId; + protected ?int $currentPageId = null; - public function __construct(TypoScriptService $typoScriptService) - { - $this->typoScriptService = $typoScriptService; + public function __construct( + private readonly TypoScriptService $typoScriptService + ) { } /** @@ -163,34 +144,23 @@ class BackendConfigurationManager implements SingletonInterface if ($extensionName === null || $extensionName === $this->extensionName && $pluginName === $this->pluginName) { $pluginConfiguration = $this->getPluginConfiguration((string)$this->extensionName, (string)$this->pluginName); ArrayUtility::mergeRecursiveWithOverrule($pluginConfiguration, $this->configuration); - $pluginConfiguration['controllerConfiguration'] = $this->getControllerConfiguration((string)$this->extensionName, (string)$this->pluginName); + $pluginConfiguration['controllerConfiguration'] = []; } else { $pluginConfiguration = $this->getPluginConfiguration((string)$extensionName, (string)$pluginName); - $pluginConfiguration['controllerConfiguration'] = $this->getControllerConfiguration((string)$extensionName, (string)$pluginName); + $pluginConfiguration['controllerConfiguration'] = []; } ArrayUtility::mergeRecursiveWithOverrule($frameworkConfiguration, $pluginConfiguration); - // only load context specific configuration when retrieving configuration of the current plugin - if ($extensionName === null || $extensionName === $this->extensionName && $pluginName === $this->pluginName) { - $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration); - } if (!empty($frameworkConfiguration['persistence']['storagePid'])) { if (is_array($frameworkConfiguration['persistence']['storagePid'])) { // We simulate the frontend to enable the use of cObjects in // stdWrap. We then convert the configuration to normal TypoScript // and apply the stdWrap to the storagePid - $isBackend = ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface - && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend(); - if ($isBackend) { - // @todo: This BE specific switch should be moved to BackendConfigurationManager to drop the dependency to $GLOBALS['TYPO3_REQUEST'] here. - // Use makeInstance here since extbase Bootstrap always setContentObject(null) in Backend, no need to call getContentObject(). - FrontendSimulatorUtility::simulateFrontendEnvironment(GeneralUtility::makeInstance(ContentObjectRenderer::class)); - } + // Use makeInstance here since extbase Bootstrap always setContentObject(null) in Backend, no need to call getContentObject(). + FrontendSimulatorUtility::simulateFrontendEnvironment(GeneralUtility::makeInstance(ContentObjectRenderer::class)); $conf = $this->typoScriptService->convertPlainArrayToTypoScriptArray($frameworkConfiguration['persistence']); $frameworkConfiguration['persistence']['storagePid'] = $GLOBALS['TSFE']->cObj->stdWrapValue('storagePid', $conf); - if ($isBackend) { - FrontendSimulatorUtility::resetFrontendEnvironment(); - } + FrontendSimulatorUtility::resetFrontendEnvironment(); } if (!empty($frameworkConfiguration['persistence']['recursive'])) { @@ -208,8 +178,6 @@ class BackendConfigurationManager implements SingletonInterface /** * Returns the TypoScript configuration found in config.tx_extbase - * - * @return array */ protected function getExtbaseConfiguration(): array { @@ -278,14 +246,6 @@ class BackendConfigurationManager implements SingletonInterface return $pluginConfiguration; } - /** - * Not in use any more, as TYPO3 Backend Route Registration takes care of this. See ModuleRegistry. - */ - protected function getControllerConfiguration(string $extensionName, string $pluginName): array - { - return []; - } - /** * Returns the page uid of the current page. * If no page is selected, we'll return the uid of the first root page. @@ -396,19 +356,6 @@ class BackendConfigurationManager implements SingletonInterface return $this->getCurrentPageId(); } - /** - * We need to set some default request handler if the framework configuration - * could not be loaded; to make sure Extbase also works in Backend modules - * in all contexts. - * - * @param array $frameworkConfiguration - * @return array - */ - protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration): array - { - return $frameworkConfiguration; - } - /** * Returns an array of storagePIDs that are below a list of storage pids. * diff --git a/typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php b/typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php index ef3053db5c21..bdce0f16c6b4 100644 --- a/typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php +++ b/typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php @@ -26,16 +26,11 @@ use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; class BackendConfigurationManagerTest extends FunctionalTestCase { /** - * Warning: white box test - * * @test */ public function getCurrentPageIdReturnsPidFromFirstRootTemplateIfIdIsNotSetAndNoRootPageWasFound(): void { - $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, ['getTypoScriptSetup'], [], '', false); - $mockTypoScriptService = $this->getMockBuilder(TypoScriptService::class)->getMock(); - $backendConfigurationManager->_set('typoScriptService', $mockTypoScriptService); - + $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, null, [new TypoScriptService()]); (new ConnectionPool())->getConnectionForTable('sys_template')->insert( 'sys_template', [ @@ -45,22 +40,16 @@ class BackendConfigurationManagerTest extends FunctionalTestCase 'root' => 1, ] ); - $actualResult = $backendConfigurationManager->_call('getCurrentPageId'); self::assertEquals(123, $actualResult); } /** - * Warning: white box test - * * @test */ public function getCurrentPageIdReturnsUidFromFirstRootPageIfIdIsNotSet(): void { - $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, ['getTypoScriptSetup'], [], '', false); - $mockTypoScriptService = $this->getMockBuilder(TypoScriptService::class)->getMock(); - $backendConfigurationManager->_set('typoScriptService', $mockTypoScriptService); - + $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, null, [new TypoScriptService()]); (new ConnectionPool())->getConnectionForTable('pages')->insert( 'pages', [ @@ -69,22 +58,16 @@ class BackendConfigurationManagerTest extends FunctionalTestCase 'is_siteroot' => 1, ] ); - $actualResult = $backendConfigurationManager->_call('getCurrentPageId'); self::assertEquals(1, $actualResult); } /** - * Warning: white box test - * * @test */ public function getCurrentPageIdReturnsDefaultStoragePidIfIdIsNotSetNoRootTemplateAndRootPageWasFound(): void { - $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, ['getTypoScriptSetup'], [], '', false); - $mockTypoScriptService = $this->getMockBuilder(TypoScriptService::class)->getMock(); - $backendConfigurationManager->_set('typoScriptService', $mockTypoScriptService); - + $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, null, [new TypoScriptService()]); $expectedResult = BackendConfigurationManager::DEFAULT_BACKEND_STORAGE_PID; $actualResult = $backendConfigurationManager->_call('getCurrentPageId'); self::assertEquals($expectedResult, $actualResult); @@ -98,7 +81,7 @@ class BackendConfigurationManagerTest extends FunctionalTestCase $this->importCSVDataSet(__DIR__ . '/Fixtures/BackendConfigurationManagerRecursivePids.csv'); $GLOBALS['BE_USER'] = new BackendUserAuthentication(); $GLOBALS['BE_USER']->user = ['admin' => true]; - $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, null, [], '', false); + $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, null, [new TypoScriptService()]); $expectedResult = [1, 2, 4, 5, 3, 6, 7]; $actualResult = $backendConfigurationManager->_call('getRecursiveStoragePids', [1, -6], 4); self::assertEquals($expectedResult, $actualResult); diff --git a/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php b/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php index 373b016d6d40..ed574cdcfd58 100644 --- a/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php +++ b/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php @@ -17,28 +17,16 @@ declare(strict_types=1); namespace TYPO3\CMS\Extbase\Tests\Unit\Configuration; -use PHPUnit\Framework\MockObject\MockObject; use TYPO3\CMS\Core\TypoScript\TypoScriptService; use TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager; use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; -use TYPO3\TestingFramework\Core\AccessibleObjectInterface; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; class BackendConfigurationManagerTest extends UnitTestCase { protected bool $resetSingletonInstances = true; - /** - * @var BackendConfigurationManager|MockObject|AccessibleObjectInterface - */ - protected $backendConfigurationManager; - - /** - * @var TypoScriptService|MockObject|AccessibleObjectInterface - */ - protected $mockTypoScriptService; - - protected array $testTypoScriptSetup = [ + private array $testTypoScriptSetup = [ 'foo.' => [ 'bar' => 'baz', ], @@ -58,27 +46,7 @@ class BackendConfigurationManagerTest extends UnitTestCase ], ]; - protected array $testTypoScriptSetupConverted = [ - 'foo' => [ - 'bar' => 'baz', - ], - 'config' => [ - 'tx_extbase' => [ - 'settings' => [ - 'setting1' => 'value1', - 'setting2' => 'value2', - ], - 'view' => [ - 'viewSub' => [ - 'key1' => 'value1', - 'key2' => 'value2', - ], - ], - ], - ], - ]; - - protected array $testPluginConfiguration = [ + private array $testPluginConfiguration = [ 'settings' => [ 'setting1' => 'overriddenValue1', 'setting3' => 'additionalValue', @@ -94,31 +62,15 @@ class BackendConfigurationManagerTest extends UnitTestCase ], ]; - /** - * Sets up this testcase - */ - protected function setUp(): void - { - parent::setUp(); - $this->backendConfigurationManager = $this->getAccessibleMock( - BackendConfigurationManager::class, - ['getTypoScriptSetup'], - [], - '', - false - ); - $this->mockTypoScriptService = $this->getMockBuilder(TypoScriptService::class)->getMock(); - $this->backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - } - /** * @test */ public function setConfigurationResetsConfigurationCache(): void { - $this->backendConfigurationManager->_set('configurationCache', ['foo' => 'bar']); - $this->backendConfigurationManager->setConfiguration([]); - self::assertEquals([], $this->backendConfigurationManager->_get('configurationCache')); + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, null, [new TypoScriptService()], '', true); + $subject->_set('configurationCache', ['foo' => 'bar']); + $subject->setConfiguration([]); + self::assertEquals([], $subject->_get('configurationCache')); } /** @@ -126,13 +78,13 @@ class BackendConfigurationManagerTest extends UnitTestCase */ public function setConfigurationSetsExtensionAndPluginName(): void { - $configuration = [ + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, null, [new TypoScriptService()], '', true); + $subject->setConfiguration([ 'extensionName' => 'SomeExtensionName', 'pluginName' => 'SomePluginName', - ]; - $this->backendConfigurationManager->setConfiguration($configuration); - self::assertEquals('SomeExtensionName', $this->backendConfigurationManager->_get('extensionName')); - self::assertEquals('SomePluginName', $this->backendConfigurationManager->_get('pluginName')); + ]); + self::assertEquals('SomeExtensionName', $subject->_get('extensionName')); + self::assertEquals('SomePluginName', $subject->_get('pluginName')); } /** @@ -150,9 +102,9 @@ class BackendConfigurationManagerTest extends UnitTestCase 'settings' => ['foo' => 'bar'], 'view' => ['subkey' => ['subsubkey' => 'subsubvalue']], ]; - $this->mockTypoScriptService->expects(self::atLeastOnce())->method('convertTypoScriptArrayToPlainArray')->with($configuration)->willReturn($expectedResult); - $this->backendConfigurationManager->setConfiguration($configuration); - self::assertEquals($expectedResult, $this->backendConfigurationManager->_get('configuration')); + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, null, [new TypoScriptService()], '', true); + $subject->setConfiguration($configuration); + self::assertEquals($expectedResult, $subject->_get('configuration')); } /** @@ -160,14 +112,15 @@ class BackendConfigurationManagerTest extends UnitTestCase */ public function getConfigurationReturnsCachedResultOfCurrentPlugin(): void { - $this->backendConfigurationManager->_set('extensionName', 'CurrentExtensionName'); - $this->backendConfigurationManager->_set('pluginName', 'CurrentPluginName'); - $this->backendConfigurationManager->_set('configurationCache', [ + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, null, [], '', false); + $subject->_set('extensionName', 'CurrentExtensionName'); + $subject->_set('pluginName', 'CurrentPluginName'); + $subject->_set('configurationCache', [ 'currentextensionname_currentpluginname' => ['foo' => 'bar'], 'someotherextension_somepluginname' => ['baz' => 'shouldnotbereturned'], ]); $expectedResult = ['foo' => 'bar']; - $actualResult = $this->backendConfigurationManager->getConfiguration(); + $actualResult = $subject->getConfiguration(); self::assertEquals($expectedResult, $actualResult); } @@ -176,12 +129,13 @@ class BackendConfigurationManagerTest extends UnitTestCase */ public function getConfigurationReturnsCachedResultForGivenExtension(): void { - $this->backendConfigurationManager->_set('configurationCache', [ + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, null, [], '', false); + $subject->_set('configurationCache', [ 'someextensionname_somepluginname' => ['foo' => 'bar'], 'someotherextension_somepluginname' => ['baz' => 'shouldnotbereturned'], ]); $expectedResult = ['foo' => 'bar']; - $actualResult = $this->backendConfigurationManager->getConfiguration('SomeExtensionName', 'SomePluginName'); + $actualResult = $subject->getConfiguration('SomeExtensionName', 'SomePluginName'); self::assertEquals($expectedResult, $actualResult); } @@ -190,80 +144,21 @@ class BackendConfigurationManagerTest extends UnitTestCase */ public function getConfigurationRecursivelyMergesCurrentPluginConfigurationWithFrameworkConfiguration(): void { - $backendConfigurationManager = $this->getAccessibleMock( - BackendConfigurationManager::class, - [ - 'getContextSpecificFrameworkConfiguration', - 'getTypoScriptSetup', - 'getPluginConfiguration', - 'getControllerConfiguration', - 'getRecursiveStoragePids', - 'getDefaultBackendStoragePid', - ], - [], - '', - false - ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - $backendConfigurationManager->_set('extensionName', 'CurrentExtensionName'); - $backendConfigurationManager->_set('pluginName', 'CurrentPluginName'); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); - $backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn($this->testTypoScriptSetup); - $this->mockTypoScriptService->expects(self::atLeastOnce())->method('convertTypoScriptArrayToPlainArray')->with($this->testTypoScriptSetup['config.']['tx_extbase.'])->willReturn($this->testTypoScriptSetupConverted['config']['tx_extbase']); - $backendConfigurationManager->expects(self::once())->method('getPluginConfiguration')->with( - 'CurrentExtensionName', - 'CurrentPluginName' - )->willReturn($this->testPluginConfiguration); - $expectedResult = [ - 'settings' => [ - 'setting1' => 'overriddenValue1', - 'setting2' => 'value2', - 'setting3' => 'additionalValue', - ], - 'view' => [ - 'viewSub' => [ - 'key1' => 'overridden', - 'key2' => 'value2', - 'key3' => 'new key', - ], - ], - 'persistence' => [ - 'storagePid' => '123', - ], - 'controllerConfiguration' => [], - ]; - $backendConfigurationManager->expects(self::once())->method('getContextSpecificFrameworkConfiguration')->with($expectedResult)->willReturn($expectedResult); - $actualResult = $backendConfigurationManager->getConfiguration(); - self::assertEquals($expectedResult, $actualResult); - } - - /** - * @test - */ - public function getConfigurationRecursivelyMergesPluginConfigurationOfSpecifiedPluginWithFrameworkConfiguration(): void - { - $backendConfigurationManager = $this->getAccessibleMock( + $subject = $this->getAccessibleMock( BackendConfigurationManager::class, [ - 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', - 'getControllerConfiguration', - 'getRecursiveStoragePids', 'getDefaultBackendStoragePid', ], - [], + [new TypoScriptService()], '', - false + true ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); - $backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn($this->testTypoScriptSetup); - $backendConfigurationManager->expects(self::once())->method('getPluginConfiguration')->with( - 'SomeExtensionName', - 'SomePluginName' - )->willReturn($this->testPluginConfiguration); - $this->mockTypoScriptService->expects(self::atLeastOnce())->method('convertTypoScriptArrayToPlainArray')->with($this->testTypoScriptSetup['config.']['tx_extbase.'])->willReturn($this->testTypoScriptSetupConverted['config']['tx_extbase']); + $subject->_set('extensionName', 'CurrentExtensionName'); + $subject->_set('pluginName', 'CurrentPluginName'); + $subject->expects(self::once())->method('getTypoScriptSetup')->willReturn($this->testTypoScriptSetup); + $subject->expects(self::once())->method('getPluginConfiguration')->with('CurrentExtensionName', 'CurrentPluginName')->willReturn($this->testPluginConfiguration); $expectedResult = [ 'settings' => [ 'setting1' => 'overriddenValue1', @@ -282,112 +177,10 @@ class BackendConfigurationManagerTest extends UnitTestCase ], 'controllerConfiguration' => [], ]; - $backendConfigurationManager->expects(self::never())->method('getContextSpecificFrameworkConfiguration'); - $actualResult = $backendConfigurationManager->getConfiguration('SomeExtensionName', 'SomePluginName'); + $actualResult = $subject->getConfiguration(); self::assertEquals($expectedResult, $actualResult); } - /** - * @test - */ - public function getConfigurationDoesNotOverrideConfigurationWithContextSpecificFrameworkConfigurationIfDifferentPluginIsSpecified(): void - { - $backendConfigurationManager = $this->getAccessibleMock( - BackendConfigurationManager::class, - [ - 'getContextSpecificFrameworkConfiguration', - 'getTypoScriptSetup', - 'getPluginConfiguration', - 'getControllerConfiguration', - 'getRecursiveStoragePids', - 'getDefaultBackendStoragePid', - ], - [], - '', - false - ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); - $backendConfigurationManager->expects(self::never())->method('getContextSpecificFrameworkConfiguration'); - $backendConfigurationManager->getConfiguration('SomeExtensionName', 'SomePluginName'); - } - - /** - * @test - */ - public function getConfigurationOverridesConfigurationWithContextSpecificFrameworkConfigurationIfNoPluginWasSpecified(): void - { - $backendConfigurationManager = $this->getAccessibleMock( - BackendConfigurationManager::class, - [ - 'getContextSpecificFrameworkConfiguration', - 'getTypoScriptSetup', - 'getPluginConfiguration', - 'getControllerConfiguration', - 'getRecursiveStoragePids', - 'getDefaultBackendStoragePid', - ], - [], - '', - false - ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); - $backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn($this->testTypoScriptSetup); - $backendConfigurationManager->expects(self::once())->method('getPluginConfiguration')->with()->willReturn($this->testPluginConfiguration); - $contextSpecificFrameworkConfiguration = [ - 'context' => [ - 'specific' => 'framework', - 'conf' => 'iguration', - ], - ]; - $backendConfigurationManager->expects(self::once())->method('getContextSpecificFrameworkConfiguration')->willReturn($contextSpecificFrameworkConfiguration); - $actualResult = $backendConfigurationManager->getConfiguration(); - self::assertEquals($contextSpecificFrameworkConfiguration, $actualResult); - } - - /** - * @test - */ - public function getConfigurationOverridesConfigurationWithContextSpecificFrameworkConfigurationIfSpecifiedPluginIsTheCurrentPlugin(): void - { - $backendConfigurationManager = $this->getAccessibleMock( - BackendConfigurationManager::class, - [ - 'getContextSpecificFrameworkConfiguration', - 'getTypoScriptSetup', - 'getPluginConfiguration', - 'getControllerConfiguration', - 'getRecursiveStoragePids', - 'getDefaultBackendStoragePid', - ], - [], - '', - false - ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - $backendConfigurationManager->_set('extensionName', 'CurrentExtensionName'); - $backendConfigurationManager->_set('pluginName', 'CurrentPluginName'); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); - $backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn($this->testTypoScriptSetup); - $backendConfigurationManager->expects(self::once())->method('getPluginConfiguration')->with( - 'CurrentExtensionName', - 'CurrentPluginName' - )->willReturn($this->testPluginConfiguration); - $contextSpecificFrameworkConfiguration = [ - 'context' => [ - 'specific' => 'framework', - 'conf' => 'iguration', - ], - ]; - $backendConfigurationManager->expects(self::once())->method('getContextSpecificFrameworkConfiguration')->willReturn($contextSpecificFrameworkConfiguration); - $actualResult = $backendConfigurationManager->getConfiguration( - 'CurrentExtensionName', - 'CurrentPluginName' - ); - self::assertEquals($contextSpecificFrameworkConfiguration, $actualResult); - } - /** * @test */ @@ -396,22 +189,15 @@ class BackendConfigurationManagerTest extends UnitTestCase $backendConfigurationManager = $this->getAccessibleMock( BackendConfigurationManager::class, [ - 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', - 'getPluginConfiguration', - 'getControllerConfiguration', - 'getRecursiveStoragePids', 'getDefaultBackendStoragePid', ], - [], + [new TypoScriptService()], '', - false + true ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); $backendConfigurationManager->_set('extensionName', 'CurrentExtensionName'); $backendConfigurationManager->_set('pluginName', 'CurrentPluginName'); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); - $backendConfigurationManager->method('getPluginConfiguration')->willReturn(['foo' => 'bar']); $backendConfigurationManager->getConfiguration(); $backendConfigurationManager->getConfiguration('SomeOtherExtensionName', 'SomeOtherCurrentPluginName'); $expectedResult = [ @@ -430,19 +216,15 @@ class BackendConfigurationManagerTest extends UnitTestCase $backendConfigurationManager = $this->getAccessibleMock( BackendConfigurationManager::class, [ - 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', - 'getControllerConfiguration', 'getRecursiveStoragePids', 'getDefaultBackendStoragePid', ], - [], + [new TypoScriptService()], '', - false + true ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); $pluginConfiguration = [ 'persistence' => [ 'storagePid' => 1, @@ -462,19 +244,15 @@ class BackendConfigurationManagerTest extends UnitTestCase $backendConfigurationManager = $this->getAccessibleMock( BackendConfigurationManager::class, [ - 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', - 'getControllerConfiguration', 'getRecursiveStoragePids', 'getDefaultBackendStoragePid', ], - [], + [new TypoScriptService()], '', - false + true ); - $backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService); - $backendConfigurationManager->expects(self::any())->method('getDefaultBackendStoragePid')->willReturn(0); $pluginConfiguration = [ 'persistence' => [ 'storagePid' => '1,25', @@ -491,7 +269,7 @@ class BackendConfigurationManagerTest extends UnitTestCase */ public function getContentObjectReturnsInstanceOfContentObjectRenderer(): void { - self::assertInstanceOf(ContentObjectRenderer::class, $this->backendConfigurationManager->getContentObject()); + self::assertInstanceOf(ContentObjectRenderer::class, (new BackendConfigurationManager(new TypoScriptService()))->getContentObject()); } /** @@ -499,9 +277,10 @@ class BackendConfigurationManagerTest extends UnitTestCase */ public function getContentObjectTheCurrentContentObject(): void { + $subject = new BackendConfigurationManager(new TypoScriptService()); $mockContentObject = $this->createMock(ContentObjectRenderer::class); - $this->backendConfigurationManager->setContentObject($mockContentObject); - self::assertSame($this->backendConfigurationManager->getContentObject(), $mockContentObject); + $subject->setContentObject($mockContentObject); + self::assertSame($mockContentObject, $subject->getContentObject()); } /** @@ -510,9 +289,9 @@ class BackendConfigurationManagerTest extends UnitTestCase public function getCurrentPageIdReturnsPageIdFromGet(): void { $_GET['id'] = 123; - $expectedResult = 123; - $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId'); - self::assertEquals($expectedResult, $actualResult); + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, null, [], '', false); + $actualResult = $subject->_call('getCurrentPageId'); + self::assertEquals(123, $actualResult); } /** @@ -522,9 +301,9 @@ class BackendConfigurationManagerTest extends UnitTestCase { $_GET['id'] = 123; $_POST['id'] = 321; - $expectedResult = 321; - $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId'); - self::assertEquals($expectedResult, $actualResult); + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, null, [], '', false); + $actualResult = $subject->_call('getCurrentPageId'); + self::assertEquals(321, $actualResult); } /** @@ -532,9 +311,10 @@ class BackendConfigurationManagerTest extends UnitTestCase */ public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound(): void { - $this->backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn(['foo' => 'bar']); + $subject = $this->getAccessibleMock(BackendConfigurationManager::class, ['getTypoScriptSetup'], [], '', false); + $subject->expects(self::once())->method('getTypoScriptSetup')->willReturn(['foo' => 'bar']); $expectedResult = []; - $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName'); + $actualResult = $subject->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName'); self::assertEquals($expectedResult, $actualResult); } @@ -548,24 +328,26 @@ class BackendConfigurationManagerTest extends UnitTestCase 'foo' => 'bar', ], ]; - $testSettingsConverted = [ - 'settings' => [ - 'foo' => 'bar', - ], - ]; $testSetup = [ 'module.' => [ 'tx_someextensionname.' => $testSettings, ], ]; - $this->mockTypoScriptService->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->willReturn($testSettingsConverted); - $this->backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn($testSetup); + $subject = $this->getAccessibleMock( + BackendConfigurationManager::class, + ['getTypoScriptSetup'], + [new TypoScriptService()], + '', + true + ); + $subject->expects(self::once())->method('getTypoScriptSetup')->willReturn($testSetup); $expectedResult = [ 'settings' => [ 'foo' => 'bar', ], ]; - $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName'); + + $actualResult = $subject->_call('getPluginConfiguration', 'SomeExtensionName'); self::assertEquals($expectedResult, $actualResult); } @@ -579,24 +361,25 @@ class BackendConfigurationManagerTest extends UnitTestCase 'foo' => 'bar', ], ]; - $testSettingsConverted = [ - 'settings' => [ - 'foo' => 'bar', - ], - ]; $testSetup = [ 'module.' => [ 'tx_someextensionname_somepluginname.' => $testSettings, ], ]; - $this->mockTypoScriptService->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->willReturn($testSettingsConverted); - $this->backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn($testSetup); + $subject = $this->getAccessibleMock( + BackendConfigurationManager::class, + ['getTypoScriptSetup'], + [new TypoScriptService()], + '', + true + ); + $subject->expects(self::once())->method('getTypoScriptSetup')->willReturn($testSetup); $expectedResult = [ 'settings' => [ 'foo' => 'bar', ], ]; - $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName'); + $actualResult = $subject->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName'); self::assertEquals($expectedResult, $actualResult); } @@ -643,10 +426,18 @@ class BackendConfigurationManagerTest extends UnitTestCase 'tx_someextensionname_somepluginname.' => $testPluginSettings, ], ]; - $this->mockTypoScriptService->expects(self::exactly(2))->method('convertTypoScriptArrayToPlainArray') + $typoScriptServiceMock = $this->getMockBuilder(TypoScriptService::class)->getMock(); + $typoScriptServiceMock->expects(self::exactly(2))->method('convertTypoScriptArrayToPlainArray') ->withConsecutive([$testExtensionSettings], [$testPluginSettings]) ->willReturnOnConsecutiveCalls($testExtensionSettingsConverted, $testPluginSettingsConverted); - $this->backendConfigurationManager->expects(self::once())->method('getTypoScriptSetup')->willReturn($testSetup); + $subject = $this->getAccessibleMock( + BackendConfigurationManager::class, + ['getTypoScriptSetup'], + [$typoScriptServiceMock], + '', + true + ); + $subject->expects(self::once())->method('getTypoScriptSetup')->willReturn($testSetup); $expectedResult = [ 'settings' => [ 'foo' => 'bar', @@ -656,18 +447,7 @@ class BackendConfigurationManagerTest extends UnitTestCase ], ], ]; - $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName'); - self::assertEquals($expectedResult, $actualResult); - } - - /** - * @test - */ - public function getControllerConfigurationReturnsEmptyArrayByDefault(): void - { - $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = null; - $expectedResult = []; - $actualResult = $this->backendConfigurationManager->_call('getControllerConfiguration', 'SomeExtensionName', 'SomePluginName'); + $actualResult = $subject->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName'); self::assertEquals($expectedResult, $actualResult); } @@ -677,15 +457,13 @@ class BackendConfigurationManagerTest extends UnitTestCase public function storagePidsAreNotExtendedIfRecursiveSearchIsNotConfigured(): void { $storagePids = [1, 2, 3]; - $backendConfigurationManager = $this->getAccessibleMock( BackendConfigurationManager::class, - ['getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getControllerConfiguration'], + ['getTypoScriptSetup', 'getPluginConfiguration'], [], '', false ); - $expectedResult = [1, 2, 3]; $actualResult = $backendConfigurationManager->_call('getRecursiveStoragePids', $storagePids); self::assertEquals($expectedResult, $actualResult); @@ -697,18 +475,15 @@ class BackendConfigurationManagerTest extends UnitTestCase public function storagePidsAreNotExtendedIfRecursiveSearchIsConfiguredForZeroLevels(): void { $storagePids = [1, 2, 3]; - $recursive = 0; - $backendConfigurationManager = $this->getAccessibleMock( BackendConfigurationManager::class, - ['getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getControllerConfiguration'], + ['getTypoScriptSetup', 'getPluginConfiguration'], [], '', false ); - $expectedResult = [1, 2, 3]; - $actualResult = $backendConfigurationManager->_call('getRecursiveStoragePids', $storagePids, $recursive); + $actualResult = $backendConfigurationManager->_call('getRecursiveStoragePids', $storagePids, 0); self::assertEquals($expectedResult, $actualResult); } } -- GitLab