1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Models;
7
8use App\Jobs\Notifications\CommentNew;
9use App\Models\Build;
10use App\Models\Comment;
11use App\Models\Notification;
12use App\Models\User;
13use App\Models\UserNotificationOption;
14use Tests\TestCase;
15
16class CommentTest extends TestCase
17{
18 /**
19 * @dataProvider commentReplyOptionDataProvider
20 */
21 public function testCommentReplyNotification($option, $shouldBeSent)
22 {
23 $user = User::factory()->create();
24 if ($option !== null) {
25 $user->notificationOptions()->create([
26 'name' => Notification::COMMENT_NEW,
27 'details' => [UserNotificationOption::COMMENT_REPLY => $option],
28 ]);
29 }
30
31 $commenter = User::factory()->create();
32 $commentable = Build::factory()->create();
33 $parentComment = $commentable->comments()->create([
34 'message' => 'Test',
35 'user_id' => $user->getKey(),
36 ]);
37
38 $comment = $commentable->comments()->create([
39 'parent_id' => $parentComment->getKey(),
40 'message' => 'Hello',
41 'user_id' => $commenter->getKey(),
42 ]);
43
44 $notification = new CommentNew($comment, $commenter);
45
46 if ($shouldBeSent) {
47 $this->assertSame([$user->getKey()], $notification->getReceiverIds());
48 } else {
49 $this->assertEmpty($notification->getReceiverIds());
50 }
51 }
52
53 public function testReplyingToDeletedComment()
54 {
55 $user = User::factory()->create();
56 $commentable = Build::factory()->create();
57 $parentComment = $commentable->comments()->create([
58 'message' => 'Test',
59 'user_id' => $user->getKey(),
60 'deleted_at' => now(),
61 ]);
62
63 $comment = new Comment([
64 'parent_id' => $parentComment->getKey(),
65 'message' => 'Hello',
66 ]);
67
68 $this->assertFalse($comment->isValid());
69 $this->assertArrayHasKey('parent_id', $comment->validationErrors()->all());
70 }
71
72 /**
73 * @dataProvider dataProviderForSetCommentableInvalid
74 */
75 public function testSetCommentableInvalid($type, $id)
76 {
77 $comment = new Comment(['commentable_type' => $type, 'commentable_id' => $id]);
78 $comment->setCommentable();
79
80 $this->assertNull($comment->commentable);
81 }
82
83 public function testUnpinOnDelete()
84 {
85 $comment = Comment::factory(['pinned' => true])->create();
86 $comment->softDelete(User::factory()->create());
87
88 $this->assertFalse($comment->fresh()->pinned);
89 }
90
91 public static function commentReplyOptionDataProvider()
92 {
93 return [
94 [null, true],
95 [false, false],
96 [true, true],
97 ];
98 }
99
100 public static function dataProviderForSetCommentableInvalid()
101 {
102 return [
103 [null, null],
104 [null, 10],
105 ['beatmapset', null],
106 ['beatmapset', 0],
107 ];
108 }
109}