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
This commit is contained in:
Boone Gorges 2015-10-02 14:13:15 +00:00
parent 991feb7043
commit 44d021b686
1 changed files with 36 additions and 8 deletions

View File

@ -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' );
}
}