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\Commands;
7
8use App\Models\Multiplayer\DailyChallengeQueueItem;
9use App\Models\Multiplayer\Room;
10use App\Models\User;
11use Tests\TestCase;
12
13class DailyChallengeCreateNextTest extends TestCase
14{
15 public function testBasicItem()
16 {
17 $queueItem = DailyChallengeQueueItem::factory()->create();
18
19 $this->artisan('daily-challenge:create-next')->assertOk();
20
21 $room = $queueItem->fresh()->multiplayerRoom()->first();
22
23 $this->assertNotNull($room);
24
25 $this->assertSame(1, $room->playlist()->count());
26 $playlistItem = $room->playlist()->first();
27
28 $this->assertSame($queueItem->beatmap_id, $playlistItem->beatmap_id);
29 $this->assertSame($queueItem->ruleset_id, $playlistItem->ruleset_id);
30 $this->assertEmpty($queueItem->required_mods);
31 $this->assertEmpty($queueItem->allowed_mods);
32 }
33
34 public function testItemWithRequiredMods()
35 {
36 $queueItem = DailyChallengeQueueItem::factory()->create([
37 'required_mods' => [[
38 'acronym' => 'DT',
39 ]],
40 ]);
41
42 $this->artisan('daily-challenge:create-next')->assertOk();
43
44 $room = $queueItem->fresh()->multiplayerRoom()->first();
45
46 $this->assertNotNull($room);
47
48 $this->assertSame(1, $room->playlist()->count());
49 $playlistItem = $room->playlist()->first();
50
51 $this->assertSame($queueItem->beatmap_id, $playlistItem->beatmap_id);
52 $this->assertSame($queueItem->ruleset_id, $playlistItem->ruleset_id);
53 $this->assertSame($queueItem->required_mods[0]['acronym'], $playlistItem->required_mods[0]->acronym);
54 $this->assertEmpty($queueItem->allowed_mods);
55 }
56
57 public function testQueueOrderIsRespected()
58 {
59 $secondItem = DailyChallengeQueueItem::factory()->create(['order' => 2]);
60 $firstItem = DailyChallengeQueueItem::factory()->create(['order' => 1]);
61
62 $this->artisan('daily-challenge:create-next')->assertOk();
63
64 $this->assertNotNull($firstItem->fresh()->multiplayerRoom()->first());
65 $this->assertNull($secondItem->fresh()->multiplayerRoom()->first());
66 }
67
68 public function testPrimaryKeyIsTiebreakerWhenNoSpecifiedOrder()
69 {
70 $firstItem = DailyChallengeQueueItem::factory()->create();
71 $secondItem = DailyChallengeQueueItem::factory()->create();
72
73 $this->artisan('daily-challenge:create-next')->assertOk();
74
75 $this->assertNotNull($firstItem->fresh()->multiplayerRoom()->first());
76 $this->assertNull($secondItem->fresh()->multiplayerRoom()->first());
77 }
78
79 public function testNothingDoneOnEmptyQueue()
80 {
81 $this->artisan('daily-challenge:create-next')
82 ->expectsOutputToContain('"Daily challenge" queue is empty')
83 ->assertFailed();
84 $this->expectCountChange(fn () => Room::all()->count(), 0);
85 }
86
87 public function testCommandFailsIfAnotherDailyChallengeRoomOpen()
88 {
89 $secondItem = DailyChallengeQueueItem::factory()->create(['order' => 2]);
90 $firstItem = DailyChallengeQueueItem::factory()->create(['order' => 1]);
91
92 $this->artisan('daily-challenge:create-next')->assertOk();
93
94 $this->assertNotNull($firstItem->fresh()->multiplayerRoom()->first());
95 $this->assertNull($secondItem->fresh()->multiplayerRoom()->first());
96
97 $this->assertNotNull(Room::where('category', 'daily_challenge')->first());
98
99 $this->artisan('daily-challenge:create-next')
100 ->expectsOutputToContain('Another "daily challenge" room is open')
101 ->assertFailed();
102
103 $this->assertNotNull($firstItem->fresh()->multiplayerRoom()->first());
104 $this->assertNull($secondItem->fresh()->multiplayerRoom()->first());
105 }
106
107 protected function setUp(): void
108 {
109 parent::setUp();
110
111 $utilityUser = User::factory()->create();
112 config_set('osu.legacy.bancho_bot_user_id', $utilityUser->getKey());
113 }
114}