Make WordPress Core


Ignore:
Timestamp:
10/21/2015 01:58:52 AM (6 years ago)
Author:
wonderboymusic
Message:

Unit Tests: consolidate the many separate implementations of _make_attachment() into a helper method on WP_UnitTestCase.

Fixes #34075.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r35258 r35309  
    421421        $GLOBALS['wp_the_query'] = new WP_Query();
    422422        $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
    423        
     423
    424424        $public_query_vars  = $GLOBALS['wp']->public_query_vars;
    425425        $private_query_vars = $GLOBALS['wp']->private_query_vars;
     
    724724        $wp_rewrite->flush_rules();
    725725    }
     726
     727    function _make_attachment($upload, $parent_post_id = 0) {
     728        $type = '';
     729        if ( !empty($upload['type']) ) {
     730            $type = $upload['type'];
     731        } else {
     732            $mime = wp_check_filetype( $upload['file'] );
     733            if ($mime)
     734                $type = $mime['type'];
     735        }
     736
     737        $attachment = array(
     738            'post_title' => basename( $upload['file'] ),
     739            'post_content' => '',
     740            'post_type' => 'attachment',
     741            'post_parent' => $parent_post_id,
     742            'post_mime_type' => $type,
     743            'guid' => $upload[ 'url' ],
     744        );
     745
     746        // Save the data
     747        $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
     748        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
     749        return $id;
     750    }
    726751}
Note: See TracChangeset for help on using the changeset viewer.