Skip to content

Commit

Permalink
NEXT-15396 - Fix seo redirects in subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Jun 7, 2021
1 parent 8680047 commit 3df3fe7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Changed RequestTransformer to work with seo urls in subfolder
issue: NEXT-15396
---
# Storefront
* Changed `RequestTransformer` to fix seo urls in sub folder
2 changes: 2 additions & 0 deletions src/Core/SalesChannelRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class SalesChannelRequest

public const ATTRIBUTE_CANONICAL_LINK = 'sw-canonical-link';

public const ATTRIBUTE_STOREFRONT_URL = 'sw-storefront-url';

public const ATTRIBUTE_CSRF_PROTECTED = 'csrf_protected';

private function __construct()
Expand Down
12 changes: 11 additions & 1 deletion src/Storefront/Framework/Routing/RequestTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,19 @@ public function transform(Request $request): Request
);

if (isset($resolved['canonicalPathInfo'])) {
$urlPath = parse_url($salesChannel['url'], \PHP_URL_PATH);
if ($urlPath === false || $urlPath === null) {
$urlPath = '';
}

$baseUrlPath = trim($urlPath, '/');
if (\strlen($baseUrlPath) > 1 && strpos($baseUrlPath, '/') !== 0) {
$baseUrlPath = '/' . $baseUrlPath;
}

$transformedRequest->attributes->set(
SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK,
$this->getSchemeAndHttpHost($request) . $baseUrl . $resolved['canonicalPathInfo']
$this->getSchemeAndHttpHost($request) . $baseUrlPath . $resolved['canonicalPathInfo']
);
}

Expand Down
69 changes: 69 additions & 0 deletions src/Storefront/Test/Framework/Routing/RequestTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shopware\Storefront\Test\Framework\Routing;

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Seo\SeoResolver;
use Shopware\Core\Defaults;
Expand Down Expand Up @@ -221,6 +222,74 @@ public function domainProvider(): array
];
}

/**
* @dataProvider seoRedirectProvider
*/
public function testRedirectLinksUsesSalesChannelPath(string $baseUrl, string $virtualUrl, string $resolvedUrl): void
{
$gerUkId = Uuid::randomHex();

$gerDomainId = Uuid::randomHex();
$ukDomainId = Uuid::randomHex();

$salesChannels = $this->getSalesChannelWithGerAndUkDomain($gerUkId, $gerDomainId, 'http://base.test' . $virtualUrl, $ukDomainId, 'http://base.test/public/en');

$this->createSalesChannels([$salesChannels]);

$con = $this->getContainer()->get(Connection::class);
$con->insert(
'seo_url',
[
'id' => Uuid::randomBytes(),
'language_id' => Uuid::fromHexToBytes($this->deLanguageId),
'sales_channel_id' => Uuid::fromHexToBytes($gerUkId),
'foreign_key' => Uuid::randomBytes(),
'route_name' => 'test',
'path_info' => '/detail/87a78cf58f114d5587ae23c140825694',
'seo_path_info' => 'Test',
'is_canonical' => 1,
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
]
);

$request = Request::create('http://base.test' . $virtualUrl . '/detail/87a78cf58f114d5587ae23c140825694');
$ref = new \ReflectionClass($request);
$prob = $ref->getProperty('baseUrl');
$prob->setAccessible(true);
$prob->setValue($request, $baseUrl);

$resolved = $this->requestTransformer->transform($request);

static::assertSame('http://base.test' . $resolvedUrl, $resolved->attributes->get(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK));
}

public function seoRedirectProvider(): iterable
{
yield 'Use with base url' => [
'/public', // baseUrl
'/public/de', // Virtual URL
'/public/de/Test', // Resolved seo url
];

yield 'Use with base url in subfolder' => [
'/sw6/public', // baseUrl
'/sw6/public/de', // Virtual URL
'/sw6/public/de/Test', // Resolved seo url
];

yield 'With Virtual url' => [
'', // baseUrl
'/de', // Virtual URL
'/de/Test', // Resolved seo url
];

yield 'Without virtual URL' => [
'', // baseUrl
'', // Virtual URL
'/Test', // Resolved seo url
];
}

private function getEnglishSalesChannel(string $salesChannelId, string $domainId, string $url): array
{
return [
Expand Down

0 comments on commit 3df3fe7

Please sign in to comment.