From f5921a0c980d00a03348d01d9ef3ed11c9a87412 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 21 Oct 2015 01:58:52 +0000 Subject: [PATCH] Unit Tests: consolidate the many separate implementations of `_make_attachment()` into a helper method on `WP_UnitTestCase`. Fixes #34075. git-svn-id: https://develop.svn.wordpress.org/trunk@35309 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/testcase.php | 27 ++++++++++- tests/phpunit/tests/ajax/MediaEdit.php | 46 +------------------ tests/phpunit/tests/general/template.php | 22 ++------- tests/phpunit/tests/image/header.php | 4 -- .../phpunit/tests/image/intermediate_size.php | 33 +------------ tests/phpunit/tests/image/site_icon.php | 22 +-------- tests/phpunit/tests/post/attachments.php | 35 +------------- .../phpunit/tests/xmlrpc/wp/getMediaItem.php | 16 ++----- 8 files changed, 39 insertions(+), 166 deletions(-) diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index b666515d3f..da4d5b9143 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -420,7 +420,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']); $GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; - + $public_query_vars = $GLOBALS['wp']->public_query_vars; $private_query_vars = $GLOBALS['wp']->private_query_vars; @@ -723,4 +723,29 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $wp_rewrite->set_permalink_structure( $structure ); $wp_rewrite->flush_rules(); } + + 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' => basename( $upload['file'] ), + 'post_content' => '', + 'post_type' => 'attachment', + 'post_parent' => $parent_post_id, + 'post_mime_type' => $type, + 'guid' => $upload[ 'url' ], + ); + + // Save the data + $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); + wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); + return $id; + } } diff --git a/tests/phpunit/tests/ajax/MediaEdit.php b/tests/phpunit/tests/ajax/MediaEdit.php index d5c99ad00d..7ef94694b2 100644 --- a/tests/phpunit/tests/ajax/MediaEdit.php +++ b/tests/phpunit/tests/ajax/MediaEdit.php @@ -14,59 +14,15 @@ require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); */ class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase { - /** - * List of media thumbnail ids - * @var array - */ - protected $_ids = array(); - - /** - * Set up the test fixture. - */ - public function setUp() { - parent::setUp(); - } - /** * Tear down the test fixture. */ public function tearDown() { // Cleanup - foreach ( $this->_ids as $id ) { - wp_delete_attachment( $id, true ); - } - + $this->remove_added_uploads(); parent::tearDown(); } - /** - * Function snagged from ./tests/post/attachments.php - */ - 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' => basename( $upload['file'] ), - 'post_content' => '', - 'post_type' => 'attachment', - 'post_parent' => $parent_post_id, - 'post_mime_type' => $type, - 'guid' => $upload[ 'url' ], - ); - - // Save the data - $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); - return $this->_ids[] = $id; - } - /** * @ticket 22985 */ diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index f935a1b121..0485468e00 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -154,27 +154,11 @@ class Tests_General_Template extends WP_UnitTestCase { $contents = file_get_contents( $filename ); $upload = wp_upload_bits( basename( $filename ), null, $contents ); - $type = ''; - if ( ! empty( $upload['type'] ) ) { - $type = $upload['type']; - } else { - $mime = wp_check_filetype( $upload['file'] ); - if ( $mime ) { - $type = $mime['type']; - } - } + $this->site_icon_url = $upload['url']; - $attachment = array( - 'post_title' => basename( $upload['file'] ), - 'post_content' => $upload['url'], - 'post_type' => 'attachment', - 'post_mime_type' => $type, - 'guid' => $upload['url'], - ); // Save the data - $this->site_icon_url = $upload['url']; - $this->site_icon_id = wp_insert_attachment( $attachment, $upload['file'] ); - wp_update_attachment_metadata( $this->site_icon_id, wp_generate_attachment_metadata( $this->site_icon_id, $upload['file'] ) ); + $this->site_icon_id = $this->_make_attachment( $upload ); + return $this->site_icon_id; } } diff --git a/tests/phpunit/tests/image/header.php b/tests/phpunit/tests/image/header.php index fba67d5d1a..cfa21fcb6e 100644 --- a/tests/phpunit/tests/image/header.php +++ b/tests/phpunit/tests/image/header.php @@ -104,8 +104,6 @@ class Tests_Image_Header extends WP_UnitTestCase { } function test_create_attachment_object() { - global $custom_image_header; - $id = wp_insert_attachment( array( 'post_status' => 'publish', 'post_title' => 'foo.png', @@ -123,8 +121,6 @@ class Tests_Image_Header extends WP_UnitTestCase { } function test_insert_cropped_attachment() { - global $custom_image_header; - $id = wp_insert_attachment( array( 'post_status' => 'publish', 'post_title' => 'foo.png', diff --git a/tests/phpunit/tests/image/intermediate_size.php b/tests/phpunit/tests/image/intermediate_size.php index d613b9d4d2..3cb8a50f80 100644 --- a/tests/phpunit/tests/image/intermediate_size.php +++ b/tests/phpunit/tests/image/intermediate_size.php @@ -5,45 +5,16 @@ * @group upload */ class Tests_Image_Intermediate_Size extends WP_UnitTestCase { - protected $ids = array(); - function tearDown() { $this->remove_added_uploads(); parent::tearDown(); } - /** - * Upload files and create attachements for testing - */ - private function _make_attachment( $file, $parent_post_id = 0 ) { + public function _make_attachment( $file, $parent_post_id = 0 ) { $contents = file_get_contents( $file ); $upload = wp_upload_bits( basename( $file ), null, $contents ); - $type = ''; - if ( ! empty( $upload['type'] ) ) { - $type = $upload['type']; - } else { - $mime = wp_check_filetype( $upload['file'] ); - if ( $mime ) { - $type = $mime['type']; - } - } - - $attachment = array( - 'post_title' => basename( $upload['file'] ), - 'post_content' => '', - 'post_type' => 'attachment', - 'post_parent' => $parent_post_id, - 'post_mime_type' => $type, - 'guid' => $upload['url'], - ); - - // Save the data - $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); - - $this->ids[] = $id; - return $id; + return parent::_make_attachment( $upload, $parent_post_id ); } function test_make_intermediate_size_no_size() { diff --git a/tests/phpunit/tests/image/site_icon.php b/tests/phpunit/tests/image/site_icon.php index cd30bf618a..be75830966 100644 --- a/tests/phpunit/tests/image/site_icon.php +++ b/tests/phpunit/tests/image/site_icon.php @@ -158,28 +158,8 @@ class Tests_WP_Site_Icon extends WP_UnitTestCase { $contents = file_get_contents( $filename ); $upload = wp_upload_bits( basename( $filename ), null, $contents ); - $type = ''; - if ( ! empty( $upload['type'] ) ) { - $type = $upload['type']; - } else { - $mime = wp_check_filetype( $upload['file'] ); - if ( $mime ) { - $type = $mime['type']; - } - } - - $attachment = array( - 'post_title' => basename( $upload['file'] ), - 'post_content' => $upload['url'], - 'post_type' => 'attachment', - 'post_mime_type' => $type, - 'guid' => $upload['url'], - ); - - // Save the data - $this->attachment_id = wp_insert_attachment( $attachment, $upload['file'] ); - wp_update_attachment_metadata( $this->attachment_id, wp_generate_attachment_metadata( $this->attachment_id, $upload['file'] ) ); + $this->attachment_id = $this->_make_attachment( $upload ); return $this->attachment_id; } } diff --git a/tests/phpunit/tests/post/attachments.php b/tests/phpunit/tests/post/attachments.php index 498f27652b..dfe89eabaf 100644 --- a/tests/phpunit/tests/post/attachments.php +++ b/tests/phpunit/tests/post/attachments.php @@ -6,7 +6,6 @@ * @group upload */ class Tests_Post_Attachments extends WP_UnitTestCase { - protected $ids = array(); function tearDown() { // Remove all uploads. @@ -14,42 +13,12 @@ class Tests_Post_Attachments extends WP_UnitTestCase { parent::tearDown(); } - 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' => basename( $upload['file'] ), - 'post_content' => '', - 'post_type' => 'attachment', - 'post_parent' => $parent_post_id, - 'post_mime_type' => $type, - 'guid' => $upload[ 'url' ], - ); - - // Save the data - $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); - - return $this->ids[] = $id; - - } - function test_insert_bogus_image() { - $filename = rand_str().'.jpg'; + $filename = rand_str() . '.jpg'; $contents = rand_str(); - $upload = wp_upload_bits($filename, null, $contents); + $upload = wp_upload_bits( $filename, null, $contents ); $this->assertTrue( empty($upload['error']) ); - - $id = $this->_make_attachment($upload); } function test_insert_image_no_thumb() { diff --git a/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php b/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php index e08fdc3460..8fdd3ed1fb 100644 --- a/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php +++ b/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php @@ -22,19 +22,9 @@ class Tests_XMLRPC_wp_getMediaItem extends WP_XMLRPC_UnitTestCase { $filename = ( DIR_TESTDATA.'/images/waffles.jpg' ); $contents = file_get_contents( $filename ); $upload = wp_upload_bits(basename($filename), null, $contents); - $mime = wp_check_filetype( $filename ); - $this->attachment_data = array( - 'post_title' => basename( $upload['file'] ), - 'post_content' => '', - 'post_type' => 'attachment', - 'post_parent' => $this->post_id, - 'post_mime_type' => $mime['type'], - 'guid' => $upload[ 'url' ] - ); - $id = wp_insert_attachment( $this->attachment_data, $upload[ 'file' ], $this->post_id ); - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); - $this->attachment_id = $id; + $this->attachment_id = $this->_make_attachment( $upload, $this->post_id ); + $this->attachment_data = get_post( $this->attachment_id, ARRAY_A ); set_post_thumbnail( $this->post_id, $this->attachment_id ); } @@ -42,6 +32,8 @@ class Tests_XMLRPC_wp_getMediaItem extends WP_XMLRPC_UnitTestCase { function tearDown() { remove_theme_support( 'post-thumbnails' ); + $this->remove_added_uploads(); + parent::tearDown(); }