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\Beatmapset;
9use App\Models\User;
10use App\Transformers\BeatmapsetCompactTransformer;
11use Tests\TestCase;
12
13class BeatmapsetCompactTransformerTest extends TestCase
14{
15 protected $beatmapset;
16 protected $viewer;
17
18 /**
19 * @dataProvider regularOAuthScopesDataProvider
20 */
21 public function testHasFavouritedWithOAuthNormalScopes($scope)
22 {
23 $this->actAsScopedUser($this->viewer, [$scope]);
24
25 $json = json_item($this->beatmapset, 'BeatmapsetCompact', ['has_favourited']);
26 $this->assertArrayNotHasKey('has_favourited', $json);
27 }
28
29 public function testHasFavouritedWithOAuthAllScope()
30 {
31 $this->actAsScopedUser($this->viewer);
32
33 $json = json_item($this->beatmapset, 'BeatmapsetCompact', ['has_favourited']);
34 $this->assertArrayHasKey('has_favourited', $json);
35 }
36
37 public function testHasFavouritedWithoutOAuth()
38 {
39 $this->actAsUser($this->viewer);
40
41 $json = json_item($this->beatmapset, 'BeatmapsetCompact', ['has_favourited']);
42 $this->assertArrayHasKey('has_favourited', $json);
43 }
44
45 /**
46 * @dataProvider propertyPermissionsDataProvider
47 */
48 public function testPropertyIsNotVisibleWithOAuth(string $property)
49 {
50 $this->actAsScopedUser($this->viewer);
51
52 $json = json_item($this->beatmapset, 'BeatmapsetCompact', [$property]);
53 $this->assertArrayNotHasKey($property, $json);
54 }
55
56 /**
57 * @dataProvider propertyPermissionsDataProvider
58 */
59 public function testPropertyIsVisibleWithoutOAuth(string $property)
60 {
61 $this->actAsUser($this->viewer);
62
63 $json = json_item($this->beatmapset, 'BeatmapsetCompact', [$property]);
64 $this->assertArrayHasKey($property, $json);
65 }
66
67 public static function propertyPermissionsDataProvider()
68 {
69 $data = [];
70 $transformer = new BeatmapsetCompactTransformer();
71 foreach ($transformer->getPermissions() as $property => $permission) {
72 if ($permission === 'IsNotOAuth') {
73 $data[] = [$property];
74 }
75 }
76
77 return $data;
78 }
79
80 protected function setUp(): void
81 {
82 parent::setUp();
83
84 $this->viewer = User::factory()->create();
85 $this->beatmapset = Beatmapset::factory()->create();
86 }
87}