From 285aee1907074cd2ea038de4fc0cca593ecdbc68 Mon Sep 17 00:00:00 2001 From: github-actions Date: Tue, 23 Jul 2024 16:01:41 +0000 Subject: [PATCH] Merge pull request #1886 from hydephp/code-cleanup Clean up and refactor code https://github.com/hydephp/develop/commit/31e3730c731e145a2a9432f452e8a01260e4150d --- .../Features/Metadata/PageMetadataBag.php | 8 ++- tests/Feature/MetadataTest.php | 68 +++++++++---------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/Framework/Features/Metadata/PageMetadataBag.php b/src/Framework/Features/Metadata/PageMetadataBag.php index efada5ec..4b560383 100644 --- a/src/Framework/Features/Metadata/PageMetadataBag.php +++ b/src/Framework/Features/Metadata/PageMetadataBag.php @@ -77,7 +77,11 @@ protected function resolveImageLink(string $image): string { // Since this is run before the page is rendered, we don't have the currentPage property. // So we need to run some of the same calculations here to resolve the image path link. - return Hyperlinks::isRemote($image) ? $image - : str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')).$image; + return Hyperlinks::isRemote($image) ? $image : $this->calculatePathTraversal().$image; + } + + private function calculatePathTraversal(): string + { + return str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')); } } diff --git a/tests/Feature/MetadataTest.php b/tests/Feature/MetadataTest.php index c5b31be5..358d5bb6 100644 --- a/tests/Feature/MetadataTest.php +++ b/tests/Feature/MetadataTest.php @@ -195,7 +195,7 @@ public function testDynamicMetadataOverridesConfigDefinedMetadata() Meta::name('twitter:title', 'bar'), ]]); - $page = MarkdownPage::make(matter: ['title' => 'baz']); + $page = new MarkdownPage(matter: ['title' => 'baz']); $this->assertEquals([ 'metadata:twitter:title' => Meta::name('twitter:title', 'HydePHP - baz'), @@ -207,7 +207,7 @@ public function testDoesNotAddCanonicalLinkWhenBaseUrlIsNotSet() { $this->withoutSiteUrl(); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringNotContainsString('metadata->render()); } @@ -216,7 +216,7 @@ public function testDoesNotAddCanonicalLinkWhenIdentifierIsNotSet() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make(); + $page = new MarkdownPage(); $this->assertStringNotContainsString('metadata->render()); } @@ -225,7 +225,7 @@ public function testAddsCanonicalLinkWhenBaseUrlAndIdentifierIsSet() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringContainsString('', $page->metadata->render()); } @@ -235,7 +235,7 @@ public function testCanonicalLinkUsesCleanUrlSetting() config(['hyde.url' => 'foo']); config(['hyde.pretty_urls' => true]); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringContainsString('', $page->metadata->render()); } @@ -244,7 +244,7 @@ public function testCanOverrideCanonicalLinkWithFrontMatter() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make('bar', [ + $page = new MarkdownPage('bar', [ 'canonicalUrl' => 'canonical', ]); @@ -253,7 +253,7 @@ public function testCanOverrideCanonicalLinkWithFrontMatter() public function testAddsTwitterAndOpenGraphTitleWhenTitleIsSet() { - $page = MarkdownPage::make(matter: ['title' => 'Foo Bar']); + $page = new MarkdownPage(matter: ['title' => 'Foo Bar']); $this->assertSame( ''."\n". @@ -264,7 +264,7 @@ public function testAddsTwitterAndOpenGraphTitleWhenTitleIsSet() public function testDoesNotAddTwitterAndOpenGraphTitleWhenNoTitleIsSet() { - $page = MarkdownPage::make(matter: ['title' => null]); + $page = new MarkdownPage(matter: ['title' => null]); $this->assertSame('', $page->metadata->render()); } @@ -285,7 +285,7 @@ public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInPost() public function testAddsAuthorWhenAuthorIsSetInPost() { - $page = MarkdownPost::make(matter: ['author' => 'My Author']); + $page = new MarkdownPost(matter: ['author' => 'My Author']); $this->assertPageHasMetadata($page, ''); } @@ -294,12 +294,12 @@ public function testDoesNotAddAuthorWhenAuthorIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'My Category']); + $page = new MarkdownPost(matter: ['category' => 'My Category']); $this->assertPageHasMetadata($page, ''); } @@ -308,12 +308,12 @@ public function testDoesNotAddKeywordsWhenCategoryIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'example.html']); + $page = new MarkdownPost(matter: ['canonicalUrl' => 'example.html']); $this->assertPageHasMetadata($page, ''); } @@ -322,19 +322,19 @@ public function testDoesNotAddUrlPropertyWhenCanonicalUrlIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' null]); + $page = new MarkdownPost(matter: ['canonicalUrl' => null]); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'My Title']); + $page = new MarkdownPost(matter: ['title' => 'My Title']); $this->assertPageHasMetadata($page, ''); } @@ -348,7 +348,7 @@ public function testDoesNotAddTitlePropertyWhenTitleIsNotSetInPost() public function testAddsPublishedTimePropertyWhenDateIsSetInPost() { - $page = MarkdownPost::make(matter: ['date' => '2022-01-01']); + $page = new MarkdownPost(matter: ['date' => '2022-01-01']); $this->assertPageHasMetadata($page, ''); } @@ -356,12 +356,12 @@ public function testAddsPublishedTimePropertyWhenDateIsSetInPost() public function testDoesNotAddPublishedTimePropertyWhenDateIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'image.jpg']); + $page = new MarkdownPost(matter: ['image' => 'image.jpg']); $this->assertPageHasMetadata($page, ''); } @@ -369,26 +369,26 @@ public function testAddsImagePropertyWhenImageIsSetInPost() public function testDoesNotAddImagePropertyWhenImageIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, 'assertPageHasMetadata($page, ''); } public function testDynamicPostMetaPropertiesReturnsBaseArrayWhenInitializedWithEmptyFrontMatter() { - $page = MarkdownPost::make(); + $page = new MarkdownPost(); $this->assertSame('', $page->metadata->render()); } public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToString() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -397,7 +397,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelative() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -406,7 +406,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelativeForNestedPosts() { - $page = MarkdownPost::make('foo/bar', matter: [ + $page = new MarkdownPost('foo/bar', matter: [ 'image' => 'foo.jpg', ]); @@ -417,7 +417,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati { MarkdownPost::setOutputDirectory('_posts/foo'); - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -428,7 +428,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati { MarkdownPost::setOutputDirectory('_posts/foo'); - $page = MarkdownPost::make('bar/baz', matter: [ + $page = new MarkdownPost('bar/baz', matter: [ 'image' => 'foo.jpg', ]); @@ -439,7 +439,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu { Hyde::setMediaDirectory('assets'); - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -448,7 +448,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToArrayWithPath() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => [ 'source' => 'foo.jpg', ], @@ -459,7 +459,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToArrayWithUrl() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => [ 'source' => 'https://example.com/foo.jpg', ], @@ -470,7 +470,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingUsername() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [ 'username' => 'username', ], @@ -481,7 +481,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingU public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingName() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [ 'name' => 'Name', ], @@ -492,7 +492,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingN public function testNoAuthorIsSetWhenAuthorSetToArrayWithoutNameOrUsername() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [], ]);