WordPress.org

Make WordPress Core

Ticket #41978: 41978.patch

File 41978.patch, 2.2 KB (added by Frank Klein, 3 years ago)
  • tests/phpunit/includes/functions.php

    diff --git tests/phpunit/includes/functions.php tests/phpunit/includes/functions.php
    index 928fcd912f..99077273e2 100644
    function _test_filter_build_unique_id($tag, $function, $priority) { 
    5151        }
    5252}
    5353
     54/**
     55 * Delete all data from the database, except:
     56 * - The default category.
     57 * - The default user.
     58 */
    5459function _delete_all_data() {
    5560        global $wpdb;
    5661
     62        // Retrieve all attachment posts, and delete them along with the attached media.
     63        $attachments = $wpdb->get_results( "SELECT ID from {$wpdb->posts} WHERE post_type = 'attachment'", ARRAY_A );
     64
     65        foreach ( $attachments as $attachment ) {
     66                wp_delete_attachment( $attachment['ID'], true );
     67        }
     68
    5769        foreach ( array(
    5870                $wpdb->posts,
    5971                $wpdb->postmeta,
    6072                $wpdb->comments,
    6173                $wpdb->commentmeta,
    6274                $wpdb->term_relationships,
    63                 $wpdb->termmeta
     75                $wpdb->termmeta,
    6476        ) as $table ) {
    6577                $wpdb->query( "DELETE FROM {$table}" );
    6678        }
    6779
    6880        foreach ( array(
    6981                $wpdb->terms,
    70                 $wpdb->term_taxonomy
     82                $wpdb->term_taxonomy,
    7183        ) as $table ) {
    7284                $wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" );
    7385        }
  • new file tests/phpunit/tests/includes/functions.php

    diff --git tests/phpunit/tests/includes/functions.php tests/phpunit/tests/includes/functions.php
    new file mode 100644
    index 0000000000..e3c3336e00
    - +  
     1<?php
     2/**
     3 * Tests for the `includes/functions.php` file of the Unit Testing Framework.
     4 *
     5 * @group testsuite
     6 */
     7
     8class Test_Includes extends WP_UnitTestCase {
     9        /**
     10         * Verify that attachment files are deleted along with attachment posts.
     11         *
     12         * @ticket 41978
     13         */
     14        public function test_delete_all_data_deletes_attachments() {
     15                // Create an attachment with an image.
     16                $attachment_id = self::factory()->attachment->create_upload_object(
     17                        DIR_TESTDATA . '/images/waffles.jpg'
     18                );
     19
     20                // Retrieve the path to the image.
     21                $attachment_file = get_attached_file( $attachment_id );
     22
     23                _delete_all_data();
     24
     25                $posts = new WP_Query( array(
     26                        'post_type'   => 'any',
     27                        'post_status' => 'any',
     28                ) );
     29
     30                // Verify that the image has been deleted along with the attachment.
     31                $this->assertSame( $posts->posts, array() );
     32                $this->assertFalse( file_exists( $attachment_file ) );
     33        }
     34}