Skip to content

Commit

Permalink
Merge branch 'master' into 2.x-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 23, 2024
2 parents faa22e6 + c415837 commit 40b7c1f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ protected function addDynamicPageMetadata(HydePage $page): void
$this->add(Meta::link('canonical', $page->getCanonicalUrl()));
}

if ($page->has('description')) {
$this->add(Meta::name('description', $page->data('description')));
$this->add(Meta::property('description', $page->data('description')));
}

if ($page->has('title')) {
$this->add(Meta::name('twitter:title', $page->title()));
$this->add(Meta::property('title', $page->title()));
Expand All @@ -46,7 +51,6 @@ protected function addDynamicPageMetadata(HydePage $page): void

protected function addMetadataForMarkdownPost(MarkdownPost $page): void
{
$this->addPostMetadataIfExists($page, 'description');
$this->addPostMetadataIfExists($page, 'author');
$this->addPostMetadataIfExists($page, 'category', 'keywords');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ interface PageSchema extends FrontMatterSchema
{
public const PAGE_SCHEMA = [
'title' => 'string',
'canonicalUrl' => 'string', // While not present in the page data as a property, it is used for the accessor method, which reads this value from the front matter.
'description' => 'string', // For <meta name='description'> values. It is used by the automatic page metadata generator, which reads this value from the front matter.
'canonicalUrl' => 'string', // While not present in the page data as a property, it is used by the accessor method, which reads this value from the front matter.
'navigation' => NavigationSchema::NAVIGATION_SCHEMA,
];
}
56 changes: 54 additions & 2 deletions packages/framework/tests/Feature/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Hyde\Framework\Testing\Feature;

use Hyde\Facades\Meta;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Framework\Features\Metadata\Elements\LinkElement;
use Hyde\Framework\Features\Metadata\Elements\MetadataElement;
use Hyde\Framework\Features\Metadata\Elements\OpenGraphElement;
Expand Down Expand Up @@ -271,16 +273,66 @@ public function testDoesNotAddTwitterAndOpenGraphTitleWhenNoTitleIsSet()

public function testAddsDescriptionWhenDescriptionIsSetInPost()
{
$page = MarkdownPost::make(matter: ['description' => 'My Description']);
$page = new MarkdownPost(matter: ['description' => 'My Description']);

$this->assertPageHasMetadata($page, '<meta name="description" content="My Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInPost()
{
$page = new MarkdownPost();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description" content="My Description">');
$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsDescriptionWhenDescriptionIsSetInMarkdownPage()
{
$page = new MarkdownPage(matter: ['description' => 'My Page Description']);

$this->assertPageHasMetadata($page, '<meta name="description" content="My Page Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Page Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInMarkdownPage()
{
$page = new MarkdownPage();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsDescriptionWhenDescriptionIsSetInBladePage()
{
$page = new BladePage(matter: ['description' => 'My Page Description']);

$this->assertPageHasMetadata($page, '<meta name="description" content="My Page Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Page Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInBladePage()
{
$page = new BladePage();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsDescriptionWhenDescriptionIsSetInDocumentationPage()
{
$page = new DocumentationPage(matter: ['description' => 'My Page Description']);

$this->assertPageHasMetadata($page, '<meta name="description" content="My Page Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Page Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInDocumentationPage()
{
$page = new DocumentationPage();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsAuthorWhenAuthorIsSetInPost()
Expand Down
27 changes: 27 additions & 0 deletions packages/framework/tests/Feature/Views/MetadataViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function testMetadataTagsInMarkdownPostWithFlatFrontMatter()
'<meta name="keywords" content="My category">',
'<meta name="url" content="https://example.com/posts/test.html">',
'<meta property="og:title" content="HydePHP - My title">',
'<meta property="og:description" content="My description">',
'<meta property="og:url" content="https://example.com/posts/test.html">',
'<meta property="og:type" content="article">',
'<meta property="og:article:published_time" content="2022-01-01T00:00:00+00:00">',
Expand Down Expand Up @@ -233,6 +234,7 @@ public function testCanonicalUrlTagsAreNotAddedWhenCanonicalUrlIsNotSet()
'<meta name="author" content="Mr. Hyde">',
'<meta name="keywords" content="My category">',
'<meta property="og:title" content="HydePHP - My title">',
'<meta property="og:description" content="My description">',
'<meta property="og:type" content="article">',
'<meta property="og:article:published_time" content="2022-01-01T00:00:00+00:00">',
'<meta property="og:image" content="../media/image.jpg">',
Expand Down Expand Up @@ -265,4 +267,29 @@ public function testCanonicalUrlTagsAreNotAddedWhenCanonicalUrlIsNotSet()
$this->assertStringNotContainsString($text, $contents);
}
}

public function testMetadataTagsInMarkdownPageWithDescription()
{
$this->file('_pages/test-page.md', <<<'MARKDOWN'
---
title: "My Page Title"
description: "My page description"
---
## Welcome to My Page
This is a test page with a description.
MARKDOWN
);
$this->build('_pages/test-page.md');

$this->assertSee('test-page', array_merge($this->getDefaultTags(), [
'<title>HydePHP - My Page Title</title>',
'<link rel="stylesheet" href="media/app.css">',
'<meta name="twitter:title" content="HydePHP - My Page Title">',
'<meta property="og:title" content="HydePHP - My Page Title">',
'<meta property="og:description" content="My page description">',
'<meta name="description" content="My page description">',
]));
}
}
1 change: 1 addition & 0 deletions packages/framework/tests/Unit/SchemaContractsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testSchemasAreNotAccidentallyChanged()
{
$this->assertSame([
'title' => 'string',
'description' => 'string',
'canonicalUrl' => 'string',
'navigation' => NavigationSchema::NAVIGATION_SCHEMA,
], PageSchema::PAGE_SCHEMA);
Expand Down

0 comments on commit 40b7c1f

Please sign in to comment.