diff --git a/src/Concerns/HasPageMetadata.php b/src/Concerns/HasPageMetadata.php index 1a079441..502976a9 100644 --- a/src/Concerns/HasPageMetadata.php +++ b/src/Concerns/HasPageMetadata.php @@ -9,6 +9,8 @@ use Hyde\Framework\Services\SitemapService; /** + * @todo Move logic into service class to make it easier to test. + * * @see \Tests\Feature\Concerns\HasPageMetadataTest */ trait HasPageMetadata @@ -38,6 +40,15 @@ public function getDynamicMetadata(): array .'" />'; } + if (isset($this->title)) { + if ($this->hasTwitterTitleInConfig()) { + $array[] = ''; + } + if ($this->hasOpenGraphTitleInConfig()) { + $array[] = ''; + } + } + if ($this instanceof MarkdownPost) { // Temporarily merge data with GeneratesPageMetadata trait for compatibility $array[] = "\n"; @@ -89,4 +100,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..85d3acb3 100644 --- a/tests/Feature/Concerns/HasPageMetadataTest.php +++ b/tests/Feature/Concerns/HasPageMetadataTest.php @@ -232,4 +232,79 @@ 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 string $title = 'bar'; + }; + + $this->assertEquals([ + '', + '', + ], + $page->getDynamicMetadata() + ); + } }