-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2ae7ca
commit 72b1356
Showing
3 changed files
with
237 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |