-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an event hook PostContentIsRendering
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Modules\Blog\Events; | ||
|
||
class PostContentIsRendering | ||
{ | ||
/** | ||
* @var string The body of the page to render | ||
*/ | ||
private $content; | ||
private $original; | ||
|
||
public function __construct($content) | ||
{ | ||
$this->content = $content; | ||
$this->original = $content; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getContent() | ||
{ | ||
return $this->content; | ||
} | ||
|
||
/** | ||
* @param string $content | ||
*/ | ||
public function setContent($content) | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getOriginal() | ||
{ | ||
return $this->original; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->getContent(); | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Modules\Blog\Tests; | ||
|
||
use Illuminate\Support\Facades\Event; | ||
use Modules\Blog\Entities\Status; | ||
use Modules\Blog\Events\PostContentIsRendering; | ||
|
||
class PostContentIsRenderingTest extends BaseBlogTestCase | ||
{ | ||
/** @test */ | ||
public function it_can_change_final_content() | ||
{ | ||
Event::listen(PostContentIsRendering::class, function (PostContentIsRendering $event) { | ||
$event->setContent('<strong>' . $event->getOriginal() . '</strong>'); | ||
}); | ||
|
||
$post = $this->post->create([ | ||
'en' => ['title' => 'lorem en', 'slug' => 'something', 'content' => 'My Post Body'], | ||
'fr' => ['title' => 'lorem fr', 'slug' => 'quelque-chose', 'content' => 'My Post Body'], | ||
'category_id' => 1, | ||
'status' => Status::PUBLISHED, | ||
]); | ||
|
||
$this->assertEquals('<strong>My Post Body</strong>', $post->content); | ||
} | ||
|
||
/** @test */ | ||
public function it_doesnt_alter_content_if_no_listeners() | ||
{ | ||
$post = $this->post->create([ | ||
'en' => ['title' => 'lorem en', 'slug' => 'something', 'content' => 'My Post Body'], | ||
'fr' => ['title' => 'lorem fr', 'slug' => 'quelque-chose', 'content' => 'My Post Body'], | ||
'category_id' => 1, | ||
'status' => Status::PUBLISHED, | ||
]); | ||
|
||
$this->assertEquals('My Post Body', $post->content); | ||
} | ||
} |
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