SECURITY: blacklist CSS var()
[lhc/web/wiklou.git] / tests / phpunit / includes / SanitizerValidateEmailTest.php
1 <?php
2
3 /**
4 * @covers Sanitizer::validateEmail
5 * @todo all test methods in this class should be refactored and...
6 * use a single test method and a single data provider...
7 */
8 class SanitizerValidateEmailTest extends PHPUnit\Framework\TestCase {
9
10 use MediaWikiCoversValidator;
11
12 private function checkEmail( $addr, $expected = true, $msg = '' ) {
13 if ( $msg == '' ) {
14 $msg = "Testing $addr";
15 }
16
17 $this->assertEquals(
18 $expected,
19 Sanitizer::validateEmail( $addr ),
20 $msg
21 );
22 }
23
24 private function valid( $addr, $msg = '' ) {
25 $this->checkEmail( $addr, true, $msg );
26 }
27
28 private function invalid( $addr, $msg = '' ) {
29 $this->checkEmail( $addr, false, $msg );
30 }
31
32 public function testEmailWellKnownUserAtHostDotTldAreValid() {
33 $this->valid( 'user@example.com' );
34 $this->valid( 'user@example.museum' );
35 }
36
37 public function testEmailWithUpperCaseCharactersAreValid() {
38 $this->valid( 'USER@example.com' );
39 $this->valid( 'user@EXAMPLE.COM' );
40 $this->valid( 'user@Example.com' );
41 $this->valid( 'USER@eXAMPLE.com' );
42 }
43
44 public function testEmailWithAPlusInUserName() {
45 $this->valid( 'user+sub@example.com' );
46 $this->valid( 'user+@example.com' );
47 }
48
49 public function testEmailDoesNotNeedATopLevelDomain() {
50 $this->valid( "user@localhost" );
51 $this->valid( "FooBar@localdomain" );
52 $this->valid( "nobody@mycompany" );
53 }
54
55 public function testEmailWithWhiteSpacesBeforeOrAfterAreInvalids() {
56 $this->invalid( " user@host.com" );
57 $this->invalid( "user@host.com " );
58 $this->invalid( "\tuser@host.com" );
59 $this->invalid( "user@host.com\t" );
60 }
61
62 public function testEmailWithWhiteSpacesAreInvalids() {
63 $this->invalid( "User user@host" );
64 $this->invalid( "first last@mycompany" );
65 $this->invalid( "firstlast@my company" );
66 }
67
68 /**
69 * T28948 : comma were matched by an incorrect regexp range
70 */
71 public function testEmailWithCommasAreInvalids() {
72 $this->invalid( "user,foo@example.org" );
73 $this->invalid( "userfoo@ex,ample.org" );
74 }
75
76 public function testEmailWithHyphens() {
77 $this->valid( "user-foo@example.org" );
78 $this->valid( "userfoo@ex-ample.org" );
79 }
80
81 public function testEmailDomainCanNotBeginWithDot() {
82 $this->invalid( "user@." );
83 $this->invalid( "user@.localdomain" );
84 $this->invalid( "user@localdomain." );
85 $this->valid( "user.@localdomain" );
86 $this->valid( ".@localdomain" );
87 $this->invalid( ".@a............" );
88 }
89
90 public function testEmailWithFunnyCharacters() {
91 $this->valid( "\$user!ex{this}@123.com" );
92 }
93
94 public function testEmailTopLevelDomainCanBeNumerical() {
95 $this->valid( "user@example.1234" );
96 }
97
98 public function testEmailWithoutAtSignIsInvalid() {
99 $this->invalid( 'useràexample.com' );
100 }
101
102 public function testEmailWithOneCharacterDomainIsValid() {
103 $this->valid( 'user@a' );
104 }
105 }