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