Make WordPress Core


Ignore:
Timestamp:
08/30/2015 02:40:40 AM (8 years ago)
Author:
wonderboymusic
Message:

Improve the reliability of the crop returned by image_get_intermediate_size().

Add a bunch of unit tests to tests/image/intermediate_size.php.

Props joemcgill, ericlewis, kitchin, SergeyBiryukov, chipbennett.
Fixes #17626.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/image/intermediate_size.php

    r29120 r33807  
    1111    }
    1212
     13    /**
     14     * Upload files and create attachements for testing
     15     */
     16    private function _make_attachment( $file, $parent_post_id = 0 ) {
     17        $contents = file_get_contents( $file );
     18        $upload = wp_upload_bits( basename( $file ), null, $contents );
     19
     20        $type = '';
     21        if ( ! empty( $upload['type'] ) ) {
     22            $type = $upload['type'];
     23        } else {
     24            $mime = wp_check_filetype( $upload['file'] );
     25            if ( $mime ) {
     26                $type = $mime['type'];
     27            }
     28        }
     29
     30        $attachment = array(
     31            'post_title' => basename( $upload['file'] ),
     32            'post_content' => '',
     33            'post_type' => 'attachment',
     34            'post_parent' => $parent_post_id,
     35            'post_mime_type' => $type,
     36            'guid' => $upload['url'],
     37        );
     38
     39        // Save the data
     40        $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
     41        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
     42
     43        $this->ids[] = $id;
     44        return $id;
     45    }
     46
    1347    function test_make_intermediate_size_no_size() {
    1448        $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );
     
    5084        unlink( DIR_TESTDATA . '/images/a2-small-100x75.jpg' );
    5185    }
     86
     87    /**
     88    * @ticket 17626
     89    */
     90    function test_get_intermediate_sizes_by_name() {
     91        add_image_size( 'test-size', 330, 220, true );
     92
     93        $file = DIR_TESTDATA . '/images/waffles.jpg';
     94        $id = $this->_make_attachment( $file, 0 );
     95
     96        // look for a size by name
     97        $image = image_get_intermediate_size( $id, 'test-size' );
     98
     99        // test for the expected string because the array will by definition
     100        // return with the correct height and width attributes
     101        $this->assertNotFalse( strpos( $image['file'], '330x220' ) );
     102
     103        // cleanup
     104        remove_image_size( 'test-size' );
     105    }
     106
     107    /**
     108    * @ticket 17626
     109    */
     110    function test_get_intermediate_sizes_by_array_exact() {
     111        // Only one dimention match shouldn't return false positive (see: 17626)
     112        add_image_size( 'test-size', 330, 220, true );
     113        add_image_size( 'false-height', 330, 400, true );
     114        add_image_size( 'false-width', 600, 220, true );
     115
     116        $file = DIR_TESTDATA . '/images/waffles.jpg';
     117        $id = $this->_make_attachment( $file, 0 );
     118
     119        // look for a size by array that exists
     120        // note: staying larger than 300px to miss default medium crop
     121        $image = image_get_intermediate_size( $id, array( 330, 220 ) );
     122
     123        // test for the expected string because the array will by definition
     124        // return with the correct height and width attributes
     125        $this->assertNotFalse( strpos( $image['file'], '330x220' ) );
     126
     127        // cleanup
     128        remove_image_size( 'test-size' );
     129        remove_image_size( 'false-height' );
     130        remove_image_size( 'false-width' );
     131    }
     132
     133    /**
     134    * @ticket 17626
     135    */
     136    function test_get_intermediate_sizes_by_array_nearest() {
     137        // If an exact size is not found, it should be returned
     138        // If not, find nearest size that is larger (see: 17626)
     139        add_image_size( 'test-size', 450, 300, true );
     140        add_image_size( 'false-height', 330, 100, true );
     141        add_image_size( 'false-width', 150, 220, true );
     142
     143        $file = DIR_TESTDATA . '/images/waffles.jpg';
     144        $id = $this->_make_attachment( $file, 0 );
     145
     146        // look for a size by array that doesn't exist
     147        // note: staying larger than 300px to miss default medium crop
     148        $image = image_get_intermediate_size( $id, array( 330, 220 ) );
     149
     150        // you have to test for the string because the image will by definition
     151        // return with the correct height and width attributes
     152        $this->assertNotFalse( strpos( $image['file'], '450x300' ) );
     153
     154        // cleanup
     155        remove_image_size( 'test-size' );
     156        remove_image_size( 'false-height' );
     157        remove_image_size( 'false-width' );
     158    }
     159
     160    /**
     161    * @ticket 17626
     162    */
     163    function test_get_intermediate_sizes_by_array_nearest_false() {
     164        // If an exact size is not found, it should be returned
     165        // If not, find nearest size that is larger, otherwise return false (see: 17626)
     166        add_image_size( 'false-height', 330, 100, true );
     167        add_image_size( 'false-width', 150, 220, true );
     168
     169        $file = DIR_TESTDATA . '/images/waffles.jpg';
     170        $id = $this->_make_attachment( $file, 0 );
     171
     172        // look for a size by array that doesn't exist
     173        // note: staying larger than 300px to miss default medium crop
     174        $image = image_get_intermediate_size( $id, array( 330, 220 ) );
     175
     176        // you have to test for the string because the image will by definition
     177        // return with the correct height and width attributes
     178        $this->assertFalse( $image );
     179
     180        // cleanup
     181        remove_image_size( 'false-height' );
     182        remove_image_size( 'false-width' );
     183    }
     184
     185    /**
     186    * @ticket 17626
     187    */
     188    function test_get_intermediate_sizes_by_array_zero_height() {
     189        // Generate random width
     190        $random_w = rand( 300, 400 );
     191
     192        // Only one dimention match shouldn't return false positive (see: 17626)
     193        add_image_size( 'test-size', $random_w, 0, false );
     194        add_image_size( 'false-height', $random_w, 100, true );
     195
     196        $file = DIR_TESTDATA . '/images/waffles.jpg';
     197        $id = $this->_make_attachment( $file, 0 );
     198
     199        $original = wp_get_attachment_metadata( $id );
     200        $image_w = $random_w;
     201        $image_h = round( ( $image_w / $original['width'] ) * $original['height'] );
     202
     203        // look for a size by array that exists
     204        // note: staying larger than 300px to miss default medium crop
     205        $image = image_get_intermediate_size( $id, array( $random_w, 0 ) );
     206
     207        // test for the expected string because the array will by definition
     208        // return with the correct height and width attributes
     209        $this->assertNotFalse( strpos( $image['file'], $image_w . 'x' . $image_h ) );
     210
     211        // cleanup
     212        remove_image_size( 'test-size' );
     213        remove_image_size( 'false-height' );
     214    }
     215
     216    /**
     217    * @ticket 17626
     218    */
     219    function test_get_intermediate_sizes_by_array_zero_width() {
     220        // Generate random height
     221        $random_h = rand( 200, 300 );
     222
     223        // Only one dimention match shouldn't return false positive (see: 17626)
     224        add_image_size( 'test-size', 0, $random_h, false );
     225        add_image_size( 'false-height', 300, $random_h, true );
     226
     227        $file = DIR_TESTDATA . '/images/waffles.jpg';
     228        $id = $this->_make_attachment( $file, 0 );
     229
     230        $original = wp_get_attachment_metadata( $id );
     231        $image_h = $random_h;
     232        $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );
     233
     234        // look for a size by array that exists
     235        // note: staying larger than 300px to miss default medium crop
     236        $image = image_get_intermediate_size( $id, array( 0, $random_h ) );
     237
     238        // test for the expected string because the array will by definition
     239        // return with the correct height and width attributes
     240        $this->assertNotFalse( strpos( $image['file'], $image_w . 'x' . $image_h ) );
     241
     242        // cleanup
     243        remove_image_size( 'test-size' );
     244        remove_image_size( 'false-height' );
     245    }
    52246}
Note: See TracChangeset for help on using the changeset viewer.