Make WordPress Core

Ticket #39476: 39476.2.diff

File 39476.2.diff, 11.9 KB (added by gitlost, 8 years ago)

Use dirname(), with unit tests.

  • src/wp-includes/post.php

     
    49144914        /** This action is documented in wp-includes/post.php */
    49154915        do_action( 'deleted_post', $post_id );
    49164916
    4917         $uploadpath = wp_get_upload_dir();
     4917        $dirname = trailingslashit( dirname( $file ) );
    49184918
    49194919        if ( ! empty($meta['thumb']) ) {
    49204920                // Don't delete the thumb if another attachment uses it.
    49214921                if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) {
    4922                         $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4923                         /** This filter is documented in wp-includes/functions.php */
    4924                         $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4925                         @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4922                        wp_delete_file( $dirname . $meta['thumb'] );
    49264923                }
    49274924        }
    49284925
    49294926        // Remove intermediate and backup images if there are any.
    49304927        if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
    4931                 foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    4932                         $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4933                         /** This filter is documented in wp-includes/functions.php */
    4934                         $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4935                         @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
     4928                foreach ( $meta['sizes'] as $size ) {
     4929                        wp_delete_file( $dirname . $size['file'] );
    49364930                }
    49374931        }
    49384932
    49394933        if ( is_array($backup_sizes) ) {
    49404934                foreach ( $backup_sizes as $size ) {
    4941                         $del_file = path_join( dirname($meta['file']), $size['file'] );
    4942                         /** This filter is documented in wp-includes/functions.php */
    4943                         $del_file = apply_filters( 'wp_delete_file', $del_file );
    4944                         @ unlink( path_join($uploadpath['basedir'], $del_file) );
     4935                        wp_delete_file( $dirname . $size['file'] );
    49454936                }
    49464937        }
    49474938
  • tests/phpunit/tests/post/attachments.php

     
    5959                if ( !function_exists( 'imagejpeg' ) )
    6060                        $this->markTestSkipped( 'jpeg support unavailable' );
    6161
     62                $prev_opt_medium_size_w = get_option( 'medium_size_w' );
     63                $prev_opt_medium_size_h = get_option( 'medium_size_h' );
     64
    6265                update_option( 'medium_size_w', 0 );
    6366                update_option( 'medium_size_h', 0 );
    6467
     
    105108                $this->assertEquals( 400, $downsize[1] );
    106109                $this->assertEquals( 300, $downsize[2] );
    107110
     111                // Cleanup.
     112                update_option( 'medium_size_w', $prev_opt_medium_size_w );
     113                update_option( 'medium_size_h', $prev_opt_medium_size_h );
    108114        }
    109115
    110116        function test_insert_image_medium_sizes() {
     
    111117                if ( !function_exists( 'imagejpeg' ) )
    112118                        $this->markTestSkipped( 'jpeg support unavailable' );
    113119
     120                $prev_opt_medium_size_w = get_option( 'medium_size_w' );
     121                $prev_opt_medium_size_h = get_option( 'medium_size_h' );
     122                $prev_opt_medium_large_size_w = get_option( 'medium_large_size_w' );
     123                $prev_opt_medium_large_size_h = get_option( 'medium_large_size_h' );
     124
    114125                update_option('medium_size_w', 400);
    115126                update_option('medium_size_h', 0);
    116127
     
    162173                $this->assertEquals( '2007-06-17DSC_4173.jpg', basename($downsize[0]) );
    163174                $this->assertEquals( 680, $downsize[1] );
    164175                $this->assertEquals( 1024, $downsize[2] );
     176
     177                // Cleanup.
     178                update_option( 'medium_size_w', $prev_opt_medium_size_w );
     179                update_option( 'medium_size_h', $prev_opt_medium_size_h );
     180                update_option( 'medium_large_size_w', $prev_opt_medium_large_size_w );
     181                update_option( 'medium_large_size_h', $prev_opt_medium_large_size_h );
    165182        }
    166183
    167184
     
    169186                if ( !function_exists( 'imagejpeg' ) )
    170187                        $this->markTestSkipped( 'jpeg support unavailable' );
    171188
     189                $prev_opt_medium_size_w = get_option( 'medium_size_w' );
     190                $prev_opt_medium_size_h = get_option( 'medium_size_h' );
     191                $prev_opt_medium_large_size_w = get_option( 'medium_large_size_w' );
     192                $prev_opt_medium_large_size_h = get_option( 'medium_large_size_h' );
     193
    172194                update_option('medium_size_w', 400);
    173195                update_option('medium_size_h', 0);
    174196
     
    197219                $this->assertEquals( '2007-06-17DSC_4173-600x904.jpg', $medium_large['file'] );
    198220                $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path']) );
    199221
     222                $large = image_get_intermediate_size( $id, 'large' );
     223                $this->assertEquals( '2007-06-17DSC_4173-680x1024.jpg', $large['file'] );
     224                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $large['path'] ) );
     225
    200226                $meta = wp_get_attachment_metadata($id);
    201227                $original = $meta['file'];
    202228                $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $original) );
     
    204230                // now delete the attachment and make sure all files are gone
    205231                wp_delete_attachment($id);
    206232
    207                 $this->assertFalse( is_file($thumb['path']) );
    208                 $this->assertFalse( is_file($medium['path']) );
    209                 $this->assertFalse( is_file($medium_large['path']) );
    210                 $this->assertFalse( is_file($original) );
     233                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     234                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
     235                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
     236                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $large['path'] ) );
     237                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     238
     239                // Cleanup.
     240                update_option( 'medium_size_w', $prev_opt_medium_size_w );
     241                update_option( 'medium_size_h', $prev_opt_medium_size_h );
     242                update_option( 'medium_large_size_w', $prev_opt_medium_large_size_w );
     243                update_option( 'medium_large_size_h', $prev_opt_medium_large_size_h );
    211244        }
    212245
    213246        /**
     247         * Test delete with edited image (and backup sizes).
     248         * @ticket 39476
     249         */
     250        function test_insert_image_delete_with_edited_image() {
     251                if ( ! function_exists( 'imagejpeg' ) ) {
     252                        $this->markTestSkipped( 'jpeg support unavailable' );
     253                }
     254
     255                $prev_opt_medium_size_w = get_option( 'medium_size_w' );
     256                $prev_opt_medium_size_h = get_option( 'medium_size_h' );
     257
     258                // Generate thumbnail only.
     259                update_option( 'medium_size_w', 0 );
     260                update_option( 'medium_size_h', 0 );
     261
     262                $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' );
     263                $basename_noext = 'blah';
     264
     265                $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents );
     266                $this->assertEmpty( $upload['error'] );
     267                $basename_noext = sanitize_file_name( $basename_noext );
     268
     269                $id = $this->_make_attachment( $upload );
     270                $uploads = wp_upload_dir();
     271                $dirname = trailingslashit( dirname( get_attached_file( $id ) ) );
     272                $expected_files = array();
     273
     274                // Check that the intermediate thumbnail exists.
     275                $thumb = image_get_intermediate_size( $id, 'thumbnail' );
     276                $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] );
     277                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     278                $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'];
     279
     280                // Check that the intermediate medium doesn't exist.
     281                $medium = image_get_intermediate_size( $id, 'medium' );
     282                $this->assertEmpty( $medium );
     283
     284                $meta = wp_get_attachment_metadata( $id );
     285                $original = $meta['file'];
     286                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     287                $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $original;
     288
     289                // Create edited version.
     290                require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     291                // a2-small.jpg is 400x300, so halve.
     292                $_REQUEST = array( 'fwidth' => 200, 'fheight' => 150, 'target' => 'all', 'do' => 'scale' );
     293                $edited = wp_save_image( $id );
     294                $this->assertTrue( is_object( $edited ) );
     295                $this->assertTrue( isset( $edited->thumbnail ) );
     296                $this->assertTrue( is_file( $dirname . wp_basename( $edited->thumbnail ) ) );
     297                $expected_files[] = $dirname . wp_basename( $edited->thumbnail );
     298
     299                // Backup sizes should exist.
     300                $backup_sizes = get_post_meta( $id, '_wp_attachment_backup_sizes', true );
     301                $this->assertTrue( is_array( $backup_sizes ) );
     302                $this->assertNotEmpty( $backup_sizes['thumbnail-orig'] );
     303
     304                // And be same as prior meta.
     305                $this->assertSame( $meta['sizes']['thumbnail'], $backup_sizes['thumbnail-orig'] );
     306                $this->assertSame( wp_basename( $original ), $backup_sizes['full-orig']['file'] );
     307
     308                // But new meta should point to edited.
     309                $new_meta = wp_get_attachment_metadata( $id );
     310                $this->assertSame( $new_meta['sizes']['thumbnail']['file'], wp_basename( $edited->thumbnail ) );
     311                $this->assertNotSame( $new_meta['file'], $original );
     312                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file'] ) );
     313                $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file'];
     314
     315                // Passthru filter.
     316                add_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) );
     317
     318                // Now delete the attachment and make sure all files are gone.
     319                wp_delete_attachment( $id );
     320
     321                foreach ( $expected_files as $expected_file ) {
     322                        $this->assertFalse( is_file( $expected_file ) );
     323                }
     324
     325                $this->assertSame( sort( $expected_files ), sort( self::$wp_delete_files ) );
     326
     327                // Cleanup.
     328                remove_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) );
     329                update_option( 'medium_size_w', $prev_opt_medium_size_w );
     330                update_option( 'medium_size_h', $prev_opt_medium_size_h );
     331        }
     332
     333        static $wp_delete_files = array();
     334
     335        function wp_delete_file_passthru_filter( $file ) {
     336                $this->assertTrue( path_is_absolute( $file ) );
     337                self::$wp_delete_files[] = $file;
     338                return $file;
     339        }
     340
     341        /**
     342         * Test delete of UTF-8 filename.
     343         * @ticket 33227
     344         * @ticket 39476
     345         */
     346        function test_insert_image_delete_utf8() {
     347                if ( ! function_exists( 'imagejpeg' ) ) {
     348                        $this->markTestSkipped( 'jpeg support unavailable' );
     349                }
     350
     351                $prev_opt_medium_size_w = get_option( 'medium_size_w' );
     352                $prev_opt_medium_size_h = get_option( 'medium_size_h' );
     353
     354                // Generate thumbnail only.
     355                update_option( 'medium_size_w', 0 );
     356                update_option( 'medium_size_h', 0 );
     357
     358                $prev_ctype_locale = setlocale( LC_CTYPE, 'C' ); // In case shell env has set LC_CTYPE.
     359                $this->assertTrue( false !== $prev_ctype_locale );
     360
     361                $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' );
     362                $basename_noext = 'هم اندیشی';
     363
     364                $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents );
     365                $this->assertEmpty( $upload['error'] );
     366                $basename_noext = sanitize_file_name( $basename_noext );
     367
     368                $id = $this->_make_attachment( $upload );
     369                $uploads = wp_upload_dir();
     370
     371                // Check that the intermediate thumbnail exists.
     372                $thumb = image_get_intermediate_size( $id, 'thumbnail' );
     373                $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] );
     374                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     375
     376                // Check that the intermediate medium doesn't exist.
     377                $medium = image_get_intermediate_size( $id, 'medium' );
     378                $this->assertEmpty( $medium );
     379
     380                $meta = wp_get_attachment_metadata( $id );
     381                $original = $meta['file'];
     382                $this->assertSame( $basename_noext . '.jpg', wp_basename( $original ) );
     383                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     384
     385                // now delete the attachment and make sure all files are gone
     386                wp_delete_attachment( $id );
     387
     388                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     389                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     390
     391                // Cleanup.
     392                setlocale( LC_CTYPE, $prev_ctype_locale );
     393                update_option( 'medium_size_w', $prev_opt_medium_size_w );
     394                update_option( 'medium_size_h', $prev_opt_medium_size_h );
     395        }
     396
     397        /**
    214398         * GUID should never be empty
    215399         * @ticket 18310
    216400         * @ticket 21963