From 1e8daa612d121a5c000aeb27852f6fbf7aae2cbf Mon Sep 17 00:00:00 2001 From: sonali195 Date: Tue, 6 Oct 2026 11:28:44 +0530 Subject: [PATCH 1/2] Add test comparing WebP and JPEG sub-size dimensions Hi! I'd like to work on this. I'm going to start by adding a PHPUnit test that compares the WebP sub-size dimensions with the JPEG sub-sizes in the metadata, to reproduce the problem. Is this still available? --- plugins/webp-uploads/tests/test-load.php | 52 +++++++++++++++--------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/plugins/webp-uploads/tests/test-load.php b/plugins/webp-uploads/tests/test-load.php index 1618e06abe..e5c43bfbe9 100644 --- a/plugins/webp-uploads/tests/test-load.php +++ b/plugins/webp-uploads/tests/test-load.php @@ -316,34 +316,48 @@ static function () { * @covers ::get_attached_file * @covers ::wp_get_attachment_metadata */ - public function test_it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted(): void { + /** + * WebP sub-sizes should have the same dimensions as the JPEG sub-sizes in the metadata. + * + * @covers ::wp_get_attachment_metadata + */ + public function test_it_should_create_webp_subsizes_with_the_same_dimensions_as_the_jpeg_subsizes(): void { + $this->opt_in_to_jpeg_and_webp(); + + // The leaves image is 1080 pixels wide, so this filter ensures a -scaled version is created. + add_filter( + 'big_image_size_threshold', + static function () { + return 850; + } + ); + $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); + $metadata = wp_get_attachment_metadata( $attachment_id ); + $directory = dirname( (string) get_attached_file( $attachment_id ) ); - $file = get_attached_file( $attachment_id, true ); - $dirname = pathinfo( $file, PATHINFO_DIRNAME ); - - $this->assertIsString( $file ); - $this->assertFileExists( $file ); - - $metadata = wp_get_attachment_metadata( $attachment_id ); - $sizes = array( 'thumbnail', 'medium' ); - - $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + $this->assertIsArray( $metadata ); + $this->assertArrayHasKey( 'sizes', $metadata ); - foreach ( $sizes as $size_name ) { - $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); - $this->assertFileExists( path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) ); - } + $checked = 0; + foreach ( $metadata['sizes'] as $size_name => $size_data ) { + if ( ! isset( $size_data['sources']['image/webp']['file'] ) ) { + continue; + } - wp_delete_attachment( $attachment_id ); + $webp_path = path_join( $directory, $size_data['sources']['image/webp']['file'] ); + $this->assertFileExists( $webp_path ); - foreach ( $sizes as $size_name ) { - $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) ); + $image_size = wp_getimagesize( $webp_path ); + $this->assertIsArray( $image_size, "Could not read the WebP size for {$size_name}." ); + $this->assertSame( $size_data['width'], $image_size[0], "WebP width differs from the JPEG width for {$size_name}." ); + $this->assertSame( $size_data['height'], $image_size[1], "WebP height differs from the JPEG height for {$size_name}." ); + ++$checked; } - $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + $this->assertGreaterThan( 0, $checked, 'Expected at least one WebP sub-size to be checked.' ); } /** From ebcc3db5efab4df8c17c0d3b4aaabd823b95b7a0 Mon Sep 17 00:00:00 2001 From: sonali195 Date: Tue, 6 Oct 2026 11:37:19 +0530 Subject: [PATCH 2/2] Fix test formatting for coding standards Fix test formatting for coding standards --- plugins/webp-uploads/tests/test-load.php | 42 ++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/plugins/webp-uploads/tests/test-load.php b/plugins/webp-uploads/tests/test-load.php index e5c43bfbe9..16852327ff 100644 --- a/plugins/webp-uploads/tests/test-load.php +++ b/plugins/webp-uploads/tests/test-load.php @@ -311,12 +311,6 @@ static function () { } /** - * Remove the generated webp images when the attachment is deleted - * - * @covers ::get_attached_file - * @covers ::wp_get_attachment_metadata - */ - /** * WebP sub-sizes should have the same dimensions as the JPEG sub-sizes in the metadata. * * @covers ::wp_get_attachment_metadata @@ -360,6 +354,42 @@ static function () { $this->assertGreaterThan( 0, $checked, 'Expected at least one WebP sub-size to be checked.' ); } + /** + * Remove the generated webp images when the attachment is deleted + * + * @covers ::get_attached_file + * @covers ::wp_get_attachment_metadata + */ + public function test_it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted(): void { + $attachment_id = self::factory()->attachment->create_upload_object( + TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' + ); + + $file = get_attached_file( $attachment_id, true ); + $dirname = pathinfo( $file, PATHINFO_DIRNAME ); + + $this->assertIsString( $file ); + $this->assertFileExists( $file ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + $sizes = array( 'thumbnail', 'medium' ); + + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + + foreach ( $sizes as $size_name ) { + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + $this->assertFileExists( path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) ); + } + + wp_delete_attachment( $attachment_id ); + + foreach ( $sizes as $size_name ) { + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) ); + } + + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + } + /** * Remove the attached WebP version if the attachment is force deleted *