Skip to content

Commit

Permalink
[BUGFIX] Model redirects in Extbase actions as proper responses
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverklee committed Sep 23, 2024
1 parent b44597c commit ff24911
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Classes/Controller/BackEnd/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@ class RegistrationController extends ActionController
use EventStatisticsTrait;
use PageUidTrait;
use PermissionsTrait;
use RedirectTrait;

/**
* @var non-empty-string
Expand Down Expand Up @@ -115,14 +117,14 @@ 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);

$message = $this->languageService
->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]);
}
}
45 changes: 45 additions & 0 deletions Classes/Controller/RedirectTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace OliverKlee\Seminars\Controller;

use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

/**
* This convenience trait provides a method to redirect to a different action that is also compatible
* with TYPO3 12LTS and 13LTS.
*
* @phpstan-require-extends ActionController
*
* @internal
*/
trait RedirectTrait
{
/**
* @param array<string, string|int> $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);
}
}
7 changes: 6 additions & 1 deletion Tests/Unit/Controller/BackEnd/RegistrationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

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

0 comments on commit ff24911

Please sign in to comment.