From d54ea721448e2c792f2d5f02eadb8de27d41bee6 Mon Sep 17 00:00:00 2001 From: github-actions Date: Tue, 23 Jul 2024 15:27:33 +0000 Subject: [PATCH] Merge pull request #1885 from hydephp/cleanup-canonical-page-helper-method Cleanup the canonical URL helper method https://github.com/hydephp/develop/commit/c4f48343a6152d6b158f8d9b3fdb55b6691aac34 --- src/Pages/Concerns/HydePage.php | 7 +++++++ tests/Unit/Pages/BladePageUnitTest.php | 14 +++++++++++++- tests/Unit/Pages/DocumentationPageUnitTest.php | 14 +++++++++++++- tests/Unit/Pages/HtmlPageUnitTest.php | 14 +++++++++++++- tests/Unit/Pages/InMemoryPageUnitTest.php | 14 +++++++++++++- tests/Unit/Pages/MarkdownPageUnitTest.php | 14 +++++++++++++- tests/Unit/Pages/MarkdownPostUnitTest.php | 14 +++++++++++++- 7 files changed, 85 insertions(+), 6 deletions(-) diff --git a/src/Pages/Concerns/HydePage.php b/src/Pages/Concerns/HydePage.php index fb741358..08a730ea 100644 --- a/src/Pages/Concerns/HydePage.php +++ b/src/Pages/Concerns/HydePage.php @@ -397,6 +397,13 @@ public function navigationMenuGroup(): ?string return $this->navigation->group; } + /** + * Get the canonical URL for the page to use in the `` tag. + * + * It can be explicitly set in the front matter using the `canonicalUrl` key, + * otherwise it will be generated based on the site URL and the output path, + * unless there is no configured base URL, leading to this returning null. + */ public function getCanonicalUrl(): ?string { /** @var ?string $value */ diff --git a/tests/Unit/Pages/BladePageUnitTest.php b/tests/Unit/Pages/BladePageUnitTest.php index f15f4292..14cec200 100644 --- a/tests/Unit/Pages/BladePageUnitTest.php +++ b/tests/Unit/Pages/BladePageUnitTest.php @@ -182,6 +182,18 @@ public function testMatter() public function testGetCanonicalUrl() { - $this->markTestSkipped('Not yet implemented'); + $page = new BladePage('foo'); + $this->assertNull($page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + + $this->assertSame('https://example.com/foo', $page->getCanonicalUrl()); + + $page = new BladePage('foo', ['canonicalUrl' => 'foo']); + $this->assertSame('foo', $page->getCanonicalUrl()); } } diff --git a/tests/Unit/Pages/DocumentationPageUnitTest.php b/tests/Unit/Pages/DocumentationPageUnitTest.php index b1ea51dd..53aa35a7 100644 --- a/tests/Unit/Pages/DocumentationPageUnitTest.php +++ b/tests/Unit/Pages/DocumentationPageUnitTest.php @@ -205,6 +205,18 @@ public function testSave() public function testGetCanonicalUrl() { - $this->markTestSkipped('Not yet implemented'); + $page = new DocumentationPage('foo'); + $this->assertNull($page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/docs/foo.html', $page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + + $this->assertSame('https://example.com/docs/foo', $page->getCanonicalUrl()); + + $page = new DocumentationPage('foo', ['canonicalUrl' => 'foo']); + $this->assertSame('foo', $page->getCanonicalUrl()); } } diff --git a/tests/Unit/Pages/HtmlPageUnitTest.php b/tests/Unit/Pages/HtmlPageUnitTest.php index ac9f12cd..4603f594 100644 --- a/tests/Unit/Pages/HtmlPageUnitTest.php +++ b/tests/Unit/Pages/HtmlPageUnitTest.php @@ -220,6 +220,18 @@ public function testMatter() public function testGetCanonicalUrl() { - $this->markTestSkipped('Not yet implemented'); + $page = new HtmlPage('foo'); + $this->assertNull($page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + + $this->assertSame('https://example.com/foo', $page->getCanonicalUrl()); + + $page = new HtmlPage('foo', ['canonicalUrl' => 'foo']); + $this->assertSame('foo', $page->getCanonicalUrl()); } } diff --git a/tests/Unit/Pages/InMemoryPageUnitTest.php b/tests/Unit/Pages/InMemoryPageUnitTest.php index 6c913084..acfb0da0 100644 --- a/tests/Unit/Pages/InMemoryPageUnitTest.php +++ b/tests/Unit/Pages/InMemoryPageUnitTest.php @@ -224,6 +224,18 @@ public function testMatter() public function testGetCanonicalUrl() { - $this->markTestSkipped('Not yet implemented'); + $page = new InMemoryPage('foo'); + $this->assertNull($page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + + $this->assertSame('https://example.com/foo', $page->getCanonicalUrl()); + + $page = new InMemoryPage('foo', ['canonicalUrl' => 'foo']); + $this->assertSame('foo', $page->getCanonicalUrl()); } } diff --git a/tests/Unit/Pages/MarkdownPageUnitTest.php b/tests/Unit/Pages/MarkdownPageUnitTest.php index 1c04e970..1c45d018 100644 --- a/tests/Unit/Pages/MarkdownPageUnitTest.php +++ b/tests/Unit/Pages/MarkdownPageUnitTest.php @@ -233,6 +233,18 @@ public function testSave() public function testGetCanonicalUrl() { - $this->markTestSkipped('Not yet implemented'); + $page = new MarkdownPage('foo'); + $this->assertNull($page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + + $this->assertSame('https://example.com/foo', $page->getCanonicalUrl()); + + $page = new MarkdownPage('foo', ['canonicalUrl' => 'foo']); + $this->assertSame('foo', $page->getCanonicalUrl()); } } diff --git a/tests/Unit/Pages/MarkdownPostUnitTest.php b/tests/Unit/Pages/MarkdownPostUnitTest.php index ade5e74b..7768d9c2 100644 --- a/tests/Unit/Pages/MarkdownPostUnitTest.php +++ b/tests/Unit/Pages/MarkdownPostUnitTest.php @@ -233,6 +233,18 @@ public function testSave() public function testGetCanonicalUrl() { - $this->markTestSkipped('Not yet implemented'); + $page = new MarkdownPost('foo'); + $this->assertNull($page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/posts/foo.html', $page->getCanonicalUrl()); + + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + + $this->assertSame('https://example.com/posts/foo', $page->getCanonicalUrl()); + + $page = new MarkdownPost('foo', ['canonicalUrl' => 'foo']); + $this->assertSame('foo', $page->getCanonicalUrl()); } }