Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion tests/phpunit/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/tests/includes/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* Tests for the `includes/functions.php` file of the Unit Testing Framework.
*
* @group testsuite
* @group phpunit
*
* @covers ::_delete_all_data
*/
class Tests_Includes_Functions extends WP_UnitTestCase {
/**
* Verify that attachment files are deleted along with attachment posts.
*
* @ticket 41978
*/
public function test_delete_all_data_deletes_attachments() {
// Create an attachment with an image.
$attachment_id = self::factory()->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 ) );
}
}
Loading