From 5cb75a136d1134cea62b2ccd595c0f7405060466 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 15 Dec 2021 19:59:32 +0000 Subject: [PATCH] Tests: Mock the HTTP request response in `download_url()` tests. This aims to speed up the tests and minimize unrelated failures by avoiding an unnecessary external HTTP request, while still performing the intended functionality checks. Update similar helpers in some other tests to use more consistent terminology. Follow-up to [37907], [46175], [51626]. See #54420, #53363. git-svn-id: https://develop.svn.wordpress.org/trunk@52382 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/admin/includesFile.php | 26 +++++++++++++++++++ tests/phpunit/tests/functions/doEnclose.php | 7 +++-- tests/phpunit/tests/http/wpGetHttpHeaders.php | 7 +++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/admin/includesFile.php b/tests/phpunit/tests/admin/includesFile.php index eb6cc7c826..421137bc14 100644 --- a/tests/phpunit/tests/admin/includesFile.php +++ b/tests/phpunit/tests/admin/includesFile.php @@ -291,6 +291,9 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase { * @covers ::download_url */ public function test_download_url_no_warning_for_url_without_path() { + // Hook a mocked HTTP request response. + add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 ); + $result = download_url( 'https://example.com' ); $this->assertIsString( $result ); @@ -306,6 +309,9 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase { * @covers ::download_url */ public function test_download_url_no_warning_for_url_without_path_with_signature_verification() { + // Hook a mocked HTTP request response. + add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 ); + add_filter( 'wp_signature_hosts', static function( $urls ) { @@ -326,4 +332,24 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase { $this->assertWPError( $error ); $this->assertSame( 'signature_verification_no_signature', $error->get_error_code() ); } + + /** + * Mock the HTTP request response. + * + * @param bool $false False. + * @param array $arguments Request arguments. + * @param string $url Request URL. + * @return array|bool + */ + public function mock_http_request( $false, $arguments, $url ) { + if ( 'https://example.com' === $url ) { + return array( + 'response' => array( + 'code' => 200, + ), + ); + } + + return $false; + } } diff --git a/tests/phpunit/tests/functions/doEnclose.php b/tests/phpunit/tests/functions/doEnclose.php index 5c8a42089f..a72b603061 100644 --- a/tests/phpunit/tests/functions/doEnclose.php +++ b/tests/phpunit/tests/functions/doEnclose.php @@ -25,7 +25,7 @@ class Tests_Functions_DoEnclose extends WP_UnitTestCase { */ public function set_up() { parent::set_up(); - add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 ); + add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 ); } /** @@ -250,17 +250,16 @@ class Tests_Functions_DoEnclose extends WP_UnitTestCase { } /** - * Fake the HTTP request response. + * Mock the HTTP request response. * * @since 5.3.0 * * @param bool $false False. * @param array $arguments Request arguments. * @param string $url Request URL. - * * @return array Header. */ - public function fake_http_request( $false, $arguments, $url ) { + public function mock_http_request( $false, $arguments, $url ) { // Video and audio headers. $fake_headers = array( diff --git a/tests/phpunit/tests/http/wpGetHttpHeaders.php b/tests/phpunit/tests/http/wpGetHttpHeaders.php index 4b7b867a49..b462d7d8da 100644 --- a/tests/phpunit/tests/http/wpGetHttpHeaders.php +++ b/tests/phpunit/tests/http/wpGetHttpHeaders.php @@ -12,8 +12,8 @@ class Tests_HTTP_wpGetHttpHeaders extends WP_UnitTestCase { public function set_up() { parent::set_up(); - // Hook a fake HTTP request response. - add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 ); + // Hook a mocked HTTP request response. + add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 ); } /** @@ -47,10 +47,9 @@ class Tests_HTTP_wpGetHttpHeaders extends WP_UnitTestCase { * @param bool $false False. * @param array $arguments Request arguments. * @param string $url Request URL. - * * @return array|bool */ - public function fake_http_request( $false, $arguments, $url ) { + public function mock_http_request( $false, $arguments, $url ) { if ( 'http://example.com' === $url ) { return array( 'headers' => true ); }