Skip to content

Commit

Permalink
Extract metadata helpers to concern
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 18, 2022
1 parent d2ae7ca commit 72b1356
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 34 deletions.
42 changes: 42 additions & 0 deletions src/Concerns/HasPageMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Hyde\Framework\Concerns;

use Hyde\Framework\Hyde;
use Hyde\Framework\Meta;

/**
* @see \Tests\Feature\Concerns\HasPageMetadataTest
*/
trait HasPageMetadata
{
public function getCanonicalUrl(): string
{
return Hyde::uriPath(Hyde::pageLink($this->getCurrentPagePath() . '.html'));
}

public function getDynamicMetadata(): array
{
$array = [];

if ($this->canUseCanonicalUrl()) {
$array[] = '<link rel="canonical" href="' . $this->getCanonicalUrl() . '" />';
}

return $array;
}

public function renderPageMetadata(): string
{
$dynamicMetadata = $this->getDynamicMetadata();

return Meta::render(
$dynamicMetadata
);
}

public function canUseCanonicalUrl(): bool
{
return Hyde::uriPath() && isset($this->slug);
}
}
37 changes: 3 additions & 34 deletions src/Contracts/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace Hyde\Framework\Contracts;

use Hyde\Framework\Hyde;
use Hyde\Framework\Meta;
use Hyde\Framework\Concerns\HasPageMetadata;

/**
* To ensure compatability with the Hyde Framework,
* all Page Models must extend this class.
*
* Markdown-based Pages should extend MarkdownDocument.
*
* @todo Extract metadata helpers to concern
*/
abstract class AbstractPage
{
use HasPageMetadata;

public static string $sourceDirectory;
public static string $fileExtension;
public static string $parserClass;
Expand All @@ -23,34 +22,4 @@ public function getCurrentPagePath(): string
{
return $this->slug;
}

public function renderPageMetadata(): string
{
$dynamicMetadata = $this->getDynamicMetadata();

return Meta::render(
$dynamicMetadata
);
}

protected function getDynamicMetadata(): array
{
$array = [];

if ($this->canUseCanonicalUrl()) {
$array[] = '<link rel="canonical" href="'.$this->getCanonicalUrl().'" />';
}

return $array;
}

protected function canUseCanonicalUrl(): bool
{
return Hyde::uriPath() && isset($this->slug);
}

protected function getCanonicalUrl(): string
{
return Hyde::uriPath(Hyde::pageLink($this->getCurrentPagePath().'.html'));
}
}
192 changes: 192 additions & 0 deletions tests/Feature/Concerns/HasPageMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

namespace Tests\Feature\Concerns;

use Hyde\Framework\Concerns\HasPageMetadata;
use Hyde\Framework\Contracts\AbstractPage;
use Hyde\Framework\Meta;
use Tests\TestCase;

/**
* @covers \Hyde\Framework\Concerns\HasPageMetadata
*/
class HasPageMetadataTest extends TestCase
{
public function testGetCanonicalUrlReturnsUrlForTopLevelPage()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => 'https://example.com']);
config(['hyde.prettyUrls' => false]);

$this->assertEquals('https://example.com/foo.html', $page->getCanonicalUrl());
}

public function testGetCanonicalUrlReturnsPrettyUrlForTopLevelPage()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => 'https://example.com']);
config(['hyde.prettyUrls' => true]);

$this->assertEquals('https://example.com/foo', $page->getCanonicalUrl());
}

public function testGetCanonicalUrlReturnsUrlForNestedPage()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';

public function getCurrentPagePath(): string
{
return 'bar/'.$this->slug;
}
};
config(['hyde.site_url' => 'https://example.com']);
config(['hyde.prettyUrls' => false]);

$this->assertEquals('https://example.com/bar/foo.html', $page->getCanonicalUrl());
}

public function testGetCanonicalUrlReturnsUrlForDeeplyNestedPage()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';

public function getCurrentPagePath(): string
{
return 'bar/baz/'.$this->slug;
}
};
config(['hyde.site_url' => 'https://example.com']);
config(['hyde.prettyUrls' => false]);

$this->assertEquals('https://example.com/bar/baz/foo.html', $page->getCanonicalUrl());
}

public function testCanUseCanonicalUrlReturnsTrueWhenBothUriPathAndSlugIsSet()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => 'https://example.com']);

$this->assertTrue($page->canUseCanonicalUrl());
}

public function testCanUseCanonicalUrlReturnsFalseNoConditionsAreMet()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug;
};
config(['hyde.site_url' => null]);

$this->assertFalse($page->canUseCanonicalUrl());
}

public function testCanUseCanonicalUrlReturnsFalseWhenOnlyOneConditionIsMet()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug;
};
config(['hyde.site_url' => 'https://example.com']);

$this->assertFalse($page->canUseCanonicalUrl());

$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => null]);

$this->assertFalse($page->canUseCanonicalUrl());
}


public function testRenderPageMetadataReturnsStringWithMergedMetadata()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => 'https://example.com']);

config(['hyde.meta' => [
Meta::name('foo', 'bar')
]]);

$this->assertEquals(
'<meta name="foo" content="bar">'."\n".
'<link rel="canonical" href="https://example.com/foo.html" />',
$page->renderPageMetadata()
);
}

public function testRenderPageMetadataOnlyAddsCanonicalIfConditionsAreMet()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => null]);
config(['hyde.meta' => []]);

$this->assertEquals(
'',
$page->renderPageMetadata()
);
}

public function testGetDynamicMetadataOnlyAddsCanonicalIfConditionsAreMet()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => null]);
config(['hyde.meta' => []]);

$this->assertEquals(
[],
$page->getDynamicMetadata()
);
}

public function testGetDynamicMetadataAddsCanonicalUrlWhenConditionsAreMet()
{
$page = new class extends AbstractPage {
use HasPageMetadata;

public string $slug = 'foo';
};
config(['hyde.site_url' => 'https://example.com']);

config(['hyde.meta' => [
Meta::name('foo', 'bar')
]]);

$this->assertEquals(['<link rel="canonical" href="https://example.com/foo.html" />'],
$page->getDynamicMetadata()
);
}
}

0 comments on commit 72b1356

Please sign in to comment.