From 99d7bf0430b142452853c155c812e57dfbba01cc Mon Sep 17 00:00:00 2001 From: Nikunj Hatkar Date: Fri, 6 Jun 2025 14:52:04 +0530 Subject: [PATCH 1/3] Added tests --- .../class-wp-unittest-factory-for-attachment.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php index 262c6c4640790..b65ac206d8fd7 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php @@ -61,8 +61,17 @@ public function create_object( $args, $legacy_parent = 0, $legacy_args = array() * @return int|WP_Error The attachment ID on success, WP_Error object on failure. */ public function create_upload_object( $file, $parent_post_id = 0 ) { - $contents = file_get_contents( $file ); - $upload = wp_upload_bits( wp_basename( $file ), null, $contents ); + $contents = file_get_contents( $file ); + $upload_dir = wp_upload_dir(); + $file_basename = wp_basename( $file ); + $file_path = $upload_dir['path'] . '/' . $file_basename; + + // Remove existing file if it exists + if ( file_exists( $file_path ) ) { + unlink( $file_path ); + } + + $upload = wp_upload_bits( $file_basename, null, $contents ); $type = ''; if ( ! empty( $upload['type'] ) ) { From 29c5d883ef0418fbd8ffc5311f8d2a86196c5448 Mon Sep 17 00:00:00 2001 From: Nikunj Hatkar Date: Fri, 6 Jun 2025 14:59:22 +0530 Subject: [PATCH 2/3] Make PHPCS happy --- .../factory/class-wp-unittest-factory-for-attachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php index b65ac206d8fd7..148ebe4024793 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php @@ -70,7 +70,7 @@ public function create_upload_object( $file, $parent_post_id = 0 ) { if ( file_exists( $file_path ) ) { unlink( $file_path ); } - + $upload = wp_upload_bits( $file_basename, null, $contents ); $type = ''; From 4cb09ac6697bf05152c048a2055f5f07fffe566c Mon Sep 17 00:00:00 2001 From: Nikunj Hatkar Date: Fri, 6 Jun 2025 17:58:36 +0530 Subject: [PATCH 3/3] Added full stop in comment for PHPCS compliance --- .../factory/class-wp-unittest-factory-for-attachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php index 148ebe4024793..989275879df1c 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php @@ -66,7 +66,7 @@ public function create_upload_object( $file, $parent_post_id = 0 ) { $file_basename = wp_basename( $file ); $file_path = $upload_dir['path'] . '/' . $file_basename; - // Remove existing file if it exists + // Remove existing file if it exists. if ( file_exists( $file_path ) ) { unlink( $file_path ); }