From 0439c1625ef0a1d417901145b9f8b81030e634a0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 12 Jun 2024 18:00:07 +0200 Subject: [PATCH] Method `Hyde::hasSiteUrl()` now returns false when URL is for localhost This better matches the documented method description of "Check if a site base URL has been set in config (or .env).", as this value is `localhost` by default and thus not set by the user. Revert "Method `Hyde::hasSiteUrl()` now returns false when URL is for localhost" This reverts commit 8a13ad94aee29ffcf06579284916aa20e19e36ee. Reapply "Method `Hyde::hasSiteUrl()` now returns false when URL is for localhost" This reverts commit 0c4ab3475a77415ad99cdeae30f84121c6a93b24. Update HelpersTest.php --- .../src/Foundation/Kernel/Hyperlinks.php | 2 +- .../Feature/Foundation/HyperlinksTest.php | 14 ++- .../framework/tests/Feature/HelpersTest.php | 33 ++++++- .../tests/Feature/Views/MetadataViewTest.php | 97 ++++++++++++++++--- 4 files changed, 127 insertions(+), 19 deletions(-) diff --git a/packages/framework/src/Foundation/Kernel/Hyperlinks.php b/packages/framework/src/Foundation/Kernel/Hyperlinks.php index bd167a07432..4768bc13e02 100644 --- a/packages/framework/src/Foundation/Kernel/Hyperlinks.php +++ b/packages/framework/src/Foundation/Kernel/Hyperlinks.php @@ -131,7 +131,7 @@ public function hasSiteUrl(): bool { $value = Config::getNullableString('hyde.url'); - return ! blank($value); + return ! blank($value) && $value !== 'http://localhost'; } /** diff --git a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php index 10eb5db949d..bd811f612e1 100644 --- a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php +++ b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php @@ -56,7 +56,8 @@ public function testAssetHelperResolvesPathsForNestedPages() public function testAssetHelperReturnsQualifiedAbsoluteUriWhenRequestedAndSiteHasBaseUrl() { - $this->assertSame('http://localhost/media/test.jpg', $this->class->asset('test.jpg', true)); + config(['hyde.url' => 'https://example.org']); + $this->assertSame('https://example.org/media/test.jpg', $this->class->asset('test.jpg', true)); } public function testAssetHelperReturnsDefaultRelativePathWhenQualifiedAbsoluteUriIsRequestedButSiteHasNoBaseUrl() @@ -65,11 +66,22 @@ public function testAssetHelperReturnsDefaultRelativePathWhenQualifiedAbsoluteUr $this->assertSame('media/test.jpg', $this->class->asset('test.jpg', true)); } + public function testAssetHelperReturnsDefaultRelativePathWhenQualifiedAbsoluteUriIsRequestedButSiteBaseUrlIsLocalhost() + { + $this->assertSame('media/test.jpg', $this->class->asset('test.jpg', true)); + } + public function testAssetHelperReturnsInputWhenQualifiedAbsoluteUriIsRequestedButImageIsAlreadyQualified() { $this->assertSame('http://localhost/media/test.jpg', $this->class->asset('http://localhost/media/test.jpg', true)); } + public function testAssetHelperReturnsInputWhenQualifiedAbsoluteUriIsRequestedButImageIsAlreadyQualifiedRegardlessOfMatchingTheConfiguredUrl() + { + config(['hyde.url' => 'https://example.org']); + $this->assertSame('http://localhost/media/test.jpg', $this->class->asset('http://localhost/media/test.jpg', true)); + } + public function testAssetHelperUsesConfiguredMediaDirectory() { Hyde::setMediaDirectory('_assets'); diff --git a/packages/framework/tests/Feature/HelpersTest.php b/packages/framework/tests/Feature/HelpersTest.php index d6f34b35e16..733a832e7ec 100644 --- a/packages/framework/tests/Feature/HelpersTest.php +++ b/packages/framework/tests/Feature/HelpersTest.php @@ -77,7 +77,14 @@ public function testAssetFunction() public function testAssetFunctionWithQualifiedUrl() { $this->assertSame(Hyde::asset('foo', true), asset('foo', true)); - $this->assertSame('http://localhost/media/foo', asset('foo', true)); + $this->assertSame('media/foo', asset('foo', true)); + } + + /** @covers ::asset */ + public function testAssetFunctionWithQualifiedUrlAndSetBaseUrl() + { + $this->app['config']->set(['hyde.url' => 'https://example.com']); + $this->assertSame('https://example.com/media/foo', asset('foo', true)); } /** @covers ::asset */ @@ -94,6 +101,13 @@ public function testAssetFunctionWithQualifiedUrlAndNoBaseUrl() $this->assertSame('media/foo', asset('foo', true)); } + /** @covers ::asset */ + public function testAssetFunctionWithQualifiedUrlAndLocalhostBaseUrl() + { + $this->app['config']->set(['hyde.url' => 'http://localhost']); + $this->assertSame('media/foo', asset('foo', true)); + } + /** @covers ::asset */ public function testAssetFunctionFromNestedPage() { @@ -145,9 +159,16 @@ public function testUrlFunction() /** @covers ::url */ public function testUrlFunctionWithBaseUrl() + { + $this->app['config']->set(['hyde.url' => 'https://example.com']); + $this->assertSame('https://example.com/foo', url('foo')); + } + + /** @covers ::url */ + public function testUrlFunctionWithLocalhostBaseUrl() { $this->app['config']->set(['hyde.url' => 'http://localhost']); - $this->assertSame('http://localhost/foo', url('foo')); + $this->assertSame('foo', url('foo')); } /** @covers ::url */ @@ -165,6 +186,14 @@ public function testUrlFunctionWithoutBaseUrlOrPath() $this->assertNull(url()); } + /** @covers ::url */ + public function testUrlFunctionWithLocalhostBaseUrlButNoPath() + { + $this->app['config']->set(['hyde.url' => 'http://localhost']); + $this->expectException(\Hyde\Framework\Exceptions\BaseUrlNotSetException::class); + $this->assertNull(url()); + } + /** @covers ::url */ public function testUrlFunctionWithAlreadyQualifiedUrl() { diff --git a/packages/framework/tests/Feature/Views/MetadataViewTest.php b/packages/framework/tests/Feature/Views/MetadataViewTest.php index 199759644d4..54ba018d2d4 100644 --- a/packages/framework/tests/Feature/Views/MetadataViewTest.php +++ b/packages/framework/tests/Feature/Views/MetadataViewTest.php @@ -25,7 +25,7 @@ protected function setUp(): void { parent::setUp(); - config(['hyde.url' => 'http://localhost']); + config(['hyde.url' => 'https://example.com']); config(['hyde.enable_cache_busting' => false]); } @@ -79,7 +79,7 @@ protected function getDefaultTags(): array '', '', '', - '', + '', '', '', ]; @@ -93,7 +93,7 @@ public function testMetadataTagsInEmptyBladePage() $assertions = $this->assertSee('test', array_merge($this->getDefaultTags(), [ 'HydePHP - Test', '', - '', + '', '', '', ])); @@ -109,7 +109,7 @@ public function testMetadataTagsInEmptyMarkdownPage() $assertions = $this->assertSee('test', array_merge($this->getDefaultTags(), [ 'HydePHP - Test', '', - '', + '', '', '', ])); @@ -125,7 +125,7 @@ public function testMetadataTagsInEmptyDocumentationPage() $assertions = $this->assertSee('docs/test', array_merge($this->getDefaultTags(), [ 'HydePHP - Test', '', - '', + '', '', '', ])); @@ -140,16 +140,16 @@ public function testMetadataTagsInEmptyMarkdownPost() $assertions = $this->assertSee('posts/test', array_merge($this->getDefaultTags(), [ 'HydePHP - Test', - '', + '', '', - '', + '', '', - '', + '', '', - '', + '', '', '', - '', + '', ])); $this->assertAllTagsWereCovered('posts/test', $assertions); @@ -177,25 +177,92 @@ public function testMetadataTagsInMarkdownPostWithFlatFrontMatter() $assertions = $this->assertSee('posts/test', array_merge($this->getDefaultTags(), [ 'HydePHP - My title', - '', + '', '', - '', + '', '', '', '', '', - '', + '', '', - '', + '', '', '', '', '', - '', + '', '', '', ])); $this->assertAllTagsWereCovered('posts/test', $assertions); } + + public function testCanonicalUrlTagsAreNotAddedWhenCanonicalUrlIsNotSet() + { + config(['hyde.url' => 'http://localhost']); + + $this->file('_posts/test.md', <<<'MARKDOWN' + --- + title: "My title" + description: "My description" + category: "My category" + date: "2022-01-01" + author: "Mr. Hyde" + image: image.jpg + --- + + ## Hello World + + Lorem Ipsum Dolor Amet. + MARKDOWN + ); + $this->build('_posts/test.md'); + + $assertions = $this->assertSee('posts/test', [ + '', + '', + '', + '', + '', + 'HydePHP - My title', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + + // '', + // '', + // '', + // '', + // '', + // '', + ]); + + $this->assertAllTagsWereCovered('posts/test', $assertions); + + $dontSee = [ + '