2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::validateEmail
|
2014-07-23 20:04:48 +00:00
|
|
|
* @todo all test methods in this class should be refactored and...
|
2013-10-21 21:09:13 +00:00
|
|
|
* use a single test method and a single data provider...
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class SanitizerValidateEmailTest extends PHPUnit\Framework\TestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2017-12-29 23:22:37 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2013-02-14 11:36:35 +00:00
|
|
|
private function checkEmail( $addr, $expected = true, $msg = '' ) {
|
|
|
|
|
if ( $msg == '' ) {
|
|
|
|
|
$msg = "Testing $addr";
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$expected,
|
2011-07-24 19:59:42 +00:00
|
|
|
Sanitizer::validateEmail( $addr ),
|
2010-12-14 16:26:35 +00:00
|
|
|
$msg
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
private function valid( $addr, $msg = '' ) {
|
|
|
|
|
$this->checkEmail( $addr, true, $msg );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
private function invalid( $addr, $msg = '' ) {
|
|
|
|
|
$this->checkEmail( $addr, false, $msg );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWellKnownUserAtHostDotTldAreValid() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->valid( 'user@example.com' );
|
|
|
|
|
$this->valid( 'user@example.museum' );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithUpperCaseCharactersAreValid() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->valid( 'USER@example.com' );
|
|
|
|
|
$this->valid( 'user@EXAMPLE.COM' );
|
|
|
|
|
$this->valid( 'user@Example.com' );
|
|
|
|
|
$this->valid( 'USER@eXAMPLE.com' );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithAPlusInUserName() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->valid( 'user+sub@example.com' );
|
|
|
|
|
$this->valid( 'user+@example.com' );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailDoesNotNeedATopLevelDomain() {
|
2011-01-21 18:01:47 +00:00
|
|
|
$this->valid( "user@localhost" );
|
|
|
|
|
$this->valid( "FooBar@localdomain" );
|
|
|
|
|
$this->valid( "nobody@mycompany" );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithWhiteSpacesBeforeOrAfterAreInvalids() {
|
2011-01-09 19:06:02 +00:00
|
|
|
$this->invalid( " user@host.com" );
|
|
|
|
|
$this->invalid( "user@host.com " );
|
|
|
|
|
$this->invalid( "\tuser@host.com" );
|
|
|
|
|
$this->invalid( "user@host.com\t" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithWhiteSpacesAreInvalids() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->invalid( "User user@host" );
|
|
|
|
|
$this->invalid( "first last@mycompany" );
|
|
|
|
|
$this->invalid( "firstlast@my company" );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
2017-02-20 23:45:58 +00:00
|
|
|
* T28948 : comma were matched by an incorrect regexp range
|
2013-10-21 21:09:13 +00:00
|
|
|
*/
|
|
|
|
|
public function testEmailWithCommasAreInvalids() {
|
2011-01-27 20:52:12 +00:00
|
|
|
$this->invalid( "user,foo@example.org" );
|
|
|
|
|
$this->invalid( "userfoo@ex,ample.org" );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithHyphens() {
|
2011-01-27 20:52:12 +00:00
|
|
|
$this->valid( "user-foo@example.org" );
|
|
|
|
|
$this->valid( "userfoo@ex-ample.org" );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailDomainCanNotBeginWithDot() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->invalid( "user@." );
|
|
|
|
|
$this->invalid( "user@.localdomain" );
|
|
|
|
|
$this->invalid( "user@localdomain." );
|
2011-01-21 18:01:47 +00:00
|
|
|
$this->valid( "user.@localdomain" );
|
|
|
|
|
$this->valid( ".@localdomain" );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->invalid( ".@a............" );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithFunnyCharacters() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->valid( "\$user!ex{this}@123.com" );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailTopLevelDomainCanBeNumerical() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->valid( "user@example.1234" );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithoutAtSignIsInvalid() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->invalid( 'useràexample.com' );
|
|
|
|
|
}
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testEmailWithOneCharacterDomainIsValid() {
|
2011-01-21 18:01:47 +00:00
|
|
|
$this->valid( 'user@a' );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
}
|