mirror of
https://github.com/flarum/core.git
synced 2025-08-05 07:57:46 +02:00
[2.x] fix(Mentions): allow renderer to be used without context (#3954)
* fix(Mentions): allow renderer to be used without context * test(Mentions): implement test for rendering post without context * Update UnparsePostMentions.php * Update PostMentionsTest.php --------- Co-authored-by: IanM <16573496+imorland@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,7 @@ namespace Flarum\Mentions\Formatter;
|
|||||||
use Flarum\Discussion\Discussion;
|
use Flarum\Discussion\Discussion;
|
||||||
use Flarum\Http\SlugManager;
|
use Flarum\Http\SlugManager;
|
||||||
use Flarum\Locale\TranslatorInterface;
|
use Flarum\Locale\TranslatorInterface;
|
||||||
|
use Flarum\Post\Post;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use s9e\TextFormatter\Renderer;
|
use s9e\TextFormatter\Renderer;
|
||||||
use s9e\TextFormatter\Utils;
|
use s9e\TextFormatter\Utils;
|
||||||
@@ -24,12 +25,22 @@ class FormatPostMentions
|
|||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __invoke(Renderer $renderer, mixed $context, ?string $xml, Request $request = null): string
|
/**
|
||||||
|
* Configure rendering for post mentions.
|
||||||
|
*
|
||||||
|
* @param \s9e\TextFormatter\Renderer $renderer
|
||||||
|
* @param mixed $context
|
||||||
|
* @param string $xml
|
||||||
|
* @param \Psr\Http\Message\ServerRequestInterface|null $request
|
||||||
|
* @return string $xml to be rendered
|
||||||
|
*/
|
||||||
|
public function __invoke(Renderer $renderer, $context, $xml, Request $request = null)
|
||||||
{
|
{
|
||||||
$post = $context;
|
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
|
||||||
|
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
|
||||||
|
? $context->mentionsPosts->find($attributes['id'])
|
||||||
|
: Post::find($attributes['id']);
|
||||||
|
|
||||||
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
|
|
||||||
$post = $post->mentionsPosts->find($attributes['id']);
|
|
||||||
if ($post && $post->user) {
|
if ($post && $post->user) {
|
||||||
$attributes['displayname'] = $post->user->display_name;
|
$attributes['displayname'] = $post->user->display_name;
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
namespace Flarum\Mentions\Formatter;
|
namespace Flarum\Mentions\Formatter;
|
||||||
|
|
||||||
use Flarum\Locale\TranslatorInterface;
|
use Flarum\Locale\TranslatorInterface;
|
||||||
|
use Flarum\Post\Post;
|
||||||
use s9e\TextFormatter\Utils;
|
use s9e\TextFormatter\Utils;
|
||||||
|
|
||||||
class UnparsePostMentions
|
class UnparsePostMentions
|
||||||
@@ -31,10 +32,11 @@ class UnparsePostMentions
|
|||||||
*/
|
*/
|
||||||
protected function updatePostMentionTags(mixed $context, string $xml): string
|
protected function updatePostMentionTags(mixed $context, string $xml): string
|
||||||
{
|
{
|
||||||
$post = $context;
|
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
|
||||||
|
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
|
||||||
|
? $context->mentionsPosts->find($attributes['id'])
|
||||||
|
: Post::find($attributes['id']);
|
||||||
|
|
||||||
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
|
|
||||||
$post = $post->mentionsPosts->find($attributes['id']);
|
|
||||||
if ($post && $post->user) {
|
if ($post && $post->user) {
|
||||||
$attributes['displayname'] = $post->user->display_name;
|
$attributes['displayname'] = $post->user->display_name;
|
||||||
}
|
}
|
||||||
|
@@ -11,7 +11,9 @@ namespace Flarum\Mentions\Tests\integration\api;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
|
use Flarum\Formatter\Formatter;
|
||||||
use Flarum\Post\CommentPost;
|
use Flarum\Post\CommentPost;
|
||||||
|
use Flarum\Post\Post;
|
||||||
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
|
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
|
||||||
use Flarum\Testing\integration\TestCase;
|
use Flarum\Testing\integration\TestCase;
|
||||||
use Flarum\User\DisplayName\DriverInterface;
|
use Flarum\User\DisplayName\DriverInterface;
|
||||||
@@ -538,6 +540,40 @@ class PostMentionsTest extends TestCase
|
|||||||
$this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
|
$this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
|
||||||
$this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(11));
|
$this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(11));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function rendering_post_mention_with_a_post_context_works()
|
||||||
|
{
|
||||||
|
/** @var Formatter $formatter */
|
||||||
|
$formatter = $this->app()->getContainer()->make(Formatter::class);
|
||||||
|
|
||||||
|
$post = Post::find(4);
|
||||||
|
$user = User::find(1);
|
||||||
|
|
||||||
|
$xml = $formatter->parse($post->content, $post, $user);
|
||||||
|
$renderedHtml = $formatter->render($xml, $post);
|
||||||
|
|
||||||
|
$this->assertStringContainsString('TOBY$', $renderedHtml);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function rendering_post_mention_without_a_context_works()
|
||||||
|
{
|
||||||
|
/** @var Formatter $formatter */
|
||||||
|
$formatter = $this->app()->getContainer()->make(Formatter::class);
|
||||||
|
|
||||||
|
$post = Post::find(4);
|
||||||
|
$user = User::find(1);
|
||||||
|
|
||||||
|
$xml = $formatter->parse($post->content, null, $user);
|
||||||
|
$renderedHtml = $formatter->render($xml);
|
||||||
|
|
||||||
|
$this->assertStringContainsString('TOBY$', $renderedHtml);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CustomOtherDisplayNameDriver implements DriverInterface
|
class CustomOtherDisplayNameDriver implements DriverInterface
|
||||||
|
Reference in New Issue
Block a user