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