Skip to content

Commit ab0e67a

Browse files
committed
Add test for cropping images with array crop value
1 parent af35deb commit ab0e67a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

plugins/webp-uploads/tests/test-helper.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,56 @@ static function () {
344344
$this->assertSame( 'image_additional_generated_error', $result->get_error_code() );
345345
}
346346

347+
/**
348+
* Test that image is cropped correctly when crop is an array.
349+
*
350+
* @covers ::webp_uploads_generate_additional_image_source
351+
*/
352+
public function test_it_should_crop_image_with_array_crop_value(): void {
353+
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
354+
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
355+
}
356+
357+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
358+
$size_data = array(
359+
'width' => 300,
360+
'height' => 300,
361+
'crop' => array( 'left', 'top' ),
362+
);
363+
364+
$captured_crop = null;
365+
// Add filter to intercept the crop value.
366+
remove_all_filters( 'image_resize_dimensions' );
367+
add_filter(
368+
'image_resize_dimensions',
369+
static function ( $passthrough, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) use ( &$captured_crop ) {
370+
$captured_crop = $crop;
371+
return $passthrough;
372+
},
373+
10,
374+
6
375+
);
376+
377+
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
378+
379+
$this->assertIsArray( $result );
380+
$this->assertArrayHasKey( 'filesize', $result );
381+
$this->assertArrayHasKey( 'file', $result );
382+
$this->assertStringEndsWith( 'image.webp', $result['file'] );
383+
$this->assertFileExists( '/tmp/image.webp' );
384+
385+
// Get image dimensions to verify crop worked.
386+
$image_editor = wp_get_image_editor( '/tmp/image.webp' );
387+
$size = $image_editor->get_size();
388+
389+
// Verify dimensions are as expected.
390+
$this->assertEquals( 300, $size['width'] );
391+
$this->assertEquals( 300, $size['height'] );
392+
393+
// Verify crop value is as expected.
394+
$this->assertEquals( array( 'left', 'top' ), $captured_crop );
395+
}
396+
347397
/**
348398
* Returns an empty array when the overwritten with empty array by webp_uploads_upload_image_mime_transforms filter.
349399
*/

0 commit comments

Comments
 (0)