Add tests which ensure the `wp_editor_set_quality` and `jpeg_quality` filters only apply if they are added before the corresponding `WP_Image_Editor` is instantiated.

Props DH-Shredder
See #29856


git-svn-id: https://develop.svn.wordpress.org/trunk@30873 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2014-12-15 21:24:21 +00:00
parent 663d12c8f8
commit 4f35288dd1
2 changed files with 32 additions and 7 deletions

View File

@ -8,6 +8,11 @@ if (class_exists( 'WP_Image_Editor' ) ) :
public static $test_return = true; public static $test_return = true;
public static $save_return = array(); public static $save_return = array();
// Allow testing of jpeg_quality filter.
public function set_mime_type( $mime_type = null ) {
$this->mime_type = $mime_type;
}
public function load() { public function load() {
return self::$load_return; return self::$load_return;
} }

View File

@ -51,22 +51,42 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase {
// Get an editor // Get an editor
$editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
$editor->set_mime_type( "image/jpeg" ); // Ensure mime-specific filters act properly.
// Check default value // Check default value
$this->assertEquals( 90, $editor->get_quality() ); $this->assertEquals( 90, $editor->get_quality() );
// Ensure set_quality works // Ensure the quality filters do not have precedence if created after editor instantiation.
$func_100_percent = create_function( '', "return 100;" );
add_filter( 'wp_editor_set_quality', $func_100_percent );
$this->assertEquals( 90, $editor->get_quality() );
$func_95_percent = create_function( '', "return 95;" );
add_filter( 'jpeg_quality', $func_95_percent );
$this->assertEquals( 90, $editor->get_quality() );
// Ensure set_quality() works and overrides the filters
$this->assertTrue( $editor->set_quality( 75 ) ); $this->assertTrue( $editor->set_quality( 75 ) );
$this->assertEquals( 75, $editor->get_quality() ); $this->assertEquals( 75, $editor->get_quality() );
// Ensure the quality filter works // Get a new editor to clear default quality state
$func = create_function( '', "return 100;"); unset( $editor );
add_filter( 'wp_editor_set_quality', $func ); $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
$this->assertTrue( $editor->set_quality( 70 ) ); $editor->set_mime_type( "image/jpeg" ); // Ensure mime-specific filters act properly.
$this->assertEquals( 70, $editor->get_quality() );
// Ensure jpeg_quality filter applies if it exists before editor instantiation.
$this->assertEquals( 95, $editor->get_quality() );
// Get a new editor to clear jpeg_quality state
remove_filter( 'jpeg_quality', $func_95_percent );
unset( $editor );
$editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
// Ensure wp_editor_set_quality filter applies if it exists before editor instantiation.
$this->assertEquals( 100, $editor->get_quality() );
// Clean up // Clean up
remove_filter( 'wp_editor_set_quality', $func ); remove_filter( 'wp_editor_set_quality', $func_100_percent );
} }
/** /**