203dfaee3e84d20408504de7c036c5b98abd2f1d
[lhc/web/wiklou.git] / tests / phpunit / includes / UserIsValidEmailAddrTest.php
1 <?php
2
3 class UserIsValidEmailAddrTest extends MediaWikiTestCase {
4
5 private function checkEmail( $addr, $expected = true, $msg = '') {
6 if( $msg == '' ) { $msg = "Testing $addr"; }
7 $this->assertEquals(
8 $expected,
9 User::isValidEmailAddr( $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 function testEmailDomainCanNotBeginWithDot() {
51 $this->invalid( "user@." );
52 $this->invalid( "user@.localdomain" );
53 $this->invalid( "user@localdomain." );
54 $this->valid( "user.@localdomain" );
55 $this->valid( ".@localdomain" );
56 $this->invalid( ".@a............" );
57 }
58 function testEmailWithFunnyCharacters() {
59 $this->valid( "\$user!ex{this}@123.com" );
60 }
61 function testEmailTopLevelDomainCanBeNumerical() {
62 $this->valid( "user@example.1234" );
63 }
64 function testEmailWithoutAtSignIsInvalid() {
65 $this->invalid( 'useràexample.com' );
66 }
67 function testEmailWithOneCharacterDomainIsValid() {
68 $this->valid( 'user@a' );
69 }
70 }