From 44d021b686582dae27bea9ae46c71e94a160982f Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 2 Oct 2015 14:13:15 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@34775 602fd350-edb4-49c9-b593-d223f7449a82 --- .../phpunit/tests/image/intermediate_size.php | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/image/intermediate_size.php b/tests/phpunit/tests/image/intermediate_size.php index db80f94abf..0d218172f8 100644 --- a/tests/phpunit/tests/image/intermediate_size.php +++ b/tests/phpunit/tests/image/intermediate_size.php @@ -214,26 +214,27 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase { } /** - * @ticket 17626 - */ + * @ticket 17626 + * @ticket 34087 + */ function test_get_intermediate_sizes_by_array_zero_width() { - // Generate random height - $random_h = rand( 200, 300 ); + // 202 is the smallest height that will trigger a miss for 'false-height'. + $height = 202; // Only one dimention match shouldn't return false positive (see: 17626) - add_image_size( 'test-size', 0, $random_h, false ); - add_image_size( 'false-height', 300, $random_h, true ); + add_image_size( 'test-size', 0, $height, false ); + add_image_size( 'false-height', 300, $height, true ); $file = DIR_TESTDATA . '/images/waffles.jpg'; $id = $this->_make_attachment( $file, 0 ); $original = wp_get_attachment_metadata( $id ); - $image_h = $random_h; + $image_h = $height; $image_w = round( ( $image_h / $original['height'] ) * $original['width'] ); // look for a size by array that exists // note: staying larger than 300px to miss default medium crop - $image = image_get_intermediate_size( $id, array( 0, $random_h ) ); + $image = image_get_intermediate_size( $id, array( 0, $height ) ); // test for the expected string because the array will by definition // return with the correct height and width attributes @@ -243,4 +244,31 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase { remove_image_size( 'test-size' ); remove_image_size( 'false-height' ); } + + /** + * @ticket 17626 + * @ticket 34087 + */ + public function test_get_intermediate_sizes_should_match_size_with_off_by_one_aspect_ratio() { + // Original is 600x400. 300x201 is close enough to match. + $width = 300; + $height = 201; + add_image_size( 'off-by-one', $width, $height, true ); + + $file = DIR_TESTDATA . '/images/waffles.jpg'; + $id = $this->_make_attachment( $file, 0 ); + + $original = wp_get_attachment_metadata( $id ); + $image_h = $height; + $image_w = round( ( $image_h / $original['height'] ) * $original['width'] ); + + // look for a size by array that exists + // note: staying larger than 300px to miss default medium crop + $image = image_get_intermediate_size( $id, array( 0, $height ) ); + + $this->assertTrue( strpos( $image['file'], $width . 'x' . $height ) > 0 ); + + // cleanup + remove_image_size( 'off-by-one' ); + } }