MediaWiki  REL1_31
SanitizerValidateEmailTest.php
Go to the documentation of this file.
1 <?php
2 
8 class SanitizerValidateEmailTest extends PHPUnit\Framework\TestCase {
9 
10  use MediaWikiCoversValidator;
11 
12  private function checkEmail( $addr, $expected = true, $msg = '' ) {
13  if ( $msg == '' ) {
14  $msg = "Testing $addr";
15  }
16 
17  $this->assertEquals(
18  $expected,
19  Sanitizer::validateEmail( $addr ),
20  $msg
21  );
22  }
23 
24  private function valid( $addr, $msg = '' ) {
25  $this->checkEmail( $addr, true, $msg );
26  }
27 
28  private function invalid( $addr, $msg = '' ) {
29  $this->checkEmail( $addr, false, $msg );
30  }
31 
33  $this->valid( 'user@example.com' );
34  $this->valid( 'user@example.museum' );
35  }
36 
38  $this->valid( 'USER@example.com' );
39  $this->valid( 'user@EXAMPLE.COM' );
40  $this->valid( 'user@Example.com' );
41  $this->valid( 'USER@eXAMPLE.com' );
42  }
43 
44  public function testEmailWithAPlusInUserName() {
45  $this->valid( 'user+sub@example.com' );
46  $this->valid( 'user+@example.com' );
47  }
48 
50  $this->valid( "user@localhost" );
51  $this->valid( "FooBar@localdomain" );
52  $this->valid( "nobody@mycompany" );
53  }
54 
56  $this->invalid( " user@host.com" );
57  $this->invalid( "user@host.com " );
58  $this->invalid( "\tuser@host.com" );
59  $this->invalid( "user@host.com\t" );
60  }
61 
63  $this->invalid( "User user@host" );
64  $this->invalid( "first last@mycompany" );
65  $this->invalid( "firstlast@my company" );
66  }
67 
71  public function testEmailWithCommasAreInvalids() {
72  $this->invalid( "user,foo@example.org" );
73  $this->invalid( "userfoo@ex,ample.org" );
74  }
75 
76  public function testEmailWithHyphens() {
77  $this->valid( "user-foo@example.org" );
78  $this->valid( "userfoo@ex-ample.org" );
79  }
80 
82  $this->invalid( "user@." );
83  $this->invalid( "user@.localdomain" );
84  $this->invalid( "user@localdomain." );
85  $this->valid( "user.@localdomain" );
86  $this->valid( ".@localdomain" );
87  $this->invalid( ".@a............" );
88  }
89 
90  public function testEmailWithFunnyCharacters() {
91  $this->valid( "\$user!ex{this}@123.com" );
92  }
93 
95  $this->valid( "user@example.1234" );
96  }
97 
98  public function testEmailWithoutAtSignIsInvalid() {
99  $this->invalid( 'useràexample.com' );
100  }
101 
103  $this->valid( 'user@a' );
104  }
105 }
SanitizerValidateEmailTest\invalid
invalid( $addr, $msg='')
Definition: SanitizerValidateEmailTest.php:28
SanitizerValidateEmailTest\testEmailTopLevelDomainCanBeNumerical
testEmailTopLevelDomainCanBeNumerical()
Definition: SanitizerValidateEmailTest.php:94
SanitizerValidateEmailTest\checkEmail
checkEmail( $addr, $expected=true, $msg='')
Definition: SanitizerValidateEmailTest.php:12
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
SanitizerValidateEmailTest\testEmailWithWhiteSpacesAreInvalids
testEmailWithWhiteSpacesAreInvalids()
Definition: SanitizerValidateEmailTest.php:62
SanitizerValidateEmailTest\testEmailWithWhiteSpacesBeforeOrAfterAreInvalids
testEmailWithWhiteSpacesBeforeOrAfterAreInvalids()
Definition: SanitizerValidateEmailTest.php:55
SanitizerValidateEmailTest\testEmailWithOneCharacterDomainIsValid
testEmailWithOneCharacterDomainIsValid()
Definition: SanitizerValidateEmailTest.php:102
Sanitizer\validateEmail
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:2105
SanitizerValidateEmailTest\testEmailWellKnownUserAtHostDotTldAreValid
testEmailWellKnownUserAtHostDotTldAreValid()
Definition: SanitizerValidateEmailTest.php:32
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
SanitizerValidateEmailTest\testEmailDomainCanNotBeginWithDot
testEmailDomainCanNotBeginWithDot()
Definition: SanitizerValidateEmailTest.php:81
SanitizerValidateEmailTest
Sanitizer::validateEmail.
Definition: SanitizerValidateEmailTest.php:8
SanitizerValidateEmailTest\valid
valid( $addr, $msg='')
Definition: SanitizerValidateEmailTest.php:24
SanitizerValidateEmailTest\testEmailWithUpperCaseCharactersAreValid
testEmailWithUpperCaseCharactersAreValid()
Definition: SanitizerValidateEmailTest.php:37
SanitizerValidateEmailTest\testEmailWithFunnyCharacters
testEmailWithFunnyCharacters()
Definition: SanitizerValidateEmailTest.php:90
SanitizerValidateEmailTest\testEmailWithHyphens
testEmailWithHyphens()
Definition: SanitizerValidateEmailTest.php:76
SanitizerValidateEmailTest\testEmailWithAPlusInUserName
testEmailWithAPlusInUserName()
Definition: SanitizerValidateEmailTest.php:44
SanitizerValidateEmailTest\testEmailWithoutAtSignIsInvalid
testEmailWithoutAtSignIsInvalid()
Definition: SanitizerValidateEmailTest.php:98
SanitizerValidateEmailTest\testEmailWithCommasAreInvalids
testEmailWithCommasAreInvalids()
T28948 : comma were matched by an incorrect regexp range.
Definition: SanitizerValidateEmailTest.php:71
SanitizerValidateEmailTest\testEmailDoesNotNeedATopLevelDomain
testEmailDoesNotNeedATopLevelDomain()
Definition: SanitizerValidateEmailTest.php:49