From 6242c634eea89c18c4d97cb569d216f9cdb2519f Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 21 Sep 2020 13:25:38 +0000 Subject: [PATCH] Tests: Convert a few more `function_exists()` and `extension_loaded()` checks to `@requires` annotations. This better utilizes the PHPUnit native functionality. Follow-up to [49024]. See #50639, #50640. git-svn-id: https://develop.svn.wordpress.org/trunk@49025 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/ajax/Compression.php | 12 +++------ tests/phpunit/tests/ajax/Response.php | 5 +--- tests/phpunit/tests/functions.php | 25 ++++------------- .../phpunit/tests/functions/anonymization.php | 6 ++--- tests/phpunit/tests/http/functions.php | 9 +------ tests/phpunit/tests/image/editorImagick.php | 5 +--- tests/phpunit/tests/image/functions.php | 27 +++++-------------- tests/phpunit/tests/image/meta.php | 12 ++------- tests/phpunit/tests/oembed/headers.php | 8 +++--- 9 files changed, 27 insertions(+), 82 deletions(-) diff --git a/tests/phpunit/tests/ajax/Compression.php b/tests/phpunit/tests/ajax/Compression.php index fdb52d3f0b..a059e5c729 100644 --- a/tests/phpunit/tests/ajax/Compression.php +++ b/tests/phpunit/tests/ajax/Compression.php @@ -54,13 +54,11 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase { /** * Fetch the test text (gzdeflate) + * + * @requires function gzdeflate */ public function test_gzdeflate() { - if ( ! function_exists( 'gzdeflate' ) ) { - $this->fail( 'gzdeflate function not available' ); - } - // Become an administrator. $this->_setRole( 'administrator' ); @@ -81,13 +79,11 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase { /** * Fetch the test text (gzencode) + * + * @requires function gzencode */ public function test_gzencode() { - if ( ! function_exists( 'gzencode' ) ) { - $this->fail( 'gzencode function not available' ); - } - // Become an administrator. $this->_setRole( 'administrator' ); diff --git a/tests/phpunit/tests/ajax/Response.php b/tests/phpunit/tests/ajax/Response.php index c8e4e3b129..3efbac3aef 100644 --- a/tests/phpunit/tests/ajax/Response.php +++ b/tests/phpunit/tests/ajax/Response.php @@ -70,13 +70,10 @@ class Tests_Ajax_Response extends WP_UnitTestCase { * @runInSeparateProcess * @preserveGlobalState disabled * @group xdebug + * @requires function xdebug_get_headers */ public function test_response_charset_in_header() { - if ( ! function_exists( 'xdebug_get_headers' ) ) { - $this->markTestSkipped( 'xdebug is required for this test' ); - } - // Generate an Ajax response. ob_start(); $ajax_response = new WP_Ajax_Response(); diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 6099901aed..7baf331662 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -942,12 +942,9 @@ class Tests_Functions extends WP_UnitTestCase { /** * @ticket 28786 + * @requires function mb_detect_order */ function test_wp_json_encode_non_utf8() { - if ( ! function_exists( 'mb_detect_order' ) ) { - $this->markTestSkipped( 'mbstring extension not available.' ); - } - $charsets = mb_detect_order(); $old_charsets = $charsets; if ( ! in_array( 'EUC-JP', $charsets, true ) ) { @@ -967,12 +964,9 @@ class Tests_Functions extends WP_UnitTestCase { /** * @ticket 28786 + * @requires function mb_detect_order */ function test_wp_json_encode_non_utf8_in_array() { - if ( ! function_exists( 'mb_detect_order' ) ) { - $this->markTestSkipped( 'mbstring extension not available.' ); - } - $charsets = mb_detect_order(); $old_charsets = $charsets; if ( ! in_array( 'EUC-JP', $charsets, true ) ) { @@ -1210,24 +1204,18 @@ class Tests_Functions extends WP_UnitTestCase { /** * @ticket 39550 * @dataProvider _wp_check_filetype_and_ext_data + * @requires extension fileinfo */ function test_wp_check_filetype_and_ext( $file, $filename, $expected ) { - if ( ! extension_loaded( 'fileinfo' ) ) { - $this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' ); - } - $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); } /** * @ticket 39550 * @group ms-excluded + * @requires extension fileinfo */ function test_wp_check_filetype_and_ext_with_filtered_svg() { - if ( ! extension_loaded( 'fileinfo' ) ) { - $this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' ); - } - $file = DIR_TESTDATA . '/uploads/video-play.svg'; $filename = 'video-play.svg'; @@ -1247,12 +1235,9 @@ class Tests_Functions extends WP_UnitTestCase { /** * @ticket 39550 * @group ms-excluded + * @requires extension fileinfo */ function test_wp_check_filetype_and_ext_with_filtered_woff() { - if ( ! extension_loaded( 'fileinfo' ) ) { - $this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' ); - } - $file = DIR_TESTDATA . '/uploads/dashicons.woff'; $filename = 'dashicons.woff'; diff --git a/tests/phpunit/tests/functions/anonymization.php b/tests/phpunit/tests/functions/anonymization.php index 4d84383828..d8a941da34 100644 --- a/tests/phpunit/tests/functions/anonymization.php +++ b/tests/phpunit/tests/functions/anonymization.php @@ -25,15 +25,13 @@ class Tests_Functions_Anonymization extends WP_UnitTestCase { * * @ticket 41083 * @ticket 43545 + * @requires function inet_ntop + * @requires function inet_pton * * @param string $raw_ip Raw IP address. * @param string $expected_result Expected result. */ public function test_wp_privacy_anonymize_ip( $raw_ip, $expected_result ) { - if ( ! function_exists( 'inet_ntop' ) || ! function_exists( 'inet_pton' ) ) { - $this->markTestSkipped( 'This test requires both the inet_ntop() and inet_pton() functions.' ); - } - $actual_result = wp_privacy_anonymize_data( 'ip', $raw_ip ); /* Todo test ipv6_fallback mode if keeping it.*/ diff --git a/tests/phpunit/tests/http/functions.php b/tests/phpunit/tests/http/functions.php index d28936fde6..a01c032051 100644 --- a/tests/phpunit/tests/http/functions.php +++ b/tests/phpunit/tests/http/functions.php @@ -3,17 +3,10 @@ /** * @group http * @group external-http + * @requires extension openssl */ class Tests_HTTP_Functions extends WP_UnitTestCase { - public function setUp() { - if ( ! extension_loaded( 'openssl' ) ) { - $this->markTestSkipped( 'Tests_HTTP_Functions requires openssl.' ); - } - - parent::setUp(); - } - function test_head_request() { // This URL gives a direct 200 response. $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; diff --git a/tests/phpunit/tests/image/editorImagick.php b/tests/phpunit/tests/image/editorImagick.php index a358c654af..261f37acf2 100644 --- a/tests/phpunit/tests/image/editorImagick.php +++ b/tests/phpunit/tests/image/editorImagick.php @@ -548,12 +548,9 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { * Test resetting Exif orientation data on rotate * * @ticket 37140 + * @requires function exif_read_data */ public function test_remove_orientation_data_on_rotate() { - if ( ! function_exists( 'exif_read_data' ) ) { - $this->markTestSkipped( 'This test requires the exif_read_data function.' ); - } - $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; $data = wp_read_image_metadata( $file ); diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 3493c81913..116acd4ab4 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -149,12 +149,9 @@ class Tests_Image_Functions extends WP_UnitTestCase { * Test save image file and mime_types * * @ticket 6821 + * @requires extension fileinfo */ public function test_wp_save_image_file() { - if ( ! extension_loaded( 'fileinfo' ) ) { - $this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' ); - } - $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); foreach ( $classes as $key => $class ) { @@ -208,12 +205,9 @@ class Tests_Image_Functions extends WP_UnitTestCase { * Test that a passed mime type overrides the extension in the filename * * @ticket 6821 + * @requires extension fileinfo */ public function test_mime_overrides_filename() { - if ( ! extension_loaded( 'fileinfo' ) ) { - $this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' ); - } - $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); foreach ( $classes as $key => $class ) { @@ -253,12 +247,9 @@ class Tests_Image_Functions extends WP_UnitTestCase { * Test that mime types are correctly inferred from file extensions * * @ticket 6821 + * @requires extension fileinfo */ public function test_inferred_mime_types() { - if ( ! extension_loaded( 'fileinfo' ) ) { - $this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' ); - } - $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); foreach ( $classes as $key => $class ) { @@ -373,12 +364,9 @@ class Tests_Image_Functions extends WP_UnitTestCase { /** * @requires function imagejpeg + * @requires extension openssl */ public function test_wp_crop_image_url() { - if ( ! extension_loaded( 'openssl' ) ) { - $this->markTestSkipped( 'Tests_Image_Functions::test_wp_crop_image_url() requires openssl.' ); - } - $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg', 0, @@ -418,11 +406,10 @@ class Tests_Image_Functions extends WP_UnitTestCase { $this->assertInstanceOf( 'WP_Error', $file ); } + /** + * @requires extension openssl + */ public function test_wp_crop_image_url_not_exist() { - if ( ! extension_loaded( 'openssl' ) ) { - $this->markTestSkipped( 'Tests_Image_Functions::test_wp_crop_image_url_not_exist() requires openssl.' ); - } - $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canoladoesnotexist.jpg', 0, diff --git a/tests/phpunit/tests/image/meta.php b/tests/phpunit/tests/image/meta.php index 13f1b5abbd..2e0f2fabd0 100644 --- a/tests/phpunit/tests/image/meta.php +++ b/tests/phpunit/tests/image/meta.php @@ -4,18 +4,10 @@ * @group image * @group media * @group upload + * @requires extension gd + * @requires extension exif */ class Tests_Image_Meta extends WP_UnitTestCase { - function setUp() { - if ( ! extension_loaded( 'gd' ) ) { - $this->markTestSkipped( 'The gd PHP extension is not loaded.' ); - } - if ( ! extension_loaded( 'exif' ) ) { - $this->markTestSkipped( 'The exif PHP extension is not loaded.' ); - } - - parent::setUp(); - } function test_exif_d70() { // Exif from a Nikon D70. diff --git a/tests/phpunit/tests/oembed/headers.php b/tests/phpunit/tests/oembed/headers.php index adc3ff93c2..6f2854a04e 100644 --- a/tests/phpunit/tests/oembed/headers.php +++ b/tests/phpunit/tests/oembed/headers.php @@ -8,11 +8,11 @@ * @group xdebug */ class Tests_oEmbed_HTTP_Headers extends WP_UnitTestCase { - function test_rest_pre_serve_request_headers() { - if ( ! function_exists( 'xdebug_get_headers' ) ) { - $this->markTestSkipped( 'xdebug is required for this test' ); - } + /** + * @requires function xdebug_get_headers + */ + function test_rest_pre_serve_request_headers() { $post = $this->factory()->post->create_and_get( array( 'post_title' => 'Hello World',