From b9ac1c8d1fa0c484d2d95fad891c2c3c5c7f039c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 May 2022 16:52:59 +0200 Subject: [PATCH] Fix bug #471, make title metadata dynamic --- src/Concerns/HasPageMetadata.php | 19 +++++ .../Feature/Concerns/HasPageMetadataTest.php | 79 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/Concerns/HasPageMetadata.php b/src/Concerns/HasPageMetadata.php index 1a079441..25e9e9ac 100644 --- a/src/Concerns/HasPageMetadata.php +++ b/src/Concerns/HasPageMetadata.php @@ -38,6 +38,15 @@ public function getDynamicMetadata(): array .'" />'; } + if (method_exists($this, 'findTitleForDocument')) { + if ($this->hasTwitterTitleInConfig()) { + $array[] = ''; + } + if ($this->hasOpenGraphTitleInConfig()) { + $array[] = ''; + } + } + if ($this instanceof MarkdownPost) { // Temporarily merge data with GeneratesPageMetadata trait for compatibility $array[] = "\n"; @@ -89,4 +98,14 @@ public function canUseRssFeedLink(): bool return false; } + + public function hasTwitterTitleInConfig(): bool + { + return str_contains(json_encode(config('hyde.meta', [])), 'twitter:title'); + } + + public function hasOpenGraphTitleInConfig(): bool + { + return str_contains(json_encode(config('hyde.meta', [])), 'og:title'); + } } diff --git a/tests/Feature/Concerns/HasPageMetadataTest.php b/tests/Feature/Concerns/HasPageMetadataTest.php index 9e6980d1..73d96318 100644 --- a/tests/Feature/Concerns/HasPageMetadataTest.php +++ b/tests/Feature/Concerns/HasPageMetadataTest.php @@ -232,4 +232,83 @@ public function test_get_dynamic_metadata_does_not_add_sitemap_link_when_conditi $page->getDynamicMetadata() ); } + + public function test_has_twitter_title_in_config_returns_true_when_present_in_config() + { + config(['hyde.meta' => [ + Meta::name('twitter:title', 'foo'), + ]]); + + $page = new class + { + use HasPageMetadata; + }; + + $this->assertTrue($page->hasTwitterTitleInConfig()); + } + + public function test_has_twitter_title_in_config_returns_false_when_not_present_in_config() + { + config(['hyde.meta' => []]); + + $page = new class + { + use HasPageMetadata; + }; + + $this->assertFalse($page->hasTwitterTitleInConfig()); + } + + public function test_has_open_graph_title_in_config_returns_true_when_present_in_config() + { + config(['hyde.meta' => [ + Meta::property('title', 'foo'), + ]]); + + $page = new class + { + use HasPageMetadata; + }; + + $this->assertTrue($page->hasOpenGraphTitleInConfig()); + } + + public function test_has_open_graph_title_in_config_returns_false_when_not_present_in_config() + { + config(['hyde.meta' => []]); + + $page = new class + { + use HasPageMetadata; + }; + + $this->assertFalse($page->hasOpenGraphTitleInConfig()); + } + + // Test meta titles can be dynamically added and override defaults + public function test_get_dynamic_metadata_adds_twitter_and_open_graph_title_when_conditions_are_met() + { + config(['hyde.meta' => [ + Meta::name('twitter:title', 'foo'), + Meta::property('title', 'foo'), + ]]); + + + $page = new class extends AbstractPage + { + use HasPageMetadata; + + public function findTitleForDocument(): string + { + return 'bar'; + } + }; + + $this->assertEquals([ + '', + '', + ], + $page->getDynamicMetadata() + ); + } }