From afd40248fbcc7e1de07abf0e21b71101ee6ca220 Mon Sep 17 00:00:00 2001 From: Claus Due Date: Mon, 9 Dec 2024 12:46:31 +0100 Subject: [PATCH] [BUGFIX] Fix support for controller arguments and redirection --- Classes/Controller/AbstractFluxController.php | 24 +++++++------- .../AbstractFluxControllerTestCase.php | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/Classes/Controller/AbstractFluxController.php b/Classes/Controller/AbstractFluxController.php index c9d053cfb..1c8e1a67d 100644 --- a/Classes/Controller/AbstractFluxController.php +++ b/Classes/Controller/AbstractFluxController.php @@ -435,7 +435,7 @@ protected function performSubRendering( ] )['content']; - return $this->createHtmlResponse($content); + return $content instanceof \Psr\Http\Message\ResponseInterface ? $content : $this->createHtmlResponse($content); } protected function hasSubControllerActionOnForeignController( @@ -458,6 +458,7 @@ protected function hasSubControllerActionOnForeignController( /** * @param class-string $controllerClassName + * @return \Psr\Http\Message\ResponseInterface|ResponseInterface|null */ protected function callSubControllerAction( string $extensionName, @@ -465,8 +466,11 @@ protected function callSubControllerAction( string $controllerActionName, string $pluginName, string $pluginSignature - ): string { - $arguments = $this->getServerRequest()->getQueryParams()[$pluginSignature] ?? []; + ) { + $serverRequest = $this->getServerRequest(); + $arguments = $serverRequest->getQueryParams()[$pluginSignature] ?? []; + $arguments = array_merge($arguments, ((array) $serverRequest->getParsedBody())[$pluginSignature] ?? []); + $request = $this->requestBuilder->buildRequestFor( $extensionName, $this->resolver->resolveControllerNameFromControllerClassName( @@ -484,7 +488,7 @@ protected function callSubControllerAction( /** @var ResponseInterface\ $response */ $response = $this->responseFactory->createResponse(); } else { - /** @var Response $response */ + /** @var ResponseInterface $response */ $response = GeneralUtility::makeInstance(Response::class); } @@ -500,7 +504,7 @@ protected function callSubControllerAction( ] ); - /** @var Response|null $responseFromCall */ + /** @var \Psr\Http\Message\ResponseInterface|ResponseInterface|null $responseFromCall */ $responseFromCall = $potentialControllerInstance->processRequest($request, $response); if ($responseFromCall) { $response = $responseFromCall; @@ -518,14 +522,8 @@ protected function callSubControllerAction( 'controllerActionName' => $controllerActionName ] ); - if (method_exists($response, 'getContent')) { - return $response->getContent(); - } - if (method_exists($response, 'getBody')) { - $response->getBody()->rewind(); - return $response->getBody()->getContents(); - } - return ''; + + return $response; } /** diff --git a/Tests/Unit/Controller/AbstractFluxControllerTestCase.php b/Tests/Unit/Controller/AbstractFluxControllerTestCase.php index 4ae65f594..c8498ff94 100644 --- a/Tests/Unit/Controller/AbstractFluxControllerTestCase.php +++ b/Tests/Unit/Controller/AbstractFluxControllerTestCase.php @@ -23,6 +23,7 @@ use FluidTYPO3\Flux\Tests\Fixtures\Data\Records; use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; @@ -305,6 +306,11 @@ public function testCanPerformSubRenderingWithNotMatchingExtensionName(): void $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock() ); $instance->injectConfigurationManager($configurationManager); + if (method_exists($instance, 'injectResponseFactory')) { + $instance->injectResponseFactory( + $this->getMockBuilder(ResponseFactoryInterface::class)->getMockForAbstractClass() + ); + } $output = $this->callInaccessibleMethod( $instance, @@ -333,6 +339,12 @@ public function testCanPerformSubRenderingWithWithoutRelay(): void $instance->expects($this->once())->method('callSubControllerAction'); $instance->method('createHtmlResponse')->willReturn($response); $this->setInaccessiblePropertyValue($instance, 'extensionName', $this->extensionName); + if (method_exists($instance, 'injectResponseFactory')) { + $instance->injectResponseFactory( + $this->getMockBuilder(ResponseFactoryInterface::class)->getMockForAbstractClass() + ); + } + $this->callInaccessibleMethod( $instance, 'performSubRendering', @@ -444,11 +456,18 @@ public function testCanInitializeSettings(): void $request->expects($this->once())->method('getPluginName')->will($this->returnValue('void')); $this->setInaccessiblePropertyValue($instance, 'request', $request); $this->setInaccessiblePropertyValue($instance, 'provider', $provider); + $this->setInaccessiblePropertyValue($instance, 'settings', []); $this->setInaccessiblePropertyValue( $instance, 'configurationManager', $this->getMockBuilder(ConfigurationManagerInterface::class)->getMockForAbstractClass() ); + if (method_exists($instance, 'injectResponseFactory')) { + $instance->injectResponseFactory( + $this->getMockBuilder(ResponseFactoryInterface::class)->getMockForAbstractClass() + ); + } + $this->callInaccessibleMethod($instance, 'initializeSettings'); } @@ -582,6 +601,12 @@ public function testPerformSubRenderingCallsViewRenderOnNativeTarget(): void $view->expects($this->once())->method('render')->will($this->returnValue('test')); $this->setInaccessiblePropertyValue($instance, 'extensionName', $this->shortExtensionName); $this->setInaccessiblePropertyValue($instance, 'view', $view); + if (method_exists($instance, 'injectResponseFactory')) { + $instance->injectResponseFactory( + $this->getMockBuilder(ResponseFactoryInterface::class)->getMockForAbstractClass() + ); + } + $result = $this->callInaccessibleMethod( $instance, 'performSubRendering', @@ -650,6 +675,11 @@ public function testCallingSubControllerActionExecutesExpectedMethodsOnNestedObj 'Content', 'tx_flux_content' ); + + if ($result instanceof ResponseInterface) { + $result = $result->getBody()->getContents(); + } + $this->assertEquals('test', $result); } @@ -730,6 +760,7 @@ public function testCanUseTypoScriptSettingsInsteadOfFlexFormDataWhenRequested() $settings = [ 'useTypoScript' => true ]; + $this->setInaccessiblePropertyValue($instance, 'settings', []); $previousSettings = $this->getInaccessiblePropertyValue($instance, 'settings'); $this->setInaccessiblePropertyValue($instance, 'settings', $settings); $this->callInaccessibleMethod($instance, 'initializeProvider');