diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index be96f06e31c7c..796421d9d2f16 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -1526,50 +1526,6 @@ public function set_permalink_structure( $structure = '' ) { $wp_rewrite->flush_rules(); } - /** - * Creates an attachment post from an uploaded file. - * - * @since 4.4.0 - * @since 6.2.0 Returns a WP_Error object on failure. - * - * @param array $upload Array of information about the uploaded file, provided by wp_upload_bits(). - * @param int $parent_post_id Optional. Parent post ID. - * @return int|WP_Error The attachment ID on success, WP_Error object on failure. - */ - public function _make_attachment( $upload, $parent_post_id = 0 ) { - $type = ''; - if ( ! empty( $upload['type'] ) ) { - $type = $upload['type']; - } else { - $mime = wp_check_filetype( $upload['file'] ); - if ( $mime ) { - $type = $mime['type']; - } - } - - $attachment = array( - 'post_title' => wp_basename( $upload['file'] ), - 'post_content' => '', - 'post_type' => 'attachment', - 'post_parent' => $parent_post_id, - 'post_mime_type' => $type, - 'guid' => $upload['url'], - ); - - $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id, true ); - - if ( is_wp_error( $attachment_id ) ) { - return $attachment_id; - } - - wp_update_attachment_metadata( - $attachment_id, - wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) - ); - - return $attachment_id; - } - /** * Updates the modified and modified GMT date of a post in the database. * diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index 1113a87eeaa01..4bc23626de8c8 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -331,6 +331,7 @@ function wp_tests_options( $value ) { require __DIR__ . '/class-wp-sitemaps-test-provider.php'; require __DIR__ . '/class-wp-sitemaps-empty-test-provider.php'; require __DIR__ . '/class-wp-sitemaps-large-test-provider.php'; +require __DIR__ . '/trait-runs-file-upload-tests.php'; /** * A class to handle additional command line arguments passed to the script. diff --git a/tests/phpunit/includes/trait-runs-file-upload-tests.php b/tests/phpunit/includes/trait-runs-file-upload-tests.php new file mode 100644 index 0000000000000..4e777696e4558 --- /dev/null +++ b/tests/phpunit/includes/trait-runs-file-upload-tests.php @@ -0,0 +1,68 @@ + wp_basename( $upload['file'] ), + 'post_content' => '', + 'post_type' => 'attachment', + 'post_parent' => $parent_post_id, + 'post_mime_type' => $type, + 'guid' => $upload['url'], + ); + + $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id, true ); + + if ( is_wp_error( $attachment_id ) ) { + return $attachment_id; + } + + wp_update_attachment_metadata( + $attachment_id, + wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) + ); + + return $attachment_id; + } + + /** + * Uploads given file and creates an attachment post from it. + * + * @since 6.2.0 + * + * @param array $filename Absolute path to the file to upload. + * @param int $parent_post_id Optional. Parent post ID. + * + * @return int|WP_Error The attachment ID on success, WP_Error object on failure. + */ + public function _upload_file_and_make_attachment( $filename, $parent_post_id = 0 ) { + $contents = file_get_contents( $filename ); + $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); + + return $this->_make_attachment( $upload, $parent_post_id ); + } +} diff --git a/tests/phpunit/tests/ajax/wpAjaxCropImage.php b/tests/phpunit/tests/ajax/wpAjaxCropImage.php index 3a23f3a425b50..d21a617600828 100644 --- a/tests/phpunit/tests/ajax/wpAjaxCropImage.php +++ b/tests/phpunit/tests/ajax/wpAjaxCropImage.php @@ -16,6 +16,8 @@ */ class Tests_Ajax_WpAjaxCropImage extends WP_Ajax_UnitTestCase { + use WP_Test_RunsFileUploadTests; + /** * @var WP_Post|null */ diff --git a/tests/phpunit/tests/ajax/wpAjaxImageEditor.php b/tests/phpunit/tests/ajax/wpAjaxImageEditor.php index f09721b74fd42..c89518be2d9a5 100644 --- a/tests/phpunit/tests/ajax/wpAjaxImageEditor.php +++ b/tests/phpunit/tests/ajax/wpAjaxImageEditor.php @@ -19,6 +19,8 @@ */ class Tests_Ajax_wpAjaxImageEditor extends WP_Ajax_UnitTestCase { + use WP_Test_RunsFileUploadTests; + /** * Tear down the test fixture. */ @@ -38,11 +40,7 @@ public function tear_down() { public function testCropImageThumbnail() { require_once ABSPATH . 'wp-admin/includes/image-edit.php'; - $filename = DIR_TESTDATA . '/images/canola.jpg'; - $contents = file_get_contents( $filename ); - - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - $id = $this->_make_attachment( $upload ); + $id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/canola.jpg' ); $_REQUEST['action'] = 'image-editor'; $_REQUEST['context'] = 'edit-attachment'; @@ -73,11 +71,7 @@ public function testImageEditOverwriteConstant() { require_once ABSPATH . 'wp-admin/includes/image-edit.php'; - $filename = DIR_TESTDATA . '/images/canola.jpg'; - $contents = file_get_contents( $filename ); - - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - $id = $this->_make_attachment( $upload ); + $id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/canola.jpg' ); $_REQUEST['action'] = 'image-editor'; $_REQUEST['context'] = 'edit-attachment'; diff --git a/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php b/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php index 277ce3d299c26..24728a48de1c5 100644 --- a/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php +++ b/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php @@ -13,6 +13,8 @@ */ class Tests_Ajax_wpAjaxSendAttachmentToEditor extends WP_Ajax_UnitTestCase { + use WP_Test_RunsFileUploadTests; + /** * @ticket 36578 * @@ -22,11 +24,7 @@ public function test_wp_ajax_send_attachment_to_editor_should_return_an_image() // Become an administrator. $this->_setRole( 'administrator' ); - $filename = DIR_TESTDATA . '/images/canola.jpg'; - $contents = file_get_contents( $filename ); - - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - $attachment = $this->_make_attachment( $upload ); + $attachment = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/canola.jpg' ); // Set up a default request. $_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' ); @@ -65,11 +63,7 @@ public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() { // Become an administrator. $this->_setRole( 'administrator' ); - $filename = DIR_TESTDATA . '/formatting/entities.txt'; - $contents = file_get_contents( $filename ); - - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - $attachment = $this->_make_attachment( $upload ); + $attachment = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/formatting/entities.txt' ); // Set up a default request. $_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' ); diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index 8a29853526960..381cf9b20a283 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -10,6 +10,9 @@ require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php'; class Tests_General_Template extends WP_UnitTestCase { + + use WP_Test_RunsFileUploadTests; + protected $wp_site_icon; public $site_icon_id; public $site_icon_url; diff --git a/tests/phpunit/tests/image/intermediateSize.php b/tests/phpunit/tests/image/intermediateSize.php index 830359427a8fd..11f64abfeee10 100644 --- a/tests/phpunit/tests/image/intermediateSize.php +++ b/tests/phpunit/tests/image/intermediateSize.php @@ -5,6 +5,9 @@ * @group upload */ class Tests_Image_Intermediate_Size extends WP_UnitTestCase { + + use WP_Test_RunsFileUploadTests; + public function tear_down() { $this->remove_added_uploads(); @@ -15,13 +18,6 @@ public function tear_down() { parent::tear_down(); } - public function _make_attachment( $file, $parent_post_id = 0 ) { - $contents = file_get_contents( $file ); - $upload = wp_upload_bits( wp_basename( $file ), null, $contents ); - - return parent::_make_attachment( $upload, $parent_post_id ); - } - public function test_make_intermediate_size_no_size() { $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false ); @@ -96,7 +92,7 @@ public function test_get_intermediate_sizes_by_name() { add_image_size( 'test-size', 330, 220, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); // Look for a size by name. $image = image_get_intermediate_size( $id, 'test-size' ); @@ -120,7 +116,7 @@ public function test_get_intermediate_sizes_by_array_exact() { add_image_size( 'false-width', 600, 220, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); // Look for a size by array that exists. // Note: Staying larger than 300px to miss default medium crop. @@ -143,7 +139,7 @@ public function test_get_intermediate_sizes_by_array_nearest() { add_image_size( 'false-width', 150, 220, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); // Look for a size by array that doesn't exist. // Note: Staying larger than 300px to miss default medium crop. @@ -165,7 +161,7 @@ public function test_get_intermediate_sizes_by_array_nearest_false() { add_image_size( 'false-width', 150, 220, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); // Look for a size by array that doesn't exist. // Note: Staying larger than 300px to miss default medium crop. @@ -189,7 +185,7 @@ public function test_get_intermediate_sizes_by_array_zero_height() { add_image_size( 'false-height', $width, 100, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); $original = wp_get_attachment_metadata( $id ); $image_w = $width; @@ -218,7 +214,7 @@ public function test_get_intermediate_sizes_by_array_zero_width() { add_image_size( 'false-height', 300, $height, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); $original = wp_get_attachment_metadata( $id ); $image_h = $height; @@ -245,7 +241,7 @@ public function test_get_intermediate_sizes_should_match_size_with_off_by_one_as add_image_size( 'off-by-one', $width, $height, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); $original = wp_get_attachment_metadata( $id ); $image_h = $height; @@ -267,7 +263,7 @@ public function test_get_intermediate_size_with_small_size_array() { add_image_size( 'test-size', 200, 100, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); // Request a size by array that doesn't exist and is smaller than the 'thumbnail'. $image = image_get_intermediate_size( $id, array( 50, 25 ) ); @@ -282,7 +278,7 @@ public function test_get_intermediate_size_with_small_size_array() { */ public function test_get_intermediate_size_with_small_size_array_fallback() { $file = DIR_TESTDATA . '/images/waffles.jpg'; - $id = $this->_make_attachment( $file, 0 ); + $id = $this->_upload_file_and_make_attachment( $file ); $original = wp_get_attachment_metadata( $id ); $thumbnail_file = $original['sizes']['thumbnail']['file']; diff --git a/tests/phpunit/tests/image/siteIcon.php b/tests/phpunit/tests/image/siteIcon.php index 369aed06f9700..3c3c23dedcb2d 100644 --- a/tests/phpunit/tests/image/siteIcon.php +++ b/tests/phpunit/tests/image/siteIcon.php @@ -8,6 +8,9 @@ require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php'; class Tests_WP_Site_Icon extends WP_UnitTestCase { + + use WP_Test_RunsFileUploadTests; + protected $wp_site_icon; public $attachment_id = 0; @@ -160,12 +163,7 @@ private function insert_attachment() { return $this->attachment_id; } - $filename = DIR_TESTDATA . '/images/test-image.jpg'; - $contents = file_get_contents( $filename ); - - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - - $this->attachment_id = $this->_make_attachment( $upload ); + $this->attachment_id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/test-image.jpg' ); return $this->attachment_id; } } diff --git a/tests/phpunit/tests/post/attachments.php b/tests/phpunit/tests/post/attachments.php index 2922c185d28ee..45f833a1b675c 100644 --- a/tests/phpunit/tests/post/attachments.php +++ b/tests/phpunit/tests/post/attachments.php @@ -7,6 +7,8 @@ */ class Tests_Post_Attachments extends WP_UnitTestCase { + use WP_Test_RunsFileUploadTests; + public function tear_down() { // Remove all uploads. $this->remove_added_uploads(); @@ -441,11 +443,7 @@ public function test_wp_get_attachment_url_should_force_https_when_administering } public function test_wp_attachment_is() { - $filename = DIR_TESTDATA . '/images/test-image.jpg'; - $contents = file_get_contents( $filename ); - - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - $attachment_id = $this->_make_attachment( $upload ); + $attachment_id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/test-image.jpg' ); $this->assertTrue( wp_attachment_is_image( $attachment_id ) ); $this->assertTrue( wp_attachment_is( 'image', $attachment_id ) ); @@ -459,11 +457,7 @@ public function test_wp_attachment_is_default() { add_filter( 'upload_mimes', array( $this, 'allow_psd_mime_type' ), 10, 2 ); } - $filename = DIR_TESTDATA . '/images/test-image.psd'; - $contents = file_get_contents( $filename ); - - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - $attachment_id = $this->_make_attachment( $upload ); + $attachment_id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/test-image.psd' ); $this->assertFalse( wp_attachment_is_image( $attachment_id ) ); $this->assertTrue( wp_attachment_is( 'psd', $attachment_id ) ); diff --git a/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php b/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php index fe1cd13f20a76..30b9d72a28fd9 100644 --- a/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php +++ b/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php @@ -5,6 +5,9 @@ * @requires function imagejpeg */ class Tests_XMLRPC_wp_getMediaItem extends WP_XMLRPC_UnitTestCase { + + use WP_Test_RunsFileUploadTests; + protected static $post_id; public $attachment_data; @@ -19,11 +22,7 @@ public function set_up() { add_theme_support( 'post-thumbnails' ); - $filename = ( DIR_TESTDATA . '/images/waffles.jpg' ); - $contents = file_get_contents( $filename ); - $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); - - $this->attachment_id = $this->_make_attachment( $upload, self::$post_id ); + $this->attachment_id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/waffles.jpg', self::$post_id ); $this->attachment_data = get_post( $this->attachment_id, ARRAY_A ); set_post_thumbnail( self::$post_id, $this->attachment_id );