From ff24911f78c033ab8584c3d774ca34a991051258 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Mon, 23 Sep 2024 10:05:52 +0200 Subject: [PATCH] [BUGFIX] Model redirects in Extbase actions as proper responses --- .../BackEnd/RegistrationController.php | 6 ++- Classes/Controller/RedirectTrait.php | 45 +++++++++++++++++++ .../BackEnd/RegistrationControllerTest.php | 7 ++- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 Classes/Controller/RedirectTrait.php diff --git a/Classes/Controller/BackEnd/RegistrationController.php b/Classes/Controller/BackEnd/RegistrationController.php index f5c354bd6..494c1f392 100644 --- a/Classes/Controller/BackEnd/RegistrationController.php +++ b/Classes/Controller/BackEnd/RegistrationController.php @@ -4,6 +4,7 @@ namespace OliverKlee\Seminars\Controller\BackEnd; +use OliverKlee\Seminars\Controller\RedirectTrait; use OliverKlee\Seminars\Csv\CsvDownloader; use OliverKlee\Seminars\Csv\CsvResponse; use OliverKlee\Seminars\Domain\Model\Event\Event; @@ -23,6 +24,7 @@ class RegistrationController extends ActionController use EventStatisticsTrait; use PageUidTrait; use PermissionsTrait; + use RedirectTrait; /** * @var non-empty-string @@ -115,7 +117,7 @@ private function exportCsvAction(): ResponseInterface * @param positive-int $registrationUid * @param positive-int $eventUid */ - public function deleteAction(int $registrationUid, int $eventUid): void + public function deleteAction(int $registrationUid, int $eventUid): ResponseInterface { $this->registrationRepository->deleteViaDataHandler($registrationUid); @@ -123,6 +125,6 @@ public function deleteAction(int $registrationUid, int $eventUid): void ->sL('LLL:EXT:seminars/Resources/Private/Language/locallang.xml:backEndModule.message.registrationDeleted'); $this->addFlashMessage($message); - $this->redirect('showForEvent', 'BackEnd\\Registration', null, ['eventUid' => $eventUid]); + return $this->buildRedirectToAction('showForEvent', 'BackEnd\\Registration', null, ['eventUid' => $eventUid]); } } diff --git a/Classes/Controller/RedirectTrait.php b/Classes/Controller/RedirectTrait.php new file mode 100644 index 000000000..04a47b323 --- /dev/null +++ b/Classes/Controller/RedirectTrait.php @@ -0,0 +1,45 @@ + $arguments + */ + private function buildRedirectToAction( + string $actionName, + ?string $controllerName = null, + ?string $extensionName = null, + array $arguments = [], + ?int $pageUid = null + ): ResponseInterface { + if ($controllerName === null) { + $controllerName = $this->request->getControllerName(); + } + $this->uriBuilder->reset()->setCreateAbsoluteUri(true); + if (\is_int($pageUid)) { + $this->uriBuilder->setTargetPageUid($pageUid); + } + if (GeneralUtility::getIndpEnv('TYPO3_SSL')) { + $this->uriBuilder->setAbsoluteUriScheme('https'); + } + $uri = $this->uriBuilder->uriFor($actionName, $arguments, $controllerName, $extensionName); + + return $this->responseFactory->createResponse(307)->withHeader('Location', $uri); + } +} diff --git a/Tests/Unit/Controller/BackEnd/RegistrationControllerTest.php b/Tests/Unit/Controller/BackEnd/RegistrationControllerTest.php index 5dbc88513..4c1e14687 100644 --- a/Tests/Unit/Controller/BackEnd/RegistrationControllerTest.php +++ b/Tests/Unit/Controller/BackEnd/RegistrationControllerTest.php @@ -20,6 +20,7 @@ use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; +use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; use TYPO3\CMS\Fluid\View\TemplateView; use TYPO3\TestingFramework\Core\AccessibleObjectInterface; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; @@ -92,6 +93,7 @@ protected function setUp(): void ); $this->subject = $subject; + $this->subject->_set('uriBuilder', $this->createStub(UriBuilder::class)); $responseStub = $this->createStub(HtmlResponse::class); $this->subject->method('htmlResponse')->willReturn($responseStub); @@ -583,6 +585,9 @@ public function deleteActionRedirectsToShowRegistrationsForEventAction(): void $this->mockRedirect('showForEvent', 'BackEnd\\Registration', null, ['eventUid' => $eventUid]); - $this->subject->deleteAction(15, $eventUid); + $result = $this->subject->deleteAction(15, $eventUid); + + self::assertSame(307, $result->getStatusCode()); + self::assertSame('Location', $result->getHeaders()['Location'][0]); } }