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