In r75682, I have implemented a PHP function to validate email address based on bug 22449. Siebrand pointed a w3.org specification which I implemented. The spec is bugged since it requires a domain and a top level domain! I could either make the first part optional or alter the second part to require 0 to x elements. I choose the later: s/+/*/ Should fix bug 22449 for good. TESTS: Added testEmailDoesNotNeedATopLevelDomain: Made following emails valid: user.@localdaomin .@localdomain user@a Test output (please add more): $ php phpunit.php -c suite.xml --filter alidEmail --tap TAP version 13 ok 1 - UserIsValidEmailAddrTest::testEmailWellKnownUserAtHostDotTldAreValid ok 2 - UserIsValidEmailAddrTest::testEmailWithUpperCaseCharactersAreValid ok 3 - UserIsValidEmailAddrTest::testEmailWithAPlusInUserName ok 4 - UserIsValidEmailAddrTest::testEmailDoesNotNeedATopLevelDomain ok 5 - UserIsValidEmailAddrTest::testEmailWithWhiteSpacesBeforeOrAfterAreInvalids ok 6 - UserIsValidEmailAddrTest::testEmailWithWhiteSpacesAreInvalids ok 7 - UserIsValidEmailAddrTest::testEmailDomainCanNotBeginWithDot ok 8 - UserIsValidEmailAddrTest::testEmailWithFunnyCharacters ok 9 - UserIsValidEmailAddrTest::testEmailTopLevelDomainCanBeNumerical ok 10 - UserIsValidEmailAddrTest::testEmailWithoutAtSignIsInvalid ok 11 - UserIsValidEmailAddrTest::testEmailWithOneCharacterDomainIsValid 1..11
70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
class UserIsValidEmailAddrTest extends MediaWikiTestCase {
|
|
|
|
private function checkEmail( $addr, $expected = true, $msg = '') {
|
|
if( $msg == '' ) { $msg = "Testing $addr"; }
|
|
$this->assertEquals(
|
|
$expected,
|
|
User::isValidEmailAddr( $addr ),
|
|
$msg
|
|
);
|
|
}
|
|
private function valid( $addr, $msg = '' ) {
|
|
$this->checkEmail( $addr, true, $msg );
|
|
}
|
|
private function invalid( $addr, $msg = '' ) {
|
|
$this->checkEmail( $addr, false, $msg );
|
|
}
|
|
|
|
function testEmailWellKnownUserAtHostDotTldAreValid() {
|
|
$this->valid( 'user@example.com' );
|
|
$this->valid( 'user@example.museum' );
|
|
}
|
|
function testEmailWithUpperCaseCharactersAreValid() {
|
|
$this->valid( 'USER@example.com' );
|
|
$this->valid( 'user@EXAMPLE.COM' );
|
|
$this->valid( 'user@Example.com' );
|
|
$this->valid( 'USER@eXAMPLE.com' );
|
|
}
|
|
function testEmailWithAPlusInUserName() {
|
|
$this->valid( 'user+sub@example.com' );
|
|
$this->valid( 'user+@example.com' );
|
|
}
|
|
function testEmailDoesNotNeedATopLevelDomain() {
|
|
$this->valid( "user@localhost" );
|
|
$this->valid( "FooBar@localdomain" );
|
|
$this->valid( "nobody@mycompany" );
|
|
}
|
|
function testEmailWithWhiteSpacesBeforeOrAfterAreInvalids() {
|
|
$this->invalid( " user@host.com" );
|
|
$this->invalid( "user@host.com " );
|
|
$this->invalid( "\tuser@host.com" );
|
|
$this->invalid( "user@host.com\t" );
|
|
}
|
|
function testEmailWithWhiteSpacesAreInvalids() {
|
|
$this->invalid( "User user@host" );
|
|
$this->invalid( "first last@mycompany" );
|
|
$this->invalid( "firstlast@my company" );
|
|
}
|
|
function testEmailDomainCanNotBeginWithDot() {
|
|
$this->invalid( "user@." );
|
|
$this->invalid( "user@.localdomain" );
|
|
$this->invalid( "user@localdomain." );
|
|
$this->valid( "user.@localdomain" );
|
|
$this->valid( ".@localdomain" );
|
|
$this->invalid( ".@a............" );
|
|
}
|
|
function testEmailWithFunnyCharacters() {
|
|
$this->valid( "\$user!ex{this}@123.com" );
|
|
}
|
|
function testEmailTopLevelDomainCanBeNumerical() {
|
|
$this->valid( "user@example.1234" );
|
|
}
|
|
function testEmailWithoutAtSignIsInvalid() {
|
|
$this->invalid( 'useràexample.com' );
|
|
}
|
|
function testEmailWithOneCharacterDomainIsValid() {
|
|
$this->valid( 'user@a' );
|
|
}
|
|
}
|