Skip to content

Commit

Permalink
[FEATURE] Add helper for handling redirects in plugins (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
twoldanski authored May 9, 2024
1 parent 671025c commit bd23453
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Classes/Utility/PluginUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Utility;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\PropagateResponseException;

class PluginUtility
{
public function __construct(private readonly UrlUtility $urlUtility) {}

public function redirect(ServerRequestInterface $request, string $uri, int $statusCode = 307): never
{
throw new PropagateResponseException(new JsonResponse([
'redirectUrl' => $this->urlUtility->withRequest($request)->prepareRelativeUrlIfPossible($uri),
'statusCode' => $statusCode,
]));
}
}
50 changes: 50 additions & 0 deletions Tests/Unit/Utility/PluginUtilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Tests\Unit\Utility;

use FriendsOfTYPO3\Headless\Utility\PluginUtility;
use FriendsOfTYPO3\Headless\Utility\UrlUtility;
use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\ExpressionLanguage\Resolver;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Site\SiteFinder;

use function json_decode;

class PluginUtilityTest extends TestCase
{
public function testProperException(): void
{
$urlUtility = new UrlUtility(new Features(), $this->createMock(Resolver::class), $this->createMock(SiteFinder::class));

$pluginRedirect = new PluginUtility($urlUtility);

$this->expectException(PropagateResponseException::class);

$pluginRedirect->redirect(new ServerRequest(), '/test');
}

public function testResponse(): void
{
$urlUtility = new UrlUtility(new Features(), $this->createMock(Resolver::class), $this->createMock(SiteFinder::class));

$pluginRedirect = new PluginUtility($urlUtility);

try {
$pluginRedirect->redirect(new ServerRequest(), '/test');
} catch (PropagateResponseException $exception) {
self::assertSame(['redirectUrl' => '/test', 'statusCode' => 307], json_decode($exception->getResponse()->getBody()->getContents(), true));
}
}
}

0 comments on commit bd23453

Please sign in to comment.