Skip to content

Commit

Permalink
Make sure the custom canonical URI can be retrieved as is
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Oct 27, 2021
1 parent a58098b commit 22d8d49
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/pages/PageRegular.php
Expand Up @@ -227,7 +227,7 @@ protected function prepare($objPage)
// Canonical
if ($objPage->enableCanonical)
{
$this->Template->canonical = $this->responseContext->get(HtmlHeadBag::class)->getCanonicalUri($request);
$this->Template->canonical = $this->responseContext->get(HtmlHeadBag::class)->getCanonicalUriForRequest($request);
}

// Fall back to the default title tag
Expand Down
Expand Up @@ -84,7 +84,12 @@ public function setCanonicalUri(string $canonicalUri): self
return $this;
}

public function getCanonicalUri(Request $request): string
public function getCanonicalUri(): string
{
return $this->canonicalUri;
}

public function getCanonicalUriForRequest(Request $request): string
{
if ($this->canonicalUri) {
return $this->canonicalUri;
Expand Down
Expand Up @@ -46,10 +46,11 @@ public function testCanonicalHandling(): void
$this->assertSame(['foo', 'page'], $manager->getKeepParamsForCanonical());

$request = Request::create('https://contao.org/foobar/page?query=test&foo=bar&baz=bak&page=12');
$this->assertSame('https://contao.org/foobar/page?foo=bar&page=12', $manager->getCanonicalUri($request));
$this->assertSame('https://contao.org/foobar/page?foo=bar&page=12', $manager->getCanonicalUriForRequest($request));

$manager->setCanonicalUri('https://example.com/i-decided-myself');
$this->assertSame('https://example.com/i-decided-myself', $manager->getCanonicalUri($request));
$manager->setCanonicalUri('https://example.com/i-decided-myself?page=23&foo=bar');
$this->assertSame('https://example.com/i-decided-myself?page=23&foo=bar', $manager->getCanonicalUri());
$this->assertSame('https://example.com/i-decided-myself?page=23&foo=bar', $manager->getCanonicalUriForRequest($request));
}

public function testCanonicalWithWildCards(): void
Expand All @@ -58,6 +59,6 @@ public function testCanonicalWithWildCards(): void
$manager->setKeepParamsForCanonical(['foo', 'page_*']);

$request = Request::create('https://contao.org/foobar/page?query=test&foo=bar&baz=bak&page_42=12');
$this->assertSame('https://contao.org/foobar/page?foo=bar&page_42=12', $manager->getCanonicalUri($request));
$this->assertSame('https://contao.org/foobar/page?foo=bar&page_42=12', $manager->getCanonicalUriForRequest($request));
}
}

0 comments on commit 22d8d49

Please sign in to comment.