Skip to content

Commit ee9cc92

Browse files
authored
Merge branch 'trunk' into 26962-retaining-user-info-in-user-edit-after-submit
2 parents 9baf444 + 4bdcb35 commit ee9cc92

9 files changed

Lines changed: 253 additions & 39 deletions
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
* Ensures that antispambot will not produce invalid UTF-8 when hiding email addresses.
11+
*
12+
* Were a non-US-ASCII email address be sent into `antispambot()`, then a naive approach
13+
* to obfuscation could break apart multibyte characters and leave invalid UTF-8 as a
14+
* result.
15+
*
16+
* @ticket 31992
17+
*
18+
* @dataProvider data_returns_valid_utf8
19+
*
20+
* @param string $email The email address to obfuscate.
21+
*/
22+
public function test_returns_valid_utf8( $email ) {
23+
$this->assertTrue( wp_is_valid_utf8( antispambot( $email ) ) );
24+
}
25+
26+
/**
27+
* Data provider.
28+
*
29+
* return array[]
30+
*/
31+
public function data_returns_valid_utf8() {
32+
return array(
33+
'plain' => array( 'bob@example.com' ),
34+
'plain with ip' => array( 'ace@204.32.222.14' ),
35+
'deep subdomain' => array( 'kevin@many.subdomains.make.a.happy.man.edu' ),
36+
'short address' => array( 'a@b.co' ),
37+
'weird but legal dots' => array( '..@example.com' ),
38+
);
39+
}
40+
41+
/**
42+
* This tests that antispambot performs some sort of obfuscation
43+
* and that the obfuscation maps back to the original value.
44+
*
45+
* @ticket 31992
46+
*
47+
* @dataProvider data_antispambot_obfuscates
48+
*
49+
* @param string $provided The email address to obfuscate.
50+
*/
51+
public function test_antispambot_obfuscates( $provided ) {
52+
// The only token should be the email address, so advance once and treat as a text node.
53+
$obfuscated = antispambot( $provided );
54+
$p = new WP_HTML_Tag_Processor( $obfuscated );
55+
$p->next_token();
56+
$decoded = rawurldecode( $p->get_modifiable_text() );
57+
58+
$this->assertNotSame( $provided, $obfuscated, 'Should have produced an obfuscated representation.' );
59+
$this->assertSame( $provided, $decoded, 'Should have decoded to the original email after restoring.' );
60+
}
61+
62+
/**
63+
* Data provider.
64+
*
65+
* @return array[]
66+
*/
67+
public function data_antispambot_obfuscates() {
68+
return array(
69+
array( 'example@example.com' ),
70+
array( '#@example.com' ),
71+
);
72+
}
73+
}

tests/phpunit/tests/formatting/isEmail.php

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
<?php
2-
32
/**
3+
* Tests for the is_email() function.
4+
*
45
* @group formatting
56
*
67
* @covers ::is_email
78
*/
89
class Tests_Formatting_IsEmail extends WP_UnitTestCase {
9-
1010
/**
11-
* @dataProvider valid_email_provider
11+
* Ensures that valid emails are returned unchanged.
12+
*
13+
* @ticket 31992
14+
*
15+
* @dataProvider data_valid_email_provider
16+
*
17+
* @param string $email Valid email address.
1218
*/
1319
public function test_returns_the_email_address_if_it_is_valid( $email ) {
14-
$this->assertSame( $email, is_email( $email ), "is_email() should return the email address for $email." );
20+
$this->assertSame(
21+
$email,
22+
is_email( $email ),
23+
'Should return the given email address unchanged when valid.'
24+
);
1525
}
1626

1727
/**
18-
* Data provider for valid email addresses.
28+
* Data provider.
1929
*
20-
* @return array
30+
* @return Generator
2131
*/
22-
public static function valid_email_provider() {
32+
public static function data_valid_email_provider() {
2333
$valid_emails = array(
2434
'bob@example.com',
2535
'phil@example.info',
36+
'phil@TLA.example',
2637
'ace@204.32.222.14',
2738
'kevin@many.subdomains.make.a.happy.man.edu',
2839
'a@b.co',
2940
'bill+ted@example.com',
41+
'..@example.com',
3042
);
3143

3244
foreach ( $valid_emails as $email ) {
@@ -35,25 +47,78 @@ public static function valid_email_provider() {
3547
}
3648

3749
/**
38-
* @dataProvider invalid_email_provider
50+
* Ensures that unrecognized email addresses are rejected.
51+
*
52+
* @ticket 31992
53+
*
54+
* @dataProvider data_invalid_email_provider
55+
*
56+
* @param string $email Invalid or unrecognized-to-WordPress email address.
3957
*/
4058
public function test_returns_false_if_given_an_invalid_email_address( $email ) {
41-
$this->assertFalse( is_email( $email ), "is_email() should return false for $email." );
59+
$this->assertFalse(
60+
is_email( $email ),
61+
'Should have rejected the email as invalid.'
62+
);
4263
}
4364

4465
/**
45-
* Data provider for invalid email addresses.
66+
* Data provider.
4667
*
47-
* @return array
68+
* @return Generator
4869
*/
49-
public static function invalid_email_provider() {
70+
public static function data_invalid_email_provider() {
5071
$invalid_emails = array(
5172
'khaaaaaaaaaaaaaaan!',
5273
'http://bob.example.com/',
5374
"sif i'd give u it, spamer!1",
5475
'com.exampleNOSPAMbob',
5576
'bob@your mom',
5677
'a@b.c',
78+
'" "@b.c',
79+
'"@"@b.c',
80+
'a@route.org@b.c',
81+
'h(aj@couc.ou', // bad comment.
82+
'hi@',
83+
'hi@hi@couc.ou', // double @.
84+
85+
/*
86+
* The next address is not deliverable as described,
87+
* SMTP servers should strip the (ab), so it is very
88+
* likely a source of confusion or a typo.
89+
* Best rejected.
90+
*/
91+
'(ab)cd@couc.ou',
92+
93+
/*
94+
* The next address is not globally deliverable,
95+
* so it may work with PHPMailer and break with
96+
* mail sending services. Best not allow users
97+
* to paint themselves into that corner. This also
98+
* avoids security problems like those that were
99+
* used to probe the WordPress server's local
100+
* network.
101+
*/
102+
'toto@to',
103+
104+
/*
105+
* Several addresses are best rejected because
106+
* we don't want to allow sending to fe80::, 192.168
107+
* and other special addresses; that too might
108+
* be used to probe the WordPress server's local
109+
* network.
110+
*/
111+
'to@[2001:db8::1]',
112+
'to@[IPv6:2001:db8::1]',
113+
'to@[192.168.1.1]',
114+
115+
/*
116+
* Ill-formed UTF-8 byte sequences must be rejected.
117+
* A lone continuation byte (0x80) is not valid UTF-8
118+
* whether it appears in the local part or the domain.
119+
*/
120+
"a\x80b@example.com", // invalid UTF-8 in local part.
121+
"abc@\x80.org", // invalid UTF-8 in domain subdomain.
57122
);
58123

59124
foreach ( $invalid_emails as $email ) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* This test checks that email addresses are properly sanitized.
11+
*
12+
* @ticket 31992
13+
*
14+
* @dataProvider data_sanitized_email_pairs
15+
*
16+
* @param string $address The email address to sanitize.
17+
* @param string $expected The expected sanitized email address.
18+
*/
19+
public function test_returns_stripped_email_address( $address, $expected ) {
20+
$this->assertSame(
21+
$expected,
22+
sanitize_email( $address ),
23+
'Should have produced the known sanitized form of the email.'
24+
);
25+
}
26+
27+
/**
28+
* Data provider.
29+
*
30+
* @return array[]
31+
*/
32+
public function data_sanitized_email_pairs() {
33+
return array(
34+
'shorter than 6 characters' => array( 'a@b', '' ),
35+
'contains no @' => array( 'ab', '' ),
36+
'just a TLD' => array( 'abc@com', '' ),
37+
'plain' => array( 'abc@example.com', 'abc@example.com' ),
38+
'invalid utf8 subdomain dropped' => array( "abc@sub.\x80.org", 'abc@sub.org' ),
39+
'all subdomains invalid utf8' => array( "abc@\x80.org", '' ),
40+
);
41+
}
42+
}

tests/phpunit/tests/oembed/WpEmbed.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/**
44
* @group oembed
5+
*
6+
* @covers WP_Embed
57
*/
68
class Tests_WP_Embed extends WP_UnitTestCase {
79
/**

tests/phpunit/tests/oembed/getResponseData.php renamed to tests/phpunit/tests/oembed/getOembedResponseData.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
/**
44
* @group oembed
5+
*
56
* @covers ::get_oembed_response_data
67
*/
7-
class Tests_oEmbed_Response_Data extends WP_UnitTestCase {
8+
class Tests_oEmbed_GetOembedResponseData extends WP_UnitTestCase {
89
public function set_up() {
910
parent::set_up();
1011

tests/phpunit/tests/oembed/postEmbedUrl.php renamed to tests/phpunit/tests/oembed/getPostEmbedUrl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
/**
44
* @group oembed
5+
*
6+
* @covers ::get_post_embed_url
57
*/
6-
class Tests_Post_Embed_URL extends WP_UnitTestCase {
8+
class Tests_oEmbed_GetPostEmbedUrl extends WP_UnitTestCase {
79
public function test_non_existent_post() {
810
$embed_url = get_post_embed_url( 0 );
911
$this->assertFalse( $embed_url );

tests/phpunit/tests/oembed/filterTitleAttributes.php renamed to tests/phpunit/tests/oembed/wpFilterOembedIframeTitleAttributes.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@
22

33
/**
44
* @group oembed
5+
*
6+
* @covers ::wp_filter_oembed_iframe_title_attribute
57
*/
6-
class Tests_Filter_oEmbed_Iframe_Title_Attribute extends WP_UnitTestCase {
7-
public function data_filter_oembed_iframe_title_attribute() {
8+
class Tests_oEmbed_wpFilterOembedIframeTitleAttribute extends WP_UnitTestCase {
9+
10+
/**
11+
* @dataProvider data_oembed_iframe_title_attribute
12+
*/
13+
public function test_oembed_iframe_title_attribute( $html, $oembed_data, $url, $expected ) {
14+
$actual = wp_filter_oembed_iframe_title_attribute( $html, (object) $oembed_data, $url );
15+
16+
$this->assertEqualHTML( $expected, $actual );
17+
}
18+
19+
public function data_oembed_iframe_title_attribute() {
820
return array(
921
array(
1022
'<p>Foo</p><iframe src=""></iframe><b>Bar</b>',
@@ -61,15 +73,6 @@ public function data_filter_oembed_iframe_title_attribute() {
6173
);
6274
}
6375

64-
/**
65-
* @dataProvider data_filter_oembed_iframe_title_attribute
66-
*/
67-
public function test_oembed_iframe_title_attribute( $html, $oembed_data, $url, $expected ) {
68-
$actual = wp_filter_oembed_iframe_title_attribute( $html, (object) $oembed_data, $url );
69-
70-
$this->assertEqualHTML( $expected, $actual );
71-
}
72-
7376
public function test_filter_oembed_iframe_title_attribute() {
7477
add_filter( 'oembed_iframe_title_attribute', array( $this, '_filter_oembed_iframe_title_attribute' ) );
7578

tests/phpunit/tests/oembed/filterResult.php renamed to tests/phpunit/tests/oembed/wpFilterOembedResult.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
/**
44
* @group oembed
5+
*
6+
* @covers ::wp_filter_oembed_result
57
*/
6-
class Tests_Filter_oEmbed_Result extends WP_UnitTestCase {
8+
class Tests_oEmbed_wpFilterOembedResult extends WP_UnitTestCase {
9+
710
public function test_filter_oembed_result_trusted_malicious_iframe() {
811
$html = '<p></p><iframe onload="alert(1)"></iframe>';
912

@@ -154,6 +157,19 @@ public function test_filter_oembed_result_allowed_html() {
154157
$this->assertEqualHTML( '<blockquote class="wp-embedded-content"><a href=""></a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe>', $actual );
155158
}
156159

160+
/**
161+
* @dataProvider data_wp_filter_pre_oembed_custom_result
162+
*/
163+
public function test_wp_filter_pre_oembed_custom_result( $html, $expected ) {
164+
$data = (object) array(
165+
'type' => 'rich',
166+
'title' => 'Hola',
167+
'html' => $html,
168+
);
169+
$actual = _wp_oembed_get_object()->data2html( $data, 'https://untrusted.localhost' );
170+
$this->assertEqualHTML( $expected, $actual );
171+
}
172+
157173
public function data_wp_filter_pre_oembed_custom_result() {
158174
return array(
159175
array(
@@ -175,19 +191,6 @@ public function data_wp_filter_pre_oembed_custom_result() {
175191
);
176192
}
177193

178-
/**
179-
* @dataProvider data_wp_filter_pre_oembed_custom_result
180-
*/
181-
public function test_wp_filter_pre_oembed_custom_result( $html, $expected ) {
182-
$data = (object) array(
183-
'type' => 'rich',
184-
'title' => 'Hola',
185-
'html' => $html,
186-
);
187-
$actual = _wp_oembed_get_object()->data2html( $data, 'https://untrusted.localhost' );
188-
$this->assertEqualHTML( $expected, $actual );
189-
}
190-
191194
/**
192195
* @group feed
193196
*/

0 commit comments

Comments
 (0)