From f9236a4c6fe0501e62ef1dbc35d3162da60d999a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 23 Jul 2024 17:21:39 +0200 Subject: [PATCH] Unit test the accessor method on all pages --- .../tests/Unit/Pages/BladePageUnitTest.php | 17 +++++++++++++++++ .../Unit/Pages/DocumentationPageUnitTest.php | 17 +++++++++++++++++ .../tests/Unit/Pages/HtmlPageUnitTest.php | 17 +++++++++++++++++ .../tests/Unit/Pages/InMemoryPageUnitTest.php | 17 +++++++++++++++++ .../tests/Unit/Pages/MarkdownPageUnitTest.php | 17 +++++++++++++++++ .../tests/Unit/Pages/MarkdownPostUnitTest.php | 17 +++++++++++++++++ 6 files changed, 102 insertions(+) diff --git a/packages/framework/tests/Unit/Pages/BladePageUnitTest.php b/packages/framework/tests/Unit/Pages/BladePageUnitTest.php index fe1834b067a..14cec200c52 100644 --- a/packages/framework/tests/Unit/Pages/BladePageUnitTest.php +++ b/packages/framework/tests/Unit/Pages/BladePageUnitTest.php @@ -179,4 +179,21 @@ public function testMatter() { $this->assertInstanceOf(FrontMatter::class, (new BladePage('foo'))->matter()); } + + public function testGetCanonicalUrl() + { + $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/packages/framework/tests/Unit/Pages/DocumentationPageUnitTest.php b/packages/framework/tests/Unit/Pages/DocumentationPageUnitTest.php index 4017fcf60e7..53aa35a73c5 100644 --- a/packages/framework/tests/Unit/Pages/DocumentationPageUnitTest.php +++ b/packages/framework/tests/Unit/Pages/DocumentationPageUnitTest.php @@ -202,4 +202,21 @@ public function testSave() $this->assertFileExists('_docs/foo.md'); Filesystem::unlink('_docs/foo.md'); } + + public function testGetCanonicalUrl() + { + $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/packages/framework/tests/Unit/Pages/HtmlPageUnitTest.php b/packages/framework/tests/Unit/Pages/HtmlPageUnitTest.php index f5f299fd81c..4603f5942bd 100644 --- a/packages/framework/tests/Unit/Pages/HtmlPageUnitTest.php +++ b/packages/framework/tests/Unit/Pages/HtmlPageUnitTest.php @@ -217,4 +217,21 @@ public function testMatter() { $this->assertInstanceOf(FrontMatter::class, (new HtmlPage('404'))->matter()); } + + public function testGetCanonicalUrl() + { + $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/packages/framework/tests/Unit/Pages/InMemoryPageUnitTest.php b/packages/framework/tests/Unit/Pages/InMemoryPageUnitTest.php index b2a9c2e6f54..acfb0da0ed2 100644 --- a/packages/framework/tests/Unit/Pages/InMemoryPageUnitTest.php +++ b/packages/framework/tests/Unit/Pages/InMemoryPageUnitTest.php @@ -221,4 +221,21 @@ public function testMatter() { $this->assertInstanceOf(FrontMatter::class, (new InMemoryPage('404'))->matter()); } + + public function testGetCanonicalUrl() + { + $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/packages/framework/tests/Unit/Pages/MarkdownPageUnitTest.php b/packages/framework/tests/Unit/Pages/MarkdownPageUnitTest.php index 42a51565760..1c45d0181b5 100644 --- a/packages/framework/tests/Unit/Pages/MarkdownPageUnitTest.php +++ b/packages/framework/tests/Unit/Pages/MarkdownPageUnitTest.php @@ -230,4 +230,21 @@ public function testSave() $this->assertFileExists('_pages/foo.md'); Filesystem::unlink('_pages/foo.md'); } + + public function testGetCanonicalUrl() + { + $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/packages/framework/tests/Unit/Pages/MarkdownPostUnitTest.php b/packages/framework/tests/Unit/Pages/MarkdownPostUnitTest.php index 4ed8dfbec2d..7768d9c2d2d 100644 --- a/packages/framework/tests/Unit/Pages/MarkdownPostUnitTest.php +++ b/packages/framework/tests/Unit/Pages/MarkdownPostUnitTest.php @@ -230,4 +230,21 @@ public function testSave() $this->assertFileExists('_posts/foo.md'); Filesystem::unlink('_posts/foo.md'); } + + public function testGetCanonicalUrl() + { + $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()); + } }