Skip to content

Commit fff7a78

Browse files
committed
Tests: Simplify offline readme and HTTP error checks.
Remove redundant `assertNotEmpty()` checks and unused match variables from the readme recommendation test. The existing `preg_match()` assertions require version text. Mock HTTP errors for empty and schemeless URLs in the `wp_remote_fopen()` tests. Remove their `external-http` annotations so they run in the default suite, and verify the safe-request flag and timeout. Developed in: #15119, #15114 Props shailu25, rejaulalomkhan. See #63914. git-svn-id: https://develop.svn.wordpress.org/trunk@64992 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b135517 commit fff7a78

2 files changed

Lines changed: 42 additions & 10 deletions

File tree

‎tests/phpunit/tests/functions/wpRemoteFopen.php‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,56 @@
88
class Tests_Functions_wpRemoteFopen extends WP_UnitTestCase {
99

1010
/**
11-
* @ticket 48845
11+
* An empty URL returns false when the HTTP API returns a WP_Error.
12+
*
13+
* On PHP 8+, `parse_url( '' )` returns `array( 'path' => '' )`, so
14+
* `wp_remote_fopen()` still calls `wp_safe_remote_get()`.
1215
*
13-
* @group external-http
16+
* @ticket 48845
17+
* @ticket 63914
1418
*/
1519
public function test_wp_remote_fopen_empty() {
20+
$request_args = null;
21+
22+
add_filter(
23+
'pre_http_request',
24+
static function ( $response, $parsed_args ) use ( &$request_args ) {
25+
$request_args = $parsed_args;
26+
27+
return new WP_Error( 'http_request_failed', 'A valid URL was not provided.' );
28+
},
29+
10,
30+
2
31+
);
32+
1633
$this->assertFalse( wp_remote_fopen( '' ) );
34+
$this->assertTrue( $request_args['reject_unsafe_urls'], 'The request should use wp_safe_remote_get().' );
35+
$this->assertSame( 10, $request_args['timeout'] );
1736
}
1837

1938
/**
20-
* @ticket 48845
39+
* A schemeless URL returns false when the HTTP API returns a WP_Error.
2140
*
22-
* @group external-http
41+
* @ticket 48845
42+
* @ticket 63914
2343
*/
2444
public function test_wp_remote_fopen_bad_url() {
45+
$request_args = null;
46+
47+
add_filter(
48+
'pre_http_request',
49+
static function ( $response, $parsed_args ) use ( &$request_args ) {
50+
$request_args = $parsed_args;
51+
52+
return new WP_Error( 'http_request_failed', 'A valid URL was not provided.' );
53+
},
54+
10,
55+
2
56+
);
57+
2558
$this->assertFalse( wp_remote_fopen( 'wp.com' ) );
59+
$this->assertTrue( $request_args['reject_unsafe_urls'], 'The request should use wp_safe_remote_get().' );
60+
$this->assertSame( 10, $request_args['timeout'] );
2661
}
2762

2863
/**

‎tests/phpunit/tests/readme.php‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ public function test_readme_contains_recommended_versions() {
1818
$readme = file_get_contents( ABSPATH . 'readme.html' );
1919
$this->assertIsString( $readme, 'readme.html should exist and be readable.' );
2020

21-
$this->assertSame( 1, preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]+)#s', $readme, $php_matches ), 'readme.html should declare a recommended PHP version.' );
22-
$this->assertNotEmpty( $php_matches[1], 'Recommended PHP version should not be empty.' );
21+
$this->assertSame( 1, preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]+)#s', $readme ), 'readme.html should declare a recommended PHP version.' );
2322

24-
$this->assertSame( 1, preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]+)#s', $readme, $mysql_matches ), 'readme.html should declare a recommended MySQL version.' );
25-
$this->assertNotEmpty( $mysql_matches[1], 'Recommended MySQL version should not be empty.' );
23+
$this->assertSame( 1, preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]+)#s', $readme ), 'readme.html should declare a recommended MySQL version.' );
2624

27-
$this->assertSame( 1, preg_match( '#Recommendations.*MariaDB</a> version <strong>([0-9.]+)#s', $readme, $mariadb_matches ), 'readme.html should declare a recommended MariaDB version.' );
28-
$this->assertNotEmpty( $mariadb_matches[1], 'Recommended MariaDB version should not be empty.' );
25+
$this->assertSame( 1, preg_match( '#Recommendations.*MariaDB</a> version <strong>([0-9.]+)#s', $readme ), 'readme.html should declare a recommended MariaDB version.' );
2926
}
3027

3128
/**

0 commit comments

Comments
 (0)