Revert "merged master"
[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 == '' ) { $msg = "Testing $addr"; }
7 $this->assertEquals(
8 $expected,
9 Sanitizer::validateEmail( $addr ),
10 $msg
11 );
12 }
13 private function valid( $addr, $msg = '' ) {
14 $this->checkEmail( $addr, true, $msg );
15 }
16 private function invalid( $addr, $msg = '' ) {
17 $this->checkEmail( $addr, false, $msg );
18 }
19
20 function testEmailWellKnownUserAtHostDotTldAreValid() {
21 $this->valid( 'user@example.com' );
22 $this->valid( 'user@example.museum' );
23 }
24 function testEmailWithUpperCaseCharactersAreValid() {
25 $this->valid( 'USER@example.com' );
26 $this->valid( 'user@EXAMPLE.COM' );
27 $this->valid( 'user@Example.com' );
28 $this->valid( 'USER@eXAMPLE.com' );
29 }
30 function testEmailWithAPlusInUserName() {
31 $this->valid( 'user+sub@example.com' );
32 $this->valid( 'user+@example.com' );
33 }
34 function testEmailDoesNotNeedATopLevelDomain() {
35 $this->valid( "user@localhost" );
36 $this->valid( "FooBar@localdomain" );
37 $this->valid( "nobody@mycompany" );
38 }
39 function testEmailWithWhiteSpacesBeforeOrAfterAreInvalids() {
40 $this->invalid( " user@host.com" );
41 $this->invalid( "user@host.com " );
42 $this->invalid( "\tuser@host.com" );
43 $this->invalid( "user@host.com\t" );
44 }
45 function testEmailWithWhiteSpacesAreInvalids() {
46 $this->invalid( "User user@host" );
47 $this->invalid( "first last@mycompany" );
48 $this->invalid( "firstlast@my company" );
49 }
50 // bug 26948 : comma were matched by an incorrect regexp range
51 function testEmailWithCommasAreInvalids() {
52 $this->invalid( "user,foo@example.org" );
53 $this->invalid( "userfoo@ex,ample.org" );
54 }
55 function testEmailWithHyphens() {
56 $this->valid( "user-foo@example.org" );
57 $this->valid( "userfoo@ex-ample.org" );
58 }
59 function testEmailDomainCanNotBeginWithDot() {
60 $this->invalid( "user@." );
61 $this->invalid( "user@.localdomain" );
62 $this->invalid( "user@localdomain." );
63 $this->valid( "user.@localdomain" );
64 $this->valid( ".@localdomain" );
65 $this->invalid( ".@a............" );
66 }
67 function testEmailWithFunnyCharacters() {
68 $this->valid( "\$user!ex{this}@123.com" );
69 }
70 function testEmailTopLevelDomainCanBeNumerical() {
71 $this->valid( "user@example.1234" );
72 }
73 function testEmailWithoutAtSignIsInvalid() {
74 $this->invalid( 'useràexample.com' );
75 }
76 function testEmailWithOneCharacterDomainIsValid() {
77 $this->valid( 'user@a' );
78 }
79 }