From 95be7bce68b366e0f1e60dc6e2bc2f335d38cdf6 Mon Sep 17 00:00:00 2001 From: SirLouen Date: Thu, 5 Jun 2025 01:05:10 +0200 Subject: [PATCH 1/2] Image Exists checks --- .../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..d41a65d94e702 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_route = $upload_dir['path'] . '/' . wp_basename( $file ); + if ( ! file_exists( $file_route ) ) { + $upload = wp_upload_bits( wp_basename( $file ), null, $contents ); + } else { + $upload = array( + 'file' => $file_route, + 'url' => $upload_dir['url'] . '/' . wp_basename( $file ), + ); + } $type = ''; if ( ! empty( $upload['type'] ) ) { From 86988d7a57ea78ef32d8c640e81e817c70bd3c9c Mon Sep 17 00:00:00 2001 From: SirLouen Date: Thu, 5 Jun 2025 01:22:12 +0200 Subject: [PATCH 2/2] Little improvement in variables naming --- .../class-wp-unittest-factory-for-attachment.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 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 d41a65d94e702..4ac10494070c9 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,15 +61,16 @@ 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_dir = wp_upload_dir(); - $file_route = $upload_dir['path'] . '/' . wp_basename( $file ); - if ( ! file_exists( $file_route ) ) { - $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; + if ( ! file_exists( $file_path ) ) { + $upload = wp_upload_bits( $file_basename, null, $contents ); } else { $upload = array( - 'file' => $file_route, - 'url' => $upload_dir['url'] . '/' . wp_basename( $file ), + 'file' => $file_path, + 'url' => $upload_dir['url'] . '/' . $file_basename, ); }