Wordpress/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php
Eric Andrew Lewis d36d6cc630 Build/Test Tools: Move PHP factory classes into their own files.
This makes the code easier to browse.

`factory.php` loads the new files, so this is backwards compatible in case `factory.php` is loaded directly for access to one of the classes.

See #35492.


git-svn-id: https://develop.svn.wordpress.org/trunk@36347 602fd350-edb4-49c9-b593-d223f7449a82
2016-01-19 03:55:19 +00:00

38 lines
988 B
PHP

<?php
class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
function create_object( $file, $parent = 0, $args = array() ) {
return wp_insert_attachment( $args, $file, $parent );
}
function create_upload_object( $file, $parent = 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_mime_type' => $type,
'guid' => $upload[ 'url' ],
);
// Save the data
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
return $id;
}
}