WordPress.org

Make WordPress Core

Ticket #47868: 47868.2.diff

File 47868.2.diff, 3.3 KB (added by donmhico, 16 months ago)
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 306e567bb9..0867e04c60 100644
    function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = fa 
    54705470 * for media is disabled, item is already in the trash, or $force_delete is true.
    54715471 *
    54725472 * @since 2.0.0
     5473 * @since 5.3.0 Return `false` if `wp_delete_attachment_files()` failed.
    54735474 *
    54745475 * @global wpdb $wpdb WordPress database abstraction object.
    54755476 *
    function wp_delete_attachment( $post_id, $force_delete = false ) { 
    55455546        }
    55465547        /** This action is documented in wp-includes/post.php */
    55475548        do_action( 'deleted_post', $post_id );
    5548 
    5549         wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     5549       
     5550        $delete_file = wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     5551        if ( ! $delete_file ) {
     5552                return false;
     5553        }
    55505554
    55515555        clean_post_cache( $post );
    55525556
  • tests/phpunit/tests/post/attachments.php

    diff --git tests/phpunit/tests/post/attachments.php tests/phpunit/tests/post/attachments.php
    index 70255d5bec..8b422e3456 100644
    class Tests_Post_Attachments extends WP_UnitTestCase { 
    529529
    530530                $this->assertContains( 'images/media/video.png', $icon );
    531531        }
     532
     533        /**
     534         * @ticket 47868
     535         */
     536        function test_wp_delete_attachment_return_false_if_file_not_deleted() {
     537                $filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' );
     538                $contents = file_get_contents( $filename );
     539
     540                $upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
     541                $this->assertTrue( empty( $upload['error'] ) );
     542
     543                $attachment_id = $this->_make_attachment( $upload );
     544
     545                // Corrupt the data by removing `_wp_attached_file` and `_wp_attachment_metadata` metas
     546                // of the newly created attachment post.
     547                delete_post_meta( $attachment_id, '_wp_attached_file' );
     548                delete_post_meta( $attachment_id, '_wp_attachment_metadata' );
     549
     550                $delete_attachment = wp_delete_attachment( $attachment_id );
     551
     552                // Since the metas are corrupted, the file wouldn't be deleted.
     553                $this->assertTrue( file_exists( $filename ) );
     554
     555                // And since no file was deleted, `wp_delete_attachment()` should return false.
     556                $this->assertFalse( $delete_attachment );
     557        }
     558
    532559}
  • tests/phpunit/tests/rest-api/rest-attachments-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php
    index 556cc6c8be..80a10cfe6a 100644
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    12221222
    12231223        public function test_delete_item() {
    12241224                wp_set_current_user( self::$editor_id );
    1225                 $attachment_id    = $this->factory->attachment->create_object(
    1226                         $this->test_file,
    1227                         0,
    1228                         array(
    1229                                 'post_mime_type' => 'image/jpeg',
    1230                                 'post_excerpt'   => 'A sample caption',
    1231                         )
    1232                 );
     1225
     1226                $contents = file_get_contents( $this->test_file );
     1227                $upload = wp_upload_bits( wp_basename( $this->test_file ), null, $contents );
     1228               
     1229                $this->assertTrue( empty( $upload['error'] ) );
     1230
     1231                $attachment_id = $this->_make_attachment( $upload );
     1232
    12331233                $request          = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id );
    12341234                $request['force'] = true;
    12351235                $response         = rest_get_server()->dispatch( $request );