Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Unit/Rules/UsernameTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Unit\Rules;
4
5
use Pterodactyl\Rules\Username;
6
use Pterodactyl\Tests\TestCase;
7
8
class UsernameTest extends TestCase
9
{
10
/**
11
* Test that this rule can be cast to a string correctly.
12
*/
13
public function testRuleIsStringable()
14
{
15
$this->assertSame('p_username', (string) new Username());
16
}
17
18
/**
19
* Test valid usernames.
20
*/
21
#[\PHPUnit\Framework\Attributes\DataProvider('validUsernameDataProvider')]
22
public function testValidUsernames(string $username)
23
{
24
$this->assertTrue((new Username())->passes('test', $username), 'Assert username is valid.');
25
}
26
27
/**
28
* Test invalid usernames return false.
29
*/
30
#[\PHPUnit\Framework\Attributes\DataProvider('invalidUsernameDataProvider')]
31
public function testInvalidUsernames(string $username)
32
{
33
$this->assertFalse((new Username())->passes('test', $username), 'Assert username is not valid.');
34
}
35
36
/**
37
* Provide valid usernames.
38
*/
39
public static function validUsernameDataProvider(): array
40
{
41
return [
42
['username'],
43
['user_name'],
44
['user.name'],
45
['user-name'],
46
['123username123'],
47
['123-user.name'],
48
['123456'],
49
];
50
}
51
52
/**
53
* Provide invalid usernames.
54
*/
55
public static function invalidUsernameDataProvider(): array
56
{
57
return [
58
['_username'],
59
['username_'],
60
['_username_'],
61
['-username'],
62
['.username'],
63
['username-'],
64
['username.'],
65
['user*name'],
66
['user^name'],
67
['user#name'],
68
['user+name'],
69
['1234_'],
70
];
71
}
72
}
73
74