Skip to content

Commit

Permalink
Add tests that mock WebP support enabled/disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsilverstein committed Dec 3, 2021
1 parent 2620b57 commit 155989b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

// Add filter to ensure the plugin's admin integration and all modules are loaded for tests.
require_once $_test_root . '/includes/functions.php';
require_once TESTS_PLUGIN_DIR . '/tests/testdata/class-wp-image-supports-webp.php';
tests_add_filter(
'plugins_loaded',
function() {
Expand Down
47 changes: 42 additions & 5 deletions tests/modules/webp-uploads/webp-uploads-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,55 @@
class WebP_Uploads_Tests extends WP_UnitTestCase {

/**
* Test if webp-uploads is loaded and filter applied when system supports WebP.
* Test if webp-uploads applies filter based on system support of WebP.
*/
function test_webp_uploads_is_loaded() {
function test_webp_uploads_applies_filter_based_on_system_support() {
// Set the expected value based on the underlying system support for WebP.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$expect = array();
} else {
$expect = array(
'image/jpeg' => 'image/webp',
);
$expect = array( 'image/jpeg' => 'image/webp' );
}

$output_format = apply_filters( 'image_editor_output_format', array(), '', 'image/jpeg' );
$this->assertEquals( $expect, $output_format );
}

/**
* Test if webp-uploads applies filter based on system that supports WebP.
*/
function test_webp_uploads_applies_filter_when_system_supports() {
// Mock a system that supports WebP.
add_filter( 'wp_image_editors', array( $this, 'mock_wp_image_editor_supports' ) );
$output_format = apply_filters( 'image_editor_output_format', array(), '', 'image/jpeg' );
$this->assertEquals( array( 'image/jpeg' => 'image/webp' ), $output_format );
remove_filter( 'wp_image_editors', array( $this, 'mock_wp_image_editor_supports' ) );
}

/**
* Test if webp-uploads applies filter based on system that doesn't support WebP.
*/
function test_webp_uploads_applies_filter_when_system_doesnt_support() {
// Mock a system that doesn't support WebP.
add_filter( 'wp_image_editors', array( $this, 'mock_wp_image_editor_doesnt_support' ) );
$output_format = apply_filters( 'image_editor_output_format', array(), '', 'image/jpeg' );
$this->assertEquals( array(), $output_format );
remove_filter( 'wp_image_editors', array( $this, 'mock_wp_image_editor_doesnt_support' ) );
}

/**
* Mock an image editor that supports WebP.
*/
public function mock_wp_image_editor_supports() {
$editors = array( 'WP_Image_Supports_WebP' );
return $editors;
}

/**
* Mock an image editor that doesn't support WebP.
*/
public function mock_wp_image_editor_doesnt_support() {
$editors = array( 'WP_Image_Doesnt_Support_WebP' );
return $editors;
}
}
36 changes: 36 additions & 0 deletions tests/testdata/class-wp-image-doesnt-support-webp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* A WP_Image_Editor mock that doesn't support WebP.
*
* @package performance-lab
* @since 1.0.0
*/

/**
* Class WP_Image_Doesnt_Support_WebP mocks a WP_Image_Editor that doesn't support WebP.
*
* @since 1.0.0
*/
class WP_Image_Doesnt_Support_WebP {
/**
* Checks to see if editor supports the mime-type specified.
*
* @param string $mime_type The mime type to check.
* @return bool
*/
public static function supports_mime_type( $mime_type ) {
if ( 'image/webp' === $mime_type ) {
return false;
}
return true;
}

/**
* Set support true.
*
* @return bool
*/
public static function test() {
return true;
}
}
36 changes: 36 additions & 0 deletions tests/testdata/class-wp-image-supports-webp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* A WP_Image_Editor mock that supports WebP.
*
* @package performance-lab
* @since 1.0.0
*/

/**
* Class WP_Image_Supports_WebP mocks a WP_Image_Editor that supports WebP.
*
* @since 1.0.0
*/
class WP_Image_Supports_WebP {
/**
* Checks to see if editor supports the mime-type specified.
*
* @param string $mime_type The mime type to check.
* @return bool
*/
public static function supports_mime_type( $mime_type ) {
if ( 'image/webp' === $mime_type ) {
return true;
}
return false;
}

/**
* Set support true.
*
* @return bool
*/
public static function test() {
return true;
}
}

0 comments on commit 155989b

Please sign in to comment.