From 3f7ba49c2c676604d2de4cf7c94e0de8f5f5bba9 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 22 Aug 2021 16:45:26 +0800 Subject: [PATCH] fix player name rule --- app/Rules/PlayerName.php | 2 +- resources/lang/en/options.yml | 2 +- tests/RulesTest/PlayerNameTest.php | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/Rules/PlayerName.php b/app/Rules/PlayerName.php index 613e0685..10195c98 100644 --- a/app/Rules/PlayerName.php +++ b/app/Rules/PlayerName.php @@ -22,7 +22,7 @@ class PlayerName implements Rule break; case 'utf8': - return mb_check_encoding($value, 'UTF-8'); + return mb_check_encoding($value, 'UTF-8') && !preg_match('/\s/', $value); case 'custom': $regexp = option('custom_player_name_regexp') ?: $regexp; diff --git a/resources/lang/en/options.yml b/resources/lang/en/options.yml index 73041336..76da38c6 100644 --- a/resources/lang/en/options.yml +++ b/resources/lang/en/options.yml @@ -115,7 +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 + utf8: Allow all valid UTF-8 characters (excluding whitespaces) custom: Use custom rules (regular expression) custom_player_name_regexp: title: Custom Player Name Rules diff --git a/tests/RulesTest/PlayerNameTest.php b/tests/RulesTest/PlayerNameTest.php index 5ec04639..c935c585 100644 --- a/tests/RulesTest/PlayerNameTest.php +++ b/tests/RulesTest/PlayerNameTest.php @@ -43,6 +43,24 @@ class PlayerNameTest extends TestCase ); } + public function testUtf8() + { + option(['player_name_rule' => 'utf8']); + $rule = new PlayerName(); + + $this->assertTrue($rule->passes('', '_name_')); + $this->assertTrue($rule->passes('', 'NaN')); + $this->assertTrue($rule->passes('', '中文')); + $this->assertTrue($rule->passes('', '§Me')); + $this->assertTrue($rule->passes('', ';')); + $this->assertTrue($rule->passes('', '\\')); + + $this->assertFalse($rule->passes('', 'a b')); + $this->assertFalse($rule->passes('', "a\n b")); + $this->assertFalse($rule->passes('', "a\tb")); + $this->assertFalse($rule->passes('', "a\fb")); + } + public function testCustom() { option(['player_name_rule' => 'custom']);