the browser-facing portion of osu!
at master 3.3 kB view raw
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\Libraries\Search; 7 8use App\Libraries\Search\ScoreSearchParams; 9use App\Models\Solo\Score; 10use App\Models\User; 11use Tests\TestCase; 12 13class ScoreSearchParamsTest extends TestCase 14{ 15 public static function showLegacyForUserAndGuestDataProvider() 16 { 17 return [ 18 [null, null, null], 19 [null, false, null], 20 [null, true, null], 21 [false, null, null], 22 [false, false, null], 23 [false, true, null], 24 [true, null, true], 25 [true, false, true], 26 [true, true, true], 27 ]; 28 } 29 30 public static function showLegacyForUserFromScoreDataProvider() 31 { 32 return [ 33 [null, null], 34 [false, null], 35 [true, true], 36 ]; 37 } 38 39 public static function showLegacyForUserSettingDataProvider() 40 { 41 return [ 42 [null, null], 43 [false, null], 44 [true, true], 45 ]; 46 } 47 48 /** 49 * @dataProvider showLegacyForUserAndGuestDataProvider 50 */ 51 public function testShowLegacyForGuest(?bool $legacyOnly, ?bool $isApiRequest, ?bool $expected) 52 { 53 $this->assertSame( 54 $expected, 55 ScoreSearchParams::showLegacyForUser(null, $legacyOnly, $isApiRequest) 56 ); 57 } 58 59 /** 60 * @dataProvider showLegacyForUserAndGuestDataProvider 61 */ 62 public function testShowLegacyForUser(?bool $legacyOnly, ?bool $isApiRequest, ?bool $expected) 63 { 64 $user = User::factory()->create(); 65 66 $this->assertSame( 67 $expected, 68 ScoreSearchParams::showLegacyForUser($user, $legacyOnly, $isApiRequest) 69 ); 70 } 71 72 /** 73 * @dataProvider showLegacyForUserFromScoreDataProvider 74 */ 75 public function testShowLegacyForUserFromScore(?bool $legacyScore, ?bool $expected) 76 { 77 $factory = User::factory(); 78 79 if ($legacyScore !== null) { 80 $factory = $factory->has(Score::factory()->state([ 81 'legacy_score_id' => $legacyScore ? 1 : null, 82 ]), 'soloScores'); 83 } 84 85 $user = $factory->create(); 86 87 $this->assertNull($user->fresh()->userProfileCustomization); 88 89 $this->assertSame( 90 $expected, 91 ScoreSearchParams::showLegacyForUser($user, null, null) 92 ); 93 94 // also test the setting was saved if there is a score and not just returning the default value. 95 $this->assertSame($legacyScore, $user->fresh()->userProfileCustomization->options['legacy_score_only'] ?? null); 96 } 97 98 /** 99 * @dataProvider showLegacyForUserSettingDataProvider 100 */ 101 public function testShowLegacyForUserSetting(?bool $setting, ?bool $expected) 102 { 103 $user = User::factory()->create(); 104 105 if ($setting !== null) { 106 $user->userProfileCustomization()->create([ 107 'legacy_score_only' => $setting, 108 ]); 109 } 110 111 $this->assertSame( 112 $expected, 113 ScoreSearchParams::showLegacyForUser($user, null, null) 114 ); 115 } 116}