diff --git a/extensions/mentions/src/Listener/UpdateMentionsMetadataWhenVisible.php b/extensions/mentions/src/Listener/UpdateMentionsMetadataWhenVisible.php
index a2d01f5217..47a94a6103 100755
--- a/extensions/mentions/src/Listener/UpdateMentionsMetadataWhenVisible.php
+++ b/extensions/mentions/src/Listener/UpdateMentionsMetadataWhenVisible.php
@@ -60,7 +60,7 @@ protected function syncUserMentions(Post $post, array $mentioned)
$users = User::whereIn('id', $mentioned)
->get()
->filter(function ($user) use ($post) {
- return $post->isVisibleTo($user) && $user->id !== $post->user->id;
+ return $post->isVisibleTo($user) && $user->id !== $post->user_id;
})
->all();
@@ -75,8 +75,8 @@ protected function syncPostMentions(Post $reply, array $mentioned)
$posts = Post::with('user')
->whereIn('id', $mentioned)
->get()
- ->filter(function ($post) use ($reply) {
- return $post->user && $post->user->id !== $reply->user_id && $reply->isVisibleTo($post->user);
+ ->filter(function (Post $post) use ($reply) {
+ return $post->user && $post->user_id !== $reply->user_id && $reply->isVisibleTo($post->user);
})
->all();
diff --git a/extensions/mentions/tests/integration/api/PostMentionsTest.php b/extensions/mentions/tests/integration/api/PostMentionsTest.php
index 3457b1d190..b0396a3d77 100644
--- a/extensions/mentions/tests/integration/api/PostMentionsTest.php
+++ b/extensions/mentions/tests/integration/api/PostMentionsTest.php
@@ -47,12 +47,14 @@ protected function setUp(): void
['id' => 8, 'number' => 6, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '@"i_am_a_deleted_user"#p2020'],
['id' => 9, 'number' => 10, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 5, 'type' => 'comment', 'content' => 'I am bad
'],
['id' => 10, 'number' => 11, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '@"Bad "#p6 User"#p9'],
+ ['id' => 11, 'number' => 12, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 40, 'type' => 'comment', 'content' => '@"Bad "#p6 User"#p9'],
+ ['id' => 12, 'number' => 13, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '@"acme"#p11'],
],
'post_mentions_post' => [
['post_id' => 4, 'mentions_post_id' => 5],
['post_id' => 5, 'mentions_post_id' => 4],
['post_id' => 6, 'mentions_post_id' => 7],
- ['post_id' => 10, 'mentions_post_id' => 9]
+ ['post_id' => 10, 'mentions_post_id' => 9],
],
]);
@@ -417,6 +419,90 @@ public function post_mentions_with_removed_bad_string_from_display_names_works()
$this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
$this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(9));
}
+
+ /**
+ * @test
+ */
+ public function editing_a_post_that_has_a_mention_works()
+ {
+ $response = $this->send(
+ $this->request('PATCH', '/api/posts/10', [
+ 'authenticatedAs' => 1,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'content' => '@"Bad _ User"#p9',
+ ],
+ ],
+ ],
+ ])
+ );
+
+ $this->assertEquals(200, $response->getStatusCode());
+
+ $response = json_decode($response->getBody(), true);
+
+ $this->assertStringContainsString('Bad "#p6 User', $response['data']['attributes']['contentHtml']);
+ $this->assertEquals('@"Bad _ User"#p9', $response['data']['attributes']['content']);
+ $this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
+ $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(9));
+ }
+
+ /**
+ * @test
+ */
+ public function editing_a_post_with_deleted_author_that_has_a_mention_works()
+ {
+ $response = $this->send(
+ $this->request('PATCH', '/api/posts/11', [
+ 'authenticatedAs' => 1,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'content' => '@"Bad _ User"#p9',
+ ],
+ ],
+ ],
+ ])
+ );
+
+ $this->assertEquals(200, $response->getStatusCode());
+
+ $response = json_decode($response->getBody(), true);
+
+ $this->assertStringContainsString('Bad "#p6 User', $response['data']['attributes']['contentHtml']);
+ $this->assertEquals('@"Bad _ User"#p9', $response['data']['attributes']['content']);
+ $this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
+ $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(9));
+ }
+
+ /**
+ * @test
+ */
+ public function editing_a_post_with_a_mention_of_a_post_with_deleted_author_works()
+ {
+ $response = $this->send(
+ $this->request('PATCH', '/api/posts/12', [
+ 'authenticatedAs' => 1,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'content' => '@"acme"#p11',
+ ],
+ ],
+ ],
+ ])
+ );
+
+ $this->assertEquals(200, $response->getStatusCode());
+
+ $response = json_decode($response->getBody(), true);
+
+ $this->assertStringContainsString('[deleted]', $response['data']['attributes']['contentHtml']);
+ $this->assertEquals('@"[deleted]"#p11', $response['data']['attributes']['content']);
+ $this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
+ $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(11));
+ }
}
class CustomOtherDisplayNameDriver implements DriverInterface
diff --git a/extensions/mentions/tests/integration/api/UserMentionsTest.php b/extensions/mentions/tests/integration/api/UserMentionsTest.php
index c04a2c5e7b..8d8708a81a 100644
--- a/extensions/mentions/tests/integration/api/UserMentionsTest.php
+++ b/extensions/mentions/tests/integration/api/UserMentionsTest.php
@@ -44,10 +44,11 @@ protected function setUp(): void
['id' => 4, 'number' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 3, 'type' => 'comment', 'content' => '@tobyuuu'],
['id' => 6, 'number' => 3, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '@"i_am_a_deleted_user"#2021'],
['id' => 10, 'number' => 11, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 5, 'type' => 'comment', 'content' => '@"Bad "#p6 User"#5'],
+ ['id' => 11, 'number' => 12, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 50, 'type' => 'comment', 'content' => '@"Bad "#p6 User"#5'],
],
'post_mentions_user' => [
['post_id' => 4, 'mentions_user_id' => 4],
- ['post_id' => 10, 'mentions_user_id' => 5]
+ ['post_id' => 10, 'mentions_user_id' => 5],
],
]);
@@ -438,6 +439,62 @@ public function user_mentions_with_removed_bad_string_from_display_names_works()
$this->assertStringContainsString('UserMention', $response['data']['attributes']['contentHtml']);
$this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsUsers->find(5));
}
+
+ /**
+ * @test
+ */
+ public function editing_a_post_that_has_a_mention_works()
+ {
+ $response = $this->send(
+ $this->request('PATCH', '/api/posts/10', [
+ 'authenticatedAs' => 1,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'content' => '@"Bad _ User"#5',
+ ],
+ ],
+ ],
+ ])
+ );
+
+ $this->assertEquals(200, $response->getStatusCode());
+
+ $response = json_decode($response->getBody(), true);
+
+ $this->assertStringContainsString('Bad "#p6 User', $response['data']['attributes']['contentHtml']);
+ $this->assertEquals('@"Bad _ User"#5', $response['data']['attributes']['content']);
+ $this->assertStringContainsString('UserMention', $response['data']['attributes']['contentHtml']);
+ $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsUsers->find(5));
+ }
+
+ /**
+ * @test
+ */
+ public function editing_a_post_with_deleted_author_that_has_a_mention_works()
+ {
+ $response = $this->send(
+ $this->request('PATCH', '/api/posts/11', [
+ 'authenticatedAs' => 1,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'content' => '@"Bad _ User"#5',
+ ],
+ ],
+ ],
+ ])
+ );
+
+ $this->assertEquals(200, $response->getStatusCode());
+
+ $response = json_decode($response->getBody(), true);
+
+ $this->assertStringContainsString('Bad "#p6 User', $response['data']['attributes']['contentHtml']);
+ $this->assertEquals('@"Bad _ User"#5', $response['data']['attributes']['content']);
+ $this->assertStringContainsString('UserMention', $response['data']['attributes']['contentHtml']);
+ $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsUsers->find(5));
+ }
}
class CustomDisplayNameDriver implements DriverInterface