From ed7a276f23583557c0ea70b8f924ae26118cf1c9 Mon Sep 17 00:00:00 2001 From: Jay Sizzla Date: Fri, 20 Dec 2024 13:28:11 +0000 Subject: [PATCH] Use shouldSend for NewComment notifications --- app/Http/Livewire/Comments.php | 23 +- app/Notifications/NewComment.php | 56 +++ .../NewCommentArticleNotificationTest.php | 174 +++++++++ .../NewCommentPlaylistNotificationTest.php | 165 +++++++++ .../NewCommentRequestNotificationTest.php | 330 ++++++++++++++++++ .../NewCommentTicketNotificationTest.php | 247 +++++++++++++ .../NewCommentTorrentNotificationTest.php | 330 ++++++++++++++++++ 7 files changed, 1316 insertions(+), 9 deletions(-) create mode 100644 tests/Feature/Notifications/NewCommentArticleNotificationTest.php create mode 100644 tests/Feature/Notifications/NewCommentPlaylistNotificationTest.php create mode 100644 tests/Feature/Notifications/NewCommentRequestNotificationTest.php create mode 100644 tests/Feature/Notifications/NewCommentTicketNotificationTest.php create mode 100644 tests/Feature/Notifications/NewCommentTorrentNotificationTest.php diff --git a/app/Http/Livewire/Comments.php b/app/Http/Livewire/Comments.php index dbc73c484b..4c776d6d8e 100644 --- a/app/Http/Livewire/Comments.php +++ b/app/Http/Livewire/Comments.php @@ -130,22 +130,27 @@ final public function postComment(): void case $this->model instanceof Ticket: $ticket = $this->model; - if ($this->user->id !== $ticket->staff_id && $ticket->staff_id !== null) { - User::find($ticket->staff_id)?->notify(new NewComment($this->model, $comment)); - } + // Notify assigned staff if needed + User::find($ticket->staff_id)?->notify(new NewComment($this->model, $comment)); - if ($this->user->id !== $ticket->user_id) { - User::find($ticket->user_id)?->notify(new NewComment($this->model, $comment)); - } + // Notify ticket creator if needed + User::find($ticket->user_id)?->notify(new NewComment($this->model, $comment)); break; case $this->model instanceof Article: + User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment)); + + break; case $this->model instanceof Playlist: + User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment)); + + break; case $this->model instanceof TorrentRequest: + User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment)); + + break; case $this->model instanceof Torrent: - if ($this->user->id !== $this->model->user_id) { - User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment)); - } + User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment)); break; } diff --git a/app/Notifications/NewComment.php b/app/Notifications/NewComment.php index cb6a05e9de..b97039758d 100644 --- a/app/Notifications/NewComment.php +++ b/app/Notifications/NewComment.php @@ -23,6 +23,7 @@ use App\Models\Ticket; use App\Models\Torrent; use App\Models\TorrentRequest; +use App\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; @@ -47,6 +48,61 @@ public function via(object $notifiable): array return ['database']; } + /** + * Determine if the notification should be sent. + * + * @return bool + */ + public function shouldSend(User $notifiable): bool + { + // Evaluate general settings + if ($this->comment->user_id === $notifiable->id || $notifiable->notification?->block_notifications == 1) { + return false; + } + + // Initialize target variables + $targetGroup = ''; + $targetSetting = ''; + + // Evaluate model based settings + switch (true) { + case $this->model instanceof Torrent: + $targetGroup = 'json_torrent_groups'; + $targetSetting = 'show_torrent_comment'; + + break; + case $this->model instanceof TorrentRequest: + $targetGroup = 'json_request_groups'; + $targetSetting = 'show_request_comment'; + + break; + case $this->model instanceof Ticket: + return ! ($this->comment->staff_id === $notifiable->id && $this->comment->staff_id !== null) + ; + + case $this->model instanceof Playlist: + case $this->model instanceof Article: + return true; + case $this->model instanceof Collection: + break; + default: + // In the unlikely case none matches, fallback to true to not lose a notification + return true; + } + + if (!$notifiable->notification?->$targetSetting) { + return false; + } + + if (\is_array($notifiable->notification->$targetGroup)) { + // If the sender's group ID is found in the "Block all notifications from the selected groups" array, + // the expression will return false. + return !\in_array($this->comment->user->group->id, $notifiable->notification->$targetGroup, true); + } + + return true; + } + /** * Get the array representation of the notification. * diff --git a/tests/Feature/Notifications/NewCommentArticleNotificationTest.php b/tests/Feature/Notifications/NewCommentArticleNotificationTest.php new file mode 100644 index 0000000000..601c75931e --- /dev/null +++ b/tests/Feature/Notifications/NewCommentArticleNotificationTest.php @@ -0,0 +1,174 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +use App\Http\Livewire\Comments; +use App\Models\Article; +use App\Models\Bot; +use App\Models\Chatroom; +use App\Models\Comment; +use App\Models\Group; +use App\Models\User; +use App\Models\UserNotification; +use App\Notifications\NewComment; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Notification; +use Livewire\Livewire; + +uses(RefreshDatabase::class); + +test('user comments on article creates a notification for staff', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + $staffGroup = Group::factory()->create([ + 'can_comment' => true, + 'is_modo' => true, + ]); + + $user = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + $staff = User::factory()->create([ + 'group_id' => $staffGroup->id, + 'can_comment' => true, + ]); + + $staffNotificationSettings = UserNotification::factory()->for($staff)->create([ + 'block_notifications' => 0, + ]); + + $article = Article::factory()->for($staff)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($user) + ->test(Comments::class, ['model' => $article]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$staff], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('staff comments on own article does not create a notification for staff', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $staffGroup = Group::factory()->create([ + 'can_comment' => true, + 'is_modo' => true, + ]); + + $staff = User::factory()->create([ + 'group_id' => $staffGroup->id, + 'can_comment' => true, + ]); + + $staffNotificationSettings = UserNotification::factory()->for($staff)->create([ + 'block_notifications' => 0, + ]); + + $article = Article::factory()->for($staff)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($staff) + ->test(Comments::class, ['model' => $article]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('user comments on article does not a notification for staff user when all notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + $staffGroup = Group::factory()->create([ + 'can_comment' => true, + 'is_modo' => true, + ]); + + $user = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + $staff = User::factory()->create([ + 'group_id' => $staffGroup->id, + 'can_comment' => true, + ]); + + $staffNotificationSettings = UserNotification::factory()->for($staff)->create([ + 'block_notifications' => 1, + ]); + + $article = Article::factory()->for($staff)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($user) + ->test(Comments::class, ['model' => $article]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); diff --git a/tests/Feature/Notifications/NewCommentPlaylistNotificationTest.php b/tests/Feature/Notifications/NewCommentPlaylistNotificationTest.php new file mode 100644 index 0000000000..0491a747f2 --- /dev/null +++ b/tests/Feature/Notifications/NewCommentPlaylistNotificationTest.php @@ -0,0 +1,165 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +use App\Http\Livewire\Comments; +use App\Models\Bot; +use App\Models\Chatroom; +use App\Models\Comment; +use App\Models\Group; +use App\Models\Playlist; +use App\Models\User; +use App\Models\UserNotification; +use App\Notifications\NewComment; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Notification; +use Livewire\Livewire; + +uses(RefreshDatabase::class); + +test('comments on playlist creates a notification for playlist owner', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + + $owner = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + + $ownerNotificationSettings = UserNotification::factory()->for($owner)->create([ + 'block_notifications' => 0, + ]); + + $playlist = Playlist::factory()->for($owner)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $playlist]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$owner], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('user comments on own playlist does not create a notification for self', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + + $owner = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + + $ownerNotificationSettings = UserNotification::factory()->for($owner)->create([ + 'block_notifications' => 0, + ]); + + $playlist = Playlist::factory()->for($owner)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($owner) + ->test(Comments::class, ['model' => $playlist]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('comments on playlist does not create a notification for playlist owner when all notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + + $owner = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + + $ownerNotificationSettings = UserNotification::factory()->for($owner)->create([ + 'block_notifications' => 1, + ]); + + $playlist = Playlist::factory()->for($owner)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $playlist]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); diff --git a/tests/Feature/Notifications/NewCommentRequestNotificationTest.php b/tests/Feature/Notifications/NewCommentRequestNotificationTest.php new file mode 100644 index 0000000000..a7703d1ae4 --- /dev/null +++ b/tests/Feature/Notifications/NewCommentRequestNotificationTest.php @@ -0,0 +1,330 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +use App\Http\Livewire\Comments; +use App\Models\Bot; +use App\Models\Chatroom; +use App\Models\Comment; +use App\Models\Group; +use App\Models\TorrentRequest; +use App\Models\User; +use App\Models\UserNotification; +use App\Notifications\NewComment; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Notification; +use Livewire\Livewire; + +uses(RefreshDatabase::class); + +test('comment own request does not create a notification for self', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $requester = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($requester)->create([ + 'block_notifications' => 0, + 'show_request_comment' => 1, + ]); + + $torrentRequest = TorrentRequest::factory()->for($requester)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($requester) + ->test(Comments::class, ['model' => $torrentRequest]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('comment a request creates a notification for the requester', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $requester = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($requester)->create([ + 'block_notifications' => 0, + 'show_request_comment' => 1, + ]); + + $torrentRequest = TorrentRequest::factory()->for($requester)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrentRequest]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$requester], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('comment a request creates a notification for the requester when request comment notifications are not disabled for specific group', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + $randomGroup = Group::factory()->create(); + + $requester = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($requester)->create([ + 'block_notifications' => 0, + 'show_request_comment' => 1, + 'json_request_groups' => [$randomGroup->id], + ]); + + $torrentRequest = TorrentRequest::factory()->for($requester)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrentRequest]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$requester], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('comment a request creates a notification for the requester when all notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $requester = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($requester)->create([ + 'block_notifications' => 1, + 'show_request_comment' => 1, + ]); + + $torrentRequest = TorrentRequest::factory()->for($requester)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrentRequest]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('comment a request creates a notification for the requester when request comment notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $requester = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($requester)->create([ + 'block_notifications' => 0, + 'show_request_comment' => 0, + ]); + + $torrentRequest = TorrentRequest::factory()->for($requester)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrentRequest]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('comment a request creates a notification for the requester when request comment notifications are disabled for specific group', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $requester = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($requester)->create([ + 'block_notifications' => 0, + 'show_request_comment' => 1, + 'json_request_groups' => [$group->id], + ]); + + $torrentRequest = TorrentRequest::factory()->for($requester)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrentRequest]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); diff --git a/tests/Feature/Notifications/NewCommentTicketNotificationTest.php b/tests/Feature/Notifications/NewCommentTicketNotificationTest.php new file mode 100644 index 0000000000..6e59dae1ab --- /dev/null +++ b/tests/Feature/Notifications/NewCommentTicketNotificationTest.php @@ -0,0 +1,247 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +use App\Http\Livewire\Comments; +use App\Models\Bot; +use App\Models\Chatroom; +use App\Models\Comment; +use App\Models\Group; +use App\Models\Ticket; +use App\Models\User; +use App\Models\UserNotification; +use App\Notifications\NewComment; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Notification; +use Livewire\Livewire; + +uses(RefreshDatabase::class); + +test('user comments own ticket does not create a notification for self but staff', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + $staffGroup = Group::factory()->create([ + 'can_comment' => true, + 'is_modo' => true, + ]); + + $user = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + $staff = User::factory()->create([ + 'group_id' => $staffGroup->id, + 'can_comment' => true, + ]); + + $userNotificationSettings = UserNotification::factory()->for($user)->create([ + 'block_notifications' => 0, + ]); + $staffNotificationSettings = UserNotification::factory()->for($staff)->create([ + 'block_notifications' => 0, + ]); + + $ticket = Ticket::factory()->for($user)->create([ + 'staff_id' => $staff->id, + ]); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($user) + ->test(Comments::class, ['model' => $ticket]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$staff], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('user comments own ticket does not create a notification for staff when none is assigned', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + $staffGroup = Group::factory()->create([ + 'can_comment' => true, + 'is_modo' => true, + ]); + + $user = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + $staff = User::factory()->create([ + 'group_id' => $staffGroup->id, + 'can_comment' => true, + ]); + + $userNotificationSettings = UserNotification::factory()->for($user)->create([ + 'block_notifications' => 0, + ]); + + $ticket = Ticket::factory()->for($user)->create([ + 'staff_id' => null, + ]); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($user) + ->test(Comments::class, ['model' => $ticket]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('staff comments a ticket creates a notification for the user but not staff', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + ]); + $staffGroup = Group::factory()->create([ + 'can_comment' => true, + 'is_modo' => true, + ]); + + $user = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + ]); + $staff = User::factory()->create([ + 'group_id' => $staffGroup->id, + 'can_comment' => true, + ]); + + $userNotificationSettings = UserNotification::factory()->for($user)->create([ + 'block_notifications' => 0, + ]); + + $ticket = Ticket::factory()->for($user)->create([ + 'staff_id' => $staff->id, + ]); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($staff) + ->test(Comments::class, ['model' => $ticket]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$user], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('staff comments a ticket does not create a notification for the user when all notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + $staffGroup = Group::factory()->create([ + 'can_comment' => true, + 'is_modo' => true, + ]); + + $user = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $staff = User::factory()->create([ + 'group_id' => $staffGroup->id, + 'can_comment' => true, + ]); + + $userNotificationSettings = UserNotification::factory()->for($user)->create([ + 'block_notifications' => 1, + ]); + + $ticket = Ticket::factory()->for($user)->create([ + 'staff_id' => $staff->id, + ]); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($staff) + ->test(Comments::class, ['model' => $ticket]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); diff --git a/tests/Feature/Notifications/NewCommentTorrentNotificationTest.php b/tests/Feature/Notifications/NewCommentTorrentNotificationTest.php new file mode 100644 index 0000000000..5dc105c6ca --- /dev/null +++ b/tests/Feature/Notifications/NewCommentTorrentNotificationTest.php @@ -0,0 +1,330 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +use App\Http\Livewire\Comments; +use App\Models\Bot; +use App\Models\Chatroom; +use App\Models\Comment; +use App\Models\Group; +use App\Models\Torrent; +use App\Models\User; +use App\Models\UserNotification; +use App\Notifications\NewComment; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Notification; +use Livewire\Livewire; + +uses(RefreshDatabase::class); + +test('comment own torrent does not create a notification for self', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $uploader = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $uploaderNotificationSettings = UserNotification::factory()->for($uploader)->create([ + 'block_notifications' => 0, + 'show_torrent_comment' => 1, + ]); + + $torrent = Torrent::factory()->for($uploader)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($uploader) + ->test(Comments::class, ['model' => $torrent]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('comment a torent creates a notification for the uploader', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $uploader = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($uploader)->create([ + 'block_notifications' => 0, + 'show_torrent_comment' => 1, + ]); + + $torrent = Torrent::factory()->for($uploader)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrent]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$uploader], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('comment a torrent creates a notification for the requester when request comment notifications are not disabled for specific group', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + $randomGroup = Group::factory()->create(); + + $uploader = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($uploader)->create([ + 'block_notifications' => 0, + 'show_torrent_comment' => 1, + 'json_torrent_groups' => [$randomGroup->id], + ]); + + $torrent = Torrent::factory()->for($uploader)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrent]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertSentTo( + [$uploader], + NewComment::class + ); + Notification::assertCount(1); +}); + +test('comment a torrent creates a notification for the requester when all notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $uploader = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($uploader)->create([ + 'block_notifications' => 1, + 'show_torrent_comment' => 1, + ]); + + $torrent = Torrent::factory()->for($uploader)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrent]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('comment a torrent creates a notification for the requester when request comment notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $uploader = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($uploader)->create([ + 'block_notifications' => 0, + 'show_torrent_comment' => 0, + ]); + + $torrent = Torrent::factory()->for($uploader)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrent]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +}); + +test('comment a torrent creates a notification for the requester when request comment notifications are disabled for specific group', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create([ + 'can_comment' => true, + 'can_request' => true, + ]); + + $uploader = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + $commenter = User::factory()->create([ + 'group_id' => $group->id, + 'can_comment' => true, + 'can_request' => true, + ]); + + $fillerNotificationSettings = UserNotification::factory()->for($uploader)->create([ + 'block_notifications' => 0, + 'show_torrent_comment' => 1, + 'json_torrent_groups' => [$group->id], + ]); + + $torrent = Torrent::factory()->for($uploader)->create(); + + $commentText = 'This is a test comment'; + + Livewire::actingAs($commenter) + ->test(Comments::class, ['model' => $torrent]) + ->set('newCommentState', $commentText) + ->set('anon', false) + ->call('postComment'); + + $this->assertEquals(1, Comment::count()); + + Notification::assertCount(0); +});