Commit 51b87e9b authored by Benni Mack's avatar Benni Mack Committed by Stefan Bürk
Browse files

[TASK] Streamline Extbase URI Builder

This change removes some workarounds in
Extbase URI Builder which are not needed
anymore due to PSR-7 request objects.

Resolves: #99676
Releases: main
Change-Id: I4e9311281f6c3ab3726e49579548910d64225144
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/77532


Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
parent f78c13ed
......@@ -1515,11 +1515,6 @@ parameters:
count: 1
path: ../../typo3/sysext/extbase/Classes/Mvc/Request.php
-
message: "#^Dead catch \\- TYPO3\\\\CMS\\\\Backend\\\\Routing\\\\Exception\\\\ResourceNotFoundException is never thrown in the try block\\.$#"
count: 1
path: ../../typo3/sysext/extbase/Classes/Mvc/Web/Routing/UriBuilder.php
-
message: "#^Call to an undefined method TYPO3\\\\CMS\\\\Extbase\\\\DomainObject\\\\DomainObjectInterface\\:\\:_isDirty\\(\\)\\.$#"
count: 1
......
......@@ -18,7 +18,6 @@ declare(strict_types=1);
namespace TYPO3\CMS\Extbase\Mvc\Web\Routing;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException;
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
use TYPO3\CMS\Backend\Routing\Route;
use TYPO3\CMS\Core\Http\ApplicationType;
......@@ -582,20 +581,18 @@ class UriBuilder
/**
* Builds the URI, backend flavour
* The resulting URI is relative and starts with "index.php".
* The settings pageUid, pageType, noCache & linkAccessRestrictedPages
* will be ignored in the backend.
*
* @return string The URI
* @internal only to be used within Extbase, not part of TYPO3 Core API.
* @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::getQueryArguments
*/
public function buildBackendUri(): string
{
$arguments = [];
$request = $this->getRequest();
if ($this->addQueryString === true) {
$arguments = GeneralUtility::_GET();
$arguments = $request?->getQueryParams() ?? [];
foreach ($this->argumentsToBeExcludedFromQueryString as $argumentToBeExcluded) {
$argumentArrayToBeExcluded = [];
parse_str($argumentToBeExcluded, $argumentArrayToBeExcluded);
......@@ -609,28 +606,24 @@ class UriBuilder
}
if (($route = $request?->getAttribute('route')) instanceof Route) {
/** @var Route $route */
$arguments['route'] = $route->getPath();
$arguments['route'] = $route->getOption('_identifier');
}
ArrayUtility::mergeRecursiveWithOverrule($arguments, $this->arguments);
$arguments = array_replace_recursive($arguments, $this->arguments);
$arguments = $this->convertDomainObjectsToIdentityArrays($arguments);
$this->lastArguments = $arguments;
$routeName = $arguments['route'] ?? null;
$routeIdentifier = $arguments['route'] ?? null;
unset($arguments['route'], $arguments['token']);
$backendUriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
try {
if ($this->createAbsoluteUri) {
$uri = (string)$backendUriBuilder->buildUriFromRoutePath($routeName, $arguments, \TYPO3\CMS\Backend\Routing\UriBuilder::ABSOLUTE_URL);
} else {
$uri = (string)$backendUriBuilder->buildUriFromRoutePath($routeName, $arguments, \TYPO3\CMS\Backend\Routing\UriBuilder::ABSOLUTE_PATH);
}
} catch (ResourceNotFoundException $e) {
$uri = '';
if ($routeIdentifier) {
$backendUriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
try {
if ($this->createAbsoluteUri) {
$uri = (string)$backendUriBuilder->buildUriFromRoute($routeName, $arguments, \TYPO3\CMS\Backend\Routing\UriBuilder::ABSOLUTE_URL);
$uri = (string)$backendUriBuilder->buildUriFromRoute($routeIdentifier, $arguments, \TYPO3\CMS\Backend\Routing\UriBuilder::ABSOLUTE_URL);
} else {
$uri = (string)$backendUriBuilder->buildUriFromRoute($routeName, $arguments, \TYPO3\CMS\Backend\Routing\UriBuilder::ABSOLUTE_PATH);
$uri = (string)$backendUriBuilder->buildUriFromRoute($routeIdentifier, $arguments);
}
} catch (RouteNotFoundException $e) {
// return empty URL
$uri = '';
}
}
......
......@@ -223,12 +223,9 @@ class UriBuilderTest extends UnitTestCase
*/
public function buildBackendUriKeepsQueryParametersIfAddQueryStringIsSet(): void
{
$_GET['id'] = 'pageId';
$_GET['foo'] = 'bar';
$extbaseParameters = new ExtbaseRequestParameters();
$serverRequest = $this->getRequestWithRouteAttribute()->withQueryParams($_GET)
->withAttribute('extbase', $extbaseParameters)
;
$serverRequest = $this->getRequestWithRouteAttribute()->withQueryParams(['id' => 'pageId', 'foo' => 'bar'])
->withAttribute('extbase', $extbaseParameters);
$request = new Request($serverRequest);
$this->uriBuilder->setRequest($request);
$this->uriBuilder->setAddQueryString(true);
......@@ -242,12 +239,10 @@ class UriBuilderTest extends UnitTestCase
*/
public function buildBackendUriRouteAttributeOverrulesGetParameterIfAddQueryStringIsSet(): void
{
$_GET = ['route' => 'test/Path', 'id' => 'pageId', 'foo' => 'bar'];
$_POST = [];
$extbaseParameters = new ExtbaseRequestParameters();
$serverRequest = $this->getRequestWithRouteAttribute('test/Path2')->withQueryParams($_GET)
->withAttribute('extbase', $extbaseParameters)
;
$serverRequest = $this->getRequestWithRouteAttribute('module_key2')
->withQueryParams(['route' => 'module_key', 'id' => 'pageId', 'foo' => 'bar'])
->withAttribute('extbase', $extbaseParameters);
$request = new Request($serverRequest);
$this->uriBuilder->setRequest($request);
$this->uriBuilder->setAddQueryString(true);
......@@ -266,7 +261,7 @@ class UriBuilderTest extends UnitTestCase
[
'id' => 'pageId',
'myparam' => 'pageId',
'route' => '/test/Path',
'route' => 'module_key',
'foo' => 'bar',
],
[
......@@ -278,7 +273,7 @@ class UriBuilderTest extends UnitTestCase
'Arguments to be excluded in the end' => [
[
'foo' => 'bar',
'route' => '/test/Path',
'route' => 'module_key',
'id' => 'pageId',
'myparam' => 'anyway',
],
......@@ -294,7 +289,7 @@ class UriBuilderTest extends UnitTestCase
'bar' => 'baz',
],
'id' => 'pageId',
'route' => '/test/Path',
'route' => 'module_key',
],
[
'id',
......@@ -310,7 +305,7 @@ class UriBuilderTest extends UnitTestCase
],
],
'id' => 'pageId',
'route' => '/test/Path',
'route' => 'module_key',
],
[
'id',
......@@ -324,11 +319,14 @@ class UriBuilderTest extends UnitTestCase
/**
* @test
* @dataProvider buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider
* @param string $expected
*/
public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSet(array $parameters, array $excluded, $expected): void
public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSet(array $parameters, array $excluded, string $expected): void
{
$_GET = array_replace_recursive($_GET, $parameters);
$serverRequest = $this->getRequestWithRouteAttribute()
->withQueryParams($parameters)
->withAttribute('extbase', new ExtbaseRequestParameters());
$request = new Request($serverRequest);
$this->uriBuilder->setRequest($request);
$this->uriBuilder->setAddQueryString(true);
$this->uriBuilder->setArgumentsToBeExcludedFromQueryString($excluded);
$actualResult = $this->uriBuilder->buildBackendUri();
......@@ -340,10 +338,9 @@ class UriBuilderTest extends UnitTestCase
*/
public function buildBackendUriKeepsModuleQueryParametersIfAddQueryStringIsNotSet(): void
{
$_GET = (['id' => 'pageId', 'foo' => 'bar']);
$extbaseParameters = new ExtbaseRequestParameters();
$serverRequest = $this->getRequestWithRouteAttribute()->withQueryParams($_GET)
->withAttribute('extbase', $extbaseParameters)
$serverRequest = $this->getRequestWithRouteAttribute()
->withQueryParams(['id' => 'pageId', 'foo' => 'bar'])
->withAttribute('extbase', new ExtbaseRequestParameters())
;
$request = new Request($serverRequest);
$this->uriBuilder->setRequest($request);
......@@ -357,14 +354,12 @@ class UriBuilderTest extends UnitTestCase
*/
public function buildBackendUriMergesAndOverrulesQueryParametersWithArguments(): void
{
$_GET = ['id' => 'pageId', 'foo' => 'bar'];
$extbaseParameters = new ExtbaseRequestParameters();
$serverRequest = $this->getRequestWithRouteAttribute()->withQueryParams($_GET)
->withAttribute('extbase', $extbaseParameters)
;
$serverRequest = $this->getRequestWithRouteAttribute()
->withQueryParams(['id' => 'pageId', 'foo' => 'bar'])
->withAttribute('extbase', new ExtbaseRequestParameters());
$request = new Request($serverRequest);
$this->uriBuilder->setRequest($request);
$this->uriBuilder->setArguments(['route' => '/test/Path2', 'somePrefix' => ['bar' => 'baz']]);
$this->uriBuilder->setArguments(['route' => 'module_key2', 'somePrefix' => ['bar' => 'baz']]);
$expectedResult = '/typo3/test/Path2?token=dummyToken&id=pageId&somePrefix%5Bbar%5D=baz';
$actualResult = $this->uriBuilder->buildBackendUri();
self::assertEquals($expectedResult, $actualResult);
......@@ -375,10 +370,8 @@ class UriBuilderTest extends UnitTestCase
*/
public function buildBackendUriConvertsDomainObjectsAfterArgumentsHaveBeenMerged(): void
{
$extbaseParameters = new ExtbaseRequestParameters();
$serverRequest = $this->getRequestWithRouteAttribute()
->withAttribute('extbase', $extbaseParameters)
;
->withAttribute('extbase', new ExtbaseRequestParameters());
$request = new Request($serverRequest);
$this->uriBuilder->setRequest($request);
$mockDomainObject = $this->getAccessibleMock(AbstractEntity::class, null);
......@@ -394,10 +387,8 @@ class UriBuilderTest extends UnitTestCase
*/
public function buildBackendUriRespectsSection(): void
{
$extbaseParameters = new ExtbaseRequestParameters();
$serverRequest = $this->getRequestWithRouteAttribute()
->withAttribute('extbase', $extbaseParameters)
;
->withAttribute('extbase', new ExtbaseRequestParameters());
$request = new Request($serverRequest);
$this->uriBuilder->setRequest($request);
$this->uriBuilder->setSection('someSection');
......@@ -414,11 +405,11 @@ class UriBuilderTest extends UnitTestCase
$request = $this->getRequestWithRouteAttribute(baseUri: 'http://baseuri/typo3/')
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE)
->withAttribute('normalizedParams', NormalizedParams::createFromServerParams($_SERVER))
->withAttribute('extbase', new ExtbaseRequestParameters(''));
->withAttribute('extbase', new ExtbaseRequestParameters());
$mvcRequest = new Request($request);
$this->uriBuilder->setRequest($mvcRequest);
$this->uriBuilder->setCreateAbsoluteUri(true);
$this->uriBuilder->setArguments(['route' => '/test/Path']);
$this->uriBuilder->setArguments(['route' => 'module_key']);
$backendUriBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class);
$backendUriBuilder->setRequestContext(new RequestContext(host: 'baseuri/typo3'));
$expectedResult = 'http://baseuri/typo3/test/Path?token=dummyToken';
......@@ -915,8 +906,8 @@ class UriBuilderTest extends UnitTestCase
self::assertIsArray($result);
}
protected function getRequestWithRouteAttribute(string $path = '/test/Path', string $baseUri = ''): ServerRequestInterface
protected function getRequestWithRouteAttribute(string $routeIdentifier = 'module_key', string $baseUri = ''): ServerRequestInterface
{
return (new ServerRequest(new Uri($baseUri)))->withAttribute('route', new Route($path, []));
return (new ServerRequest(new Uri($baseUri)))->withAttribute('route', new Route('/test/Path', ['_identifier' => $routeIdentifier]));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment