Make WordPress Core


Ignore:
Timestamp:
07/05/2020 11:30:36 PM (6 years ago)
Author:
azaozz
Message:

Media:

  • Introduce wp_image_file_matches_image_meta() utility function to check whether the image meta (retrieved by attachment ID) matches an image path or URI. A mismatch may happen in some cases, for example after the posts have been exported from one website and imported in another.
  • Add unit tests for the new function.
  • Improve wp_image_src_get_dimensions() a bit and use the new function to prevent these edge cases.

Fixes #50543.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r48275 r48329  
    28092809                );
    28102810        }
     2811
     2812        /**
     2813         * @ticket 50543
     2814         */
     2815        function test_wp_image_file_matches_image_meta() {
     2816                $image_meta       = wp_get_attachment_metadata( self::$large_id );
     2817                $image_src_full   = wp_get_attachment_image_url( self::$large_id, 'full' );
     2818                $image_src_medium = wp_get_attachment_image_url( self::$large_id, 'medium' );
     2819
     2820                $this->assertTrue( wp_image_file_matches_image_meta( $image_src_full, $image_meta ) );
     2821                $this->assertTrue( wp_image_file_matches_image_meta( $image_src_medium, $image_meta ) );
     2822        }
     2823
     2824        /**
     2825         * @ticket 50543
     2826         */
     2827        function test_wp_image_file_matches_image_meta_no_subsizes() {
     2828                $image_meta = wp_get_attachment_metadata( self::$large_id );
     2829                $image_src  = wp_get_attachment_image_url( self::$large_id, 'full' );
     2830                $image_meta['sizes'] = array();
     2831
     2832                $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2833        }
     2834
     2835        /**
     2836         * @ticket 50543
     2837         */
     2838        function test_wp_image_file_matches_image_meta_invalid_meta() {
     2839                $image_meta = ''; // Attachment is not an image.
     2840                $image_src  = $this->img_url;
     2841
     2842                $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2843        }
     2844
     2845        /**
     2846         * @ticket 50543
     2847         */
     2848        function test_wp_image_file_matches_image_meta_different_meta() {
     2849                $image_meta = wp_get_attachment_metadata( self::$large_id );
     2850                $image_src  = $this->img_url; // Different image.
     2851
     2852                $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2853        }
    28112854}
    28122855
Note: See TracChangeset for help on using the changeset viewer.