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
6declare(strict_types=1);
7
8namespace Tests\Libraries;
9
10use App\Libraries\Chat;
11use App\Libraries\UserChannelList;
12use App\Models\Chat\Channel;
13use App\Models\User;
14use App\Models\UserRelation;
15use Illuminate\Testing\Fluent\AssertableJson;
16use Tests\TestCase;
17
18class UserChannelListTest extends TestCase
19{
20 private User $target;
21 private User $user;
22
23 public function testChannelList()
24 {
25 $channel = Channel::factory()->type('public')->create();
26 $channel->addUser($this->user);
27
28 AssertableJson::fromArray((new UserChannelList($this->user))->get())
29 ->where('0.channel_id', $channel->getKey());
30 }
31
32 public function testChannelHidden()
33 {
34 $message = Chat::sendPrivateMessage($this->user, $this->target, 'message', false);
35 $channel = $message->channel;
36
37 $channel->removeUser($this->user);
38 $this->resetCache();
39
40 // TODO: future update will make this not empty.
41 $this->assertEmpty((new UserChannelList($this->user))->get());
42
43 $channel->addUser($this->user);
44 $this->resetCache();
45
46 AssertableJson::fromArray((new UserChannelList($this->user))->get())
47 ->whereContains('0.users', $this->target->getKey())
48 ->where('0.channel_id', $channel->getKey());
49 }
50
51 public function testUserBlocked()
52 {
53 $message = Chat::sendPrivateMessage($this->user, $this->target, 'message', false);
54
55 // block $this->target
56 $block = UserRelation::factory()->block()->create([
57 'user_id' => $this->user,
58 'zebra_id' => $this->target,
59 ]);
60 $this->resetCache();
61
62 $this->assertEmpty((new UserChannelList($this->user))->get());
63
64 // unblock $this->target
65 $block->delete();
66 $this->resetCache();
67
68 // ensure conversation with $this->target is visible again
69 AssertableJson::fromArray((new UserChannelList($this->user))->get())
70 ->whereContains('0.users', $this->target->getKey())
71 ->where('0.channel_id', $message->channel->getKey());
72 }
73
74 public function testUserRestricted()
75 {
76 $message = Chat::sendPrivateMessage($this->user, $this->target, 'message', false);
77
78 // restrict $this->target
79 $this->target->update(['user_warnings' => 1]);
80 $this->resetCache();
81
82 $this->assertEmpty((new UserChannelList($this->user))->get());
83
84 // unrestrict $this->target
85 $this->target->update(['user_warnings' => 0]);
86 $this->resetCache();
87
88 // ensure conversation with $this->target is visible again
89 AssertableJson::fromArray((new UserChannelList($this->user))->get())
90 ->whereContains('0.users', $this->target->getKey())
91 ->where('0.channel_id', $message->channel->getKey());
92 }
93
94 protected function setUp(): void
95 {
96 parent::setUp();
97
98 $this->user = User::factory()->create()->markSessionVerified();
99 $this->target = User::factory()->create();
100 }
101
102 private function resetCache(): void
103 {
104 $this->user->refresh();
105 $this->target->refresh();
106 app('OsuAuthorize')->resetCache();
107 }
108}