From 00166cb2681e76c7e605af7c19d6242226c720b0 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 9 Jan 2024 11:48:25 +0100 Subject: [PATCH 1/2] fix(Mentions): allow renderer to be used without context --- .../mentions/src/Formatter/FormatPostMentions.php | 14 ++++++++------ .../mentions/src/Formatter/UnparsePostMentions.php | 8 ++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/extensions/mentions/src/Formatter/FormatPostMentions.php b/extensions/mentions/src/Formatter/FormatPostMentions.php index a13266c74a..48e1ccc37a 100644 --- a/extensions/mentions/src/Formatter/FormatPostMentions.php +++ b/extensions/mentions/src/Formatter/FormatPostMentions.php @@ -11,6 +11,7 @@ use Flarum\Discussion\Discussion; use Flarum\Http\SlugManager; +use Flarum\Post\Post; use Psr\Http\Message\ServerRequestInterface as Request; use s9e\TextFormatter\Renderer; use s9e\TextFormatter\Utils; @@ -39,16 +40,17 @@ public function __construct(TranslatorInterface $translator, SlugManager $slugMa * * @param \s9e\TextFormatter\Renderer $renderer * @param mixed $context - * @param string|null $xml - * @param \Psr\Http\Message\ServerRequestInterface $request - * @return string + * @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) { $attributes['displayname'] = $post->user->display_name; } diff --git a/extensions/mentions/src/Formatter/UnparsePostMentions.php b/extensions/mentions/src/Formatter/UnparsePostMentions.php index 4f8d016148..f223dd638f 100644 --- a/extensions/mentions/src/Formatter/UnparsePostMentions.php +++ b/extensions/mentions/src/Formatter/UnparsePostMentions.php @@ -9,6 +9,7 @@ namespace Flarum\Mentions\Formatter; +use Flarum\Post\Post; use s9e\TextFormatter\Utils; use Symfony\Contracts\Translation\TranslatorInterface; @@ -50,8 +51,11 @@ protected function updatePostMentionTags($context, string $xml): string { $post = $context; - return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) { - $post = $post->mentionsPosts->find($attributes['id']); + 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']); + if ($post && $post->user) { $attributes['displayname'] = $post->user->display_name; } From 487912d2452ad742f11425b704484f62a144cb53 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Wed, 10 Jan 2024 10:40:20 +0100 Subject: [PATCH 2/2] test(Mentions): implement test for rendering post without context --- .../integration/api/PostMentionsTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/extensions/mentions/tests/integration/api/PostMentionsTest.php b/extensions/mentions/tests/integration/api/PostMentionsTest.php index 9bb48d8c0c..6a21e71b90 100644 --- a/extensions/mentions/tests/integration/api/PostMentionsTest.php +++ b/extensions/mentions/tests/integration/api/PostMentionsTest.php @@ -11,6 +11,8 @@ use Carbon\Carbon; use Flarum\Extend; +use Flarum\Formatter\Formatter; +use Flarum\Post\Post; use Flarum\Post\CommentPost; use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; @@ -538,6 +540,41 @@ public function editing_a_post_with_a_mention_of_a_post_with_deleted_author_work $this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']); $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