new player name rule: allow UTF-8

This commit is contained in:
Pig Fang 2021-07-25 13:14:08 +08:00
parent 1b0e9ae8d5
commit baf4921479
No known key found for this signature in database
GPG Key ID: A8198F548DADA9E2
5 changed files with 17 additions and 3 deletions

View File

@ -164,6 +164,7 @@ class OptionsController extends Controller
$form->select('player_name_rule')
->option('official', trans('options.general.player_name_rule.official'))
->option('cjk', trans('options.general.player_name_rule.cjk'))
->option('utf8', trans('options.general.player_name_rule.utf8'))
->option('custom', trans('options.general.player_name_rule.custom'));
$form->text('custom_player_name_regexp')->hint()->placeholder();

View File

@ -21,6 +21,9 @@ class PlayerName implements Rule
$regexp = '/^[A-Za-z0-9_§\x{4e00}-\x{9fff}]+$/u';
break;
case 'utf8':
return mb_check_encoding($value, 'UTF-8');
case 'custom':
$regexp = option('custom_player_name_regexp') ?: $regexp;
break;

View File

@ -115,6 +115,7 @@ general:
title: Player Name Rule
official: Letters, numbers and underscores (Mojang's official rule)
cjk: Allow CJK Unified Ideographs
utf8: Allow all valid UTF-8 characters
custom: Use custom rules (regular expression)
custom_player_name_regexp:
title: Custom Player Name Rules

View File

@ -49,6 +49,7 @@ player:
player-name-rule:
official: Player name may only contains letters, numbers and underscores.
cjk: Player name may contains letters, numbers, underscores and CJK Unified Ideographs.
utf8: Player name must be a UTF-8 string.
custom: Custom player name rules are applied on this site. Please contact admins for further information.
player-name-length: The player name should be at least :min characters and not greater than :max characters.

View File

@ -53,9 +53,6 @@ class PlayerControllerTest extends TestCase
public function testAdd()
{
Event::fake();
$filter = Fakes\Filter::fake();
// Without player name
$this->postJson(route('user.player.add'))->assertJsonValidationErrors('name');
@ -74,12 +71,23 @@ class PlayerControllerTest extends TestCase
['name' => 'yjsnpi']
)->assertJsonValidationErrors('name');
// allow UTF-8
option(['player_name_rule' => 'utf8']);
$this->postJson(route('user.player.add'), ['name' => '響け!ユーフォニアム'])
->assertJson(['code' => 0]);
$this->postJson(route('user.player.add'), ['name' => 'मूलपाठ'])
->assertJson(['code' => 0]);
// with an existed player name
option(['player_name_rule' => 'official']);
$existed = Player::factory()->create();
$this->postJson(route('user.player.add'), ['name' => $existed->name])
->assertJsonValidationErrors('name');
Event::fake();
$filter = Fakes\Filter::fake();
// Lack of score
$user = User::factory()->create(['score' => 0]);
$this->actingAs($user)->postJson(