diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index d27af5c172a7a..473ed341e9571 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -105,11 +105,19 @@ function _test_filter_build_unique_id( $hook_name, $callback, $priority ) { } /** - * Deletes all data from the database. + * Deletes all data from the database, except: + * - The default category. + * - The default user. */ function _delete_all_data() { global $wpdb; + // Retrieve all attachment posts, and delete them along with the attached media. + $attachments = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment'" ); + foreach ( $attachments as $attachment ) { + wp_delete_attachment( $attachment, true ); + } + foreach ( array( $wpdb->posts, $wpdb->postmeta, diff --git a/tests/phpunit/tests/includes/functions.php b/tests/phpunit/tests/includes/functions.php new file mode 100644 index 0000000000000..df5fc31a6156e --- /dev/null +++ b/tests/phpunit/tests/includes/functions.php @@ -0,0 +1,33 @@ +attachment->create_upload_object( + DIR_TESTDATA . '/images/waffles.jpg' + ); + + // Retrieve the path to the image. + $attachment_file = get_attached_file( $attachment_id ); + $this->assertFileExists( $attachment_file ); + + _delete_all_data(); + + // Verify that the image has been deleted along with the attachment. + $this->assertFileDoesNotExist( $attachment_file ); + $this->assertNull( get_post( $attachment_id ) ); + } +}