diff --git a/extensions/nicknames/src/AddNicknameValidation.php b/extensions/nicknames/src/AddNicknameValidation.php index 2f9b27418f..5ce43e840d 100644 --- a/extensions/nicknames/src/AddNicknameValidation.php +++ b/extensions/nicknames/src/AddNicknameValidation.php @@ -40,7 +40,7 @@ public function __invoke($flarumValidator, Validator $validator) function ($attribute, $value, $fail) { $regex = $this->settings->get('flarum-nicknames.regex'); if ($regex && ! preg_match_all("/$regex/", $value)) { - $this->translator->trans('flarum-nicknames.api.invalid_nickname_message'); + $fail($this->translator->trans('flarum-nicknames.api.invalid_nickname_message')); } }, 'min:'.$this->settings->get('flarum-nicknames.min'), diff --git a/extensions/nicknames/tests/integration/api/RegisterTest.php b/extensions/nicknames/tests/integration/api/RegisterTest.php index e1ae8e0407..af050ab3fc 100644 --- a/extensions/nicknames/tests/integration/api/RegisterTest.php +++ b/extensions/nicknames/tests/integration/api/RegisterTest.php @@ -74,4 +74,48 @@ public function cant_register_with_nickname_if_not_allowed() $this->assertEquals(403, $response->getStatusCode()); } + + /** + * @test + */ + public function cant_register_with_nickname_if_invalid_regex() + { + $this->setting('flarum-nicknames.set_on_registration', true); + $this->setting('flarum-nicknames.regex', '^[A-z]+$'); + + $response = $this->send( + $this->request('POST', '/register', [ + 'json' => [ + 'nickname' => '007', + 'username' => 'test', + 'password' => 'too-obscure', + 'email' => 'test@machine.local', + ] + ]) + ); + + $this->assertEquals(422, $response->getStatusCode()); + } + + /** + * @test + */ + public function can_register_with_nickname_if_valid_regex() + { + $this->setting('flarum-nicknames.set_on_registration', true); + $this->setting('flarum-nicknames.regex', '^[A-z]+$'); + + $response = $this->send( + $this->request('POST', '/register', [ + 'json' => [ + 'nickname' => 'Acme', + 'username' => 'test', + 'password' => 'too-obscure', + 'email' => 'test@machine.local', + ] + ]) + ); + + $this->assertEquals(201, $response->getStatusCode()); + } }