Skip to content

Commit 4b924e3

Browse files
Tests: Skip test_readme() if the HTTP request to secure.php.net or dev.mysql.com failed on timeout.
Move `skipTestOnTimeout()` to `WP_UnitTestCase_Base` to avoid duplication. Merges [46682] and [46996] to the 5.0 branch. See #51669. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@50095 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d717a87 commit 4b924e3

File tree

4 files changed

+38
-47
lines changed

4 files changed

+38
-47
lines changed

tests/phpunit/includes/testcase.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function clean_up_global_scope() {
182182
}
183183

184184
/**
185-
* Allow tests to be skipped on some automated runs
185+
* Allow tests to be skipped on some automated runs.
186186
*
187187
* For test runs on Travis for something other than trunk/master
188188
* we want to skip tests that only need to run for master.
@@ -224,6 +224,29 @@ public function skipWithMultisite() {
224224
}
225225
}
226226

227+
/**
228+
* Allow tests to be skipped if the HTTP request times out.
229+
*
230+
* @param array|WP_Error $response HTTP response.
231+
*/
232+
public function skipTestOnTimeout( $response ) {
233+
if ( ! is_wp_error( $response ) ) {
234+
return;
235+
}
236+
if ( 'connect() timed out!' === $response->get_error_message() ) {
237+
$this->markTestSkipped( 'HTTP timeout' );
238+
}
239+
240+
if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
241+
$this->markTestSkipped( 'HTTP timeout' );
242+
}
243+
244+
if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
245+
$this->markTestSkipped( 'HTTP timeout' );
246+
}
247+
248+
}
249+
227250
/**
228251
* Unregister existing post types and register defaults.
229252
*

tests/phpunit/tests/external-http/basic.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ function test_readme() {
1313
preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]*)#s', $readme, $matches );
1414

1515
$response = wp_remote_get( 'https://secure.php.net/supported-versions.php' );
16-
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
17-
$this->fail( 'Could not contact PHP.net to check versions.' );
16+
17+
$this->skipTestOnTimeout( $response );
18+
19+
$response_code = wp_remote_retrieve_response_code( $response );
20+
if ( 200 !== $response_code ) {
21+
$this->fail( sprintf( 'Could not contact PHP.net to check versions. Response code: %s', $response_code ) );
1822
}
23+
1924
$php = wp_remote_retrieve_body( $response );
2025

2126
preg_match_all( '#<tr class="stable">\s*<td>\s*<a [^>]*>\s*([0-9.]*)#s', $php, $phpmatches );
@@ -25,9 +30,14 @@ function test_readme() {
2530
preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );
2631

2732
$response = wp_remote_get( "https://dev.mysql.com/doc/relnotes/mysql/{$matches[1]}/en/" );
28-
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
29-
$this->fail( 'Could not contact dev.MySQL.com to check versions.' );
33+
34+
$this->skipTestOnTimeout( $response );
35+
36+
$response_code = wp_remote_retrieve_response_code( $response );
37+
if ( 200 !== $response_code ) {
38+
$this->fail( sprintf( 'Could not contact dev.MySQL.com to check versions. Response code: %s', $response_code ) );
3039
}
40+
3141
$mysql = wp_remote_retrieve_body( $response );
3242

3343
preg_match( '#(\d{4}-\d{2}-\d{2}), General Availability#', $mysql, $mysqlmatches );

tests/phpunit/tests/http/base.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,6 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
1717

1818
protected $http_request_args;
1919

20-
/**
21-
* Mark test as skipped if the HTTP request times out.
22-
*/
23-
function skipTestOnTimeout( $response ) {
24-
if( ! is_wp_error( $response ) ){
25-
return;
26-
}
27-
if ( 'connect() timed out!' === $response->get_error_message() ){
28-
$this->markTestSkipped( 'HTTP timeout' );
29-
}
30-
31-
if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
32-
$this->markTestSkipped( 'HTTP timeout' );
33-
}
34-
35-
if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
36-
$this->markTestSkipped( 'HTTP timeout' );
37-
}
38-
39-
}
40-
4120
function setUp() {
4221

4322
if ( is_callable( array('WP_Http', '_getTransport') ) ) {

tests/phpunit/tests/http/functions.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,6 @@
66
*/
77
class Tests_HTTP_Functions extends WP_UnitTestCase {
88

9-
/**
10-
* Mark test as skipped if the HTTP request times out.
11-
*/
12-
function skipTestOnTimeout( $response ) {
13-
if ( ! is_wp_error( $response ) ) {
14-
return;
15-
}
16-
if ( 'connect() timed out!' === $response->get_error_message() ) {
17-
$this->markTestSkipped( 'HTTP timeout' );
18-
}
19-
20-
if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
21-
$this->markTestSkipped( 'HTTP timeout' );
22-
}
23-
24-
if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
25-
$this->markTestSkipped( 'HTTP timeout' );
26-
}
27-
28-
}
29-
309
public function setUp() {
3110
if ( ! extension_loaded( 'openssl' ) ) {
3211
$this->markTestSkipped( 'Tests_HTTP_Functions requires openssl.' );

0 commit comments

Comments
 (0)