2018-02-11 22:39:50 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
namespace Pterodactyl\Tests\Unit\Rules;
|
2018-02-11 22:39:50 +00:00
|
|
|
|
|
|
|
use Pterodactyl\Rules\Username;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Tests\TestCase;
|
2018-02-11 22:39:50 +00:00
|
|
|
|
|
|
|
class UsernameTest extends TestCase
|
|
|
|
{
|
2018-02-18 19:29:28 +00:00
|
|
|
/**
|
|
|
|
* Test that this rule can be cast to a string correctly.
|
|
|
|
*/
|
|
|
|
public function testRuleIsStringable()
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
$this->assertSame('p_username', (string) new Username());
|
2018-02-18 19:29:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 22:39:50 +00:00
|
|
|
/**
|
|
|
|
* Test valid usernames.
|
|
|
|
*
|
|
|
|
* @dataProvider validUsernameDataProvider
|
|
|
|
*/
|
|
|
|
public function testValidUsernames(string $username)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
$this->assertTrue((new Username())->passes('test', $username), 'Assert username is valid.');
|
2018-02-11 22:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test invalid usernames return false.
|
|
|
|
*
|
|
|
|
* @dataProvider invalidUsernameDataProvider
|
|
|
|
*/
|
|
|
|
public function testInvalidUsernames(string $username)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
$this->assertFalse((new Username())->passes('test', $username), 'Assert username is not valid.');
|
2018-02-11 22:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide valid usernames.
|
|
|
|
*/
|
2023-02-23 19:30:16 +00:00
|
|
|
public static function validUsernameDataProvider(): array
|
2018-02-11 22:39:50 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['username'],
|
|
|
|
['user_name'],
|
|
|
|
['user.name'],
|
|
|
|
['user-name'],
|
|
|
|
['123username123'],
|
|
|
|
['123-user.name'],
|
|
|
|
['123456'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide invalid usernames.
|
|
|
|
*/
|
2023-02-23 19:30:16 +00:00
|
|
|
public static function invalidUsernameDataProvider(): array
|
2018-02-11 22:39:50 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['_username'],
|
|
|
|
['username_'],
|
|
|
|
['_username_'],
|
|
|
|
['-username'],
|
|
|
|
['.username'],
|
|
|
|
['username-'],
|
|
|
|
['username.'],
|
|
|
|
['user*name'],
|
|
|
|
['user^name'],
|
|
|
|
['user#name'],
|
|
|
|
['user+name'],
|
|
|
|
['1234_'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|