From b3e93bf4ad564e87b7d379ed483d02fadde275b5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 12 Jun 2024 17:58:13 +0200 Subject: [PATCH 01/14] Introduce local variable --- packages/framework/src/Foundation/Kernel/Hyperlinks.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Foundation/Kernel/Hyperlinks.php b/packages/framework/src/Foundation/Kernel/Hyperlinks.php index 98b2acdcac8..09362909c91 100644 --- a/packages/framework/src/Foundation/Kernel/Hyperlinks.php +++ b/packages/framework/src/Foundation/Kernel/Hyperlinks.php @@ -129,7 +129,9 @@ public function asset(string $name, bool $preferQualifiedUrl = false): string */ public function hasSiteUrl(): bool { - return ! blank(Config::getNullableString('hyde.url')); + $value = Config::getNullableString('hyde.url'); + + return ! blank($value); } /** From 3cfa8a525eae9d2bf339c6126606c42a9c286d84 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 12 Jun 2024 18:41:52 +0200 Subject: [PATCH 02/14] Method `Hyde::url()` now resolves relative paths instead of throwing We now return the relative path even if the base URL is not set, as this is more likely to be the desired behavior the user's expecting. If the user is trying to get the base URL, but it's not set, we throw the exception. --- .../src/Foundation/Kernel/Hyperlinks.php | 7 +++++++ .../framework/tests/Feature/HelpersTest.php | 9 ++++++++- .../HyperlinksUrlPathHelpersTest.php | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Foundation/Kernel/Hyperlinks.php b/packages/framework/src/Foundation/Kernel/Hyperlinks.php index 09362909c91..bd167a07432 100644 --- a/packages/framework/src/Foundation/Kernel/Hyperlinks.php +++ b/packages/framework/src/Foundation/Kernel/Hyperlinks.php @@ -149,6 +149,13 @@ public function url(string $path = ''): string return rtrim(rtrim(Config::getString('hyde.url'), '/')."/$path", '/'); } + // Since v1.7.0, we return the relative path even if the base URL is not set, + // as this is more likely to be the desired behavior the user's expecting. + if (! blank($path)) { + return $path; + } + + // User is trying to get the base URL, but it's not set throw new BaseUrlNotSetException(); } diff --git a/packages/framework/tests/Feature/HelpersTest.php b/packages/framework/tests/Feature/HelpersTest.php index a28e49e9497..d6f34b35e16 100644 --- a/packages/framework/tests/Feature/HelpersTest.php +++ b/packages/framework/tests/Feature/HelpersTest.php @@ -152,10 +152,17 @@ public function testUrlFunctionWithBaseUrl() /** @covers ::url */ public function testUrlFunctionWithoutBaseUrl() + { + $this->app['config']->set(['hyde.url' => null]); + $this->assertSame('foo', url('foo')); + } + + /** @covers ::url */ + public function testUrlFunctionWithoutBaseUrlOrPath() { $this->app['config']->set(['hyde.url' => null]); $this->expectException(\Hyde\Framework\Exceptions\BaseUrlNotSetException::class); - $this->assertNull(url('foo')); + $this->assertNull(url()); } /** @covers ::url */ diff --git a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index be8fafa99c9..9342341526f 100644 --- a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -92,6 +92,26 @@ public function testQualifiedUrlThrowsExceptionWhenNoSiteUrlIsSet() $this->class->url(); } + public function testHelperFallsBackToRelativeLinksWhenNoSiteUrlIsSet() + { + config(['hyde.url' => '']); + + $this->assertSame('index.html', $this->class->url('index.html')); + $this->assertSame('foo.html', $this->class->url('foo.html')); + $this->assertSame('foo/bar.html', $this->class->url('foo/bar.html')); + $this->assertSame('docs/index.html', $this->class->url('docs/index.html')); + } + + public function testHelperFallsBackToPrettyRelativeLinksWhenNoSiteUrlIsSetAndPrettyUrlsAreEnabled() + { + config(['hyde.url' => '', 'hyde.pretty_urls' => true]); + + $this->assertSame('/', $this->class->url('index.html')); + $this->assertSame('foo', $this->class->url('foo.html')); + $this->assertSame('docs/', $this->class->url('docs/index.html')); + $this->assertSame('foo/bar', $this->class->url('foo/bar.html')); + } + public function testHelperReturnsExpectedStringWhenSiteUrlIsSet() { config(['hyde.url' => 'https://example.com']); From ef6598e6fc4ca220cc416e86cda6ca17264b484f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 12 Jun 2024 19:01:45 +0200 Subject: [PATCH 03/14] 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 ++++++++++++++++--- .../HyperlinksUrlPathHelpersTest.php | 20 ++++ 5 files changed, 147 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 = [ + '