Skip to content

Commit 2c6ec19

Browse files
committed
Build/Test Tools: Delete attachment files during test cleanup.
Delete attachments before the bulk database cleanup so their uploaded files and generated image sizes are removed while attachment metadata is still available. Preserve the existing bulk SQL cleanup for other records. Add regression coverage for attachment file and post deletion. Developed in: WordPress#8898 Props sirlouen, johnbillion, adrianmoldovanwp. Fixes #41978. git-svn-id: https://develop.svn.wordpress.org/trunk@63586 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0ee9f30 commit 2c6ec19

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎tests/phpunit/includes/functions.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,19 @@ function _test_filter_build_unique_id( $hook_name, $callback, $priority ) {
105105
}
106106

107107
/**
108-
* Deletes all data from the database.
108+
* Deletes all data from the database, except:
109+
* - The default category.
110+
* - The default user.
109111
*/
110112
function _delete_all_data() {
111113
global $wpdb;
112114

115+
// Retrieve all attachment posts, and delete them along with the attached media.
116+
$attachments = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment'" );
117+
foreach ( $attachments as $attachment ) {
118+
wp_delete_attachment( $attachment, true );
119+
}
120+
113121
foreach ( array(
114122
$wpdb->posts,
115123
$wpdb->postmeta,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* Tests for the `includes/functions.php` file of the Unit Testing Framework.
5+
*
6+
* @group testsuite
7+
* @group phpunit
8+
*
9+
* @covers ::_delete_all_data
10+
*/
11+
class Tests_Includes_Functions extends WP_UnitTestCase {
12+
/**
13+
* Verify that attachment files are deleted along with attachment posts.
14+
*
15+
* @ticket 41978
16+
*/
17+
public function test_delete_all_data_deletes_attachments() {
18+
// Create an attachment with an image.
19+
$attachment_id = self::factory()->attachment->create_upload_object(
20+
DIR_TESTDATA . '/images/waffles.jpg'
21+
);
22+
23+
// Retrieve the path to the image.
24+
$attachment_file = get_attached_file( $attachment_id );
25+
$this->assertFileExists( $attachment_file );
26+
27+
_delete_all_data();
28+
29+
// Verify that the image has been deleted along with the attachment.
30+
$this->assertFileDoesNotExist( $attachment_file );
31+
$this->assertNull( get_post( $attachment_id ) );
32+
}
33+
}

0 commit comments

Comments
 (0)