Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Improve handling of frontend urls #772

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Classes/Utility/UrlUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FriendsOfTYPO3\Headless\Utility;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\ExpressionLanguage\SyntaxError;
Expand All @@ -29,6 +30,8 @@
use function ltrim;
use function rtrim;
use function str_contains;
use function strlen;
use function substr;

class UrlUtility implements LoggerAwareInterface, HeadlessFrontendUrlInterface
{
Expand Down Expand Up @@ -83,8 +86,6 @@ public function getFrontendUrlWithSite($url, SiteInterface $site, string $return
}

try {
$base = $site->getBase()->getHost();
$port = $site->getBase()->getPort();
$frontendBaseUrl = $this->resolveWithVariants(
$this->conf[$returnField] ?? '',
$this->variants,
Expand All @@ -101,14 +102,14 @@ public function getFrontendUrlWithSite($url, SiteInterface $site, string $return
$frontPort = $frontendBase->getPort();
$targetUri = new Uri($this->sanitizeBaseUrl($url));

if (str_contains($url, $base)) {
if (str_contains($url, $site->getBase()->getHost())) {
$targetUri = $targetUri->withHost($frontBase);
if ($frontExtraPath) {
$targetUri = $targetUri->withPath($frontExtraPath . ($targetUri->getPath() !== '' ? '/' . ltrim($targetUri->getPath(), '/') : ''));
$targetUri = $targetUri->withPath($this->handleFrontendAndBackendPaths($frontExtraPath, $targetUri, $site->getBase()->getPath()));
}
}

if ($port === $frontPort) {
if ($site->getBase()->getPort() === $frontPort) {
return (string)$targetUri;
}

Expand Down Expand Up @@ -286,4 +287,9 @@ private function extractConfigurationFromRequest(ServerRequestInterface $request

return $object;
}

private function handleFrontendAndBackendPaths(string $frontendPath, UriInterface $targetUri, string $baseBackendPath = ''): string
{
return rtrim($frontendPath, '/') . ($targetUri->getPath() !== '' ? '/' . ltrim(substr($targetUri->getPath(), strlen($baseBackendPath)), '/') : '');
}
}
34 changes: 31 additions & 3 deletions Tests/Unit/Utility/UrlUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
class UrlUtilityTest extends UnitTestCase
{
use ProphecyTrait;

public function testFrontendUrls(): void
{
$headlessMode = $this->createHeadlessMode();

$site = $this->prophesize(Site::class);
$site->getBase()->shouldBeCalled(2)->willReturn(new Uri('https://test-backend-api.tld'));
$site->getBase()->shouldBeCalled(2)->willReturn(new Uri('https://test-backend-api.tld/'));
$site->getConfiguration()->shouldBeCalled(3)->willReturn([
'base' => 'https://www.typo3.org',
'languages' => [],
Expand Down Expand Up @@ -102,7 +101,7 @@ public function testFrontendUrls(): void

$siteFinder = $this->prophesize(SiteFinder::class);

$site->getBase()->shouldBeCalled(2)->willReturn(new Uri('https://test-backend3-api.tld'));
$site->getBase()->shouldBeCalled(2)->willReturn(new Uri('https://test-backend3-api.tld/'));

$urlUtility = new UrlUtility(null, $resolver->reveal(), $siteFinder->reveal(), null, $headlessMode);
$urlUtility = $urlUtility->withSite($site->reveal());
Expand All @@ -115,6 +114,35 @@ public function testFrontendUrls(): void
self::assertSame('#fragment-123', $urlUtility->getFrontendUrlWithSite('#fragment-123', $site->reveal()));
}

public function testFrontendUrlsWithDifferentPaths(): void
{
$headlessMode = $this->createHeadlessMode();

$site = $this->prophesize(Site::class);
$site->getBase()->shouldBeCalled(1)->willReturn(new Uri('https://test-backend-api.tld/dev-path/'));
$site->getConfiguration()->shouldBeCalled(3)->willReturn([
'base' => 'https://www.typo3.org',
'languages' => [],
'baseVariants' => [
[
'base' => 'https://test-backend-api.tld/dev-path/',
'condition' => 'applicationContext == "Development"',
'frontendBase' => 'https://test-frontend.tld/frontend',
],
],
]);

$resolver = $this->prophesize(Resolver::class);
$resolver->evaluate(Argument::containingString('Development'))->willReturn(true);

$siteFinder = $this->prophesize(SiteFinder::class);

$urlUtility = new UrlUtility(null, $resolver->reveal(), $siteFinder->reveal(), null, $headlessMode);
$urlUtility = $urlUtility->withSite($site->reveal());

self::assertSame('https://test-frontend.tld/frontend/content-page', $urlUtility->getFrontendUrlWithSite('https://test-backend-api.tld/dev-path/content-page', $site->reveal()));
}

public function testFrontendUrlsWithBaseProductionAndLocalOverride(): void
{
$site = $this->prophesize(Site::class);
Expand Down