http://www.mediawiki.org/wiki/Special:Code/MediaWiki/75682
Revision: 75682 Author: hashar Date: 2010-10-29 22:03:17 +0000 (Fri, 29 Oct 2010) Log Message: ----------- Follow up r75627. Implements r75670 in PHP to validate emails. * Server side validation of email according to an HTML5 specifications provided by Simetrical : http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state * This is NOT a fix of bug 959 (which wants RFC 2822 validation) * Basic unit tests Modified Paths: -------------- trunk/phase3/includes/User.php Added Paths: ----------- trunk/phase3/maintenance/tests/phpunit/includes/UserIsValidEmailAddrTest.php Modified: trunk/phase3/includes/User.php =================================================================== --- trunk/phase3/includes/User.php 2010-10-29 21:59:45 UTC (rev 75681) +++ trunk/phase3/includes/User.php 2010-10-29 22:03:17 UTC (rev 75682) @@ -648,8 +648,19 @@ if( !wfRunHooks( 'isValidEmailAddr', array( $addr, &$result ) ) ) { return $result; } + $rfc5322_atext = "a-z0-9!#$%&'*+-\/=?^_`{|}—~" ; + $rfc1034_ldh_str = "a-z0-9-" ; - return strpos( $addr, '@' ) !== false; + $HTML5_email_regexp = "/ + ^ # start of string + [$rfc5322_atext\\.]+ # user part which is liberal :p + @ # 'apostrophe' + [$rfc1034_ldh_str] # Domain first character + [$rfc1034_ldh_str\\.]+ # Second char and following can include dot + $ # End of string + /ix" ; // case Insensitive, eXtended + + return (bool) preg_match( $HTML5_email_regexp, $addr ); } /** Added: trunk/phase3/maintenance/tests/phpunit/includes/UserIsValidEmailAddrTest.php =================================================================== --- trunk/phase3/maintenance/tests/phpunit/includes/UserIsValidEmailAddrTest.php (rev 0) +++ trunk/phase3/maintenance/tests/phpunit/includes/UserIsValidEmailAddrTest.php 2010-10-29 22:03:17 UTC (rev 75682) @@ -0,0 +1,63 @@ +<?php + +class UserIsValidEmailAddrTest extends PHPUnit_Framework_TestCase { + + private function testEmail( $addr, $expected = true, $msg = '') { + $this->assertEquals( + $expected, + User::isValidEmailAddr( $addr ), + $msg + ); + } + private function valid( $addr, $msg = '' ) { + $this->testEmail( $addr, true, $msg ); + } + private function invalid( $addr, $msg = '' ) { + $this->testEmail( $addr, false, $msg ); + } + + function testEmailWellKnownUserAtHostDotTldAreValid() { + $this->valid( 'u...@example.com' ); + $this->valid( 'u...@example.museum' ); + } + function testEmailWithUpperCaseCharactersAreValid() { + $this->valid( 'u...@example.com' ); + $this->valid( 'u...@example.com' ); + $this->valid( 'u...@example.com' ); + $this->valid( 'u...@example.com' ); + } + function testEmailWithAPlusInUserName() { + $this->valid( 'user+...@localdomain' ); + } + function testEmailWithWhiteSpacesBeforeOrAfterAreInvalids() { + $this->invalid( " u...@host" ); + $this->invalid( "u...@host " ); + $this->invalid( "\tu...@host" ); + $this->invalid( "u...@host\t" ); + } + function testEmailWithWhiteSpacesAreInvalids() { + $this->invalid( "User u...@host" ); + $this->invalid( "first l...@mycompany" ); + $this->invalid( "firstl...@my company" ); + } + function testEmailDomainCanNotBeginWithDot() { + $this->invalid( "u...@." ); + $this->invalid( "u...@.localdomain" ); + $this->valid( "u...@localdomain." ); + $this->valid( "us...@localdomain" ); + $this->valid( "....@localdomain" ); + $this->valid( "....@a............" ); + } + function testEmailWithFunnyCharacters() { + $this->valid( "\$user!ex{th...@123.com" ); + } + function testEmailTopLevelDomainCanBeNumerical() { + $this->valid( "u...@example.1234" ); + } + function testEmailWithoutAtSignIsInvalid() { + $this->invalid( 'userà example.com' ); + } + function testEmailWithOneCharacterDomainIsInvalid() { + $this->invalid( 'u...@a' ); + } +} _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs