WordPress.org

Make WordPress Core

Ticket #36452: 36452.2.diff

File 36452.2.diff, 4.9 KB (added by westonruter, 3 years ago)

Add unit tests

  • src/wp-includes/class-wp-customize-setting.php

    diff --git src/wp-includes/class-wp-customize-setting.php src/wp-includes/class-wp-customize-setting.php
    index 1e29bdd..94e7ded 100644
    class WP_Customize_Setting { 
    657657                         * functions for available hooks.
    658658                         *
    659659                         * @since 3.4.0
     660                         * @since 4.6.0 Added the `$this` setting instance as the second param.
    660661                         *
    661                          * @param mixed $default The setting default value. Default empty.
     662                         * @param mixed                $default The setting default value. Default empty.
     663                         * @param WP_Customize_Setting $this    The setting instance.
    662664                         */
    663                         $value = apply_filters( "customize_value_{$id_base}", $value );
    664                 } else if ( $this->is_multidimensional_aggregated ) {
     665                        $value = apply_filters( "customize_value_{$id_base}", $value, $this );
     666                } elseif ( $this->is_multidimensional_aggregated ) {
    665667                        $root_value = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
    666668                        $value = $this->multidimensional_get( $root_value, $this->id_data['keys'], $this->default );
    667669                } else {
  • tests/phpunit/tests/customize/setting.php

    diff --git tests/phpunit/tests/customize/setting.php tests/phpunit/tests/customize/setting.php
    index 380d2d6..c2ff7f5 100644
    class Tests_WP_Customize_Setting extends WP_UnitTestCase { 
    277277                $this->custom_type_data_saved[ $name ] = $value;
    278278        }
    279279
    280         function custom_type_value_filter( $default ) {
     280        /**
     281         * Filter for `customize_value_{$id_base}`.
     282         *
     283         * @param mixed $default
     284         * @param WP_Customize_Setting $setting
     285         *
     286         * @return mixed|null
     287         */
     288        function custom_type_value_filter( $default, $setting = null ) {
    281289                $name = preg_replace( '/^customize_value_/', '', current_filter() );
     290                $this->assertInstanceOf( 'WP_Customize_Setting', $setting );
     291                $id_data = $setting->id_data();
     292                $this->assertEquals( $name, $id_data['base'] );
    282293                return $this->custom_type_getter( $name, $default );
    283294        }
    284295
    class Tests_WP_Customize_Setting extends WP_UnitTestCase { 
    314325                $default = "default_value_{$name}";
    315326                $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
    316327                // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need.
    317                 add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
     328                add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ), 10, 2 );
    318329                $this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) );
    319330                $this->assertEquals( $default, $setting->value() );
    320331                $this->assertTrue( $setting->preview() );
    class Tests_WP_Customize_Setting extends WP_UnitTestCase { 
    330341                $this->custom_type_setter( $name, $initial_value );
    331342                $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
    332343                // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need.
    333                 add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
     344                add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ), 10, 2 );
    334345                $this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) );
    335346                $this->assertEquals( $initial_value, $setting->value() );
    336347                $this->assertFalse( $setting->preview(), "Preview for $setting->id should not apply because existing type without an override." );
    class Tests_WP_Customize_Setting extends WP_UnitTestCase { 
    350361                $default = "default_value_{$name}";
    351362                $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
    352363                // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need.
    353                 add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
     364                add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ), 10, 2 );
    354365                $this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) );
    355366                $this->assertEquals( $default, $setting->value() );
    356367                $this->assertTrue( $setting->preview() );
    class Tests_WP_Customize_Setting extends WP_UnitTestCase { 
    366377                $this->custom_type_setter( $name, $initial_value );
    367378                $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
    368379                // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need.
    369                 add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
     380                add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ), 10, 2 );
    370381                $this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) );
    371382                $this->assertEquals( $initial_value, $setting->value() );
    372383                $this->assertTrue( $setting->preview() );