From dd50193d6aab0b8c92d3ee3124d9839af2bed11a Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sun, 12 Jul 2020 21:18:38 +0000 Subject: [PATCH] Media: Fix `wp_image_file_matches_image_meta()` to also match when the original image URL is used. Also refreshes and fixes some of the unit tests for media, and replaces test-image-large.png with test-image-large.jpg which is larger to allow testing of `original_image` in the image meta. Fixes #50628. git-svn-id: https://develop.svn.wordpress.org/trunk@48454 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 18 +- .../phpunit/data/images/test-image-large.jpg | Bin 0 -> 29575 bytes .../phpunit/data/images/test-image-large.png | Bin 4409 -> 0 bytes tests/phpunit/tests/image/functions.php | 5 + tests/phpunit/tests/media.php | 186 +++++++++++++----- 5 files changed, 159 insertions(+), 50 deletions(-) create mode 100644 tests/phpunit/data/images/test-image-large.jpg delete mode 100644 tests/phpunit/data/images/test-image-large.png diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index d316f1a6f3..4f1eed831f 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1522,7 +1522,7 @@ function wp_image_file_matches_image_meta( $image_location, $image_meta ) { // Check if the relative image path from the image meta is at the end of $image_location. if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) { $match = true; - } elseif ( ! empty( $image_meta['sizes'] ) ) { + } else { // Retrieve the uploads sub-directory from the full size image. $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); @@ -1530,12 +1530,22 @@ function wp_image_file_matches_image_meta( $image_location, $image_meta ) { $dirname = trailingslashit( $dirname ); } - foreach ( $image_meta['sizes'] as $image_size_data ) { - $relative_path = $dirname . $image_size_data['file']; + if ( ! empty( $image_meta['original_image'] ) ) { + $relative_path = $dirname . $image_meta['original_image']; if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) { $match = true; - break; + } + } + + if ( ! $match && ! empty( $image_meta['sizes'] ) ) { + foreach ( $image_meta['sizes'] as $image_size_data ) { + $relative_path = $dirname . $image_size_data['file']; + + if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) { + $match = true; + break; + } } } } diff --git a/tests/phpunit/data/images/test-image-large.jpg b/tests/phpunit/data/images/test-image-large.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1d0a16298b9a7e20b851fe4bb90837ee4c1bab8d GIT binary patch literal 29575 zcmeI44K&nQ8^G^>X3WfMGG-)Fvy3sR(1%t?Gt40LA)ynym72EImtOg3O*Bj>ZM{VF ztq+YpY(+MeiYBR6)UK31g%PbB$tbIkdH*xaNVRsRbM~C~?0f%zXYMoiKF@RS^SjUe zJIDV#WDjMvfHK2vx*I?c1bCn~kUaq|K$}1y67bqYB9Wx6O(GAckacv(Ukx*$4mUFy z#blT;OwBFrCYX=48EeXz$hEeaGS$)1anyL%=`Jh}JGSFg*;POy5=%+>2toz2YhVOG z09HOl|ApYRFj&0oF(4|skxowk7?dCZNB|Om1Rw!O01|)%AOT1K5`Y9C0Z0H6fCL}` zNB|Om1Rw!OU~mHVC(v=vgPg&&J{(0{ZUKM^7k=0`xQq`%$KBF3urNA(i4HJnHkpGv z9x!@)Q(6Kmq8y;DMl)5U1Y`u3Bl~p54sbX4m#=w8MjJ_@?$I7J-3ZcjnLC=a_Hp&? zbAxS1_zXVHfYHF>8D1l}D@*2Pe}Sb(Mp0!?{4(e31rJxCuW{79P%LA@w-iJyJ0#8Y zKd&*PfV1K?m{UtMpVXq=?>%|dyY`pOqWUkH_e4e47S^_?J>3T5o)^#Hs2eT-Lmsa1 z`)T1H8P7?^teszC8x?sEe9@#{3joRBHSY*r{l(&gY)(`H@CE5mIOl(UvH}YLzB#LD z%4KGifT6)BHBEB#>>Hh~NLC*`*4B7C0IWof9vX`QKs4E3t#7XYVBDepssJ{L1Arz^ za}9h$RY(95fCL}`NB|Om1Rw!O01|)%AOT1K5`Y9C0Z0H6fCL}`NZ=nyzyvK-GN%9p zhe3bPd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_jw%(KA-W3&J_6&vOj8ROn^6rSHEQtS_LNH>_9y;= zKQ(NkX21gS@|@1V4tV6Pq?D|etYP#ahpMdzN;VXaC=pmw& XqTo%cOVa3lkcT67F1E$roW1c+%XkZs diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 4a7713d52b..838f14c5a1 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -345,6 +345,11 @@ class Tests_Image_Functions extends WP_UnitTestCase { false, DIR_TESTDATA . '/images/' . __FUNCTION__ . '.jpg' ); + + if ( is_wp_error( $file ) && $file->get_error_code() === 'invalid_image' ) { + $this->markTestSkipped( 'Tests_Image_Functions::test_wp_crop_image_url() cannot access remote image.' ); + } + $this->assertNotWPError( $file ); $this->assertFileExists( $file ); $image = wp_get_image_editor( $file ); diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 216fc6a00a..14794a90e1 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -7,12 +7,13 @@ class Tests_Media extends WP_UnitTestCase { protected static $large_id; protected static $_sizes; + protected static $large_filename = 'test-image-large.jpg'; public static function wpSetUpBeforeClass( $factory ) { self::$_sizes = wp_get_additional_image_sizes(); $GLOBALS['_wp_additional_image_sizes'] = array(); - $filename = DIR_TESTDATA . '/images/test-image-large.png'; + $filename = DIR_TESTDATA . '/images/' . self::$large_filename; self::$large_id = $factory->attachment->create_upload_object( $filename ); } @@ -1332,7 +1333,12 @@ EOF; */ function test_wp_get_attachment_image_defaults() { $image = image_downsize( self::$large_id, 'thumbnail' ); - $expected = sprintf( '', $image[1], $image[2], $image[0] ); + $expected = sprintf( + '', + $image[1], + $image[2], + $image[0] + ); $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); } @@ -1347,7 +1353,12 @@ EOF; update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true ); $image = image_downsize( self::$large_id, 'thumbnail' ); - $expected = sprintf( 'Some very clever alt text', $image[1], $image[2], $image[0] ); + $expected = sprintf( + 'Some very clever alt text', + $image[1], + $image[2], + $image[0] + ); $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); @@ -1479,13 +1490,20 @@ EOF; } } - // Add the full size width at the end. - $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w'; + $expected = trim( $expected, ' ,' ); foreach ( $intermediates as $int ) { - $image_url = wp_get_attachment_image_url( self::$large_id, $int ); - $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); - $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); + $image_url = wp_get_attachment_image_url( self::$large_id, $int ); + $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); + + if ( 'full' === $int ) { + // Add the full size image. Expected to be in the srcset when the full size image is used as src. + $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected; + } else { + $_expected = $expected; + } + + $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] ); $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); } } @@ -1500,7 +1518,7 @@ EOF; add_filter( 'upload_dir', '_upload_dir_no_subdir' ); // Make an image. - $filename = DIR_TESTDATA . '/images/test-image-large.png'; + $filename = DIR_TESTDATA . '/images/' . self::$large_filename; $id = self::factory()->attachment->create_upload_object( $filename ); $image_meta = wp_get_attachment_metadata( $id ); @@ -1524,13 +1542,20 @@ EOF; } } - // Add the full size width at the end. - $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w'; + $expected = trim( $expected, ' ,' ); foreach ( $intermediates as $int ) { - $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); - $image_url = wp_get_attachment_image_url( $id, $int ); - $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); + $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); + $image_url = wp_get_attachment_image_url( $id, $int ); + + if ( 'full' === $int ) { + // Add the full size image. Expected to be in the srcset when the full size image is used as src. + $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected; + } else { + $_expected = $expected; + } + + $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] ); $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); } @@ -1552,16 +1577,16 @@ EOF; // Copy hash generation method used in wp_save_image(). $hash = 'e' . time() . rand( 100, 999 ); - $filename_base = wp_basename( $image_meta['file'], '.png' ); + $filename_base = wp_basename( self::$large_filename, '.jpg' ); + $filename_hash = "{$filename_base}-{$hash}"; // Add the hash to the image URL. - $image_url = str_replace( $filename_base, $filename_base . '-' . $hash, $image_url ); + $image_url = str_replace( $filename_base, $filename_hash, $image_url ); // Replace file paths for full and medium sizes with hashed versions. - $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] ); - $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] ); - $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] ); - $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] ); + $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium']['file'] ); + $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium_large']['file'] ); + $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['large']['file'] ); // Calculate a srcset array. $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); @@ -1601,16 +1626,24 @@ EOF; } } - // Add the full size width at the end. - $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w'; + $expected = trim( $expected, ' ,' ); + $full_size_file = $image_meta['file']; // Prepend an absolute path to simulate a pre-2.7 upload. $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file']; foreach ( $intermediates as $int ) { - $image_url = wp_get_attachment_image_url( self::$large_id, $int ); - $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); - $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); + $image_url = wp_get_attachment_image_url( self::$large_id, $int ); + $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); + + if ( 'full' === $int ) { + // Add the full size image. Expected to be in the srcset when the full size image is used as src. + $_expected = $uploads_dir_url . $full_size_file . ' ' . $image_meta['width'] . 'w, ' . $expected; + } else { + $_expected = $expected; + } + + $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] ); $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); } } @@ -1681,7 +1714,11 @@ EOF; ), ); - $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png 218w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-600x824.png 600w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055.png 768w'; + $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; + + $expected_srcset = $uploads_url . 'test-768x1055-218x300.png 218w, ' . + $uploads_url . 'test-768x1055-600x824.png 600w, ' . + $uploads_url . 'test-768x1055.png 768w'; $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); } @@ -1726,7 +1763,12 @@ EOF; ), ); - $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 2000w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w'; + $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; + + $expected_srcset = $uploads_url . 'test.png 2000w, ' . + $uploads_url . 'test-300x150.png 300w, ' . + $uploads_url . 'test-768x384.png 768w, ' . + $uploads_url . 'test-1024x512.png 1024w'; $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); } @@ -1843,7 +1885,11 @@ EOF; ), ); - $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-1024x512.png 1024w'; + $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; + + $expected_srcset = $uploads_url . 'test%20image-300x150.png 300w, ' . + $uploads_url . 'test%20image-768x384.png 768w, ' . + $uploads_url . 'test%20image-1024x512.png 1024w'; $this->assertSame( $expected_srcset, wp_calculate_image_srcset( array( 300, 150 ), $image_src, $image_meta ) ); } @@ -1855,7 +1901,7 @@ EOF; $_wp_additional_image_sizes = wp_get_additional_image_sizes(); $image_meta = wp_get_attachment_metadata( self::$large_id ); - $size_array = array( 1600, 1200 ); // Full size. + $size_array = array( $image_meta['width'], $image_meta['height'] ); // Full size. $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ); @@ -2011,8 +2057,27 @@ EOF;

Image, HTML 5.0 style. Should have srcset and sizes.

%7$s'; - $content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr, $img_xhtml, $img_html5 ); - $content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr, $respimg_xhtml, $respimg_html5 ); + $content_unfiltered = sprintf( + $content, + $img, + $img_no_size_in_class, + $img_no_width_height, + $img_no_size_id, + $img_with_sizes_attr, + $img_xhtml, + $img_html5 + ); + + $content_filtered = sprintf( + $content, + $respimg, + $respimg_no_size_in_class, + $respimg_no_width_height, + $img_no_size_id, + $respimg_with_sizes_attr, + $respimg_xhtml, + $respimg_html5 + ); // Do not add width, height, and loading. add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); @@ -2134,9 +2199,21 @@ EOF;

Image, protocol-relative. Should have srcset and sizes.

%3$s'; - $unfiltered = sprintf( $content, $img, $img_https, $img_relative ); - $expected = sprintf( $content, $respimg, $respimg_https, $respimg_relative ); - $actual = wp_filter_content_tags( $unfiltered ); + $unfiltered = sprintf( + $content, + $img, + $img_https, + $img_relative + ); + + $expected = sprintf( + $content, + $respimg, + $respimg_https, + $respimg_relative + ); + + $actual = wp_filter_content_tags( $unfiltered ); $this->assertSame( $expected, $actual ); } @@ -2176,8 +2253,13 @@ EOF; $_SERVER['HTTPS'] = 'on'; - $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w'; - $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta ); + $uploads_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; + + $expected = $uploads_url . 'test-1024x512.jpg 1024w, ' . + $uploads_url . 'test-300x150.jpg 300w, ' . + $uploads_url . 'test.jpg 1200w'; + + $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta ); $this->assertSame( $expected, $actual ); } @@ -2265,15 +2347,17 @@ EOF; remove_all_filters( 'wp_calculate_image_sizes' ); - $actual = wp_get_attachment_image( self::$large_id, 'testsize' ); - $year = gmdate( 'Y' ); - $month = gmdate( 'm' ); + $basename = wp_basename( self::$large_filename, '.jpg' ); + $year_month = gmdate( 'Y/m' ); + $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/'; - $expected = ''; + $expected = ''; + + $actual = wp_get_attachment_image( self::$large_id, 'testsize' ); remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) ); @@ -2282,10 +2366,10 @@ EOF; function _filter_36246( $data, $attachment_id ) { $data['sizes']['testsize'] = array( - 'file' => 'test-image-testsize-999x999.png', + 'file' => 'test-image-testsize-999x999.jpg', 'width' => 999, 'height' => 999, - 'mime-type' => 'image/png', + 'mime-type' => 'image/jpg', ); return $data; } @@ -2852,6 +2936,16 @@ EOF; $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); } + + /** + * @ticket 50543 + */ + function test_wp_image_file_matches_image_meta_original_image() { + $image_meta = wp_get_attachment_metadata( self::$large_id ); + $image_src = wp_get_original_image_url( self::$large_id ); + + $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); + } } /**