From 1f336b6ccb4bffdaa423fda55fa44e6d86e1ee3b Mon Sep 17 00:00:00 2001 From: SirLouen Date: Wed, 4 Jun 2025 21:31:53 +0200 Subject: [PATCH 1/2] Refreshing 41978.patch --- tests/phpunit/includes/functions.php | 10 ++++++- tests/phpunit/tests/includes/functions.php | 34 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/includes/functions.php diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index c2976dba9fe41..04bda48d9c115 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_results( "SELECT ID from {$wpdb->posts} WHERE post_type = 'attachment'", ARRAY_A ); + foreach ( $attachments as $attachment ) { + wp_delete_attachment( $attachment['ID'], 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..e3c3336e008f9 --- /dev/null +++ b/tests/phpunit/tests/includes/functions.php @@ -0,0 +1,34 @@ +attachment->create_upload_object( + DIR_TESTDATA . '/images/waffles.jpg' + ); + + // Retrieve the path to the image. + $attachment_file = get_attached_file( $attachment_id ); + + _delete_all_data(); + + $posts = new WP_Query( array( + 'post_type' => 'any', + 'post_status' => 'any', + ) ); + + // Verify that the image has been deleted along with the attachment. + $this->assertSame( $posts->posts, array() ); + $this->assertFalse( file_exists( $attachment_file ) ); + } +} From 640c25ff66540a4a564f7615eb36915ede98d7dc Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Thu, 10 Sep 2026 08:24:01 -0700 Subject: [PATCH 2/2] Tests: Tighten attachment cleanup and regression coverage --- tests/phpunit/includes/functions.php | 4 ++-- tests/phpunit/tests/includes/functions.php | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index b75aa3890e41c..473ed341e9571 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -113,9 +113,9 @@ function _delete_all_data() { global $wpdb; // Retrieve all attachment posts, and delete them along with the attached media. - $attachments = $wpdb->get_results( "SELECT ID from {$wpdb->posts} WHERE post_type = 'attachment'", ARRAY_A ); + $attachments = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment'" ); foreach ( $attachments as $attachment ) { - wp_delete_attachment( $attachment['ID'], true ); + wp_delete_attachment( $attachment, true ); } foreach ( array( diff --git a/tests/phpunit/tests/includes/functions.php b/tests/phpunit/tests/includes/functions.php index e3c3336e008f9..df5fc31a6156e 100644 --- a/tests/phpunit/tests/includes/functions.php +++ b/tests/phpunit/tests/includes/functions.php @@ -1,11 +1,14 @@ assertFileExists( $attachment_file ); _delete_all_data(); - $posts = new WP_Query( array( - 'post_type' => 'any', - 'post_status' => 'any', - ) ); - // Verify that the image has been deleted along with the attachment. - $this->assertSame( $posts->posts, array() ); - $this->assertFalse( file_exists( $attachment_file ) ); + $this->assertFileDoesNotExist( $attachment_file ); + $this->assertNull( get_post( $attachment_id ) ); } }