Make WordPress Core

Ticket #49412: 49412.3.diff

File 49412.3.diff, 22.3 KB (added by adamsilverstein, 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 ) { 
    25532553
    25542554        return false;
    25552555}
     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 */
     2567function 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 
    210210        // Store the original image file name in image_meta.
    211211        $image_meta['original_image'] = wp_basename( $original_file );
    212212
     213        // Add image file size.
     214        $image_meta['filesize'] = wp_filesize( $new_file );
     215
    213216        return $image_meta;
    214217}
    215218
    function wp_create_image_subsizes( $file, $attachment_id ) { 
    235238
    236239        // Default image meta.
    237240        $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(),
    242246        );
    243247
    244248        // Fetch additional metadata from EXIF/IPTC.
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    629633        // Remove the blob of binary data from the array.
    630634        unset( $metadata['image']['data'] );
    631635
     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
    632641        /**
    633642         * Filters the generated attachment meta data.
    634643         *
  • 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() { 
    33783378        if ( isset( $meta['filesize'] ) ) {
    33793379                $file_size = $meta['filesize'];
    33803380        } 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                }
    33823386        }
    33833387
    33843388        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 { 
    497497                        'width'     => $this->size['width'],
    498498                        'height'    => $this->size['height'],
    499499                        'mime-type' => $mime_type,
     500                        'filesize'  => wp_filesize( $filename ),
    500501                );
    501502        }
    502503
  • 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 { 
    729729                        'width'     => $this->size['width'],
    730730                        'height'    => $this->size['height'],
    731731                        'mime-type' => $mime_type,
     732                        'filesize'  => wp_filesize( $filename ),
    732733                );
    733734        }
    734735
  • 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 ) { 
    40464046        if ( isset( $meta['filesize'] ) ) {
    40474047                $bytes = $meta['filesize'];
    40484048        } 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                }
    40504054        } else {
    40514055                $bytes = '';
    40524056        }
  • 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 { 
    262262
    263263                return $keys;
    264264        }
     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        }
    265301}
  • 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 { 
    100100                                'width'     => 50,
    101101                                'height'    => 33,
    102102                                'mime-type' => 'image/jpeg',
     103                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
    103104                        ),
    104105                );
    105106
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    300301                                'width'     => 10,
    301302                                'height'    => 7,
    302303                                'mime-type' => 'image/jpeg',
     304                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
    303305                        ),
    304306
    305307                        // #1
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    308310                                'width'     => 75,
    309311                                'height'    => 50,
    310312                                'mime-type' => 'image/jpeg',
     313                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
    311314                        ),
    312315
    313316                        // #2
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    316319                                'width'     => 30,
    317320                                'height'    => 20,
    318321                                'mime-type' => 'image/jpeg',
     322                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
    319323                        ),
    320324
    321325                        // #3
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    324328                                'width'     => 45,
    325329                                'height'    => 400,
    326330                                'mime-type' => 'image/jpeg',
     331                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
    327332                        ),
    328333
    329334                        // #4
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    332337                                'width'     => 50,
    333338                                'height'    => 33,
    334339                                'mime-type' => 'image/jpeg',
     340                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
    335341                        ),
    336342
    337343                        // #5
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    340346                                'width'     => 55,
    341347                                'height'    => 37,
    342348                                'mime-type' => 'image/jpeg',
     349                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
    343350                        ),
    344351
    345352                        // #6
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    348355                                'width'     => 83,
    349356                                'height'    => 55,
    350357                                'mime-type' => 'image/jpeg',
     358                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
    351359                        ),
    352360
    353361                        // #7
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    356364                                'width'     => 90,
    357365                                'height'    => 60,
    358366                                'mime-type' => 'image/jpeg',
     367                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
    359368                        ),
    360369
    361370                        // #8
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    364373                                'width'     => 105,
    365374                                'height'    => 70,
    366375                                'mime-type' => 'image/jpeg',
     376                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
    367377                        ),
    368378
    369379                        // #9
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    372382                                'width'     => 200,
    373383                                'height'    => 133,
    374384                                'mime-type' => 'image/jpeg',
     385                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
    375386                        ),
    376387                );
    377388
  • 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 { 
    9090                                'width'     => 50,
    9191                                'height'    => 33,
    9292                                'mime-type' => 'image/jpeg',
     93                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
    9394                        ),
    9495                );
    9596
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    289290                                'width'     => 10,
    290291                                'height'    => 7,
    291292                                'mime-type' => 'image/jpeg',
     293                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
    292294                        ),
    293295
    294296                        // #1
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    297299                                'width'     => 75,
    298300                                'height'    => 50,
    299301                                'mime-type' => 'image/jpeg',
     302                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
    300303                        ),
    301304
    302305                        // #2
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    305308                                'width'     => 30,
    306309                                'height'    => 20,
    307310                                'mime-type' => 'image/jpeg',
     311                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
    308312                        ),
    309313
    310314                        // #3
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    313317                                'width'     => 45,
    314318                                'height'    => 400,
    315319                                'mime-type' => 'image/jpeg',
     320                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
    316321                        ),
    317322
    318323                        // #4
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    321326                                'width'     => 50,
    322327                                'height'    => 33,
    323328                                'mime-type' => 'image/jpeg',
     329                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
    324330                        ),
    325331
    326332                        // #5
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    329335                                'width'     => 55,
    330336                                'height'    => 37,
    331337                                'mime-type' => 'image/jpeg',
     338                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
    332339                        ),
    333340
    334341                        // #6
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    337344                                'width'     => 83,
    338345                                'height'    => 55,
    339346                                'mime-type' => 'image/jpeg',
     347                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
    340348                        ),
    341349
    342350                        // #7
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    345353                                'width'     => 90,
    346354                                'height'    => 60,
    347355                                'mime-type' => 'image/jpeg',
     356                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
    348357                        ),
    349358
    350359                        // #8
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    353362                                'width'     => 105,
    354363                                'height'    => 70,
    355364                                'mime-type' => 'image/jpeg',
     365                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
    356366                        ),
    357367
    358368                        // #9
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    361371                                'width'     => 200,
    362372                                'height'    => 133,
    363373                                'mime-type' => 'image/jpeg',
     374                                'filesize'  => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
    364375                        ),
    365376                );
    366377
  • 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 { 
    174174        public function test_wp_save_image_file() {
    175175                $classes = $this->get_image_editor_engine_classes();
    176176
     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
    177188                require_once ABSPATH . 'wp-admin/includes/image-edit.php';
    178189
    179190                // Mime types.
    class Tests_Image_Functions extends WP_UnitTestCase { 
    224235        public function test_mime_overrides_filename() {
    225236                $classes = $this->get_image_editor_engine_classes();
    226237
     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
    227249                // Test each image editor engine.
    228250                foreach ( $classes as $class ) {
    229251                        $img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
    class Tests_Image_Functions extends WP_UnitTestCase { 
    255277        public function test_inferred_mime_types() {
    256278                $classes = $this->get_image_editor_engine_classes();
    257279
     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
    258291                // Mime types.
    259292                $mime_types = array(
    260293                        'jpg'  => 'image/jpeg',
    class Tests_Image_Functions extends WP_UnitTestCase { 
    340373                        $this->markTestSkipped( 'Image editor engines WP_Image_Editor_GD and WP_Image_Editor_Imagick are not supported on this system.' );
    341374                }
    342375
     376                // Then, test with editors.
     377                foreach ( $classes as $class ) {
     378                        $editor = new $class( DIR_TESTDATA );
     379                        $loaded = $editor->load();
     380
    343381                return $classes;
    344382        }
    345383
    class Tests_Image_Functions extends WP_UnitTestCase { 
    479517
    480518                $this->assertNotEmpty( $attachment_id );
    481519
     520                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     521
     522                $temp_dir = get_temp_dir();
     523
    482524                $expected = array(
    483                         'sizes' => array(
     525                        'sizes'    => array(
    484526                                'full'      => array(
    485527                                        'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
    486528                                        'width'     => 1088,
    487529                                        'height'    => 1408,
    488530                                        'mime-type' => 'image/jpeg',
     531                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
    489532                                ),
    490533                                'medium'    => array(
    491534                                        'file'      => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
    492535                                        'width'     => 232,
    493536                                        'height'    => 300,
    494537                                        'mime-type' => 'image/jpeg',
     538                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
    495539                                ),
    496540                                'large'     => array(
    497541                                        'file'      => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
    498542                                        'width'     => 791,
    499543                                        'height'    => 1024,
    500544                                        'mime-type' => 'image/jpeg',
     545                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
    501546                                ),
    502547                                'thumbnail' => array(
    503548                                        'file'      => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
    504549                                        'width'     => 116,
    505550                                        'height'    => 150,
    506551                                        'mime-type' => 'image/jpeg',
     552                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
    507553                                ),
    508554                        ),
     555                        'filesize' => wp_filesize( $test_file ),
    509556                );
    510557
    511558                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     559
    512560                $this->assertSame( $expected, $metadata );
    513561
    514562                unlink( $test_file );
    515                 $temp_dir = get_temp_dir();
    516563                foreach ( $metadata['sizes'] as $size ) {
    517564                        unlink( $temp_dir . $size['file'] );
    518565                }
    class Tests_Image_Functions extends WP_UnitTestCase { 
    549596
    550597                $this->assertNotEmpty( $attachment_id );
    551598
     599                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     600
     601                $temp_dir = get_temp_dir();
     602
    552603                $expected = array(
    553                         'sizes' => array(
     604                        'sizes'    => array(
    554605                                'full'      => array(
    555606                                        'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
    556607                                        'width'     => 1088,
    557608                                        'height'    => 1408,
    558609                                        'mime-type' => 'image/jpeg',
     610                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
    559611                                ),
    560612                                'medium'    => array(
    561613                                        'file'      => 'wordpress-gsoc-flyer-pdf-300x300.jpg',
    562614                                        'width'     => 300,
    563615                                        'height'    => 300,
    564616                                        'mime-type' => 'image/jpeg',
     617                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
    565618                                ),
    566619                                'large'     => array(
    567620                                        'file'      => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
    568621                                        'width'     => 791,
    569622                                        'height'    => 1024,
    570623                                        'mime-type' => 'image/jpeg',
     624                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
    571625                                ),
    572626                                'thumbnail' => array(
    573627                                        'file'      => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
    574628                                        'width'     => 116,
    575629                                        'height'    => 150,
    576630                                        'mime-type' => 'image/jpeg',
     631                                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
    577632                                ),
    578633                        ),
     634                        'filesize' => wp_filesize( $test_file ),
    579635                );
    580636
    581637                $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
    582645                $this->assertSame( $expected, $metadata );
    583646
    584647                unlink( $test_file );
    585648                foreach ( $metadata['sizes'] as $size ) {
    586                         unlink( get_temp_dir() . $size['file'] );
     649                        unlink( $temp_dir . $size['file'] );
    587650                }
    588651        }
    589652
    class Tests_Image_Functions extends WP_UnitTestCase { 
    617680                add_image_size( 'test-size', 100, 100 );
    618681                add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
    619682
     683                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     684
     685                $temp_dir = get_temp_dir();
     686
    620687                $expected = array(
    621688                        'file'      => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
    622689                        'width'     => 77,
    623690                        'height'    => 100,
    624691                        'mime-type' => 'image/jpeg',
     692                        'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
    625693                );
    626694
    627695                $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
    628701                $this->assertArrayHasKey( 'test-size', $metadata['sizes'], 'The `test-size` was not added to the metadata.' );
    629702                $this->assertSame( $expected, $metadata['sizes']['test-size'] );
    630703
    class Tests_Image_Functions extends WP_UnitTestCase { 
    632705                remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );
    633706
    634707                unlink( $test_file );
    635                 $temp_dir = get_temp_dir();
    636708                foreach ( $metadata['sizes'] as $size ) {
    637709                        unlink( $temp_dir . $size['file'] );
    638710                }
  • 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 */
     7class 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}