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
This commit is contained in:
parent
ede4050ace
commit
f5921a0c98
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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',
|
||||
|
@ -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() {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user