-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move post permalink seo redirect value to PostObject (#1269)
- Loading branch information
Showing
7 changed files
with
198 additions
and
45 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
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 was deleted.
Oops, something went wrong.
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
90 changes: 90 additions & 0 deletions
90
library/PostObject/Decorators/PostObjectWithSeoRedirect.php
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,90 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Decorators; | ||
|
||
use Municipio\PostObject\Icon\IconInterface; | ||
use Municipio\PostObject\PostObjectInterface; | ||
use WpService\Contracts\GetPostMeta; | ||
|
||
/** | ||
* PostObjectWithSeoRedirect class. | ||
* | ||
* Applies the SEO redirect to the post object permalink if a redirect is set. | ||
*/ | ||
class PostObjectWithSeoRedirect implements PostObjectInterface | ||
{ | ||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(private PostObjectInterface $postObject, private GetPostMeta $wpService) | ||
{ | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getId(): int | ||
{ | ||
return $this->postObject->getId(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getTitle(): string | ||
{ | ||
return $this->postObject->getTitle(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getPermalink(): string | ||
{ | ||
$seoRedirectMetaUrl = $this->wpService->getPostMeta($this->postObject->getId(), 'redirect', true); | ||
|
||
if (filter_var($seoRedirectMetaUrl, FILTER_VALIDATE_URL)) { | ||
return $seoRedirectMetaUrl; | ||
} | ||
|
||
return $this->postObject->getPermalink(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getCommentCount(): int | ||
{ | ||
return $this->postObject->getCommentCount(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getPostType(): string | ||
{ | ||
return $this->postObject->getPostType(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getBlogId(): int | ||
{ | ||
return $this->postObject->getBlogId(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getIcon(): ?IconInterface | ||
{ | ||
return $this->postObject->getIcon(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
library/PostObject/Decorators/PostObjectWithSeoRedirect.test.php
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,58 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Decorators; | ||
|
||
use Municipio\PostObject\Icon\IconInterface; | ||
use Municipio\PostObject\PostObjectInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use WpService\Contracts\GetPostMeta; | ||
|
||
class PostObjectWithSeoRedirectTest extends TestCase | ||
{ | ||
/** | ||
* @testdox class can be instantiated | ||
*/ | ||
public function testClassCanBeInstantiated() | ||
{ | ||
$postObject = $this->createMock(PostObjectInterface::class); | ||
$wpService = $this->createMock(GetPostMeta::class); | ||
|
||
$this->assertInstanceOf(PostObjectWithSeoRedirect::class, new PostObjectWithSeoRedirect($postObject, $wpService)); | ||
} | ||
|
||
/** | ||
* @testdox getPermalink returns redirect url if set | ||
*/ | ||
public function testGetPermalinkReturnsRedirectUrlIfSet() | ||
{ | ||
$postObject = $this->createMock(PostObjectInterface::class); | ||
$wpService = $this->createMock(GetPostMeta::class); | ||
|
||
$postObject->method('getId')->willReturn(1); | ||
$postObject->method('getPermalink')->willReturn('http://example.com/post'); | ||
|
||
$wpService->method('getPostMeta')->willReturn('http://example.com/redirect'); | ||
|
||
$postObjectWithSeoRedirect = new PostObjectWithSeoRedirect($postObject, $wpService); | ||
|
||
$this->assertEquals('http://example.com/redirect', $postObjectWithSeoRedirect->getPermalink()); | ||
} | ||
|
||
/** | ||
* @testdox getPermalink returns original permalink if redirect url is not set | ||
*/ | ||
public function testGetPermalinkReturnsOriginalPermalinkIfRedirectUrlIsNotSet() | ||
{ | ||
$postObject = $this->createMock(PostObjectInterface::class); | ||
$wpService = $this->createMock(GetPostMeta::class); | ||
|
||
$postObject->method('getId')->willReturn(1); | ||
$postObject->method('getPermalink')->willReturn('http://example.com/post'); | ||
|
||
$wpService->method('getPostMeta')->willReturn(''); | ||
|
||
$postObjectWithSeoRedirect = new PostObjectWithSeoRedirect($postObject, $wpService); | ||
|
||
$this->assertEquals('http://example.com/post', $postObjectWithSeoRedirect->getPermalink()); | ||
} | ||
} |