Ticket #49412: 49412.3.diff
File 49412.3.diff, 22.3 KB (added by , 3 years ago) |
---|
-
src/wp-admin/includes/file.php
diff --git src/wp-admin/includes/file.php src/wp-admin/includes/file.php index 12206b78b5..8cd45c4c7a 100644
function wp_opcache_invalidate( $filepath, $force = false ) { 2553 2553 2554 2554 return false; 2555 2555 } 2556 2557 /** 2558 * Wrapper for PHP filesize with filters and casting the result as an integer. 2559 * 2560 * @since 5.7.0 2561 * 2562 * @link https://www.php.net/manual/en/function.filesize.php 2563 * 2564 * @param string $path Path to the file. 2565 * @return int The size of the file in bytes, or 0 in the event of an error. 2566 */ 2567 function wp_filesize( $path ) { 2568 /** 2569 * Filters the result of wp_filesize before the PHP function is run. 2570 * 2571 * @since 5.7.0 2572 * 2573 * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call. 2574 * @param string $path Path to the file. 2575 */ 2576 $size = apply_filters( 'pre_wp_filesize', null, $path ); 2577 2578 if ( is_int( $size ) ) { 2579 return $size; 2580 } 2581 2582 $size = (int) @filesize( $path ); 2583 2584 /** 2585 * Filters the size of the file. 2586 * 2587 * @since 5.7.0 2588 * 2589 * @param int $size The result of PHP filesize on the file. 2590 * @param string $path Path to the file. 2591 */ 2592 return apply_filters( 'wp_filesize', $size, $path ); 2593 } -
src/wp-admin/includes/image.php
diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php index 4d6e99f749..5a6d380323 100644
function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me 210 210 // Store the original image file name in image_meta. 211 211 $image_meta['original_image'] = wp_basename( $original_file ); 212 212 213 // Add image file size. 214 $image_meta['filesize'] = wp_filesize( $new_file ); 215 213 216 return $image_meta; 214 217 } 215 218 … … function wp_create_image_subsizes( $file, $attachment_id ) { 235 238 236 239 // Default image meta. 237 240 $image_meta = array( 238 'width' => $imagesize[0], 239 'height' => $imagesize[1], 240 'file' => _wp_relative_upload_path( $file ), 241 'sizes' => array(), 241 'width' => $imagesize[0], 242 'height' => $imagesize[1], 243 'file' => _wp_relative_upload_path( $file ), 244 'filesize' => wp_filesize( $file ), 245 'sizes' => array(), 242 246 ); 243 247 244 248 // Fetch additional metadata from EXIF/IPTC. … … function wp_generate_attachment_metadata( $attachment_id, $file ) { 629 633 // Remove the blob of binary data from the array. 630 634 unset( $metadata['image']['data'] ); 631 635 636 // Capture file size for cases where it has not been captured yet, such as PDFs. 637 if ( ! isset( $metadata['filesize'] ) && file_exists( $file ) ) { 638 $metadata['filesize'] = wp_filesize( $file ); 639 } 640 632 641 /** 633 642 * Filters the generated attachment meta data. 634 643 * -
src/wp-admin/includes/media.php
diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php index d9dcbd5d7d..89382a4f0f 100644
function attachment_submitbox_metadata() { 3378 3378 if ( isset( $meta['filesize'] ) ) { 3379 3379 $file_size = $meta['filesize']; 3380 3380 } elseif ( file_exists( $file ) ) { 3381 $file_size = filesize( $file ); 3381 $meta = wp_generate_attachment_metadata( $attachment_id, $file ); 3382 3383 if ( is_array( $meta ) && isset( $meta['filesize'] ) ) { 3384 $file_size = $meta['filesize']; 3385 } 3382 3386 } 3383 3387 3384 3388 if ( ! empty( $file_size ) ) { -
src/wp-includes/class-wp-image-editor-gd.php
diff --git src/wp-includes/class-wp-image-editor-gd.php src/wp-includes/class-wp-image-editor-gd.php index f175b90d20..c933368441 100644
class WP_Image_Editor_GD extends WP_Image_Editor { 497 497 'width' => $this->size['width'], 498 498 'height' => $this->size['height'], 499 499 'mime-type' => $mime_type, 500 'filesize' => wp_filesize( $filename ), 500 501 ); 501 502 } 502 503 -
src/wp-includes/class-wp-image-editor-imagick.php
diff --git src/wp-includes/class-wp-image-editor-imagick.php src/wp-includes/class-wp-image-editor-imagick.php index b7336fe5ae..0ffff73900 100644
class WP_Image_Editor_Imagick extends WP_Image_Editor { 729 729 'width' => $this->size['width'], 730 730 'height' => $this->size['height'], 731 731 'mime-type' => $mime_type, 732 'filesize' => wp_filesize( $filename ), 732 733 ); 733 734 } 734 735 -
src/wp-includes/media.php
diff --git src/wp-includes/media.php src/wp-includes/media.php index c7ef8bab5d..8cda9a06fc 100644
function wp_prepare_attachment_for_js( $attachment ) { 4046 4046 if ( isset( $meta['filesize'] ) ) { 4047 4047 $bytes = $meta['filesize']; 4048 4048 } elseif ( file_exists( $attached_file ) ) { 4049 $bytes = filesize( $attached_file ); 4049 $meta = wp_generate_attachment_metadata( $attachment->ID, $attached_file ); 4050 4051 if ( is_array( $meta ) && isset( $meta['filesize'] ) ) { 4052 $bytes = $meta['filesize']; 4053 } 4050 4054 } else { 4051 4055 $bytes = ''; 4052 4056 } -
tests/phpunit/tests/file.php
diff --git tests/phpunit/tests/file.php tests/phpunit/tests/file.php index 311ba30339..11dd63d156 100644
class Tests_File extends WP_UnitTestCase { 262 262 263 263 return $keys; 264 264 } 265 266 /** 267 * @ticket 49412 268 */ 269 function test_wp_filesize_with_nonexistent_file() { 270 $file = 'nonexistent/file.jpg'; 271 $this->assertEquals( 0, wp_filesize( $file ) ); 272 } 273 274 /** 275 * @ticket 49412 276 */ 277 function test_wp_filesize() { 278 $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; 279 280 $this->assertEquals( filesize( $file ), wp_filesize( $file ) ); 281 282 $filter = function() { 283 return 999; 284 }; 285 286 add_filter( 'wp_filesize', $filter ); 287 288 $this->assertEquals( 999, wp_filesize( $file ) ); 289 290 $pre_filter = function() { 291 return 111; 292 }; 293 294 add_filter( 'pre_wp_filesize', $pre_filter ); 295 296 $this->assertEquals( 111, wp_filesize( $file ) ); 297 298 remove_filter( 'wp_filesize', $filter ); 299 remove_filter( 'pre_wp-filesize', $pre_filter ); 300 } 265 301 } -
tests/phpunit/tests/image/editorGd.php
diff --git tests/phpunit/tests/image/editorGd.php tests/phpunit/tests/image/editorGd.php index 781ff96cb4..5d967bdcdb 100644
class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 100 100 'width' => 50, 101 101 'height' => 33, 102 102 'mime-type' => 'image/jpeg', 103 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 103 104 ), 104 105 ); 105 106 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 300 301 'width' => 10, 301 302 'height' => 7, 302 303 'mime-type' => 'image/jpeg', 304 'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ), 303 305 ), 304 306 305 307 // #1 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 308 310 'width' => 75, 309 311 'height' => 50, 310 312 'mime-type' => 'image/jpeg', 313 'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ), 311 314 ), 312 315 313 316 // #2 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 316 319 'width' => 30, 317 320 'height' => 20, 318 321 'mime-type' => 'image/jpeg', 322 'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ), 319 323 ), 320 324 321 325 // #3 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 324 328 'width' => 45, 325 329 'height' => 400, 326 330 'mime-type' => 'image/jpeg', 331 'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ), 327 332 ), 328 333 329 334 // #4 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 332 337 'width' => 50, 333 338 'height' => 33, 334 339 'mime-type' => 'image/jpeg', 340 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 335 341 ), 336 342 337 343 // #5 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 340 346 'width' => 55, 341 347 'height' => 37, 342 348 'mime-type' => 'image/jpeg', 349 'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ), 343 350 ), 344 351 345 352 // #6 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 348 355 'width' => 83, 349 356 'height' => 55, 350 357 'mime-type' => 'image/jpeg', 358 'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ), 351 359 ), 352 360 353 361 // #7 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 356 364 'width' => 90, 357 365 'height' => 60, 358 366 'mime-type' => 'image/jpeg', 367 'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ), 359 368 ), 360 369 361 370 // #8 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 364 373 'width' => 105, 365 374 'height' => 70, 366 375 'mime-type' => 'image/jpeg', 376 'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ), 367 377 ), 368 378 369 379 // #9 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 372 382 'width' => 200, 373 383 'height' => 133, 374 384 'mime-type' => 'image/jpeg', 385 'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ), 375 386 ), 376 387 ); 377 388 -
tests/phpunit/tests/image/editorImagick.php
diff --git tests/phpunit/tests/image/editorImagick.php tests/phpunit/tests/image/editorImagick.php index e82486d828..02e0590a2e 100644
class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 90 90 'width' => 50, 91 91 'height' => 33, 92 92 'mime-type' => 'image/jpeg', 93 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 93 94 ), 94 95 ); 95 96 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 289 290 'width' => 10, 290 291 'height' => 7, 291 292 'mime-type' => 'image/jpeg', 293 'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ), 292 294 ), 293 295 294 296 // #1 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 297 299 'width' => 75, 298 300 'height' => 50, 299 301 'mime-type' => 'image/jpeg', 302 'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ), 300 303 ), 301 304 302 305 // #2 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 305 308 'width' => 30, 306 309 'height' => 20, 307 310 'mime-type' => 'image/jpeg', 311 'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ), 308 312 ), 309 313 310 314 // #3 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 313 317 'width' => 45, 314 318 'height' => 400, 315 319 'mime-type' => 'image/jpeg', 320 'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ), 316 321 ), 317 322 318 323 // #4 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 321 326 'width' => 50, 322 327 'height' => 33, 323 328 'mime-type' => 'image/jpeg', 329 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 324 330 ), 325 331 326 332 // #5 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 329 335 'width' => 55, 330 336 'height' => 37, 331 337 'mime-type' => 'image/jpeg', 338 'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ), 332 339 ), 333 340 334 341 // #6 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 337 344 'width' => 83, 338 345 'height' => 55, 339 346 'mime-type' => 'image/jpeg', 347 'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ), 340 348 ), 341 349 342 350 // #7 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 345 353 'width' => 90, 346 354 'height' => 60, 347 355 'mime-type' => 'image/jpeg', 356 'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ), 348 357 ), 349 358 350 359 // #8 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 353 362 'width' => 105, 354 363 'height' => 70, 355 364 'mime-type' => 'image/jpeg', 365 'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ), 356 366 ), 357 367 358 368 // #9 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 361 371 'width' => 200, 362 372 'height' => 133, 363 373 'mime-type' => 'image/jpeg', 374 'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ), 364 375 ), 365 376 ); 366 377 -
tests/phpunit/tests/image/functions.php
diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php index 8cd42a13b5..594e52e919 100644
class Tests_Image_Functions extends WP_UnitTestCase { 174 174 public function test_wp_save_image_file() { 175 175 $classes = $this->get_image_editor_engine_classes(); 176 176 177 foreach ( $classes as $key => $class ) { 178 if ( ! call_user_func( array( $class, 'test' ) ) ) { 179 // If the image editor isn't available, skip it. 180 unset( $classes[ $key ] ); 181 } 182 } 183 184 if ( ! $classes ) { 185 $this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) ); 186 } 187 177 188 require_once ABSPATH . 'wp-admin/includes/image-edit.php'; 178 189 179 190 // Mime types. … … class Tests_Image_Functions extends WP_UnitTestCase { 224 235 public function test_mime_overrides_filename() { 225 236 $classes = $this->get_image_editor_engine_classes(); 226 237 238 foreach ( $classes as $key => $class ) { 239 if ( ! call_user_func( array( $class, 'test' ) ) ) { 240 // If the image editor isn't available, skip it. 241 unset( $classes[ $key ] ); 242 } 243 } 244 245 if ( ! $classes ) { 246 $this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) ); 247 } 248 227 249 // Test each image editor engine. 228 250 foreach ( $classes as $class ) { 229 251 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' ); … … class Tests_Image_Functions extends WP_UnitTestCase { 255 277 public function test_inferred_mime_types() { 256 278 $classes = $this->get_image_editor_engine_classes(); 257 279 280 foreach ( $classes as $key => $class ) { 281 if ( ! call_user_func( array( $class, 'test' ) ) ) { 282 // If the image editor isn't available, skip it. 283 unset( $classes[ $key ] ); 284 } 285 } 286 287 if ( ! $classes ) { 288 $this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) ); 289 } 290 258 291 // Mime types. 259 292 $mime_types = array( 260 293 'jpg' => 'image/jpeg', … … class Tests_Image_Functions extends WP_UnitTestCase { 340 373 $this->markTestSkipped( 'Image editor engines WP_Image_Editor_GD and WP_Image_Editor_Imagick are not supported on this system.' ); 341 374 } 342 375 376 // Then, test with editors. 377 foreach ( $classes as $class ) { 378 $editor = new $class( DIR_TESTDATA ); 379 $loaded = $editor->load(); 380 343 381 return $classes; 344 382 } 345 383 … … class Tests_Image_Functions extends WP_UnitTestCase { 479 517 480 518 $this->assertNotEmpty( $attachment_id ); 481 519 520 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 521 522 $temp_dir = get_temp_dir(); 523 482 524 $expected = array( 483 'sizes' => array(525 'sizes' => array( 484 526 'full' => array( 485 527 'file' => 'wordpress-gsoc-flyer-pdf.jpg', 486 528 'width' => 1088, 487 529 'height' => 1408, 488 530 'mime-type' => 'image/jpeg', 531 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ), 489 532 ), 490 533 'medium' => array( 491 534 'file' => 'wordpress-gsoc-flyer-pdf-232x300.jpg', 492 535 'width' => 232, 493 536 'height' => 300, 494 537 'mime-type' => 'image/jpeg', 538 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ), 495 539 ), 496 540 'large' => array( 497 541 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', 498 542 'width' => 791, 499 543 'height' => 1024, 500 544 'mime-type' => 'image/jpeg', 545 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 501 546 ), 502 547 'thumbnail' => array( 503 548 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', 504 549 'width' => 116, 505 550 'height' => 150, 506 551 'mime-type' => 'image/jpeg', 552 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 507 553 ), 508 554 ), 555 'filesize' => wp_filesize( $test_file ), 509 556 ); 510 557 511 558 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 559 512 560 $this->assertSame( $expected, $metadata ); 513 561 514 562 unlink( $test_file ); 515 $temp_dir = get_temp_dir();516 563 foreach ( $metadata['sizes'] as $size ) { 517 564 unlink( $temp_dir . $size['file'] ); 518 565 } … … class Tests_Image_Functions extends WP_UnitTestCase { 549 596 550 597 $this->assertNotEmpty( $attachment_id ); 551 598 599 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 600 601 $temp_dir = get_temp_dir(); 602 552 603 $expected = array( 553 'sizes' => array(604 'sizes' => array( 554 605 'full' => array( 555 606 'file' => 'wordpress-gsoc-flyer-pdf.jpg', 556 607 'width' => 1088, 557 608 'height' => 1408, 558 609 'mime-type' => 'image/jpeg', 610 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ), 559 611 ), 560 612 'medium' => array( 561 613 'file' => 'wordpress-gsoc-flyer-pdf-300x300.jpg', 562 614 'width' => 300, 563 615 'height' => 300, 564 616 'mime-type' => 'image/jpeg', 617 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ), 565 618 ), 566 619 'large' => array( 567 620 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', 568 621 'width' => 791, 569 622 'height' => 1024, 570 623 'mime-type' => 'image/jpeg', 624 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 571 625 ), 572 626 'thumbnail' => array( 573 627 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', 574 628 'width' => 116, 575 629 'height' => 150, 576 630 'mime-type' => 'image/jpeg', 631 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 577 632 ), 578 633 ), 634 'filesize' => wp_filesize( $test_file ), 579 635 ); 580 636 581 637 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 638 639 // Different environments produce slightly different filesize results. 640 foreach ( $metadata['sizes'] as &$item ) { 641 $item['filesize'] = round_to_nearest_thousand( $item['filesize'] ); 642 } 643 $metadata['filesize'] = round_to_nearest_thousand( $metadata['filesize'] ); 644 582 645 $this->assertSame( $expected, $metadata ); 583 646 584 647 unlink( $test_file ); 585 648 foreach ( $metadata['sizes'] as $size ) { 586 unlink( get_temp_dir(). $size['file'] );649 unlink( $temp_dir . $size['file'] ); 587 650 } 588 651 } 589 652 … … class Tests_Image_Functions extends WP_UnitTestCase { 617 680 add_image_size( 'test-size', 100, 100 ); 618 681 add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 ); 619 682 683 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 684 685 $temp_dir = get_temp_dir(); 686 620 687 $expected = array( 621 688 'file' => 'wordpress-gsoc-flyer-pdf-77x100.jpg', 622 689 'width' => 77, 623 690 'height' => 100, 624 691 'mime-type' => 'image/jpeg', 692 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ), 625 693 ); 626 694 627 695 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 696 697 // Different environments produce slightly different filesize results. 698 $metadata['sizes']['test-size']['filesize'] = round_to_nearest_thousand( $metadata['sizes']['test-size']['filesize'] ); 699 $this->assertSame( $metadata['sizes']['test-size'], $expected ); 700 628 701 $this->assertArrayHasKey( 'test-size', $metadata['sizes'], 'The `test-size` was not added to the metadata.' ); 629 702 $this->assertSame( $expected, $metadata['sizes']['test-size'] ); 630 703 … … class Tests_Image_Functions extends WP_UnitTestCase { 632 705 remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 ); 633 706 634 707 unlink( $test_file ); 635 $temp_dir = get_temp_dir();636 708 foreach ( $metadata['sizes'] as $size ) { 637 709 unlink( $temp_dir . $size['file'] ); 638 710 } -
new file tests/phpunit/tests/media/filesize.php
diff --git tests/phpunit/tests/media/filesize.php tests/phpunit/tests/media/filesize.php new file mode 100644 index 0000000000..57a0ec6592
- + 1 <?php 2 3 /** 4 * @group media 5 * @group media_filesize 6 */ 7 class Tests_Image_Filesize extends WP_UnitTestCase { 8 function tearDown() { 9 $this->remove_added_uploads(); 10 11 parent::tearDown(); 12 } 13 14 /** 15 * Check that filesize meta is generated for jpegs. 16 * 17 * @ticket 49412 18 */ 19 function test_filesize_in_jpg_meta() { 20 $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/33772.jpg' ); 21 22 $metadata = wp_get_attachment_metadata( $attachment ); 23 24 $this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); 25 26 foreach ( $metadata['sizes'] as $intermediate_size ) { 27 $this->assertTrue( ! empty( $intermediate_size['filesize'] ) && is_numeric( $intermediate_size['filesize'] ) ); 28 } 29 } 30 31 /** 32 * Check that filesize meta is generated for pngs. 33 * 34 * @ticket 49412 35 */ 36 function test_filesize_in_png_meta() { 37 $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.png' ); 38 39 $metadata = wp_get_attachment_metadata( $attachment ); 40 41 $this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); 42 43 foreach ( $metadata['sizes'] as $intermediate_size ) { 44 $this->assertTrue( ! empty( $intermediate_size['filesize'] ) && is_numeric( $intermediate_size['filesize'] ) ); 45 } 46 } 47 48 /** 49 * Check that filesize meta is generated for pdfs. 50 * 51 * @ticket 49412 52 */ 53 function test_filesize_in_pdf_meta() { 54 $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf' ); 55 56 $metadata = wp_get_attachment_metadata( $attachment ); 57 58 $this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); 59 } 60 61 /** 62 * Adds psd to allowed mime types for multisite testing. 63 * 64 * @param array $mimes Unfiltered mime types. 65 * @return array Filtered mime types. 66 */ 67 public function allow_psd_mime_type( $mimes ) { 68 $mimes['psd'] = 'application/octet-stream'; 69 return $mimes; 70 } 71 72 /** 73 * Check that filesize meta is generated for psds. 74 * 75 * @ticket 49412 76 */ 77 function test_filesize_in_psd_meta() { 78 // PSD mime type is not allowed by default on multisite. 79 if ( is_multisite() ) { 80 add_filter( 'upload_mimes', array( $this, 'allow_psd_mime_type' ), 10, 2 ); 81 } 82 83 $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.psd' ); 84 85 $metadata = wp_get_attachment_metadata( $attachment ); 86 87 $this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); 88 89 if ( is_multisite() ) { 90 remove_filter( 'upload_mimes', array( $this, 'allow_psd_mime_type' ) ); 91 } 92 } 93 }