Skip to content

Commit 56e8dac

Browse files
committed
Add test to assert converted image filesize does not exceed original
1 parent e646941 commit 56e8dac

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @package webp-uploads
66
*/
77

8+
use PHP_CodeSniffer\Tokenizers\PHP;
89
use WebP_Uploads\Tests\TestCase;
910

1011
class Test_WebP_Uploads_Load extends TestCase {
@@ -1154,8 +1155,9 @@ static function () {
11541155
'error' => UPLOAD_ERR_OK,
11551156
);
11561157

1157-
// Store the original file hash to compare later.
1158+
// Store the original file hash and the original file size for later comparison.
11581159
$original_file_hash = isset( $file['tmp_name'] ) ? md5_file( $file['tmp_name'] ) : '';
1160+
$original_file_size = (int) filesize( $file['tmp_name'] );
11591161

11601162
// This will trigger the `wp_handle_sideload_prefilter` filter.
11611163
$attachment_id = media_handle_sideload( $file );
@@ -1191,7 +1193,11 @@ static function () {
11911193
$modern_image_format_path = get_attached_file( $attachment_id );
11921194
$this->assertNotFalse( $modern_image_format_path );
11931195
$this->assertFileExists( $modern_image_format_path );
1194-
$this->assertGreaterThan( 0, (int) filesize( $modern_image_format_path ) );
1196+
$modern_image_format_filesize = (int) filesize( $modern_image_format_path );
1197+
$this->assertGreaterThan( 0, $modern_image_format_filesize );
1198+
1199+
// Ensure the file size of the converted image is less than or equal to the original indexed PNG file size.
1200+
$this->assertLessThanOrEqual( $original_file_size, $modern_image_format_filesize );
11951201
}
11961202
} finally {
11971203
wp_delete_attachment( $attachment_id );

0 commit comments

Comments
 (0)