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) { |
51 | 51 | } |
52 | 52 | } |
53 | 53 | |
| 54 | /** |
| 55 | * Delete all data from the database, except: |
| 56 | * - The default category. |
| 57 | * - The default user. |
| 58 | */ |
54 | 59 | function _delete_all_data() { |
55 | 60 | global $wpdb; |
56 | 61 | |
| 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 | |
57 | 69 | foreach ( array( |
58 | 70 | $wpdb->posts, |
59 | 71 | $wpdb->postmeta, |
60 | 72 | $wpdb->comments, |
61 | 73 | $wpdb->commentmeta, |
62 | 74 | $wpdb->term_relationships, |
63 | | $wpdb->termmeta |
| 75 | $wpdb->termmeta, |
64 | 76 | ) as $table ) { |
65 | 77 | $wpdb->query( "DELETE FROM {$table}" ); |
66 | 78 | } |
67 | 79 | |
68 | 80 | foreach ( array( |
69 | 81 | $wpdb->terms, |
70 | | $wpdb->term_taxonomy |
| 82 | $wpdb->term_taxonomy, |
71 | 83 | ) as $table ) { |
72 | 84 | $wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" ); |
73 | 85 | } |
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 | |
| 8 | class 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 | } |