Make WordPress Core

Changeset 50814


Ignore:
Timestamp:
05/05/2021 05:06:17 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r50810 r50814  
    372372}
    373373
    374 // WebP constants may not be defined, even in cases where the format is supported.
     374// WebP constants are only defined in PHP 7.1+.
    375375if ( ! defined( 'IMAGETYPE_WEBP' ) ) {
    376376    define( 'IMAGETYPE_WEBP', 18 );
  • trunk/src/wp-includes/functions.php

    r50810 r50814  
    30803080        }
    30813081
    3082         // Add WebP fallback detection when image library doesn't support WebP.
    3083         // Note: detection values come from LibWebP, see
    3084         // https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30
     3082        /*
     3083         * Add WebP fallback detection when image library doesn't support WebP.
     3084         * Note: detection values come from LibWebP, see
     3085         * https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30
     3086         */
    30853087        $magic = bin2hex( $magic );
    30863088        if (
  • trunk/src/wp-includes/media.php

    r50810 r50814  
    50255025 * @since 5.8.0
    50265026 *
    5027  * @param [type] $filename Path to a WebP file.
     5027 * @param string $filename Path to a WebP file.
    50285028 * @return array $webp_info {
    50295029 *     An array of WebP image information.
     
    50395039    $height = false;
    50405040    $type   = false;
     5041
    50415042    if ( ! 'image/webp' === wp_get_image_mime( $filename ) ) {
    50425043        return compact( 'width', 'height', 'type' );
    50435044    }
     5045
    50445046    try {
    50455047        $handle = fopen( $filename, 'rb' );
     
    50845086    } catch ( Exception $e ) {
    50855087    }
     5088
    50865089    return compact( 'width', 'height', 'type' );
    50875090}
     
    50985101    $webp_info = wp_get_webp_info( $filename );
    50995102    $type      = $webp_info['type'];
     5103
    51005104    return $type && 'lossy' === $type;
    51015105}
     
    51145118    // Try getimagesize() first.
    51155119    $info = getimagesize( $filename, $imageinfo );
     5120
    51165121    if ( false !== $info ) {
    51175122        return $info;
    51185123    }
    5119     // For PHP versions that don't support WebP images, extract the image
    5120     // size info from the file headers.
     5124
     5125    // For PHP versions that don't support WebP images,
     5126    // extract the image size info from the file headers.
    51215127    if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
    51225128        $webp_info = wp_get_webp_info( $filename );
     
    51245130        $height    = $webp_info['height'];
    51255131
    5126             // Mimic the native return format.
     5132        // Mimic the native return format.
    51275133        if ( $width && $height ) {
    51285134            return array(
  • trunk/tests/phpunit/tests/functions.php

    r50810 r50814  
    12411241        if ( is_array( $expected ) ) {
    12421242            foreach ( $expected as $k => $v ) {
    1243                 $this->assertEquals( true, isset( $result[ $k ] ) );
    1244                 $this->assertEquals( $expected[ $k ], $result[ $k ] );
     1243                $this->assertArrayHasKey( $k, $result );
     1244                $this->assertSame( $expected[ $k ], $result[ $k ] );
    12451245            }
    12461246        } else {
    1247             $this->assertEquals( $expected, $result );
     1247            $this->assertSame( $expected, $result );
    12481248        }
    12491249    }
     
    13131313
    13141314    /**
    1315      * Data provider for test_wp_get_image_mime();
     1315     * Data provider for test_wp_get_image_mime().
    13161316     */
    13171317    public function _wp_get_image_mime() {
     
    13681368
    13691369    /**
    1370      * Data profider for test_wp_getimagesize();
     1370     * Data profider for test_wp_getimagesize().
    13711371     */
    13721372    public function data_wp_getimagesize() {
  • trunk/tests/phpunit/tests/image/editor.php

    r50810 r50814  
    206206    public function test_wp_get_webp_info( $file, $expected ) {
    207207        $editor = wp_get_image_editor( $file );
     208
    208209        if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
    209             $this->markTestSkipped( sprintf( 'Skipping test: no WebP support in the editor engine %s on this system.', $this->editor_engine ) );
    210         } else {
    211             $file_data = wp_get_webp_info( $file );
    212             $this->assertSame( $file_data, $expected );
     210            $this->markTestSkipped( sprintf( 'No WebP support in the editor engine %s on this system.', $this->editor_engine ) );
    213211        }
    214     }
    215 
    216     /**
    217      * Data provider for test_wp_get_webp_info();
     212
     213        $file_data = wp_get_webp_info( $file );
     214        $this->assertSame( $file_data, $expected );
     215    }
     216
     217    /**
     218     * Data provider for test_wp_get_webp_info().
    218219     */
    219220    public function _test_wp_get_webp_info() {
  • trunk/tests/phpunit/tests/image/functions.php

    r50810 r50814  
    100100        $file   = DIR_TESTDATA . '/images/test-image.webp';
    101101        $editor = wp_get_image_editor( $file );
    102         if ( ( ! is_wp_error( $editor ) ) && $editor->supports_mime_type( 'image/webp' ) ) {
     102
     103        if ( ! is_wp_error( $editor ) && $editor->supports_mime_type( 'image/webp' ) ) {
    103104            $files = array_merge(
    104105                $files,
  • trunk/tests/phpunit/tests/image/resize.php

    r50810 r50814  
    7171        // Check if the editor supports the webp mime type.
    7272        if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
    73             $this->markTestSkipped( sprintf( 'Skipping test: no WebP support in the editor engine %s on this system.', $this->editor_engine ) );
    74         } else {
    75             $image = $this->resize_helper( $file, 25, 25 );
    76             $this->assertSame( 'test-image-25x25.webp', wp_basename( $image ) );
    77             list($w, $h, $type) = wp_getimagesize( $image );
    78             $this->assertSame( 25, $w );
    79             $this->assertSame( 25, $h );
    80             $this->assertSame( IMAGETYPE_WEBP, $type );
    81             unlink( $image );
    82         }
     73            $this->markTestSkipped( sprintf( 'No WebP support in the editor engine %s on this system.', $this->editor_engine ) );
     74        }
     75
     76        $image = $this->resize_helper( $file, 25, 25 );
     77        $this->assertSame( 'test-image-25x25.webp', wp_basename( $image ) );
     78        list($w, $h, $type) = wp_getimagesize( $image );
     79        $this->assertSame( 25, $w );
     80        $this->assertSame( 25, $h );
     81        $this->assertSame( IMAGETYPE_WEBP, $type );
     82        unlink( $image );
    8383    }
    8484
Note: See TracChangeset for help on using the changeset viewer.