Skip to content

Commit

Permalink
fix(mentions): accessing id of null user relation (#3618)
Browse files Browse the repository at this point in the history
* test: editing a post with deleted author and mentions
* fix(mentions): accessing `id` of null `user` relation

Signed-off-by: Sami Mazouz <ilyasmazouz@gmail.com>
  • Loading branch information
SychO9 authored Aug 26, 2022
1 parent 434c459 commit 6ffa9e3
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand 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();

Expand Down
88 changes: 87 additions & 1 deletion extensions/mentions/tests/integration/api/PostMentionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<r><POSTMENTION displayname="i_am_a_deleted_user" id="2020" number="8" discussionid="2" username="i_am_a_deleted_user">@"i_am_a_deleted_user"#p2020</POSTMENTION></r>'],
['id' => 9, 'number' => 10, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 5, 'type' => 'comment', 'content' => '<r><p>I am bad</p></r>'],
['id' => 10, 'number' => 11, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '<r><POSTMENTION displayname="Bad &quot;#p6 User" id="9" number="10" discussionid="2">@"Bad "#p6 User"#p9</POSTMENTION></r>'],
['id' => 11, 'number' => 12, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 40, 'type' => 'comment', 'content' => '<r><POSTMENTION displayname="Bad &quot;#p6 User" id="9" number="10" discussionid="2">@"Bad "#p6 User"#p9</POSTMENTION></r>'],
['id' => 12, 'number' => 13, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '<r><POSTMENTION displayname="deleted_user" id="11" number="12" discussionid="2">@"acme"#p11</POSTMENTION></r>'],
],
'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],
],
]);

Expand Down Expand Up @@ -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
Expand Down
59 changes: 58 additions & 1 deletion extensions/mentions/tests/integration/api/UserMentionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<r><USERMENTION displayname="TobyFlarum___" id="4" username="toby">@tobyuuu</USERMENTION></r>'],
['id' => 6, 'number' => 3, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '<r><USERMENTION displayname="i_am_a_deleted_user" id="2021" username="i_am_a_deleted_user">@"i_am_a_deleted_user"#2021</USERMENTION></r>'],
['id' => 10, 'number' => 11, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 5, 'type' => 'comment', 'content' => '<r><USERMENTION displayname="Bad &quot;#p6 User" id="5">@"Bad "#p6 User"#5</USERMENTION></r>'],
['id' => 11, 'number' => 12, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 50, 'type' => 'comment', 'content' => '<r><USERMENTION displayname="Bad &quot;#p6 User" id="5">@"Bad "#p6 User"#5</USERMENTION></r>'],
],
'post_mentions_user' => [
['post_id' => 4, 'mentions_user_id' => 4],
['post_id' => 10, 'mentions_user_id' => 5]
['post_id' => 10, 'mentions_user_id' => 5],
],
]);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6ffa9e3

Please sign in to comment.