Make WordPress Core

Changeset 34775


Ignore:
Timestamp:
10/02/2015 02:13:15 PM (9 years ago)
Author:
boonebgorges
Message:

More explicit tests for image_get_intermediate_size().

After [33807], Tests_Image_Intermediate_Size::test_get_intermediate_sizes_by_array_zero_width()
was failing intermittently. This failure was due to the use of a random number
for the image height. When the height was sufficiently large - $height >= 202 -
to change the aspect ratio of the cropped image (based on WP's threshold of a
1px difference), the test passed. And when the height was exactly 200, the
medium image size was hit exactly. The failure occurred only with a height of
201, which is close enough to 200 so that WP determined that the aspect ratio
of the potential crop was close enough to match.

The current changeset splits the test into two, in order to demonstrate the
failure.

See #17626. Fixes #34087.

File:
1 edited

Legend:

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

    r33846 r34775  
    215215
    216216    /**
    217     * @ticket 17626
    218     */
     217     * @ticket 17626
     218     * @ticket 34087
     219     */
    219220    function test_get_intermediate_sizes_by_array_zero_width() {
    220         // Generate random height
    221         $random_h = rand( 200, 300 );
     221        // 202 is the smallest height that will trigger a miss for 'false-height'.
     222        $height = 202;
    222223
    223224        // 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 );
     225        add_image_size( 'test-size', 0, $height, false );
     226        add_image_size( 'false-height', 300, $height, true );
    226227
    227228        $file = DIR_TESTDATA . '/images/waffles.jpg';
     
    229230
    230231        $original = wp_get_attachment_metadata( $id );
    231         $image_h = $random_h;
     232        $image_h = $height;
    232233        $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );
    233234
    234235        // look for a size by array that exists
    235236        // note: staying larger than 300px to miss default medium crop
    236         $image = image_get_intermediate_size( $id, array( 0, $random_h ) );
     237        $image = image_get_intermediate_size( $id, array( 0, $height ) );
    237238
    238239        // test for the expected string because the array will by definition
     
    244245        remove_image_size( 'false-height' );
    245246    }
     247
     248    /**
     249     * @ticket 17626
     250     * @ticket 34087
     251     */
     252    public function test_get_intermediate_sizes_should_match_size_with_off_by_one_aspect_ratio() {
     253        // Original is 600x400. 300x201 is close enough to match.
     254        $width  = 300;
     255        $height = 201;
     256        add_image_size( 'off-by-one', $width, $height, true );
     257
     258        $file = DIR_TESTDATA . '/images/waffles.jpg';
     259        $id = $this->_make_attachment( $file, 0 );
     260
     261        $original = wp_get_attachment_metadata( $id );
     262        $image_h = $height;
     263        $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );
     264
     265        // look for a size by array that exists
     266        // note: staying larger than 300px to miss default medium crop
     267        $image = image_get_intermediate_size( $id, array( 0, $height ) );
     268
     269        $this->assertTrue( strpos( $image['file'], $width . 'x' . $height ) > 0 );
     270
     271        // cleanup
     272        remove_image_size( 'off-by-one' );
     273    }
    246274}
Note: See TracChangeset for help on using the changeset viewer.