Skip to content

Commit 0ecc492

Browse files
committed
Add various unit tests for antispambot, is_email and sanitize_email
1 parent 9d9a0f1 commit 0ecc492

3 files changed

Lines changed: 151 additions & 1 deletion

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Tests for the antispambot() function.
4+
*
5+
* @group formatting
6+
* @covers ::antispambot
7+
*/
8+
class Tests_Formatting_Antispambot extends WP_UnitTestCase {
9+
10+
/**
11+
* This is basically a driveby test. While working on ticket
12+
* 31992 I noticed that there was no unit testing for
13+
* antispambot, so I added a little, just so I'd leave the code
14+
* better than I found it.
15+
*
16+
* @ticket 31992
17+
*
18+
* @dataProvider data_returns_valid_utf8
19+
* @param string $address The email address to obfuscate.
20+
* @param bool $validity Whether the obfuscated address should be valid UTF-8.
21+
*/
22+
public function test_returns_valid_utf8( $address, $validity ) {
23+
$this->assertSame( wp_is_valid_utf8( antispambot( $address ) ), $validity );
24+
}
25+
26+
/**
27+
* Data provider for test_returns_valid_utf8.
28+
*/
29+
public function data_returns_valid_utf8() {
30+
return array(
31+
'plain' => array( 'bob@example.com', true ),
32+
'plain with ip' => array( 'ace@204.32.222.14', true ),
33+
'deep subdomain' => array( 'kevin@many.subdomains.make.a.happy.man.edu', true ),
34+
'short address' => array( 'a@b.co', true ),
35+
'weird but legal dots' => array( '..@example.com', true ),
36+
);
37+
}
38+
39+
/**
40+
* This tests that antispambot performs some sort of
41+
* obfuscation, and that its obfuscated form will be rendered
42+
* sensibly by browsers.
43+
*
44+
* @dataProvider data_antispambot_obfuscates
45+
* @param string $provided The email address to obfuscate.
46+
*/
47+
public function test_antispambot_obfuscates( $provided ) {
48+
$obfuscated = antispambot( $provided );
49+
$p = new WP_HTML_Tag_Processor( $obfuscated );
50+
$p->next_token();
51+
$decoded = $p->get_modifiable_text();
52+
$decoded = preg_replace_callback( '~%\d\d~', function () { }, $decoded );
53+
54+
$this->assertNotEquals( $provided, $obfuscated );
55+
$this->assertSame( $provided, $decoded );
56+
}
57+
58+
/**
59+
* Data provider for test_antispambot_obfuscates.
60+
*/
61+
public function data_antispambot_obfuscates() {
62+
return array(
63+
'example@example.com',
64+
'#@example.com',
65+
);
66+
}
67+
}

tests/phpunit/tests/formatting/isEmail.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
2-
32
/**
3+
* Tests for the is_email() function.
4+
*
45
* @group formatting
56
*
67
* @covers ::is_email
@@ -23,10 +24,12 @@ public static function valid_email_provider() {
2324
$valid_emails = array(
2425
'bob@example.com',
2526
'phil@example.info',
27+
'phil@TLA.example',
2628
'ace@204.32.222.14',
2729
'kevin@many.subdomains.make.a.happy.man.edu',
2830
'a@b.co',
2931
'bill+ted@example.com',
32+
'..@example.com',
3033
);
3134

3235
foreach ( $valid_emails as $email ) {
@@ -54,6 +57,50 @@ public static function invalid_email_provider() {
5457
'com.exampleNOSPAMbob',
5558
'bob@your mom',
5659
'a@b.c',
60+
'" "@b.c',
61+
'"@"@b.c',
62+
'a@route.org@b.c',
63+
'h(aj@couc.ou', // bad comment.
64+
'hi@',
65+
'hi@hi@couc.ou', // double @.
66+
67+
/*
68+
* The next address is not deliverable as described,
69+
* SMTP servers should strip the (ab), so it is very
70+
* likely a source of confusion or a typo.
71+
* Best rejected.
72+
*/
73+
'(ab)cd@couc.ou',
74+
75+
/*
76+
* The next address is not globally deliverable,
77+
* so it may work with PHPMailer and break with
78+
* mail sending services. Best not allow users
79+
* to paint themselves into that corner. This also
80+
* avoids security problems like those that were
81+
* used to probe the Wordpress server's local
82+
* network.
83+
*/
84+
'toto@to',
85+
86+
/*
87+
* Several addresses are best rejected because
88+
* we don't want to allow sending to fe80::, 192.168
89+
* and other special addresses; that too might
90+
* be used to probe the Wordpress server's local
91+
* network.
92+
*/
93+
'to@[2001:db8::1]',
94+
'to@[IPv6:2001:db8::1]',
95+
'to@[192.168.1.1]',
96+
97+
/*
98+
* Ill-formed UTF-8 byte sequences must be rejected.
99+
* A lone continuation byte (0x80) is not valid UTF-8
100+
* whether it appears in the local part or the domain.
101+
*/
102+
"a\x80b@example.com", // invalid UTF-8 in local part.
103+
"abc@\x80.org", // invalid UTF-8 in domain subdomain.
57104
);
58105

59106
foreach ( $invalid_emails as $email ) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Tests for the sanitize_email() function.
4+
*
5+
* @group formatting
6+
* @covers ::sanitize_email
7+
*/
8+
class Tests_Formatting_SanitizeEmail extends WP_UnitTestCase {
9+
10+
/**
11+
* This test checks that email addresses are properly sanitized.
12+
*
13+
* @ticket 31992
14+
* @dataProvider data_for_sanitation
15+
* @param string $address The email address to sanitize.
16+
* @param string $expected The expected sanitized email address.
17+
*/
18+
public function test_returns_stripped_email_address( $address, $expected ) {
19+
$this->assertSame( sanitize_email( $address ), $expected );
20+
}
21+
22+
/**
23+
* Data provider for test_returns_stripped_email_address.
24+
*/
25+
public function data_for_sanitation() {
26+
return array(
27+
'shorter than 6 characters' => array( 'a@b', '' ),
28+
'contains no @' => array( 'ab', '' ),
29+
'just a TLD' => array( 'abc@com', '' ),
30+
'plain' => array( 'abc@example.com', 'abc@example.com' ),
31+
'invalid utf8 in local' => array( "a\x80b@example.com", '' ),
32+
'invalid utf8 subdomain dropped' => array( "abc@sub.\x80.org", 'abc@sub.org' ),
33+
'all subdomains invalid utf8' => array( "abc@\x80.org", '' ),
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)