mirror of
https://github.com/flarum/core.git
synced 2025-08-04 07:27:39 +02:00
fix: nickname regex validation not working (#3430)
* test: regex validation for nickname on registration * fix: nickname regex validation not working * test: regex validation works with valid inputs
This commit is contained in:
@@ -40,7 +40,7 @@ class AddNicknameValidation
|
||||
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'),
|
||||
|
@@ -74,4 +74,48 @@ class RegisterTest extends TestCase
|
||||
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user