Changeset 54085 for trunk/tests/phpunit/tests/image/functions.php
- Timestamp:
- 09/06/2022 09:11:41 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/image/functions.php
r53751 r54085 640 640 } 641 641 642 // Use legacy JPEG output.643 add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );644 645 642 $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf'; 646 643 $test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf'; … … 681 678 'mime-type' => 'image/jpeg', 682 679 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ), 683 'sources' => array(684 'image/jpeg' => array(685 'file' => 'wordpress-gsoc-flyer-pdf-232x300.jpg',686 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),687 ),688 ),689 680 ), 690 681 'large' => array( … … 694 685 'mime-type' => 'image/jpeg', 695 686 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 696 'sources' => array(697 'image/jpeg' => array(698 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',699 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),700 ),701 ),702 687 ), 703 688 'thumbnail' => array( … … 707 692 'mime-type' => 'image/jpeg', 708 693 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 709 'sources' => array(710 'image/jpeg' => array(711 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',712 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),713 ),714 ),715 694 ), 716 695 ), … … 724 703 unlink( $temp_dir . $size['file'] ); 725 704 } 726 remove_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );727 705 } 728 706 … … 738 716 739 717 update_option( 'medium_crop', 1 ); 740 741 // Use legacy JPEG output.742 add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );743 718 744 719 $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf'; … … 780 755 'mime-type' => 'image/jpeg', 781 756 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ), 782 'sources' => array(783 'image/jpeg' => array(784 'file' => 'wordpress-gsoc-flyer-pdf-300x300.jpg',785 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),786 ),787 ),788 757 ), 789 758 'large' => array( … … 793 762 'mime-type' => 'image/jpeg', 794 763 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 795 'sources' => array(796 'image/jpeg' => array(797 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',798 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),799 ),800 ),801 802 764 ), 803 765 'thumbnail' => array( … … 807 769 'mime-type' => 'image/jpeg', 808 770 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 809 'sources' => array(810 'image/jpeg' => array(811 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',812 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),813 ),814 ),815 771 ), 816 772 ), … … 824 780 unlink( $temp_dir . $size['file'] ); 825 781 } 826 remove_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );827 828 782 } 829 783 … … 835 789 $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' ); 836 790 } 837 838 // Use legacy JPEG output.839 add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );840 791 841 792 $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf'; … … 871 822 'mime-type' => 'image/jpeg', 872 823 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ), 873 'sources' => array(874 'image/jpeg' => array(875 'file' => 'wordpress-gsoc-flyer-pdf-77x100.jpg',876 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),877 ),878 ),879 824 ); 880 825 … … 892 837 unlink( $temp_dir . $size['file'] ); 893 838 } 894 remove_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );895 839 } 896 840 … … 1083 1027 ); 1084 1028 } 1085 1086 /**1087 * @ticket 554431088 */1089 public function test_wp_upload_image_mime_transforms_generates_webp_and_jpeg_for_both_by_default() {1090 $result = wp_upload_image_mime_transforms( 42 );1091 $this->assertArrayHasKey( 'image/jpeg', $result );1092 $this->assertArrayHasKey( 'image/webp', $result );1093 $this->assertSameSets( array( 'image/jpeg', 'image/webp' ), $result['image/jpeg'] );1094 $this->assertSameSets( array( 'image/jpeg', 'image/webp' ), $result['image/webp'] );1095 }1096 1097 /**1098 * @ticket 554431099 */1100 public function test_wp_upload_image_mime_transforms_filter_always_use_webp_instead_of_jpeg() {1101 add_filter(1102 'wp_upload_image_mime_transforms',1103 function( $transforms ) {1104 // Ensure JPG only results in WebP files.1105 $transforms['image/jpeg'] = array( 'image/webp' );1106 // Unset WebP since it does not need any transformation in that case.1107 unset( $transforms['image/webp'] );1108 return $transforms;1109 }1110 );1111 1112 $result = wp_upload_image_mime_transforms( 42 );1113 $this->assertArrayHasKey( 'image/jpeg', $result );1114 $this->assertArrayNotHasKey( 'image/webp', $result );1115 $this->assertSameSets( array( 'image/webp' ), $result['image/jpeg'] );1116 }1117 1118 /**1119 * @ticket 554431120 */1121 public function test_wp_upload_image_mime_transforms_filter_receives_parameters() {1122 $attachment_id = null;1123 add_filter(1124 'wp_upload_image_mime_transforms',1125 function( $transforms, $param1 ) use ( &$attachment_id ) {1126 $attachment_id = $param1;1127 return $transforms;1128 },1129 10,1130 21131 );1132 1133 wp_upload_image_mime_transforms( 23 );1134 $this->assertSame( 23, $attachment_id );1135 }1136 1137 /**1138 * @ticket 554431139 */1140 public function test_wp_upload_image_mime_transforms_filter_with_empty_array() {1141 add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );1142 $result = wp_upload_image_mime_transforms( 42 );1143 $this->assertSame( array(), $result );1144 }1145 1146 /**1147 * @ticket 554431148 */1149 public function test_wp_upload_image_mime_transforms_filter_with_invalid_usage() {1150 $default = wp_upload_image_mime_transforms( 42 );1151 1152 add_filter( 'wp_upload_image_mime_transforms', '__return_false' );1153 $result = wp_upload_image_mime_transforms( 42 );1154 $this->assertSame( $default, $result );1155 }1156 1157 /**1158 * @ticket 554431159 */1160 public function test__wp_get_primary_and_additional_mime_types_default() {1161 $jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';1162 1163 list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );1164 $this->assertSame( 'image/jpeg', $primary_mime_type );1165 1166 // WebP may not be supported by the server, in which case it will be stripped from the results.1167 if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {1168 $this->assertSame( array( 'image/webp' ), $additional_mime_types );1169 } else {1170 $this->assertSame( array(), $additional_mime_types );1171 }1172 }1173 1174 /**1175 * @ticket 554431176 */1177 public function test__wp_get_primary_and_additional_mime_types_prefer_original_mime() {1178 $jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';1179 1180 // Set 'image/jpeg' only as secondary output MIME type.1181 // Still, because it is the original, it should be chosen as primary over 'image/webp'.1182 add_filter(1183 'wp_upload_image_mime_transforms',1184 function( $transforms ) {1185 $transforms['image/jpeg'] = array( 'image/webp', 'image/jpeg' );1186 return $transforms;1187 }1188 );1189 1190 list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );1191 $this->assertSame( 'image/jpeg', $primary_mime_type );1192 1193 // WebP may not be supported by the server, in which case it will be stripped from the results.1194 if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {1195 $this->assertSame( array( 'image/webp' ), $additional_mime_types );1196 } else {1197 $this->assertSame( array(), $additional_mime_types );1198 }1199 }1200 1201 /**1202 * @ticket 554431203 */1204 public function test__wp_get_primary_and_additional_mime_types_use_original_mime_when_no_transformation_rules() {1205 $jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';1206 1207 // Strip all transformation rules.1208 add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );1209 1210 list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );1211 $this->assertSame( 'image/jpeg', $primary_mime_type );1212 $this->assertSame( array(), $additional_mime_types );1213 }1214 1215 /**1216 * @ticket 554431217 */1218 public function test__wp_get_primary_and_additional_mime_types_different_output_mime() {1219 $jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';1220 1221 // Set 'image/webp' as the only output MIME type.1222 // In that case, JPEG is not generated at all, so WebP becomes the primary MIME type.1223 add_filter(1224 'wp_upload_image_mime_transforms',1225 function( $transforms ) {1226 $transforms['image/jpeg'] = array( 'image/webp' );1227 return $transforms;1228 }1229 );1230 1231 list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );1232 1233 // WebP may not be supported by the server, in which case it will fall back to the original MIME type.1234 if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {1235 $this->assertSame( 'image/webp', $primary_mime_type );1236 } else {1237 $this->assertSame( 'image/jpeg', $primary_mime_type );1238 }1239 1240 $this->assertSame( array(), $additional_mime_types );1241 }1242 1243 /**1244 * @ticket 554431245 */1246 public function test__wp_get_primary_and_additional_mime_types_different_output_mimes() {1247 $jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';1248 1249 // Set 'image/webp' and 'image/avif' as output MIME types.1250 // In that case, JPEG is not generated at all, with WebP being the primary MIME type and AVIF the secondary.1251 add_filter(1252 'wp_upload_image_mime_transforms',1253 function( $transforms ) {1254 $transforms['image/jpeg'] = array( 'image/webp', 'image/avif' );1255 return $transforms;1256 }1257 );1258 1259 list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );1260 1261 // WebP may not be supported by the server, in which case it will fall back to the original MIME type.1262 if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {1263 $this->assertSame( 'image/webp', $primary_mime_type );1264 } else {1265 $this->assertSame( 'image/jpeg', $primary_mime_type );1266 }1267 1268 // AVIF may not be supported by the server, in which case it will be stripped from the results.1269 if ( wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {1270 $this->assertSame( array( 'image/avif' ), $additional_mime_types );1271 } else {1272 $this->assertSame( array(), $additional_mime_types );1273 }1274 }1275 1276 /**1277 * @ticket 554431278 * @dataProvider data__wp_filter_image_sizes_additional_mime_type_support1279 */1280 public function test__wp_filter_image_sizes_additional_mime_type_support( $input_size_data, $filter_callback, $expected_size_names ) {1281 remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );1282 if ( $filter_callback ) {1283 add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );1284 }1285 1286 $expected_size_data = array_intersect_key( $input_size_data, array_flip( $expected_size_names ) );1287 1288 $output_size_data = _wp_filter_image_sizes_additional_mime_type_support( $input_size_data, 42 );1289 $this->assertEqualSetsWithIndex( $expected_size_data, $output_size_data );1290 }1291 1292 public function data__wp_filter_image_sizes_additional_mime_type_support() {1293 $thumbnail_data = array(1294 'width' => 150,1295 'height' => 150,1296 'crop' => true,1297 );1298 $medium_data = array(1299 'width' => 300,1300 'height' => 300,1301 'crop' => false,1302 );1303 $medium_large_data = array(1304 'width' => 768,1305 'height' => 0,1306 'crop' => false,1307 );1308 $large_data = array(1309 'width' => 1024,1310 'height' => 1024,1311 'crop' => false,1312 );1313 $custom_data = array(1314 'width' => 512,1315 'height' => 512,1316 'crop' => true,1317 );1318 1319 return array(1320 array(1321 array(1322 'thumbnail' => $thumbnail_data,1323 'medium' => $medium_data,1324 'medium_large' => $medium_large_data,1325 'large' => $large_data,1326 ),1327 null,1328 array( 'thumbnail', 'medium', 'medium_large', 'large' ),1329 ),1330 array(1331 array(1332 'thumbnail' => $thumbnail_data,1333 'medium' => $medium_data,1334 'custom' => $custom_data,1335 ),1336 null,1337 array( 'thumbnail', 'medium' ),1338 ),1339 array(1340 array(1341 'thumbnail' => $thumbnail_data,1342 'medium' => $medium_data,1343 'medium_large' => $medium_large_data,1344 'large' => $large_data,1345 ),1346 function( $enabled_sizes ) {1347 unset( $enabled_sizes['medium_large'], $enabled_sizes['large'] );1348 return $enabled_sizes;1349 },1350 array( 'thumbnail', 'medium' ),1351 ),1352 array(1353 array(1354 'thumbnail' => $thumbnail_data,1355 'medium' => $medium_data,1356 'medium_large' => $medium_large_data,1357 'large' => $large_data,1358 ),1359 function( $enabled_sizes ) {1360 $enabled_sizes['medium_large'] = false;1361 $enabled_sizes['large'] = false;1362 return $enabled_sizes;1363 },1364 array( 'thumbnail', 'medium' ),1365 ),1366 array(1367 array(1368 'thumbnail' => $thumbnail_data,1369 'medium' => $medium_data,1370 'custom' => $custom_data,1371 ),1372 function( $enabled_sizes ) {1373 unset( $enabled_sizes['medium'] );1374 $enabled_sizes['custom'] = true;1375 return $enabled_sizes;1376 },1377 array( 'thumbnail', 'custom' ),1378 ),1379 );1380 }1381 1382 /**1383 * Test the `_wp_maybe_scale_and_rotate_image()` function.1384 *1385 * @dataProvider data_test__wp_maybe_scale_and_rotate_image1386 *1387 * @ticket 554431388 */1389 public function test__wp_maybe_scale_and_rotate_image( $file, $imagesize, $mime_type, $expected ) {1390 if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {1391 $this->markTestSkipped( sprintf( 'This test requires %s support.', $mime_type ) );1392 }1393 1394 $attributes = array( 'post_mime_type' => $mime_type );1395 $attachment_id = $this->factory->attachment->create_object( $file, 0, $attributes );1396 $exif_meta = wp_read_image_metadata( $file );1397 1398 list( $editor, $resized, $rotated ) = _wp_maybe_scale_and_rotate_image( $file, $attachment_id, $imagesize, $exif_meta, $mime_type );1399 1400 $this->assertSame( $expected['rotated'], $rotated );1401 $this->assertSame( $expected['resized'], $resized );1402 $this->assertSame( $expected['size'], $editor->get_size() );1403 }1404 1405 /**1406 * Data provider for the `test__wp_maybe_scale_and_rotate_image()` test.1407 *1408 * @return array1409 */1410 public function data_test__wp_maybe_scale_and_rotate_image() {1411 return array(1412 1413 // Image that will be scaled.1414 array(1415 DIR_TESTDATA . '/images/test-image-large.jpg',1416 array( 3000, 2250 ),1417 'image/jpeg',1418 array(1419 'rotated' => false,1420 'resized' => true,1421 'size' => array(1422 'width' => 2560,1423 'height' => 1920,1424 ),1425 ),1426 ),1427 1428 // Image that will not be scaled.1429 array(1430 DIR_TESTDATA . '/images/canola.jpg',1431 array( 640, 480 ),1432 'image/jpeg',1433 array(1434 'rotated' => false,1435 'resized' => false,1436 'size' => array(1437 'width' => 640,1438 'height' => 480,1439 ),1440 ),1441 ),1442 1443 // Image that will be flipped.1444 array(1445 DIR_TESTDATA . '/images/test-image-upside-down.jpg',1446 array( 600, 450 ),1447 'image/jpeg',1448 array(1449 'rotated' => true,1450 'resized' => false,1451 'size' => array(1452 'width' => 600,1453 'height' => 450,1454 ),1455 ),1456 ),1457 1458 // Image that will be rotated.1459 array(1460 DIR_TESTDATA . '/images/test-image-rotated-90ccw.jpg',1461 array( 1200, 1800 ),1462 'image/jpeg',1463 array(1464 'rotated' => true,1465 'resized' => false,1466 'size' => array(1467 'width' => 1800,1468 'height' => 1200,1469 ),1470 ),1471 ),1472 1473 // Image that will not be rotated - WebP Exif is not supported in PHP.1474 array(1475 DIR_TESTDATA . '/images/test-image-rotated-90cw.webp',1476 array( 1024, 768 ),1477 'image/webp',1478 array(1479 'rotated' => false,1480 'resized' => false,1481 'size' => array(1482 'width' => 1024,1483 'height' => 768,1484 ),1485 ),1486 ),1487 1488 );1489 }1490 1491 /**1492 * Test the `_wp_get_image_suffix()` function.1493 * @dataProvider data_test__wp_get_image_suffix1494 *1495 * @ticket 554431496 */1497 public function test__wp_get_image_suffix( $resized, $rotated, $expected ) {1498 $this->assertSame( $expected, _wp_get_image_suffix( $resized, $rotated ) );1499 }1500 1501 /**1502 * Data provider for the `test__wp_get_image_suffix()` test.1503 */1504 public function data_test__wp_get_image_suffix() {1505 return array(1506 array( false, false, '' ),1507 array( true, false, 'scaled' ),1508 array( false, true, 'rotated' ),1509 array( true, true, 'scaled' ),1510 );1511 }1512 1029 }
Note: See TracChangeset
for help on using the changeset viewer.