Tests: Bring some consistency to WP_Image_Editor_GD and WP_Image_Editor_Imagick tests.

Includes:
* Adjusting test method descriptions and comments per the documentation standards.
* Creating image editor class instances directly, instead of calling `wp_get_image_editor()`.
* Cleaning up temporary files before performing assertions, where possible.
* Using more consistent variable names for image editor class instances.
* Reordering some test methods.

Follow-up to [1182/tests], [1188/tests], [27794], [30549], [30990], [31040], [39580], [40123], [49230], [49488], [49542], [49751].

See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@54401 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-10-07 00:40:07 +00:00
parent b26d9a0b32
commit 8128f19599
2 changed files with 132 additions and 126 deletions

View File

@ -52,7 +52,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test resizing an image, not using crop
* Tests resizing an image, not using crop.
*
* @requires function imagejpeg
*/
@ -74,7 +74,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test multi_resize with single image resize and no crop
* Tests multi_resize() with single image resize and no crop.
*
* @requires function imagejpeg
*/
@ -116,7 +116,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Ensure multi_resize doesn't create an image when
* Tests that multi_resize() does not create an image when
* both height and weight are missing, null, or 0.
*
* @ticket 26823
@ -185,7 +185,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test multi_resize with multiple sizes
* Tests multi_resize() with multiple sizes.
*
* @ticket 26823
* @requires function imagejpeg
@ -198,7 +198,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
$sizes_array = array(
/**
/*
* #0 - 10x10 resize, no cropping.
* By aspect, should be 10x6 output.
*/
@ -208,7 +208,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'crop' => false,
),
/**
/*
* #1 - 75x50 resize, with cropping.
* Output dimensions should be 75x50
*/
@ -218,7 +218,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'crop' => true,
),
/**
/*
* #2 - 20 pixel max height, no cropping.
* By aspect, should be 30x20 output.
*/
@ -228,7 +228,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'crop' => false,
),
/**
/*
* #3 - 45 pixel max height, with cropping.
* By aspect, should be 45x400 output.
*/
@ -238,7 +238,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'crop' => true,
),
/**
/*
* #4 - 50 pixel max width, no cropping.
* By aspect, should be 50x33 output.
*/
@ -246,7 +246,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 50,
),
/**
/*
* #5 - 55 pixel max width, no cropping, null height
* By aspect, should be 55x36 output.
*/
@ -255,7 +255,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'height' => null,
),
/**
/*
* #6 - 55 pixel max height, no cropping, no width specified.
* By aspect, should be 82x55 output.
*/
@ -263,7 +263,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'height' => 55,
),
/**
/*
* #7 - 60 pixel max height, no cropping, null width.
* By aspect, should be 90x60 output.
*/
@ -272,7 +272,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'height' => 60,
),
/**
/*
* #8 - 70 pixel max height, no cropping, negative width.
* By aspect, should be 105x70 output.
*/
@ -281,7 +281,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'height' => 70,
),
/**
/*
* #9 - 200 pixel max width, no cropping, negative height.
* By aspect, should be 200x133 output.
*/
@ -402,7 +402,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test resizing an image with cropping
* Tests resizing an image with cropping.
*/
public function test_resize_and_crop() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
@ -422,7 +422,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test cropping an image.
* Tests cropping an image.
*
* @ticket 51937
*
@ -479,7 +479,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test should return WP_Error when dimensions are not integer or are <= 0.
* Tests that crop() returns WP_Error when dimensions are not integer or are <= 0.
*
* @ticket 51937
*
@ -545,7 +545,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test rotating an image 180 deg
* Tests rotating an image 180 deg.
*/
public function test_rotate() {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
@ -564,7 +564,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test flipping an image
* Tests flipping an image.
*/
public function test_flip() {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
@ -583,34 +583,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
}
/**
* Test the image created with WP_Image_Editor_GD preserves alpha when resizing
*
* @ticket 23039
*/
public function test_image_preserves_alpha_on_resize() {
if ( ! ( imagetypes() & IMG_PNG ) ) {
$this->fail( 'This test requires PHP to be compiled with PNG support.' );
}
$file = DIR_TESTDATA . '/images/transparent.png';
$editor = wp_get_image_editor( $file );
$this->assertNotWPError( $editor );
$editor->load();
$editor->resize( 5, 5 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
unlink( $save_to_file );
}
/**
* Test the image created with WP_Image_Editor_GD preserves alpha with no resizing etc
* Tests that an image created with WP_Image_Editor_GD preserves alpha with no resizing.
*
* @ticket 23039
*/
@ -621,15 +594,37 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
$file = DIR_TESTDATA . '/images/transparent.png';
$editor = wp_get_image_editor( $file );
$this->assertNotWPError( $editor );
$editor->load();
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$gd_image_editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
unlink( $save_to_file );
}
/**
* Tests that an image created with WP_Image_Editor_GD preserves alpha when resizing.
*
* @ticket 23039
*/
public function test_image_preserves_alpha_on_resize() {
if ( ! ( imagetypes() & IMG_PNG ) ) {
$this->fail( 'This test requires PHP to be compiled with PNG support.' );
}
$file = DIR_TESTDATA . '/images/transparent.png';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$gd_image_editor->resize( 5, 5 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$gd_image_editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
@ -650,27 +645,29 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
$rgb = imagecolorat( $image, 0, 0 );
$expected = imagecolorsforindex( $image, $rgb );
$editor = new WP_Image_Editor_GD( $file );
$editor->load();
$editor->rotate( 180 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$editor->save( $save_to_file );
$gd_image_editor->rotate( 180 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), $expected['alpha'] );
unlink( $save_to_file );
$gd_image_editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), $expected['alpha'] );
unlink( $save_to_file );
}
/**
* Test WP_Image_Editor_GD handles extension-less images
* Tests that WP_Image_Editor_GD handles extensionless images.
*
* @ticket 39195
*/
public function test_image_non_existent_extension() {
$image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/test-image-no-extension' );
$result = $image_editor->load();
$gd_image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/test-image-no-extension' );
$this->assertTrue( $result );
$loaded = $gd_image_editor->load();
$this->assertTrue( $loaded );
}
}

View File

@ -35,7 +35,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Check support for ImageMagick compatible mime types.
* Tests support for ImageMagick compatible mime types.
*/
public function test_supports_mime_type() {
$imagick_image_editor = new WP_Image_Editor_Imagick( null );
@ -46,7 +46,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test resizing an image, not using crop
* Tests resizing an image, not using crop.
*/
public function test_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
@ -66,7 +66,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test multi_resize with single image resize and no crop
* Tests multi_resize() with single image resize and no crop.
*/
public function test_single_multi_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
@ -106,7 +106,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Ensure multi_resize doesn't create an image when
* Tests that multi_resize() does not create an image when
* both height and weight are missing, null, or 0.
*
* @ticket 26823
@ -175,7 +175,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test multi_resize with multiple sizes
* Tests multi_resize() with multiple sizes.
*
* @ticket 26823
*/
@ -187,7 +187,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
$sizes_array = array(
/**
/*
* #0 - 10x10 resize, no cropping.
* By aspect, should be 10x6 output.
*/
@ -197,7 +197,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'crop' => false,
),
/**
/*
* #1 - 75x50 resize, with cropping.
* Output dimensions should be 75x50
*/
@ -207,7 +207,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'crop' => true,
),
/**
/*
* #2 - 20 pixel max height, no cropping.
* By aspect, should be 30x20 output.
*/
@ -217,7 +217,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'crop' => false,
),
/**
/*
* #3 - 45 pixel max height, with cropping.
* By aspect, should be 45x400 output.
*/
@ -227,7 +227,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'crop' => true,
),
/**
/*
* #4 - 50 pixel max width, no cropping.
* By aspect, should be 50x33 output.
*/
@ -235,7 +235,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 50,
),
/**
/*
* #5 - 55 pixel max width, no cropping, null height
* By aspect, should be 55x36 output.
*/
@ -244,7 +244,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'height' => null,
),
/**
/*
* #6 - 55 pixel max height, no cropping, no width specified.
* By aspect, should be 82x55 output.
*/
@ -252,7 +252,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'height' => 55,
),
/**
/*
* #7 - 60 pixel max height, no cropping, null width.
* By aspect, should be 90x60 output.
*/
@ -261,7 +261,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'height' => 60,
),
/**
/*
* #8 - 70 pixel max height, no cropping, negative width.
* By aspect, should be 105x70 output.
*/
@ -270,7 +270,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'height' => 70,
),
/**
/*
* #9 - 200 pixel max width, no cropping, negative height.
* By aspect, should be 200x133 output.
*/
@ -391,7 +391,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test resizing an image with cropping
* Tests resizing an image with cropping.
*/
public function test_resize_and_crop() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
@ -411,7 +411,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test cropping an image
* Tests cropping an image.
*/
public function test_crop() {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
@ -431,7 +431,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test rotating an image 180 deg
* Tests rotating an image 180 deg.
*/
public function test_rotate() {
$file = DIR_TESTDATA . '/images/one-blue-pixel-100x100.png';
@ -450,7 +450,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test flipping an image
* Tests flipping an image.
*/
public function test_flip() {
$file = DIR_TESTDATA . '/images/one-blue-pixel-100x100.png';
@ -469,19 +469,19 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test the image created with WP_Image_Editor_Imagick preserves alpha when resizing
* Tests that an image created with WP_Image_Editor_Imagick preserves alpha with no resizing.
*
* @ticket 24871
*/
public function test_image_preserves_alpha_on_resize() {
public function test_image_preserves_alpha() {
$file = DIR_TESTDATA . '/images/transparent.png';
$editor = new WP_Image_Editor_Imagick( $file );
$editor->load();
$editor->resize( 5, 5 );
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$imagick_image_editor->save( $save_to_file );
$im = new Imagick( $save_to_file );
$pixel = $im->getImagePixelColor( 0, 0 );
@ -493,19 +493,20 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
* Test the image created with WP_Image_Editor_Imagick preserves alpha with no resizing etc
* Tests that an image created with WP_Image_Editor_Imagick preserves alpha when resizing.
*
* @ticket 24871
*/
public function test_image_preserves_alpha() {
public function test_image_preserves_alpha_on_resize() {
$file = DIR_TESTDATA . '/images/transparent.png';
$editor = new WP_Image_Editor_Imagick( $file );
$editor->load();
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$imagick_image_editor->resize( 5, 5 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$imagick_image_editor->save( $save_to_file );
$im = new Imagick( $save_to_file );
$pixel = $im->getImagePixelColor( 0, 0 );
@ -525,33 +526,37 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
$pre_rotate_editor = new Imagick( $file );
$pre_rotate_pixel = $pre_rotate_editor->getImagePixelColor( 0, 0 );
$pre_rotate_alpha = $pre_rotate_pixel->getColorValue( imagick::COLOR_ALPHA );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$pre_rotate_editor->writeImage( $save_to_file );
$pre_rotate_editor->destroy();
$image_editor = new WP_Image_Editor_Imagick( $save_to_file );
$image_editor->load();
$image_editor->rotate( 180 );
$image_editor->save( $save_to_file );
$imagick_image_editor = new WP_Image_Editor_Imagick( $save_to_file );
$imagick_image_editor->load();
$imagick_image_editor->rotate( 180 );
$imagick_image_editor->save( $save_to_file );
$this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $pre_rotate_alpha );
unlink( $save_to_file );
}
/**
* Test WP_Image_Editor_Imagick handles extension-less images
* Tests that WP_Image_Editor_Imagick handles extensionless images.
*
* @ticket 39195
*/
public function test_image_non_existent_extension() {
$image_editor = new WP_Image_Editor_Imagick( DIR_TESTDATA . '/images/test-image-no-extension' );
$result = $image_editor->load();
$imagick_image_editor = new WP_Image_Editor_Imagick( DIR_TESTDATA . '/images/test-image-no-extension' );
$this->assertTrue( $result );
$loaded = $imagick_image_editor->load();
$this->assertTrue( $loaded );
}
/**
* Test resetting Exif orientation data on rotate
* Tests resetting Exif orientation data on rotate.
*
* @ticket 37140
* @requires function exif_read_data
@ -564,24 +569,26 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
$this->assertSame( 3, (int) $data['orientation'], 'Orientation value read from does not match image file Exif data: ' . $file );
$temp_file = wp_tempnam( $file );
$image = wp_get_image_editor( $file );
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
// Test a value that would not lead back to 1, as WP is resetting the value to 1 manually.
$image->rotate( 90 );
$ret = $image->save( $temp_file, 'image/jpeg' );
$imagick_image_editor->rotate( 90 );
$saved = $imagick_image_editor->save( $temp_file, 'image/jpeg' );
$data = wp_read_image_metadata( $ret['path'] );
// Make sure the image is no longer in The Upside Down Exif orientation.
$this->assertSame( 1, (int) $data['orientation'], 'Orientation Exif data was not updated after rotating image: ' . $file );
$data = wp_read_image_metadata( $saved['path'] );
// Remove both the generated file ending in .tmp and tmp.jpg due to wp_tempnam().
unlink( $temp_file );
unlink( $ret['path'] );
unlink( $saved['path'] );
// Make sure the image is no longer in The Upside Down Exif orientation.
$this->assertSame( 1, (int) $data['orientation'], 'Orientation Exif data was not updated after rotating image: ' . $file );
}
/**
* Test that images can be loaded and written over streams
* Tests that images can be loaded and written over streams.
*/
public function test_streams() {
stream_wrapper_register( 'wptest', 'WP_Test_Stream' );
@ -594,20 +601,20 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
$file = 'wptest://Tests_Image_Editor_Imagick/read.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$ret = $imagick_image_editor->load();
$this->assertNotWPError( $ret );
$loaded = $imagick_image_editor->load();
$this->assertNotWPError( $loaded );
$temp_file = 'wptest://Tests_Image_Editor_Imagick/write.jpg';
$ret = $imagick_image_editor->save( $temp_file );
$this->assertNotWPError( $ret );
$saved = $imagick_image_editor->save( $temp_file );
$this->assertSame( $temp_file, $ret['path'] );
if ( $temp_file !== $ret['path'] ) {
unlink( $ret['path'] );
if ( $temp_file !== $saved['path'] ) {
unlink( $saved['path'] );
}
unlink( $temp_file );
$this->assertNotWPError( $saved );
$this->assertSame( $temp_file, $saved['path'] );
}
/**
@ -616,20 +623,22 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
public function test_directory_creation() {
$file = realpath( DIR_TESTDATA ) . '/images/a2-small.jpg';
$directory = realpath( DIR_TESTDATA ) . '/images/nonexistent-directory';
$editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$this->assertFileDoesNotExist( $directory );
$loaded = $editor->load();
$loaded = $imagick_image_editor->load();
$this->assertNotWPError( $loaded );
$resized = $editor->resize( 100, 100, true );
$resized = $imagick_image_editor->resize( 100, 100, true );
$this->assertNotWPError( $resized );
$saved = $editor->save( $directory . '/a2-small-cropped.jpg' );
$this->assertNotWPError( $saved );
$saved = $imagick_image_editor->save( $directory . '/a2-small-cropped.jpg' );
unlink( $directory . '/a2-small-cropped.jpg' );
rmdir( $directory );
$this->assertNotWPError( $saved );
}
}