From b9ac1c8d1fa0c484d2d95fad891c2c3c5c7f039c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 May 2022 16:52:59 +0200 Subject: [PATCH 1/4] 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() + ); + } } From 6aaa612b80600ae4ef8136fb779623b66206119b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 May 2022 16:56:40 +0200 Subject: [PATCH 2/4] Make dynamic meta title use title property instead --- src/Concerns/HasPageMetadata.php | 6 +++--- tests/Feature/Concerns/HasPageMetadataTest.php | 5 +---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Concerns/HasPageMetadata.php b/src/Concerns/HasPageMetadata.php index 25e9e9ac..f8a502c1 100644 --- a/src/Concerns/HasPageMetadata.php +++ b/src/Concerns/HasPageMetadata.php @@ -38,12 +38,12 @@ public function getDynamicMetadata(): array .'" />'; } - if (method_exists($this, 'findTitleForDocument')) { + if (isset($this->title)) { if ($this->hasTwitterTitleInConfig()) { - $array[] = ''; + $array[] = ''; } if ($this->hasOpenGraphTitleInConfig()) { - $array[] = ''; + $array[] = ''; } } diff --git a/tests/Feature/Concerns/HasPageMetadataTest.php b/tests/Feature/Concerns/HasPageMetadataTest.php index 73d96318..4abf0186 100644 --- a/tests/Feature/Concerns/HasPageMetadataTest.php +++ b/tests/Feature/Concerns/HasPageMetadataTest.php @@ -298,10 +298,7 @@ public function test_get_dynamic_metadata_adds_twitter_and_open_graph_title_when { use HasPageMetadata; - public function findTitleForDocument(): string - { - return 'bar'; - } + public string $title = 'bar'; }; $this->assertEquals([ From 6066b282a0cd1f193fd278a3f3dbd86b2dd23765 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 May 2022 16:57:02 +0200 Subject: [PATCH 3/4] Add todo --- src/Concerns/HasPageMetadata.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Concerns/HasPageMetadata.php b/src/Concerns/HasPageMetadata.php index f8a502c1..9d09bc4f 100644 --- a/src/Concerns/HasPageMetadata.php +++ b/src/Concerns/HasPageMetadata.php @@ -9,6 +9,7 @@ use Hyde\Framework\Services\SitemapService; /** + * @todo Move logic into service class to make it easier to test. * @see \Tests\Feature\Concerns\HasPageMetadataTest */ trait HasPageMetadata From cf18dd8c6596caf1601e914f7b5903ec8329cf16 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 30 May 2022 14:57:14 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- src/Concerns/HasPageMetadata.php | 1 + tests/Feature/Concerns/HasPageMetadataTest.php | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Concerns/HasPageMetadata.php b/src/Concerns/HasPageMetadata.php index 9d09bc4f..502976a9 100644 --- a/src/Concerns/HasPageMetadata.php +++ b/src/Concerns/HasPageMetadata.php @@ -10,6 +10,7 @@ /** * @todo Move logic into service class to make it easier to test. + * * @see \Tests\Feature\Concerns\HasPageMetadataTest */ trait HasPageMetadata diff --git a/tests/Feature/Concerns/HasPageMetadataTest.php b/tests/Feature/Concerns/HasPageMetadataTest.php index 4abf0186..85d3acb3 100644 --- a/tests/Feature/Concerns/HasPageMetadataTest.php +++ b/tests/Feature/Concerns/HasPageMetadataTest.php @@ -293,7 +293,6 @@ public function test_get_dynamic_metadata_adds_twitter_and_open_graph_title_when Meta::property('title', 'foo'), ]]); - $page = new class extends AbstractPage { use HasPageMetadata;