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
6declare(strict_types=1);
7
8namespace Tests\Transformers;
9
10use App\Models\Forum\Topic;
11use App\Models\User;
12use Carbon\Carbon;
13use Tests\TestCase;
14
15class PollOptionTransformerTest extends TestCase
16{
17 /**
18 * @dataProvider voteCountPermissionsDataProvider
19 */
20 public function testVoteCountPermissions(
21 bool $isOAuth,
22 bool $pollEnded,
23 bool $isTopicOwner,
24 ?string $groupIdentifier,
25 bool $voteCountVisible,
26 ): void {
27 $actor = User::factory()->withGroup($groupIdentifier)->create();
28 $topicAttributes = [
29 'poll_hide_results' => true,
30 'poll_length' => 86400, // 1 day
31 'topic_time' => Carbon::now(),
32 ];
33
34 if ($pollEnded) {
35 $topicAttributes['topic_time']->subRealDay();
36 }
37
38 if ($isTopicOwner) {
39 $topicAttributes['topic_poster'] = $actor->getKey();
40 }
41
42 $topic = Topic::factory()->poll()->withPost()->create($topicAttributes);
43
44 if ($isOAuth) {
45 $this->actAsScopedUser($actor);
46 } else {
47 $this->actAsUser($actor);
48 }
49
50 $pollOptionJson = json_item($topic->pollOptions()->first(), 'Forum\PollOption');
51
52 if ($voteCountVisible) {
53 $this->assertArrayHasKey('vote_count', $pollOptionJson);
54 } else {
55 $this->assertArrayNotHasKey('vote_count', $pollOptionJson);
56 }
57 }
58
59 /**
60 * Data in order:
61 * - Whether request is OAuth
62 * - Whether poll has ended
63 * - Whether authenticated user is the topic owner
64 * - Authenticated user's group identifier
65 * - Whether vote count should be visible
66 */
67 public static function voteCountPermissionsDataProvider(): array
68 {
69 return [
70 [true, true, true, 'admin', true],
71 [true, true, true, 'gmt', true],
72 [true, true, true, null, true],
73 [true, true, false, 'admin', true],
74 [true, true, false, 'gmt', true],
75 [true, true, false, null, true],
76 [true, false, true, 'admin', false],
77 [true, false, true, 'gmt', false],
78 [true, false, true, null, false],
79 [true, false, false, 'admin', false],
80 [true, false, false, 'gmt', false],
81 [true, false, false, null, false],
82 [false, true, true, 'admin', true],
83 [false, true, true, 'gmt', true],
84 [false, true, true, null, true],
85 [false, true, false, 'admin', true],
86 [false, true, false, 'gmt', true],
87 [false, true, false, null, true],
88 [false, false, true, 'admin', true],
89 [false, false, true, 'gmt', true],
90 [false, false, true, null, true],
91 [false, false, false, 'admin', true],
92 [false, false, false, 'gmt', true],
93 [false, false, false, null, false],
94 ];
95 }
96}