Changeset 48454 for trunk/tests/phpunit/tests/media.php
- Timestamp:
- 07/12/2020 09:18:38 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/media.php
r48331 r48454 8 8 protected static $large_id; 9 9 protected static $_sizes; 10 protected static $large_filename = 'test-image-large.jpg'; 10 11 11 12 public static function wpSetUpBeforeClass( $factory ) { … … 13 14 $GLOBALS['_wp_additional_image_sizes'] = array(); 14 15 15 $filename = DIR_TESTDATA . '/images/ test-image-large.png';16 $filename = DIR_TESTDATA . '/images/' . self::$large_filename; 16 17 self::$large_id = $factory->attachment->create_upload_object( $filename ); 17 18 } … … 1333 1334 function test_wp_get_attachment_image_defaults() { 1334 1335 $image = image_downsize( self::$large_id, 'thumbnail' ); 1335 $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />', $image[1], $image[2], $image[0] ); 1336 $expected = sprintf( 1337 '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />', 1338 $image[1], 1339 $image[2], 1340 $image[0] 1341 ); 1336 1342 1337 1343 $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); … … 1348 1354 1349 1355 $image = image_downsize( self::$large_id, 'thumbnail' ); 1350 $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />', $image[1], $image[2], $image[0] ); 1356 $expected = sprintf( 1357 '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />', 1358 $image[1], 1359 $image[2], 1360 $image[0] 1361 ); 1351 1362 1352 1363 $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); … … 1480 1491 } 1481 1492 1482 // Add the full size width at the end. 1483 $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w'; 1493 $expected = trim( $expected, ' ,' ); 1484 1494 1485 1495 foreach ( $intermediates as $int ) { 1486 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 1487 $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); 1488 $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); 1496 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 1497 $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); 1498 1499 if ( 'full' === $int ) { 1500 // Add the full size image. Expected to be in the srcset when the full size image is used as src. 1501 $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected; 1502 } else { 1503 $_expected = $expected; 1504 } 1505 1506 $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] ); 1489 1507 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 1490 1508 } … … 1501 1519 1502 1520 // Make an image. 1503 $filename = DIR_TESTDATA . '/images/ test-image-large.png';1521 $filename = DIR_TESTDATA . '/images/' . self::$large_filename; 1504 1522 $id = self::factory()->attachment->create_upload_object( $filename ); 1505 1523 … … 1525 1543 } 1526 1544 1527 // Add the full size width at the end. 1528 $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w'; 1545 $expected = trim( $expected, ' ,' ); 1529 1546 1530 1547 foreach ( $intermediates as $int ) { 1531 $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); 1532 $image_url = wp_get_attachment_image_url( $id, $int ); 1533 $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); 1548 $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); 1549 $image_url = wp_get_attachment_image_url( $id, $int ); 1550 1551 if ( 'full' === $int ) { 1552 // Add the full size image. Expected to be in the srcset when the full size image is used as src. 1553 $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected; 1554 } else { 1555 $_expected = $expected; 1556 } 1557 1558 $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] ); 1534 1559 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 1535 1560 } … … 1553 1578 $hash = 'e' . time() . rand( 100, 999 ); 1554 1579 1555 $filename_base = wp_basename( $image_meta['file'], '.png' ); 1580 $filename_base = wp_basename( self::$large_filename, '.jpg' ); 1581 $filename_hash = "{$filename_base}-{$hash}"; 1556 1582 1557 1583 // Add the hash to the image URL. 1558 $image_url = str_replace( $filename_base, $filename_ base . '-' . $hash, $image_url );1584 $image_url = str_replace( $filename_base, $filename_hash, $image_url ); 1559 1585 1560 1586 // Replace file paths for full and medium sizes with hashed versions. 1561 $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] ); 1562 $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] ); 1563 $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] ); 1564 $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] ); 1587 $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium']['file'] ); 1588 $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium_large']['file'] ); 1589 $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['large']['file'] ); 1565 1590 1566 1591 // Calculate a srcset array. … … 1602 1627 } 1603 1628 1604 // Add the full size width at the end.1605 $ expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';1629 $expected = trim( $expected, ' ,' ); 1630 $full_size_file = $image_meta['file']; 1606 1631 1607 1632 // Prepend an absolute path to simulate a pre-2.7 upload. … … 1609 1634 1610 1635 foreach ( $intermediates as $int ) { 1611 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 1612 $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); 1613 $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); 1636 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 1637 $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int ); 1638 1639 if ( 'full' === $int ) { 1640 // Add the full size image. Expected to be in the srcset when the full size image is used as src. 1641 $_expected = $uploads_dir_url . $full_size_file . ' ' . $image_meta['width'] . 'w, ' . $expected; 1642 } else { 1643 $_expected = $expected; 1644 } 1645 1646 $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] ); 1614 1647 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 1615 1648 } … … 1682 1715 ); 1683 1716 1684 $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'; 1717 $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; 1718 1719 $expected_srcset = $uploads_url . 'test-768x1055-218x300.png 218w, ' . 1720 $uploads_url . 'test-768x1055-600x824.png 600w, ' . 1721 $uploads_url . 'test-768x1055.png 768w'; 1685 1722 1686 1723 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); … … 1727 1764 ); 1728 1765 1729 $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'; 1766 $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; 1767 1768 $expected_srcset = $uploads_url . 'test.png 2000w, ' . 1769 $uploads_url . 'test-300x150.png 300w, ' . 1770 $uploads_url . 'test-768x384.png 768w, ' . 1771 $uploads_url . 'test-1024x512.png 1024w'; 1730 1772 1731 1773 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); … … 1844 1886 ); 1845 1887 1846 $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'; 1888 $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; 1889 1890 $expected_srcset = $uploads_url . 'test%20image-300x150.png 300w, ' . 1891 $uploads_url . 'test%20image-768x384.png 768w, ' . 1892 $uploads_url . 'test%20image-1024x512.png 1024w'; 1847 1893 1848 1894 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( array( 300, 150 ), $image_src, $image_meta ) ); … … 1856 1902 1857 1903 $image_meta = wp_get_attachment_metadata( self::$large_id ); 1858 $size_array = array( 1600, 1200); // Full size.1904 $size_array = array( $image_meta['width'], $image_meta['height'] ); // Full size. 1859 1905 1860 1906 $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ); … … 2012 2058 %7$s'; 2013 2059 2014 $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 ); 2015 $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 ); 2060 $content_unfiltered = sprintf( 2061 $content, 2062 $img, 2063 $img_no_size_in_class, 2064 $img_no_width_height, 2065 $img_no_size_id, 2066 $img_with_sizes_attr, 2067 $img_xhtml, 2068 $img_html5 2069 ); 2070 2071 $content_filtered = sprintf( 2072 $content, 2073 $respimg, 2074 $respimg_no_size_in_class, 2075 $respimg_no_width_height, 2076 $img_no_size_id, 2077 $respimg_with_sizes_attr, 2078 $respimg_xhtml, 2079 $respimg_html5 2080 ); 2016 2081 2017 2082 // Do not add width, height, and loading. … … 2135 2200 %3$s'; 2136 2201 2137 $unfiltered = sprintf( $content, $img, $img_https, $img_relative ); 2138 $expected = sprintf( $content, $respimg, $respimg_https, $respimg_relative ); 2139 $actual = wp_filter_content_tags( $unfiltered ); 2202 $unfiltered = sprintf( 2203 $content, 2204 $img, 2205 $img_https, 2206 $img_relative 2207 ); 2208 2209 $expected = sprintf( 2210 $content, 2211 $respimg, 2212 $respimg_https, 2213 $respimg_relative 2214 ); 2215 2216 $actual = wp_filter_content_tags( $unfiltered ); 2140 2217 2141 2218 $this->assertSame( $expected, $actual ); … … 2177 2254 $_SERVER['HTTPS'] = 'on'; 2178 2255 2179 $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'; 2180 $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta ); 2256 $uploads_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; 2257 2258 $expected = $uploads_url . 'test-1024x512.jpg 1024w, ' . 2259 $uploads_url . 'test-300x150.jpg 300w, ' . 2260 $uploads_url . 'test.jpg 1200w'; 2261 2262 $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta ); 2181 2263 2182 2264 $this->assertSame( $expected, $actual ); … … 2266 2348 remove_all_filters( 'wp_calculate_image_sizes' ); 2267 2349 2350 $basename = wp_basename( self::$large_filename, '.jpg' ); 2351 $year_month = gmdate( 'Y/m' ); 2352 $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/'; 2353 2354 $expected = '<img width="999" height="999" ' . 2355 'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' . 2356 'class="attachment-testsize size-testsize" alt="" loading="lazy" ' . 2357 'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' . 2358 'sizes="(max-width: 999px) 100vw, 999px" />'; 2359 2268 2360 $actual = wp_get_attachment_image( self::$large_id, 'testsize' ); 2269 $year = gmdate( 'Y' );2270 $month = gmdate( 'm' );2271 2272 $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' .2273 ' class="attachment-testsize size-testsize" alt="" loading="lazy"' .2274 ' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' .2275 ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' .2276 ' sizes="(max-width: 999px) 100vw, 999px" />';2277 2361 2278 2362 remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) ); … … 2283 2367 function _filter_36246( $data, $attachment_id ) { 2284 2368 $data['sizes']['testsize'] = array( 2285 'file' => 'test-image-testsize-999x999. png',2369 'file' => 'test-image-testsize-999x999.jpg', 2286 2370 'width' => 999, 2287 2371 'height' => 999, 2288 'mime-type' => 'image/ png',2372 'mime-type' => 'image/jpg', 2289 2373 ); 2290 2374 return $data; … … 2853 2937 $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); 2854 2938 } 2939 2940 /** 2941 * @ticket 50543 2942 */ 2943 function test_wp_image_file_matches_image_meta_original_image() { 2944 $image_meta = wp_get_attachment_metadata( self::$large_id ); 2945 $image_src = wp_get_original_image_url( self::$large_id ); 2946 2947 $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); 2948 } 2855 2949 } 2856 2950
Note: See TracChangeset
for help on using the changeset viewer.