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\User;
9
10use App\Exceptions\InvariantException;
11use App\Libraries\User\CountryChange;
12use App\Models\Beatmap;
13use App\Models\Country;
14use App\Models\Score\Best\Model as ScoreBestModel;
15use App\Models\User;
16use Tests\TestCase;
17
18class CountryChangeTest extends TestCase
19{
20 /**
21 * @group RequiresScoreIndexer
22 */
23 public function testDo(): void
24 {
25 $user = User::factory();
26 foreach (Beatmap::MODES as $ruleset => $_rulesetId) {
27 $user = $user->withPlays(rand(1, 20), $ruleset);
28 }
29 $user = $user->create();
30 foreach (Beatmap::MODES as $ruleset => $_rulesetId) {
31 ScoreBestModel
32 ::getClass($ruleset)
33 ::factory(['user_id' => $user, 'country_acronym' => $user->country_acronym])
34 ->count(rand(1, 5))
35 ->create();
36 }
37 $targetCountry = Country::factory()->create()->getKey();
38
39 $this->expectCountChange(fn () => $user->accountHistories()->count(), 1);
40 CountryChange::handle($user, $targetCountry, 'test');
41
42 $user->refresh();
43 $this->assertSame($user->country_acronym, $targetCountry);
44 foreach (Beatmap::MODES as $ruleset => $_rulesetId) {
45 $this->assertSame($user->statistics($ruleset)->country_acronym, $targetCountry);
46
47 foreach (Beatmap::VARIANTS[$ruleset] ?? [] as $variant) {
48 $this->assertSame(
49 $user->statistics($ruleset, false, $variant)->country_acronym,
50 $targetCountry,
51 );
52 }
53
54 foreach ($user->scoresBest($ruleset) as $score) {
55 $this->assertSame($score->country_acronym, $targetCountry);
56 }
57 }
58
59 // TODO: add test for solo score country change (in es index)
60 }
61
62 public function testDoInvalidCountry(): void
63 {
64 $user = User::factory()->create();
65 $oldCountry = $user->country_acronym;
66
67 $this->expectException(InvariantException::class);
68 CountryChange::handle($user, '__', 'test');
69 }
70}