the browser-facing portion of osu!
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\Transformers;
7
8use App\Models\User;
9use App\Transformers\UserTransformer;
10use Tests\TestCase;
11
12class UserCompactTransformerTest extends TestCase
13{
14 /**
15 * @dataProvider regularOAuthScopesDataProvider
16 */
17 public function testFriendsIsNotVisibleWithOAuth($scopes)
18 {
19 $viewer = User::factory()->create();
20
21 $this->actAsScopedUser($viewer, [$scopes]);
22
23 $json = json_item($viewer, 'UserCompact', ['friends']);
24 $this->assertArrayNotHasKey('friends', $json);
25 }
26
27 /**
28 * @dataProvider groupsDataProvider
29 */
30 public function testGroupPermissionsUserSilenceShowExtendedInfo(?string $groupIdentifier)
31 {
32 $viewer = User::factory()->withGroup($groupIdentifier)->create();
33 $user = User::factory()->restricted()->silenced()->tournamentBanned()->withNote()->create();
34
35 $this->assertSame(4, $user->accountHistories()->count());
36
37 $this->actAsScopedUser($viewer);
38
39 $json = json_item($user, 'UserCompact', ['account_history.actor', 'account_history.supporting_url']);
40
41 $accountHistories = array_get($json, 'account_history');
42 $publicInfringements = array_filter($accountHistories, function ($item) {
43 return $item['type'] === 'silence' || $item['type'] === 'tournament_ban';
44 });
45 $this->assertCount(2, $accountHistories);
46 $this->assertSame($accountHistories, $publicInfringements);
47 }
48
49 /**
50 * @dataProvider groupsDataProvider
51 */
52 public function testGroupPermissionsWithOAuth(?string $groupIdentifier)
53 {
54 $viewer = User::factory()->withGroup($groupIdentifier)->create();
55 $user = User::factory()->silenced()->create();
56 $this->actAsScopedUser($viewer);
57
58 $json = json_item($user, 'UserCompact', ['account_history.actor', 'account_history.supporting_url']);
59
60 $accountHistory = array_get($json, 'account_history.0');
61 $this->assertArrayNotHasKey('actor', $accountHistory);
62 $this->assertArrayNotHasKey('supporting_url', $accountHistory);
63 }
64
65 /**
66 * @dataProvider groupsDataProvider
67 */
68 public function testGroupPermissionsWithoutOAuth(?string $groupIdentifier, bool $visible)
69 {
70 $viewer = User::factory()->withGroup($groupIdentifier)->create();
71 $user = User::factory()->silenced()->create();
72 $this->actAsUser($viewer);
73
74 $json = json_item($user, 'UserCompact', ['account_history.actor', 'account_history.supporting_url']);
75
76 $accountHistory = array_get($json, 'account_history.0');
77 if ($visible) {
78 $this->assertArrayHasKey('actor', $accountHistory);
79 $this->assertArrayHasKey('supporting_url', $accountHistory);
80 } else {
81 $this->assertArrayNotHasKey('actor', $accountHistory);
82 $this->assertArrayNotHasKey('supporting_url', $accountHistory);
83 }
84 }
85
86 /**
87 * @dataProvider propertyPermissionsDataProvider
88 */
89 public function testPropertyIsNotVisibleWithOAuth(string $property)
90 {
91 $viewer = User::factory()->create();
92
93 $this->actAsScopedUser($viewer);
94
95 $json = json_item($viewer, 'User', [$property]);
96 $this->assertArrayNotHasKey($property, $json);
97 }
98
99 /**
100 * @dataProvider propertyPermissionsDataProvider
101 */
102 public function testPropertyIsVisibleWithoutOAuth(string $property)
103 {
104 $viewer = User::factory()->create();
105
106 $this->actAsUser($viewer);
107
108 $json = json_item($viewer, 'User', [$property]);
109 $this->assertArrayHasKey($property, $json);
110 }
111
112 public static function groupsDataProvider()
113 {
114 return [
115 ['admin', true],
116 ['bng', false],
117 ['gmt', false],
118 ['nat', false],
119 [null, false],
120 ];
121 }
122
123 public static function propertyPermissionsDataProvider()
124 {
125 $data = [];
126 $transformer = new UserTransformer();
127 foreach ($transformer->getPermissions() as $property => $permission) {
128 if ($permission === 'IsNotOAuth') {
129 $data[] = [$property];
130 }
131 }
132
133 return $data;
134 }
135}