From 462560edbaa73b168849dc1ccc4eb337975ac7b9 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 5 May 2021 17:06:17 +0000 Subject: [PATCH] Media: Some documentation and test improvements for WebP support: * Document that WebP constants are only defined in PHP 7.1+. * Correct the `$filename` parameter type in `wp_get_webp_info()`. * Use a consistent message when skipping tests due to the lack of WebP support. * Remove unnecessary `else` branches after `markTestSkipped()`. * Replace `assertEquals()` with more appropriate assertions. Follow-up to [50810]. See #35725. git-svn-id: https://develop.svn.wordpress.org/trunk@50814 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/compat.php | 2 +- src/wp-includes/functions.php | 8 +++++--- src/wp-includes/media.php | 14 ++++++++++---- tests/phpunit/tests/functions.php | 10 +++++----- tests/phpunit/tests/image/editor.php | 11 ++++++----- tests/phpunit/tests/image/functions.php | 3 ++- tests/phpunit/tests/image/resize.php | 18 +++++++++--------- 7 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php index 59e5700439..aec742d77a 100644 --- a/src/wp-includes/compat.php +++ b/src/wp-includes/compat.php @@ -371,7 +371,7 @@ if ( ! function_exists( 'is_iterable' ) ) { } } -// WebP constants may not be defined, even in cases where the format is supported. +// WebP constants are only defined in PHP 7.1+. if ( ! defined( 'IMAGETYPE_WEBP' ) ) { define( 'IMAGETYPE_WEBP', 18 ); } diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 3bb257ab8b..d8e90d82c7 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3079,9 +3079,11 @@ function wp_get_image_mime( $file ) { return false; } - // Add WebP fallback detection when image library doesn't support WebP. - // Note: detection values come from LibWebP, see - // https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30 + /* + * Add WebP fallback detection when image library doesn't support WebP. + * Note: detection values come from LibWebP, see + * https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30 + */ $magic = bin2hex( $magic ); if ( // RIFF. diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 82d1b32627..100fe5dfab 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5024,7 +5024,7 @@ function wp_getimagesize( $filename, array &$image_info = null ) { * * @since 5.8.0 * - * @param [type] $filename Path to a WebP file. + * @param string $filename Path to a WebP file. * @return array $webp_info { * An array of WebP image information. * @@ -5038,9 +5038,11 @@ function wp_get_webp_info( $filename ) { $width = false; $height = false; $type = false; + if ( ! 'image/webp' === wp_get_image_mime( $filename ) ) { return compact( 'width', 'height', 'type' ); } + try { $handle = fopen( $filename, 'rb' ); if ( $handle ) { @@ -5083,6 +5085,7 @@ function wp_get_webp_info( $filename ) { } } catch ( Exception $e ) { } + return compact( 'width', 'height', 'type' ); } @@ -5097,6 +5100,7 @@ function wp_get_webp_info( $filename ) { function _wp_webp_is_lossy( $filename ) { $webp_info = wp_get_webp_info( $filename ); $type = $webp_info['type']; + return $type && 'lossy' === $type; } @@ -5113,17 +5117,19 @@ function _wp_webp_is_lossy( $filename ) { function _wp_get_image_size( $filename, &$imageinfo = array() ) { // Try getimagesize() first. $info = getimagesize( $filename, $imageinfo ); + if ( false !== $info ) { return $info; } - // For PHP versions that don't support WebP images, extract the image - // size info from the file headers. + + // For PHP versions that don't support WebP images, + // extract the image size info from the file headers. if ( 'image/webp' === wp_get_image_mime( $filename ) ) { $webp_info = wp_get_webp_info( $filename ); $width = $webp_info['width']; $height = $webp_info['height']; - // Mimic the native return format. + // Mimic the native return format. if ( $width && $height ) { return array( $width, diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index a088db940e..d3c7abbef0 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -1240,11 +1240,11 @@ class Tests_Functions extends WP_UnitTestCase { // let's restrict comparison to expected keys only. if ( is_array( $expected ) ) { foreach ( $expected as $k => $v ) { - $this->assertEquals( true, isset( $result[ $k ] ) ); - $this->assertEquals( $expected[ $k ], $result[ $k ] ); + $this->assertArrayHasKey( $k, $result ); + $this->assertSame( $expected[ $k ], $result[ $k ] ); } } else { - $this->assertEquals( $expected, $result ); + $this->assertSame( $expected, $result ); } } @@ -1312,7 +1312,7 @@ class Tests_Functions extends WP_UnitTestCase { } /** - * Data provider for test_wp_get_image_mime(); + * Data provider for test_wp_get_image_mime(). */ public function _wp_get_image_mime() { $data = array( @@ -1367,7 +1367,7 @@ class Tests_Functions extends WP_UnitTestCase { } /** - * Data profider for test_wp_getimagesize(); + * Data profider for test_wp_getimagesize(). */ public function data_wp_getimagesize() { $data = array( diff --git a/tests/phpunit/tests/image/editor.php b/tests/phpunit/tests/image/editor.php index dce95ee836..ab6fe9f666 100644 --- a/tests/phpunit/tests/image/editor.php +++ b/tests/phpunit/tests/image/editor.php @@ -205,16 +205,17 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase { */ public function test_wp_get_webp_info( $file, $expected ) { $editor = wp_get_image_editor( $file ); + if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) { - $this->markTestSkipped( sprintf( 'Skipping test: no WebP support in the editor engine %s on this system.', $this->editor_engine ) ); - } else { - $file_data = wp_get_webp_info( $file ); - $this->assertSame( $file_data, $expected ); + $this->markTestSkipped( sprintf( 'No WebP support in the editor engine %s on this system.', $this->editor_engine ) ); } + + $file_data = wp_get_webp_info( $file ); + $this->assertSame( $file_data, $expected ); } /** - * Data provider for test_wp_get_webp_info(); + * Data provider for test_wp_get_webp_info(). */ public function _test_wp_get_webp_info() { return array( diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 0658e45117..30459c5e77 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -99,7 +99,8 @@ class Tests_Image_Functions extends WP_UnitTestCase { // Add WebP images if the image editor supports them. $file = DIR_TESTDATA . '/images/test-image.webp'; $editor = wp_get_image_editor( $file ); - if ( ( ! is_wp_error( $editor ) ) && $editor->supports_mime_type( 'image/webp' ) ) { + + if ( ! is_wp_error( $editor ) && $editor->supports_mime_type( 'image/webp' ) ) { $files = array_merge( $files, array( diff --git a/tests/phpunit/tests/image/resize.php b/tests/phpunit/tests/image/resize.php index 517ab45389..afe7aa05c0 100644 --- a/tests/phpunit/tests/image/resize.php +++ b/tests/phpunit/tests/image/resize.php @@ -70,16 +70,16 @@ abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase // Check if the editor supports the webp mime type. if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) { - $this->markTestSkipped( sprintf( 'Skipping test: no WebP support in the editor engine %s on this system.', $this->editor_engine ) ); - } else { - $image = $this->resize_helper( $file, 25, 25 ); - $this->assertSame( 'test-image-25x25.webp', wp_basename( $image ) ); - list($w, $h, $type) = wp_getimagesize( $image ); - $this->assertSame( 25, $w ); - $this->assertSame( 25, $h ); - $this->assertSame( IMAGETYPE_WEBP, $type ); - unlink( $image ); + $this->markTestSkipped( sprintf( 'No WebP support in the editor engine %s on this system.', $this->editor_engine ) ); } + + $image = $this->resize_helper( $file, 25, 25 ); + $this->assertSame( 'test-image-25x25.webp', wp_basename( $image ) ); + list($w, $h, $type) = wp_getimagesize( $image ); + $this->assertSame( 25, $w ); + $this->assertSame( 25, $h ); + $this->assertSame( IMAGETYPE_WEBP, $type ); + unlink( $image ); } function test_resize_larger() {