MediaWiki
REL1_21
|
00001 <?php 00002 00003 class SanitizerValidateEmailTest extends MediaWikiTestCase { 00004 00005 private function checkEmail( $addr, $expected = true, $msg = '' ) { 00006 if ( $msg == '' ) { 00007 $msg = "Testing $addr"; 00008 } 00009 00010 $this->assertEquals( 00011 $expected, 00012 Sanitizer::validateEmail( $addr ), 00013 $msg 00014 ); 00015 } 00016 00017 private function valid( $addr, $msg = '' ) { 00018 $this->checkEmail( $addr, true, $msg ); 00019 } 00020 00021 private function invalid( $addr, $msg = '' ) { 00022 $this->checkEmail( $addr, false, $msg ); 00023 } 00024 00025 function testEmailWellKnownUserAtHostDotTldAreValid() { 00026 $this->valid( '[email protected]' ); 00027 $this->valid( '[email protected]' ); 00028 } 00029 00030 function testEmailWithUpperCaseCharactersAreValid() { 00031 $this->valid( '[email protected]' ); 00032 $this->valid( '[email protected]' ); 00033 $this->valid( '[email protected]' ); 00034 $this->valid( '[email protected]' ); 00035 } 00036 00037 function testEmailWithAPlusInUserName() { 00038 $this->valid( '[email protected]' ); 00039 $this->valid( '[email protected]' ); 00040 } 00041 00042 function testEmailDoesNotNeedATopLevelDomain() { 00043 $this->valid( "user@localhost" ); 00044 $this->valid( "FooBar@localdomain" ); 00045 $this->valid( "nobody@mycompany" ); 00046 } 00047 00048 function testEmailWithWhiteSpacesBeforeOrAfterAreInvalids() { 00049 $this->invalid( " [email protected]" ); 00050 $this->invalid( "[email protected] " ); 00051 $this->invalid( "\[email protected]" ); 00052 $this->invalid( "[email protected]\t" ); 00053 } 00054 00055 function testEmailWithWhiteSpacesAreInvalids() { 00056 $this->invalid( "User user@host" ); 00057 $this->invalid( "first last@mycompany" ); 00058 $this->invalid( "firstlast@my company" ); 00059 } 00060 00061 // bug 26948 : comma were matched by an incorrect regexp range 00062 function testEmailWithCommasAreInvalids() { 00063 $this->invalid( "user,[email protected]" ); 00064 $this->invalid( "userfoo@ex,ample.org" ); 00065 } 00066 00067 function testEmailWithHyphens() { 00068 $this->valid( "[email protected]" ); 00069 $this->valid( "[email protected]" ); 00070 } 00071 00072 function testEmailDomainCanNotBeginWithDot() { 00073 $this->invalid( "user@." ); 00074 $this->invalid( "[email protected]" ); 00075 $this->invalid( "user@localdomain." ); 00076 $this->valid( "user.@localdomain" ); 00077 $this->valid( ".@localdomain" ); 00078 $this->invalid( ".@a............" ); 00079 } 00080 00081 function testEmailWithFunnyCharacters() { 00082 $this->valid( "\$user!ex{this}@123.com" ); 00083 } 00084 00085 function testEmailTopLevelDomainCanBeNumerical() { 00086 $this->valid( "[email protected]" ); 00087 } 00088 00089 function testEmailWithoutAtSignIsInvalid() { 00090 $this->invalid( 'userà example.com' ); 00091 } 00092 00093 function testEmailWithOneCharacterDomainIsValid() { 00094 $this->valid( 'user@a' ); 00095 } 00096 }