Make WordPress Core

Changeset 35309


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

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

Fixes #34075.

Location:
trunk/tests/phpunit
Files:
8 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}
  • trunk/tests/phpunit/tests/ajax/MediaEdit.php

    r29120 r35309  
    1616
    1717    /**
    18      * List of media thumbnail ids
    19      * @var array
    20      */
    21     protected $_ids = array();
    22 
    23     /**
    24      * Set up the test fixture.
    25      */
    26     public function setUp() {
    27         parent::setUp();
    28     }
    29 
    30     /**
    3118     * Tear down the test fixture.
    3219     */
    3320    public function tearDown() {
    3421        // Cleanup
    35         foreach ( $this->_ids as $id ) {
    36             wp_delete_attachment( $id, true );
    37         }
    38 
     22        $this->remove_added_uploads();
    3923        parent::tearDown();
    40     }
    41 
    42     /**
    43      * Function snagged from ./tests/post/attachments.php
    44      */
    45     function _make_attachment($upload, $parent_post_id = 0) {
    46         $type = '';
    47         if ( !empty($upload['type']) ) {
    48             $type = $upload['type'];
    49         } else {
    50             $mime = wp_check_filetype( $upload['file'] );
    51             if ($mime)
    52                 $type = $mime['type'];
    53         }
    54 
    55         $attachment = array(
    56             'post_title' => basename( $upload['file'] ),
    57             'post_content' => '',
    58             'post_type' => 'attachment',
    59             'post_parent' => $parent_post_id,
    60             'post_mime_type' => $type,
    61             'guid' => $upload[ 'url' ],
    62         );
    63 
    64         // Save the data
    65         $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
    66         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    67         return $this->_ids[] = $id;
    6824    }
    6925
  • trunk/tests/phpunit/tests/general/template.php

    r34983 r35309  
    155155
    156156        $upload = wp_upload_bits( basename( $filename ), null, $contents );
    157         $type   = '';
    158         if ( ! empty( $upload['type'] ) ) {
    159             $type = $upload['type'];
    160         } else {
    161             $mime = wp_check_filetype( $upload['file'] );
    162             if ( $mime ) {
    163                 $type = $mime['type'];
    164             }
    165         }
     157        $this->site_icon_url = $upload['url'];
    166158
    167         $attachment = array(
    168             'post_title'     => basename( $upload['file'] ),
    169             'post_content'   => $upload['url'],
    170             'post_type'      => 'attachment',
    171             'post_mime_type' => $type,
    172             'guid'           => $upload['url'],
    173         );
    174159
    175160        // Save the data
    176         $this->site_icon_url = $upload['url'];
    177         $this->site_icon_id  = wp_insert_attachment( $attachment, $upload['file'] );
    178         wp_update_attachment_metadata( $this->site_icon_id, wp_generate_attachment_metadata( $this->site_icon_id, $upload['file'] ) );
     161        $this->site_icon_id = $this->_make_attachment( $upload );
     162        return $this->site_icon_id;
    179163    }
    180164}
  • trunk/tests/phpunit/tests/image/header.php

    r34214 r35309  
    105105
    106106    function test_create_attachment_object() {
    107         global $custom_image_header;
    108 
    109107        $id = wp_insert_attachment( array(
    110108            'post_status' => 'publish',
     
    124122
    125123    function test_insert_cropped_attachment() {
    126         global $custom_image_header;
    127 
    128124        $id = wp_insert_attachment( array(
    129125            'post_status' => 'publish',
  • trunk/tests/phpunit/tests/image/intermediate_size.php

    r35243 r35309  
    66 */
    77class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
    8     protected $ids = array();
    9 
    108    function tearDown() {
    119        $this->remove_added_uploads();
     
    1311    }
    1412
    15     /**
    16      * Upload files and create attachements for testing
    17      */
    18     private function _make_attachment( $file, $parent_post_id = 0 ) {
     13    public function _make_attachment( $file, $parent_post_id = 0 ) {
    1914        $contents = file_get_contents( $file );
    2015        $upload = wp_upload_bits( basename( $file ), null, $contents );
    2116
    22         $type = '';
    23         if ( ! empty( $upload['type'] ) ) {
    24             $type = $upload['type'];
    25         } else {
    26             $mime = wp_check_filetype( $upload['file'] );
    27             if ( $mime ) {
    28                 $type = $mime['type'];
    29             }
    30         }
    31 
    32         $attachment = array(
    33             'post_title' => basename( $upload['file'] ),
    34             'post_content' => '',
    35             'post_type' => 'attachment',
    36             'post_parent' => $parent_post_id,
    37             'post_mime_type' => $type,
    38             'guid' => $upload['url'],
    39         );
    40 
    41         // Save the data
    42         $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
    43         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    44 
    45         $this->ids[] = $id;
    46         return $id;
     17        return parent::_make_attachment( $upload, $parent_post_id );
    4718    }
    4819
  • trunk/tests/phpunit/tests/image/site_icon.php

    r35299 r35309  
    159159
    160160        $upload = wp_upload_bits( basename( $filename ), null, $contents );
    161         $type   = '';
    162         if ( ! empty( $upload['type'] ) ) {
    163             $type = $upload['type'];
    164         } else {
    165             $mime = wp_check_filetype( $upload['file'] );
    166             if ( $mime ) {
    167                 $type = $mime['type'];
    168             }
    169         }
    170161
    171         $attachment = array(
    172             'post_title'     => basename( $upload['file'] ),
    173             'post_content'   => $upload['url'],
    174             'post_type'      => 'attachment',
    175             'post_mime_type' => $type,
    176             'guid'           => $upload['url'],
    177         );
    178 
    179         // Save the data
    180         $this->attachment_id  = wp_insert_attachment( $attachment, $upload['file'] );
    181         wp_update_attachment_metadata( $this->attachment_id, wp_generate_attachment_metadata( $this->attachment_id, $upload['file'] ) );
    182 
     162        $this->attachment_id = $this->_make_attachment( $upload );
    183163        return $this->attachment_id;
    184164    }
  • trunk/tests/phpunit/tests/post/attachments.php

    r35243 r35309  
    77 */
    88class Tests_Post_Attachments extends WP_UnitTestCase {
    9     protected $ids = array();
    109
    1110    function tearDown() {
     
    1514    }
    1615
    17     function _make_attachment( $upload, $parent_post_id = 0 ) {
    18 
    19         $type = '';
    20         if ( !empty($upload['type']) ) {
    21             $type = $upload['type'];
    22         } else {
    23             $mime = wp_check_filetype( $upload['file'] );
    24             if ($mime)
    25                 $type = $mime['type'];
    26         }
    27 
    28         $attachment = array(
    29             'post_title' => basename( $upload['file'] ),
    30             'post_content' => '',
    31             'post_type' => 'attachment',
    32             'post_parent' => $parent_post_id,
    33             'post_mime_type' => $type,
    34             'guid' => $upload[ 'url' ],
    35         );
    36 
    37         // Save the data
    38         $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
    39         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    40 
    41         return $this->ids[] = $id;
    42 
    43     }
    44 
    4516    function test_insert_bogus_image() {
    46         $filename = rand_str().'.jpg';
     17        $filename = rand_str() . '.jpg';
    4718        $contents = rand_str();
    4819
    49         $upload = wp_upload_bits($filename, null, $contents);
     20        $upload = wp_upload_bits( $filename, null, $contents );
    5021        $this->assertTrue( empty($upload['error']) );
    51 
    52         $id = $this->_make_attachment($upload);
    5322    }
    5423
  • trunk/tests/phpunit/tests/xmlrpc/wp/getMediaItem.php

    r25002 r35309  
    2323        $contents = file_get_contents( $filename );
    2424        $upload = wp_upload_bits(basename($filename), null, $contents);
    25         $mime = wp_check_filetype( $filename );
    26         $this->attachment_data = array(
    27             'post_title' => basename( $upload['file'] ),
    28             'post_content' => '',
    29             'post_type' => 'attachment',
    30             'post_parent' => $this->post_id,
    31             'post_mime_type' => $mime['type'],
    32             'guid' => $upload[ 'url' ]
    33         );
    3425
    35         $id = wp_insert_attachment( $this->attachment_data, $upload[ 'file' ], $this->post_id );
    36         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    37         $this->attachment_id = $id;
     26        $this->attachment_id = $this->_make_attachment( $upload, $this->post_id );
     27        $this->attachment_data = get_post( $this->attachment_id, ARRAY_A );
    3828
    3929        set_post_thumbnail( $this->post_id, $this->attachment_id );
     
    4232    function tearDown() {
    4333        remove_theme_support( 'post-thumbnails' );
     34
     35        $this->remove_added_uploads();
    4436
    4537        parent::tearDown();
Note: See TracChangeset for help on using the changeset viewer.