Skip to content

Commit 026b261

Browse files
[1.x] fix(Mentions): allow renderer to be used without context (#3953)
* fix(Mentions): allow renderer to be used without context * test(Mentions): implement test for rendering post without context
1 parent e309a63 commit 026b261

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/Formatter/FormatPostMentions.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Flarum\Discussion\Discussion;
1313
use Flarum\Http\SlugManager;
14+
use Flarum\Post\Post;
1415
use Psr\Http\Message\ServerRequestInterface as Request;
1516
use s9e\TextFormatter\Renderer;
1617
use s9e\TextFormatter\Utils;
@@ -39,16 +40,17 @@ public function __construct(TranslatorInterface $translator, SlugManager $slugMa
3940
*
4041
* @param \s9e\TextFormatter\Renderer $renderer
4142
* @param mixed $context
42-
* @param string|null $xml
43-
* @param \Psr\Http\Message\ServerRequestInterface $request
44-
* @return string
43+
* @param string $xml
44+
* @param \Psr\Http\Message\ServerRequestInterface|null $request
45+
* @return string $xml to be rendered
4546
*/
4647
public function __invoke(Renderer $renderer, $context, $xml, Request $request = null)
4748
{
48-
$post = $context;
49+
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
50+
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
51+
? $context->mentionsPosts->find($attributes['id'])
52+
: Post::find($attributes['id']);
4953

50-
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
51-
$post = $post->mentionsPosts->find($attributes['id']);
5254
if ($post && $post->user) {
5355
$attributes['displayname'] = $post->user->display_name;
5456
}

src/Formatter/UnparsePostMentions.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Flarum\Mentions\Formatter;
1111

12+
use Flarum\Post\Post;
1213
use s9e\TextFormatter\Utils;
1314
use Symfony\Contracts\Translation\TranslatorInterface;
1415

@@ -50,8 +51,11 @@ protected function updatePostMentionTags($context, string $xml): string
5051
{
5152
$post = $context;
5253

53-
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
54-
$post = $post->mentionsPosts->find($attributes['id']);
54+
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
55+
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
56+
? $context->mentionsPosts->find($attributes['id'])
57+
: Post::find($attributes['id']);
58+
5559
if ($post && $post->user) {
5660
$attributes['displayname'] = $post->user->display_name;
5761
}

tests/integration/api/PostMentionsTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use Carbon\Carbon;
1313
use Flarum\Extend;
14+
use Flarum\Formatter\Formatter;
15+
use Flarum\Post\Post;
1416
use Flarum\Post\CommentPost;
1517
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
1618
use Flarum\Testing\integration\TestCase;
@@ -538,6 +540,41 @@ public function editing_a_post_with_a_mention_of_a_post_with_deleted_author_work
538540
$this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
539541
$this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(11));
540542
}
543+
544+
/**
545+
* @test
546+
*/
547+
public function rendering_post_mention_with_a_post_context_works()
548+
{
549+
/** @var Formatter $formatter */
550+
$formatter = $this->app()->getContainer()->make(Formatter::class);
551+
552+
$post = Post::find(4);
553+
$user = User::find(1);
554+
555+
$xml = $formatter->parse($post->content, $post, $user);
556+
$renderedHtml = $formatter->render($xml, $post);
557+
558+
$this->assertStringContainsString('TOBY$', $renderedHtml);
559+
}
560+
561+
/**
562+
* @test
563+
*/
564+
public function rendering_post_mention_without_a_context_works()
565+
{
566+
/** @var Formatter $formatter */
567+
$formatter = $this->app()->getContainer()->make(Formatter::class);
568+
569+
$post = Post::find(4);
570+
$user = User::find(1);
571+
572+
$xml = $formatter->parse($post->content, null, $user);
573+
$renderedHtml = $formatter->render($xml);
574+
575+
$this->assertStringContainsString('TOBY$', $renderedHtml);
576+
}
577+
541578
}
542579

543580
class CustomOtherDisplayNameDriver implements DriverInterface

0 commit comments

Comments
 (0)