Make WordPress Core

Ticket #29856: 29856.docs_test.diff

File 29856.docs_test.diff, 3.9 KB (added by kirasong, 10 years ago)

Tests and Docs to clarify filter precedence.

  • src/wp-includes/class-wp-image-editor.php

    diff --git src/wp-includes/class-wp-image-editor.php src/wp-includes/class-wp-image-editor.php
    index 2db73f0..f6e1c12 100644
    abstract class WP_Image_Editor { 
    233233                        /**
    234234                         * Filter the default image compression quality setting.
    235235                         *
     236                         * Applies only during initial editor instantiation, or when set_quality() is run
     237                         * manually without an argument after editor instantiation.
     238                         *
     239                         * set_quality() has priority over the filter.
     240                         *
    236241                         * @since 3.5.0
    237242                         *
    238243                         * @param int    $quality   Quality level between 1 (low) and 100 (high).
    abstract class WP_Image_Editor { 
    244249                                /**
    245250                                 * Filter the JPEG compression quality for backward-compatibility.
    246251                                 *
     252                                 * Applies only during initial editor instantiation, or when set_quality() is run
     253                                 * manually without an argument after instantiation.
     254                                 *
     255                                 * set_quality() has priority over the filter.
     256                                 *
    247257                                 * The filter is evaluated under two contexts: 'image_resize', and 'edit_image',
    248258                                 * (when a JPEG image is saved to file).
    249259                                 *
  • tests/phpunit/includes/mock-image-editor.php

    diff --git tests/phpunit/includes/mock-image-editor.php tests/phpunit/includes/mock-image-editor.php
    index 808b32d..a799bd0 100644
    if (class_exists( 'WP_Image_Editor' ) ) : 
    88                public static $test_return = true;
    99                public static $save_return = array();
    1010
     11                // Allow testing of jpeg_quality filter.
     12                public function set_mime_type( $mime_type = null ) {
     13                        $this->mime_type = $mime_type;
     14                }
     15
    1116                public function load() {
    1217                        return self::$load_return;
    1318                }
  • tests/phpunit/tests/image/editor.php

    diff --git tests/phpunit/tests/image/editor.php tests/phpunit/tests/image/editor.php
    index 2844a08..14261e7 100644
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    5151
    5252                // Get an editor
    5353                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
     54                $editor->set_mime_type( "image/jpeg" ); // Ensure mime-specific filters act properly.
    5455
    5556                // Check default value
    5657                $this->assertEquals( 90, $editor->get_quality() );
    5758
    58                 // Ensure set_quality works
     59                // Ensure the quality filters do not have precedence if created after editor instantiation.
     60                $func_100_percent = create_function( '', "return 100;" );
     61                add_filter( 'wp_editor_set_quality', $func_100_percent );
     62                $this->assertEquals( 90, $editor->get_quality() );
     63
     64                $func_95_percent = create_function( '', "return 95;" );
     65                add_filter( 'jpeg_quality', $func_95_percent );
     66                $this->assertEquals( 90, $editor->get_quality() );
     67
     68                // Ensure set_quality() works and overrides the filters
    5969                $this->assertTrue( $editor->set_quality( 75 ) );
    6070                $this->assertEquals( 75, $editor->get_quality() );
    6171
    62                 // Ensure the quality filter works
    63                 $func = create_function( '', "return 100;");
    64                 add_filter( 'wp_editor_set_quality', $func );
    65                 $this->assertTrue( $editor->set_quality( 70 ) );
    66                 $this->assertEquals( 70, $editor->get_quality() );
     72                // Get a new editor to clear default quality state
     73                unset( $editor );
     74                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
     75                $editor->set_mime_type( "image/jpeg" ); // Ensure mime-specific filters act properly.
     76
     77                // Ensure jpeg_quality filter applies if it exists before editor instantiation.
     78                $this->assertEquals( 95, $editor->get_quality() );
     79
     80                // Get a new editor to clear jpeg_quality state
     81                remove_filter( 'jpeg_quality', $func_95_percent );
     82                unset( $editor );
     83                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
     84
     85                // Ensure wp_editor_set_quality filter applies if it exists before editor instantiation.
     86                $this->assertEquals( 100, $editor->get_quality() );
    6787
    6888                // Clean up
    69                 remove_filter( 'wp_editor_set_quality', $func );
     89                remove_filter( 'wp_editor_set_quality', $func_100_percent );
    7090        }
    7191
    7292        /**