From ebf1e5d061d8db369e537cd978a523a1524e6a33 Mon Sep 17 00:00:00 2001 From: Claus Due Date: Sat, 12 Aug 2023 18:10:32 +0200 Subject: [PATCH] [TASK] Move getPageConfiguration from FluxService to PageService --- Classes/Service/FluxService.php | 110 +----------- Classes/Service/PageService.php | 86 ++++++++- Tests/Fixtures/Classes/DummyFluxService.php | 21 --- Tests/Fixtures/Classes/DummyPageService.php | 12 -- Tests/Unit/Service/FluxServiceTest.php | 188 -------------------- Tests/Unit/Service/PageServiceTest.php | 145 ++++++++++++++- 6 files changed, 216 insertions(+), 346 deletions(-) diff --git a/Classes/Service/FluxService.php b/Classes/Service/FluxService.php index b1e66a881..b816dbe79 100644 --- a/Classes/Service/FluxService.php +++ b/Classes/Service/FluxService.php @@ -9,45 +9,25 @@ * LICENSE.md file that was distributed with this source code. */ -use FluidTYPO3\Flux\Content\TypeDefinition\FluidFileBased\DropInContentTypeDefinition; -use FluidTYPO3\Flux\Core; use FluidTYPO3\Flux\Form; use FluidTYPO3\Flux\Form\Transformation\FormDataTransformer; -use FluidTYPO3\Flux\Utility\ExtensionConfigurationUtility; -use FluidTYPO3\Flux\Utility\ExtensionNamingUtility; -use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; -use Psr\Log\LoggerInterface; -use TYPO3\CMS\Core\Http\ServerRequest; -use TYPO3\CMS\Core\Resource\File; -use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Service\FlexFormService; use TYPO3\CMS\Core\SingletonInterface; -use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Fluid\View\TemplatePaths; /** * Flux FlexForm integration Service * * Main API Service for interacting with Flux-based FlexForms */ -class FluxService implements SingletonInterface, LoggerAwareInterface +class FluxService implements SingletonInterface { - use LoggerAwareTrait; - - protected ServerRequest $serverRequest; - protected ResourceFactory $resourceFactory; protected FormDataTransformer $transformer; protected FlexFormService $flexFormService; public function __construct( - ServerRequest $serverRequest, - ResourceFactory $resourceFactory, FormDataTransformer $transformer, FlexFormService $flexFormService ) { - $this->serverRequest = $serverRequest; - $this->resourceFactory = $resourceFactory; $this->transformer = $transformer; $this->flexFormService = $flexFormService; } @@ -90,92 +70,4 @@ public function convertFlexFormContentToArray( } return $settings; } - - public function getPageConfiguration(?string $extensionName = null): array - { - if (null !== $extensionName && true === empty($extensionName)) { - // Note: a NULL extensionName means "fetch ALL defined collections" whereas - // an empty value that is not null indicates an incorrect caller. Instead - // of returning ALL paths here, an empty array is the proper return value. - // However, dispatch a debug message to inform integrators of the problem. - if ($this->logger instanceof LoggerInterface) { - $this->logger->log( - 'notice', - 'Template paths have been attempted fetched using an empty value that is NOT NULL in ' . - get_class($this) . '. This indicates a potential problem with your TypoScript configuration - a ' . - 'value which is expected to be an array may be defined as a string. This error is not fatal but ' . - 'may prevent the affected collection (which cannot be identified here) from showing up' - ); - } - return []; - } - - $plugAndPlayEnabled = ExtensionConfigurationUtility::getOption( - ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY - ); - $plugAndPlayDirectory = ExtensionConfigurationUtility::getOption( - ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY - ); - if (!is_scalar($plugAndPlayDirectory)) { - return []; - } - $plugAndPlayTemplatesDirectory = trim((string) $plugAndPlayDirectory, '/.') . '/'; - if ($plugAndPlayEnabled && $extensionName === 'Flux') { - return [ - TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [ - $plugAndPlayTemplatesDirectory - . DropInContentTypeDefinition::TEMPLATES_DIRECTORY - . DropInContentTypeDefinition::PAGE_DIRECTORY - ], - TemplatePaths::CONFIG_PARTIALROOTPATHS => [ - $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY - ], - TemplatePaths::CONFIG_LAYOUTROOTPATHS => [ - $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY - ], - ]; - } - if (null !== $extensionName) { - $templatePaths = $this->createTemplatePaths($extensionName); - return $templatePaths->toArray(); - } - $configurations = []; - $registeredExtensionKeys = Core::getRegisteredProviderExtensionKeys('Page'); - foreach ($registeredExtensionKeys as $registeredExtensionKey) { - $templatePaths = $this->createTemplatePaths($registeredExtensionKey); - $configurations[$registeredExtensionKey] = $templatePaths->toArray(); - } - if ($plugAndPlayEnabled) { - $configurations['FluidTYPO3.Flux'] = array_replace( - $configurations['FluidTYPO3.Flux'] ?? [], - [ - TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [ - $plugAndPlayTemplatesDirectory - . DropInContentTypeDefinition::TEMPLATES_DIRECTORY - . DropInContentTypeDefinition::PAGE_DIRECTORY - ], - TemplatePaths::CONFIG_PARTIALROOTPATHS => [ - $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY - ], - TemplatePaths::CONFIG_LAYOUTROOTPATHS => [ - $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY - ], - ] - ); - } - return $configurations; - } - - /** - * @codeCoverageIgnore - */ - protected function createTemplatePaths(string $registeredExtensionKey): TemplatePaths - { - /** @var TemplatePaths $templatePaths */ - $templatePaths = GeneralUtility::makeInstance( - TemplatePaths::class, - ExtensionNamingUtility::getExtensionKey($registeredExtensionKey) - ); - return $templatePaths; - } } diff --git a/Classes/Service/PageService.php b/Classes/Service/PageService.php index 6b27dbc08..3968b3e1b 100755 --- a/Classes/Service/PageService.php +++ b/Classes/Service/PageService.php @@ -8,8 +8,11 @@ * LICENSE.md file that was distributed with this source code. */ +use FluidTYPO3\Flux\Content\TypeDefinition\FluidFileBased\DropInContentTypeDefinition; +use FluidTYPO3\Flux\Core; use FluidTYPO3\Flux\Form; use FluidTYPO3\Flux\Provider\PageProvider; +use FluidTYPO3\Flux\Utility\ExtensionConfigurationUtility; use FluidTYPO3\Flux\Utility\ExtensionNamingUtility; use FluidTYPO3\Flux\ViewHelpers\FormViewHelper; use Psr\Log\LoggerAwareInterface; @@ -37,19 +40,13 @@ class PageService implements SingletonInterface, LoggerAwareInterface { use LoggerAwareTrait; - protected ConfigurationManagerInterface $configurationManager; - protected FluxService $configurationService; protected WorkspacesAwareRecordService $workspacesAwareRecordService; protected FrontendInterface $runtimeCache; public function __construct( - ConfigurationManagerInterface $configurationManager, - FluxService $configurationService, WorkspacesAwareRecordService $recordService, CacheManager $cacheManager ) { - $this->configurationManager = $configurationManager; - $this->configurationService = $configurationService; $this->workspacesAwareRecordService = $recordService; $this->runtimeCache = $cacheManager->getCache('runtime'); } @@ -137,6 +134,81 @@ public function getPageFlexFormSource(int $pageUid): ?string return $page['tx_fed_page_flexform'] ?? null; } + public function getPageConfiguration(?string $extensionName = null): array + { + if (null !== $extensionName && true === empty($extensionName)) { + // Note: a NULL extensionName means "fetch ALL defined collections" whereas + // an empty value that is not null indicates an incorrect caller. Instead + // of returning ALL paths here, an empty array is the proper return value. + // However, dispatch a debug message to inform integrators of the problem. + if ($this->logger instanceof LoggerInterface) { + $this->logger->log( + 'notice', + 'Template paths have been attempted fetched using an empty value that is NOT NULL in ' . + get_class($this) . '. This indicates a potential problem with your TypoScript configuration - a ' . + 'value which is expected to be an array may be defined as a string. This error is not fatal but ' . + 'may prevent the affected collection (which cannot be identified here) from showing up' + ); + } + return []; + } + + $plugAndPlayEnabled = ExtensionConfigurationUtility::getOption( + ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY + ); + $plugAndPlayDirectory = ExtensionConfigurationUtility::getOption( + ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY + ); + if (!is_scalar($plugAndPlayDirectory)) { + return []; + } + $plugAndPlayTemplatesDirectory = trim((string) $plugAndPlayDirectory, '/.') . '/'; + if ($plugAndPlayEnabled && $extensionName === 'Flux') { + return [ + TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [ + $plugAndPlayTemplatesDirectory + . DropInContentTypeDefinition::TEMPLATES_DIRECTORY + . DropInContentTypeDefinition::PAGE_DIRECTORY + ], + TemplatePaths::CONFIG_PARTIALROOTPATHS => [ + $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY + ], + TemplatePaths::CONFIG_LAYOUTROOTPATHS => [ + $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY + ], + ]; + } + if (null !== $extensionName) { + $templatePaths = $this->createTemplatePaths($extensionName); + return $templatePaths->toArray(); + } + $configurations = []; + $registeredExtensionKeys = Core::getRegisteredProviderExtensionKeys('Page'); + foreach ($registeredExtensionKeys as $registeredExtensionKey) { + $templatePaths = $this->createTemplatePaths($registeredExtensionKey); + $configurations[$registeredExtensionKey] = $templatePaths->toArray(); + } + if ($plugAndPlayEnabled) { + $configurations['FluidTYPO3.Flux'] = array_replace( + $configurations['FluidTYPO3.Flux'] ?? [], + [ + TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [ + $plugAndPlayTemplatesDirectory + . DropInContentTypeDefinition::TEMPLATES_DIRECTORY + . DropInContentTypeDefinition::PAGE_DIRECTORY + ], + TemplatePaths::CONFIG_PARTIALROOTPATHS => [ + $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY + ], + TemplatePaths::CONFIG_LAYOUTROOTPATHS => [ + $plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY + ], + ] + ); + } + return $configurations; + } + /** * Gets a list of usable Page Templates from defined page template TypoScript. * Returns a list of Form instances indexed by the path ot the template file. @@ -152,7 +224,7 @@ public function getAvailablePageTemplateFiles(): array if ($fromCache) { return $fromCache; } - $typoScript = $this->configurationService->getPageConfiguration(); + $typoScript = $this->getPageConfiguration(); $output = []; /** @var TemplateView $view */ diff --git a/Tests/Fixtures/Classes/DummyFluxService.php b/Tests/Fixtures/Classes/DummyFluxService.php index 2bc964c08..37e83f679 100644 --- a/Tests/Fixtures/Classes/DummyFluxService.php +++ b/Tests/Fixtures/Classes/DummyFluxService.php @@ -2,41 +2,20 @@ namespace FluidTYPO3\Flux\Tests\Fixtures\Classes; use FluidTYPO3\Flux\Form\Transformation\FormDataTransformer; -use FluidTYPO3\Flux\Provider\ProviderResolver; use FluidTYPO3\Flux\Service\FluxService; use PHPUnit\Framework\MockObject\Generator; use Psr\Log\LoggerInterface; -use TYPO3\CMS\Core\Http\ServerRequest; -use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Service\FlexFormService; class DummyFluxService extends FluxService { public function __construct() { - $this->serverRequest = $this->createMock(ServerRequest::class); - $this->resourceFactory = $this->createMock(ResourceFactory::class); - $this->providerResolver = $this->createMock(ProviderResolver::class); $this->transformer = $this->createMock(FormDataTransformer::class); $this->flexFormService = $this->createMock(FlexFormService::class); $this->logger = $this->createMock(LoggerInterface::class); } - public function setServerRequest(ServerRequest $serverRequest): void - { - $this->serverRequest = $serverRequest; - } - - public function setResourceFactory(ResourceFactory $resourceFactory): void - { - $this->resourceFactory = $resourceFactory; - } - - public function setProviderResolver(ProviderResolver $providerResolver): void - { - $this->providerResolver = $providerResolver; - } - public function setFormDataTransformer(FormDataTransformer $transformer): void { $this->transformer = $transformer; diff --git a/Tests/Fixtures/Classes/DummyPageService.php b/Tests/Fixtures/Classes/DummyPageService.php index efa98daa8..26feec306 100644 --- a/Tests/Fixtures/Classes/DummyPageService.php +++ b/Tests/Fixtures/Classes/DummyPageService.php @@ -13,22 +13,10 @@ class DummyPageService extends PageService { public function __construct() { - $this->configurationManager = $this->createMock(ConfigurationManagerInterface::class); - $this->configurationService = new DummyFluxService(); $this->workspacesAwareRecordService = $this->createMock(WorkspacesAwareRecordService::class); $this->runtimeCache = $this->createMock(FrontendInterface::class); } - public function setConfigurationManager(ConfigurationManagerInterface $configurationManager): void - { - $this->configurationManager = $configurationManager; - } - - public function setConfigurationService(DummyFluxService $configurationService): void - { - $this->configurationService = $configurationService; - } - public function setWorkspacesAwareRecordService(WorkspacesAwareRecordService $workspacesAwareRecordService): void { $this->workspacesAwareRecordService = $workspacesAwareRecordService; diff --git a/Tests/Unit/Service/FluxServiceTest.php b/Tests/Unit/Service/FluxServiceTest.php index 9a4db7bd0..bcaf3d626 100644 --- a/Tests/Unit/Service/FluxServiceTest.php +++ b/Tests/Unit/Service/FluxServiceTest.php @@ -11,15 +11,9 @@ use FluidTYPO3\Flux\Core; use FluidTYPO3\Flux\Form; use FluidTYPO3\Flux\Service\FluxService; -use FluidTYPO3\Flux\Tests\Fixtures\Classes\AccessibleCore; use FluidTYPO3\Flux\Tests\Fixtures\Data\Xml; use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase; -use FluidTYPO3\Flux\Utility\ExtensionConfigurationUtility; -use PHPUnit\Framework\MockObject\MockObject; -use TYPO3\CMS\Core\Resource\File; -use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Service\FlexFormService; -use TYPO3\CMS\Fluid\View\TemplatePaths; /** * FluxServiceTest @@ -39,188 +33,6 @@ public function setup(): void } } - /** - * @return array - */ - public function getConvertFileReferenceToTemplatePathAndFilenameTestValues() - { - $relativeReference = 'Tests/Fixtures/Templates/Page/Dummy.html'; - return [ - [$relativeReference, null, $relativeReference], - ['1', $relativeReference, $relativeReference], - ]; - } - - /** - * @return array - */ - public function getViewConfigurationByFileReferenceTestValues() - { - $fluxPaths = [ - 'templateRootPaths' => ['Resources/Private/Templates/'], - 'partialRootPaths' => ['Resources/Private/Partials/'], - 'layoutRootPaths' => ['Resources/Private/Layouts/'], - ]; - return [ - ['some/file', $fluxPaths], - ['EXT:flux/some/file', $fluxPaths], - ]; - } - - /** - * @dataProvider getPageConfigurationInvalidTestValues - * @param mixed $input - * @return void - */ - public function testGetPageConfigurationReturnsEmptyArrayOnInvalidInput($input) - { - $instance = $this->createFluxServiceInstance(); - $result = $instance->getPageConfiguration($input); - $this->assertEquals([], $result); - } - - /** - * @return array - */ - public function getPageConfigurationInvalidTestValues() - { - return [ - [''], - [0], - ]; - } - - public function testGetPageConfigurationReturnsEmptyArrayOnInvalidPlugAndPlayDirectorySetting(): void - { - $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY] = true; - $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY] - = ['foo']; - - $instance = $this->createFluxServiceInstance(); - - $result = $instance->getPageConfiguration('Flux'); - unset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']); - - self::assertEquals([], $result); - } - - public function testGetPageConfigurationReturnsExpectedArrayOnPlugAndPlayDirectorySetting(): void - { - $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY] = true; - $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY] - = './'; - - $instance = $this->createFluxServiceInstance(); - - $result = $instance->getPageConfiguration('Flux'); - unset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']); - - self::assertEquals( - [ - TemplatePaths::CONFIG_TEMPLATEROOTPATHS => ['/Templates/Page/'], - TemplatePaths::CONFIG_PARTIALROOTPATHS => ['/Partials/'], - TemplatePaths::CONFIG_LAYOUTROOTPATHS => ['/Layouts/'], - ], - $result - ); - } - - public function testGetPageConfigurationReturnsExpectedArrayOnPlugAndPlayDirectorySettingWithForeignExt(): void - { - $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY] = true; - $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY] - = './'; - - $templatePaths = $this->getMockBuilder(TemplatePaths::class) - ->setMethods(['toArray']) - ->disableOriginalConstructor() - ->getMock(); - $templatePaths->method('toArray')->willReturn(['foo' => 'bar']); - - $instance = $this->getMockBuilder(FluxService::class) - ->setMethods(['createTemplatePaths']) - ->disableOriginalConstructor() - ->getMock(); - $instance->method('createTemplatePaths')->willReturn($templatePaths); - - Core::registerProviderExtensionKey('FluidTYPO3.Testing', 'Page'); - $result = $instance->getPageConfiguration(null); - unset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']); - AccessibleCore::resetQueuedRegistrations(); - - self::assertEquals( - [ - 'FluidTYPO3.Testing' => ['foo' => 'bar'], - 'FluidTYPO3.Flux' => [ - TemplatePaths::CONFIG_TEMPLATEROOTPATHS => ['/Templates/Page/'], - TemplatePaths::CONFIG_PARTIALROOTPATHS => ['/Partials/'], - TemplatePaths::CONFIG_LAYOUTROOTPATHS => ['/Layouts/'], - ], - ], - $result - ); - } - - public function testGetPageConfigurationReturnsDefaultTemplatePaths(): void - { - $templatePaths = $this->getMockBuilder(TemplatePaths::class) - ->setMethods(['toArray']) - ->disableOriginalConstructor() - ->getMock(); - $templatePaths->method('toArray')->willReturn(['foo' => 'bar']); - - $instance = $this->getMockBuilder(FluxService::class) - ->setMethods(['createTemplatePaths']) - ->disableOriginalConstructor() - ->getMock(); - $instance->method('createTemplatePaths')->willReturn($templatePaths); - - $result = $instance->getPageConfiguration('Flux'); - - self::assertEquals(['foo' => 'bar'], $result); - } - - /** - * @return void - */ - public function testGetPageConfigurationWithoutExtensionNameReadsRegisteredProviders() - { - $templatePaths = new TemplatePaths(); - $instance = $this->getMockBuilder(FluxService::class) - ->setMethods(['createTemplatePaths']) - ->disableOriginalConstructor() - ->getMock(); - $instance->method('createTemplatePaths')->willReturn($templatePaths); - Core::registerProviderExtensionKey('foo', 'Page'); - Core::registerProviderExtensionKey('bar', 'Page'); - $result = $instance->getPageConfiguration(); - $this->assertCount(2, $result); - } - - /** - * @return array - */ - public function getSortObjectsTestValues() - { - return [ - [ - [['foo' => 'b'], ['foo' => 'a']], - 'foo', 'ASC', - [1 => ['foo' => 'a'], 0 => ['foo' => 'b']] - ], - [ - ['a1' => ['foo' => 'b'], 'a2' => ['foo' => 'a']], - 'foo', 'ASC', - ['a2' => ['foo' => 'a'], 'a1' => ['foo' => 'b']], - ], - [ - ['a1' => ['foo' => 'b'], 'a2' => ['foo' => 'a']], - 'foo', 'DESC', - ['a1' => ['foo' => 'b'], 'a2' => ['foo' => 'a']], - ], - ]; - } - /** * @test */ diff --git a/Tests/Unit/Service/PageServiceTest.php b/Tests/Unit/Service/PageServiceTest.php index 4cedf4fd8..b960d57c9 100644 --- a/Tests/Unit/Service/PageServiceTest.php +++ b/Tests/Unit/Service/PageServiceTest.php @@ -8,10 +8,13 @@ * LICENSE.md file that was distributed with this source code. */ +use FluidTYPO3\Flux\Core; +use FluidTYPO3\Flux\Service\PageService; use FluidTYPO3\Flux\Service\WorkspacesAwareRecordService; -use FluidTYPO3\Flux\Tests\Fixtures\Classes\DummyFluxService; +use FluidTYPO3\Flux\Tests\Fixtures\Classes\AccessibleCore; use FluidTYPO3\Flux\Tests\Fixtures\Classes\DummyPageService; use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase; +use FluidTYPO3\Flux\Utility\ExtensionConfigurationUtility; use Psr\Log\LoggerInterface; use TYPO3\CMS\Core\Cache\Backend\BackendInterface; use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend; @@ -102,18 +105,12 @@ public function testGetAvailablePageTemplateFiles($typoScript, $expected): void $templatePaths->method('getTemplateRootPaths')->willReturn([__DIR__ . '/../../Fixtures/Templates']); $templatePaths->method('ensureAbsolutePath')->willReturnArgument(0); - $fluxService = $this->getMockBuilder(DummyFluxService::class) - ->onlyMethods(['getPageConfiguration']) - ->disableOriginalConstructor() - ->getMock(); - $fluxService->method('getPageConfiguration')->willReturn($typoScript); - $instance = $this->getMockBuilder(DummyPageService::class) - ->onlyMethods(['createTemplatePaths']) + ->onlyMethods(['createTemplatePaths', 'getPageConfiguration']) ->getMock(); $instance->setLogger($this->getMockBuilder(LoggerInterface::class)->getMockForAbstractClass()); $instance->method('createTemplatePaths')->willReturn($templatePaths); - $instance->setConfigurationService($fluxService); + $instance->method('getPageConfiguration')->willReturn($typoScript); GeneralUtility::addInstance(TemplateView::class, $templateView); @@ -144,4 +141,134 @@ public function getAvailablePageTemplateFilesTestValues(): array ], ]; } + + /** + * @dataProvider getPageConfigurationInvalidTestValues + * @param mixed $input + * @return void + */ + public function testGetPageConfigurationReturnsEmptyArrayOnInvalidInput($input) + { + $instance = new DummyPageService(); + $result = $instance->getPageConfiguration($input); + $this->assertEquals([], $result); + } + + /** + * @return array + */ + public function getPageConfigurationInvalidTestValues() + { + return [ + [''], + [0], + ]; + } + + public function testGetPageConfigurationReturnsEmptyArrayOnInvalidPlugAndPlayDirectorySetting(): void + { + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY] = true; + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY] + = ['foo']; + + $instance = new DummyPageService(); + + $result = $instance->getPageConfiguration('Flux'); + unset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']); + + self::assertEquals([], $result); + } + + public function testGetPageConfigurationReturnsExpectedArrayOnPlugAndPlayDirectorySetting(): void + { + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY] = true; + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY] + = './'; + + $instance = new DummyPageService(); + + $result = $instance->getPageConfiguration('Flux'); + unset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']); + + self::assertEquals( + [ + TemplatePaths::CONFIG_TEMPLATEROOTPATHS => ['/Templates/Page/'], + TemplatePaths::CONFIG_PARTIALROOTPATHS => ['/Partials/'], + TemplatePaths::CONFIG_LAYOUTROOTPATHS => ['/Layouts/'], + ], + $result + ); + } + + public function testGetPageConfigurationReturnsExpectedArrayOnPlugAndPlayDirectorySettingWithForeignExt(): void + { + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY] = true; + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['flux'][ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY] + = './'; + + $templatePaths = $this->getMockBuilder(TemplatePaths::class) + ->onlyMethods(['toArray']) + ->disableOriginalConstructor() + ->getMock(); + $templatePaths->method('toArray')->willReturn(['foo' => 'bar']); + + $instance = $this->getMockBuilder(PageService::class) + ->onlyMethods(['createTemplatePaths']) + ->disableOriginalConstructor() + ->getMock(); + $instance->method('createTemplatePaths')->willReturn($templatePaths); + + Core::registerProviderExtensionKey('FluidTYPO3.Testing', 'Page'); + $result = $instance->getPageConfiguration(null); + unset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']); + AccessibleCore::resetQueuedRegistrations(); + + self::assertEquals( + [ + 'FluidTYPO3.Testing' => ['foo' => 'bar'], + 'FluidTYPO3.Flux' => [ + TemplatePaths::CONFIG_TEMPLATEROOTPATHS => ['/Templates/Page/'], + TemplatePaths::CONFIG_PARTIALROOTPATHS => ['/Partials/'], + TemplatePaths::CONFIG_LAYOUTROOTPATHS => ['/Layouts/'], + ], + ], + $result + ); + } + + public function testGetPageConfigurationReturnsDefaultTemplatePaths(): void + { + $templatePaths = $this->getMockBuilder(TemplatePaths::class) + ->onlyMethods(['toArray']) + ->disableOriginalConstructor() + ->getMock(); + $templatePaths->method('toArray')->willReturn(['foo' => 'bar']); + + $instance = $this->getMockBuilder(PageService::class) + ->onlyMethods(['createTemplatePaths']) + ->disableOriginalConstructor() + ->getMock(); + $instance->method('createTemplatePaths')->willReturn($templatePaths); + + $result = $instance->getPageConfiguration('Flux'); + + self::assertEquals(['foo' => 'bar'], $result); + } + + /** + * @return void + */ + public function testGetPageConfigurationWithoutExtensionNameReadsRegisteredProviders() + { + $templatePaths = new TemplatePaths(); + $instance = $this->getMockBuilder(PageService::class) + ->onlyMethods(['createTemplatePaths']) + ->disableOriginalConstructor() + ->getMock(); + $instance->method('createTemplatePaths')->willReturn($templatePaths); + Core::registerProviderExtensionKey('foo', 'Page'); + Core::registerProviderExtensionKey('bar', 'Page'); + $result = $instance->getPageConfiguration(); + $this->assertCount(2, $result); + } }