Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for webp_uploads_should_discard_additional_image_file and re…
…name some test name
  • Loading branch information
mukeshpanchal27 committed Jul 11, 2022
commit aa68c7cfa0328734a0c25a11a01adbbc2a1e8c46
7 changes: 3 additions & 4 deletions modules/images/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,9 @@ function webp_uploads_should_discard_additional_image_file( array $original, arr
*
* @param bool $preferred_filesize Prioritize file size over mime type. Default true.
*/
if (
apply_filters( 'webp_uploads_discard_larger_generated_images', true )
&& $additional_image_filesize >= $original_image_filesize
) {
$webp_discard_larger_images = apply_filters( 'webp_uploads_discard_larger_generated_images', true );

if ( $webp_discard_larger_images && $additional_image_filesize >= $original_image_filesize ) {
return true;
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/modules/images/webp-uploads/helper-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,52 @@ function () {
$this->assertIsArray( $transforms );
$this->assertSame( array( 'image/jpeg' => array( 'image/jpeg', 'image/webp' ) ), $transforms );
}

/**
* Tests the webp_uploads_discard_larger_generated_images filter.
*
* @dataProvider data_provider_image_filesize
*
* @test
*/
public function it_should_check_if_the_additional_image_is_larger_than_the_original_image( $original_filesize, $additional_filesize, $expected_status ) {
add_filter( 'webp_uploads_discard_larger_generated_images', '__return_true' );

$output = webp_uploads_should_discard_additional_image_file( $original_filesize, $additional_filesize );
$this->assertSame( $output, $expected_status );
}

public function data_provider_image_filesize() {
return array(
array(
array( 'filesize' => 120101 ),
array( 'filesize' => 100101 ),
false,
),
array(
array( 'filesize' => 100101 ),
array( 'filesize' => 120101 ),
true,
),
array(
array( 'filesize' => 10101 ),
array( 'filesize' => 10101 ),
true,
),
);
}

/**
* Check the disabled behaviour of the webp_uploads_discard_larger_generated_images filter.
*
* @dataProvider data_provider_image_filesize
*
* @test
*/
public function it_should_return_an_additional_image_if_it_is_bigger_than_original( $original_filesize, $additional_filesize ) {
Comment thread
mukeshpanchal27 marked this conversation as resolved.
Outdated
add_filter( 'webp_uploads_discard_larger_generated_images', '__return_false' );

$output = webp_uploads_should_discard_additional_image_file( $original_filesize, $additional_filesize );
$this->assertSame( $output, false );
Comment thread
mukeshpanchal27 marked this conversation as resolved.
Outdated
}
}
7 changes: 3 additions & 4 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,10 @@ function() {

/**
* The image with the smaller filesize should be used when webp_uploads_discard_larger_generated_images is set to true.
* This test has all WebP smaller than the original size.
*
* @test
*/
public function it_should_use_smaller_mime_type_image_from_source_for_all_sizes() {
public function it_should_create_webp_when_webp_is_smaller_than_jpegs() {
add_filter( 'webp_uploads_discard_larger_generated_images', '__return_true' );

// Look for an image that contains all of the additional mime type images.
Expand Down Expand Up @@ -669,7 +668,7 @@ public function it_should_use_smaller_mime_type_image_from_source_for_all_sizes(
*
* @test
*/
public function it_should_use_smaller_mime_type_image_for_full_size() {
public function it_should_create_webp_for_full_size_which_is_smaller_in_webp_format() {
add_filter( 'webp_uploads_discard_larger_generated_images', '__return_true' );

// Look for an image that contains only full size mime type images.
Expand All @@ -692,7 +691,7 @@ public function it_should_use_smaller_mime_type_image_for_full_size() {
*
* @test
*/
public function it_should_use_smaller_mime_type_image_from_specific_source_for_webp_image() {
public function it_should_create_webp_for_some_sizes_which_are_smaller_in_webp_format() {
add_filter( 'webp_uploads_discard_larger_generated_images', '__return_true' );

// Look for an image that contains all of the additional mime type images.
Expand Down