Skip to content

Commit

Permalink
[BUGFIX] Fix support for controller arguments and redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Dec 9, 2024
1 parent a167840 commit 8610fcd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
24 changes: 11 additions & 13 deletions Classes/Controller/AbstractFluxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -458,15 +458,19 @@ protected function hasSubControllerActionOnForeignController(

/**
* @param class-string $controllerClassName
* @return \Psr\Http\Message\ResponseInterface|ResponseInterface|null
*/
protected function callSubControllerAction(
string $extensionName,
string $controllerClassName,
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(
Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions Tests/Unit/Controller/AbstractFluxControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -650,6 +675,11 @@ public function testCallingSubControllerActionExecutesExpectedMethodsOnNestedObj
'Content',
'tx_flux_content'
);

if ($result instanceof ResponseInterface) {
$result = $result->getBody()->getContents();
}

$this->assertEquals('test', $result);
}

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 8610fcd

Please sign in to comment.