Make WordPress Core

Changeset 1061 in tests


Ignore:
Timestamp:
10/01/2012 09:08:41 PM (12 years ago)
Author:
kurtpayne
Message:

Unit tests for #WP6821

Location:
trunk
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/image/functions.php

    r909 r1061  
    77 */
    88class Tests_Image_Functions extends WP_UnitTestCase {
     9
     10    /**
     11     * Get the MIME type of a file
     12     * @param string $filename
     13     * @return string
     14     */
     15    protected function get_mime_type( $filename ) {
     16        $mime_type = '';
     17        if ( extension_loaded( 'fileinfo' ) ) {
     18            $finfo = new finfo();
     19            $mime_type = $finfo->file( $filename, FILEINFO_MIME );
     20        } elseif ( function_exists('mime_content_type') ) {
     21            $mime_type = mime_content_type( $filename );
     22        }
     23        if ( false !== strpos( $mime_type, ';' ) ) {
     24            list( $mime_type, $charset ) = explode( ';', $mime_type, 2 );
     25        }
     26        return $mime_type;
     27    }
     28   
    929    function test_is_image_positive() {
    1030        // these are all image files recognized by php
     
    7393            $this->assertFalse( file_is_displayable_image( DIR_TESTDATA.'/images/'.$file ), "file_is_valid_image($file) should return false" );
    7494        }
    75     }
    76 
     95    }   
     96   
     97    /**
     98     * Test save image file and mime_types
     99     * @ticket 6821
     100     */
     101    public function test_wp_save_image_file() {
     102        include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
     103
     104        // Mime types
     105        $mime_types = array(
     106            'image/jpeg',
     107            'image/gif',
     108            'image/png'
     109        );
     110       
     111        // Test each image editor engine
     112        $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     113        foreach ( $classes as $class ) {
     114           
     115            // If the image editor isn't available, skip it
     116            if ( !$class::test() ) {
     117                continue;
     118            }
     119            $filter = create_function( '', "return '$class';" );
     120            add_filter( 'image_editor_class', $filter );
     121
     122            // Call wp_save_image_file
     123            $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
     124           
     125            // Save a file as each mime type, assert it works
     126            foreach ( $mime_types as $mime_type ) {
     127                $file = wp_tempnam();               
     128                $ret = wp_save_image_file( $file, $img, $mime_type, 1 );
     129                $this->assertNotEmpty( $ret );
     130                $this->assertNotInstanceOf( 'WP_Error', $ret );
     131                $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
     132               
     133                // Clean up
     134                @unlink( $file );
     135                @unlink( $ret['path'] );
     136            }
     137
     138            // Clean up
     139            unset( $img );
     140        }
     141    }
     142   
     143    /**
     144     * Test that a passed mime type overrides the extension in the filename
     145     * @ticket 6821
     146     */
     147    public function test_mime_overrides_filename() {
     148
     149        // Test each image editor engine
     150        $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     151        foreach ( $classes as $class ) {
     152
     153            // If the image editor isn't available, skip it
     154            if ( !$class::test() ) {
     155                continue;
     156            }
     157            $filter = create_function( '', "return '$class';" );
     158            add_filter( 'image_editor_class', $filter );
     159
     160            // Save the file
     161            $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
     162            $mime_type = 'image/gif';
     163            $file = wp_tempnam( 'tmp.jpg' );
     164            $ret = $img->save( $file, $mime_type );
     165           
     166            // Make assertions
     167            $this->assertNotEmpty( $ret );
     168            $this->assertNotInstanceOf( 'WP_Error', $ret );
     169            $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
     170           
     171            // Clean up
     172            @unlink( $file );
     173            @unlink( $ret['path'] );
     174            unset( $img );
     175        }
     176    }
     177
     178    /**
     179     * Test that mime types are correctly inferred from file extensions
     180     * @ticket 6821
     181     */
     182    public function test_inferred_mime_types() {
     183
     184        // Mime types
     185        $mime_types = array(
     186            'jpg'  => 'image/jpeg',
     187            'jpeg' => 'image/jpeg',
     188            'jpe'  => 'image/jpeg',
     189            'gif'  => 'image/gif',
     190            'png'  => 'image/png',
     191            'unk'  => 'image/jpeg' // Default, unknown
     192        );
     193
     194        // Test each image editor engine
     195        $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     196        foreach ( $classes as $class ) {
     197
     198            // If the image editor isn't available, skip it
     199            if ( !$class::test() ) {
     200                continue;
     201            }
     202            $filter = create_function( '', "return '$class';" );
     203            add_filter( 'image_editor_class', $filter );
     204           
     205            // Save the image as each file extension, check the mime type
     206            $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
     207            $temp = get_temp_dir();
     208            foreach ( $mime_types as $ext => $mime_type ) {
     209                $file = wp_unique_filename( $temp, uniqid() . ".$ext" );
     210                $ret = $img->save( trailingslashit( $temp ) . $file );
     211                $this->assertNotEmpty( $ret );
     212                $this->assertNotInstanceOf( 'WP_Error', $ret );
     213                $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
     214                @unlink( $file );
     215                @unlink( $ret['path'] );
     216            }
     217
     218            // Clean up
     219            unset( $img );
     220        }
     221    }
    77222}
  • trunk/tests/image/resize.php

    r909 r1061  
    124124    }
    125125
     126    /**
     127     * Try resizing a non-existent image
     128     * @ticket 6821
     129     */
     130    public function test_resize_non_existent_image() {
     131        $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     132        foreach ( $classes as $class ) {
     133            if ( !$class::test() ) {
     134                continue;
     135            }
     136            $filter = create_function( '', "return $class;" );
     137            add_filter( 'image_editor_class', $filter );
     138            $image = image_resize( DIR_TESTDATA.'/images/test-non-existent-image.jpg', 25, 25 );
     139            $this->assertInstanceOf( 'WP_Error', $image );
     140            $this->assertEquals( 'error_loading_image', $image->get_error_code() );
     141        }
     142    }
     143   
     144    /**
     145     * Try resizing a php file (bad image)
     146     * @ticket 6821
     147     */
     148    public function test_resize_bad_image() {
     149        $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     150        foreach ( $classes as $class ) {
     151            if ( !$class::test() ) {
     152                continue;
     153            }
     154            $filter = create_function( '', "return $class;" );
     155            add_filter( 'image_editor_class', $filter );
     156            $image = image_resize( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 );
     157            $this->assertInstanceOf( 'WP_Error', $image );
     158            $this->assertEquals( 'invalid_image', $image->get_error_code() );
     159        }
     160    }
    126161}
Note: See TracChangeset for help on using the changeset viewer.