]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/SanitizerValidateEmailTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / SanitizerValidateEmailTest.php
1 <?php
2
3 /**
4  * @covers Sanitizer::validateEmail
5  * @todo all test methods in this class should be refactored and...
6  *    use a single test method and a single data provider...
7  */
8 class SanitizerValidateEmailTest extends PHPUnit_Framework_TestCase {
9
10         private function checkEmail( $addr, $expected = true, $msg = '' ) {
11                 if ( $msg == '' ) {
12                         $msg = "Testing $addr";
13                 }
14
15                 $this->assertEquals(
16                         $expected,
17                         Sanitizer::validateEmail( $addr ),
18                         $msg
19                 );
20         }
21
22         private function valid( $addr, $msg = '' ) {
23                 $this->checkEmail( $addr, true, $msg );
24         }
25
26         private function invalid( $addr, $msg = '' ) {
27                 $this->checkEmail( $addr, false, $msg );
28         }
29
30         public function testEmailWellKnownUserAtHostDotTldAreValid() {
31                 $this->valid( 'user@example.com' );
32                 $this->valid( 'user@example.museum' );
33         }
34
35         public function testEmailWithUpperCaseCharactersAreValid() {
36                 $this->valid( 'USER@example.com' );
37                 $this->valid( 'user@EXAMPLE.COM' );
38                 $this->valid( 'user@Example.com' );
39                 $this->valid( 'USER@eXAMPLE.com' );
40         }
41
42         public function testEmailWithAPlusInUserName() {
43                 $this->valid( 'user+sub@example.com' );
44                 $this->valid( 'user+@example.com' );
45         }
46
47         public function testEmailDoesNotNeedATopLevelDomain() {
48                 $this->valid( "user@localhost" );
49                 $this->valid( "FooBar@localdomain" );
50                 $this->valid( "nobody@mycompany" );
51         }
52
53         public function testEmailWithWhiteSpacesBeforeOrAfterAreInvalids() {
54                 $this->invalid( " user@host.com" );
55                 $this->invalid( "user@host.com " );
56                 $this->invalid( "\tuser@host.com" );
57                 $this->invalid( "user@host.com\t" );
58         }
59
60         public function testEmailWithWhiteSpacesAreInvalids() {
61                 $this->invalid( "User user@host" );
62                 $this->invalid( "first last@mycompany" );
63                 $this->invalid( "firstlast@my company" );
64         }
65
66         /**
67          * T28948 : comma were matched by an incorrect regexp range
68          */
69         public function testEmailWithCommasAreInvalids() {
70                 $this->invalid( "user,foo@example.org" );
71                 $this->invalid( "userfoo@ex,ample.org" );
72         }
73
74         public function testEmailWithHyphens() {
75                 $this->valid( "user-foo@example.org" );
76                 $this->valid( "userfoo@ex-ample.org" );
77         }
78
79         public function testEmailDomainCanNotBeginWithDot() {
80                 $this->invalid( "user@." );
81                 $this->invalid( "user@.localdomain" );
82                 $this->invalid( "user@localdomain." );
83                 $this->valid( "user.@localdomain" );
84                 $this->valid( ".@localdomain" );
85                 $this->invalid( ".@a............" );
86         }
87
88         public function testEmailWithFunnyCharacters() {
89                 $this->valid( "\$user!ex{this}@123.com" );
90         }
91
92         public function testEmailTopLevelDomainCanBeNumerical() {
93                 $this->valid( "user@example.1234" );
94         }
95
96         public function testEmailWithoutAtSignIsInvalid() {
97                 $this->invalid( 'useràexample.com' );
98         }
99
100         public function testEmailWithOneCharacterDomainIsValid() {
101                 $this->valid( 'user@a' );
102         }
103 }