|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Activity; |
| 4 | + |
| 5 | +use BookStack\Activity\Notifications\Messages\CommentMentionNotification; |
| 6 | +use BookStack\Permissions\Permission; |
| 7 | +use Illuminate\Support\Facades\Notification; |
| 8 | +use Tests\TestCase; |
| 9 | + |
| 10 | +class CommentMentionTest extends TestCase |
| 11 | +{ |
| 12 | + public function test_mentions_are_notified() |
| 13 | + { |
| 14 | + $userToMention = $this->users->viewer(); |
| 15 | + $this->permissions->grantUserRolePermissions($userToMention, [Permission::ReceiveNotifications]); |
| 16 | + $editor = $this->users->editor(); |
| 17 | + $page = $this->entities->pageWithinChapter(); |
| 18 | + $notifications = Notification::fake(); |
| 19 | + |
| 20 | + $this->actingAs($editor)->post("/comment/{$page->id}", [ |
| 21 | + 'html' => '<p>Hello <a data-mention-user-id="' . $userToMention->id . '">@user</a></p>' |
| 22 | + ])->assertOk(); |
| 23 | + |
| 24 | + $notifications->assertSentTo($userToMention, function (CommentMentionNotification $notification) use ($userToMention, $editor, $page) { |
| 25 | + $mail = $notification->toMail($userToMention); |
| 26 | + $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES); |
| 27 | + return $mail->subject === 'You have been mentioned in a comment on page: ' . $page->name |
| 28 | + && str_contains($mailContent, 'View Comment') |
| 29 | + && str_contains($mailContent, 'Page Name: ' . $page->name) |
| 30 | + && str_contains($mailContent, 'Page Path: ' . $page->book->getShortName(24) . ' > ' . $page->chapter->getShortName(24)) |
| 31 | + && str_contains($mailContent, 'Commenter: ' . $editor->name) |
| 32 | + && str_contains($mailContent, 'Comment: Hello @user'); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + public function test_mentions_are_not_notified_if_mentioned_by_same_user() |
| 37 | + { |
| 38 | + $editor = $this->users->editor(); |
| 39 | + $this->permissions->grantUserRolePermissions($editor, [Permission::ReceiveNotifications]); |
| 40 | + $page = $this->entities->page(); |
| 41 | + $notifications = Notification::fake(); |
| 42 | + |
| 43 | + $this->actingAs($editor)->post("/comment/{$page->id}", [ |
| 44 | + 'html' => '<p>Hello <a data-mention-user-id="' . $editor->id . '"></a></p>' |
| 45 | + ])->assertOk(); |
| 46 | + |
| 47 | + $notifications->assertNothingSent(); |
| 48 | + } |
| 49 | + |
| 50 | + public function test_mentions_are_logged_to_the_database_even_if_not_notified() |
| 51 | + { |
| 52 | + $editor = $this->users->editor(); |
| 53 | + $otherUser = $this->users->viewer(); |
| 54 | + $this->permissions->grantUserRolePermissions($editor, [Permission::ReceiveNotifications]); |
| 55 | + $page = $this->entities->page(); |
| 56 | + $notifications = Notification::fake(); |
| 57 | + |
| 58 | + $this->actingAs($editor)->post("/comment/{$page->id}", [ |
| 59 | + 'html' => '<p>Hello <a data-mention-user-id="' . $editor->id . '"></a> and <a data-mention-user-id="' . $otherUser->id . '"></a></p>' |
| 60 | + ])->assertOk(); |
| 61 | + |
| 62 | + $notifications->assertNothingSent(); |
| 63 | + |
| 64 | + $comment = $page->comments()->latest()->first(); |
| 65 | + $this->assertDatabaseHas('mention_history', [ |
| 66 | + 'mentionable_id' => $comment->id, |
| 67 | + 'mentionable_type' => 'comment', |
| 68 | + 'from_user_id' => $editor->id, |
| 69 | + 'to_user_id' => $otherUser->id, |
| 70 | + ]); |
| 71 | + $this->assertDatabaseHas('mention_history', [ |
| 72 | + 'mentionable_id' => $comment->id, |
| 73 | + 'mentionable_type' => 'comment', |
| 74 | + 'from_user_id' => $editor->id, |
| 75 | + 'to_user_id' => $editor->id, |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + public function test_comment_updates_will_send_notifications_only_if_mention_is_new() |
| 80 | + { |
| 81 | + $userToMention = $this->users->viewer(); |
| 82 | + $this->permissions->grantUserRolePermissions($userToMention, [Permission::ReceiveNotifications]); |
| 83 | + $editor = $this->users->editor(); |
| 84 | + $this->permissions->grantUserRolePermissions($editor, [Permission::CommentUpdateOwn]); |
| 85 | + $page = $this->entities->page(); |
| 86 | + $notifications = Notification::fake(); |
| 87 | + |
| 88 | + $this->actingAs($editor)->post("/comment/{$page->id}", [ |
| 89 | + 'html' => '<p>Hello there</p>' |
| 90 | + ])->assertOk(); |
| 91 | + $comment = $page->comments()->latest()->first(); |
| 92 | + |
| 93 | + $notifications->assertNothingSent(); |
| 94 | + |
| 95 | + $this->put("/comment/{$comment->id}", [ |
| 96 | + 'html' => '<p>Hello <a data-mention-user-id="' . $userToMention->id . '"></a></p>' |
| 97 | + ])->assertOk(); |
| 98 | + |
| 99 | + $notifications->assertSentTo($userToMention, CommentMentionNotification::class); |
| 100 | + $notifications->assertCount(1); |
| 101 | + |
| 102 | + $this->put("/comment/{$comment->id}", [ |
| 103 | + 'html' => '<p>Hello again<a data-mention-user-id="' . $userToMention->id . '"></a></p>' |
| 104 | + ])->assertOk(); |
| 105 | + |
| 106 | + $notifications->assertCount(1); |
| 107 | + } |
| 108 | + |
| 109 | + public function test_notification_limited_to_those_with_view_permissions() |
| 110 | + { |
| 111 | + $userA = $this->users->newUser(); |
| 112 | + $userB = $this->users->newUser(); |
| 113 | + $this->permissions->grantUserRolePermissions($userA, [Permission::ReceiveNotifications]); |
| 114 | + $this->permissions->grantUserRolePermissions($userB, [Permission::ReceiveNotifications]); |
| 115 | + $notifications = Notification::fake(); |
| 116 | + $page = $this->entities->page(); |
| 117 | + |
| 118 | + $this->permissions->disableEntityInheritedPermissions($page); |
| 119 | + $this->permissions->addEntityPermission($page, ['view'], $userA->roles()->first()); |
| 120 | + |
| 121 | + $this->asAdmin()->post("/comment/{$page->id}", [ |
| 122 | + 'html' => '<p>Hello <a data-mention-user-id="' . $userA->id . '"></a> and <a data-mention-user-id="' . $userB->id . '"></a></p>' |
| 123 | + ])->assertOk(); |
| 124 | + |
| 125 | + $notifications->assertCount(1); |
| 126 | + $notifications->assertSentTo($userA, CommentMentionNotification::class); |
| 127 | + } |
| 128 | +} |
0 commit comments