Make WordPress Core

Changeset 34838


Ignore:
Timestamp:
10/05/2015 09:57:32 PM (9 years ago)
Author:
westonruter
Message:

Customizer: Ensure WP_Customize_Setting::update() returns boolean value.

Adds unit tests for WP_Customize_Setting::save() (and WP_Customize_Setting::update()), along with the actions customize_update_{$type}, and customize_save_{$id_base} which they trigger.

Fixes #34140.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-setting.php

    r33911 r34838  
    305305     *
    306306     * @param mixed $value The value to update.
    307      * @return mixed The result of saving the value.
     307     * @return bool The result of saving the value.
    308308     */
    309309    protected function update( $value ) {
    310         switch( $this->type ) {
     310        switch ( $this->type ) {
    311311            case 'theme_mod' :
    312                 return $this->_update_theme_mod( $value );
     312                $this->_update_theme_mod( $value );
     313                return true;
    313314
    314315            case 'option' :
     
    328329                 * @param WP_Customize_Setting $this  WP_Customize_Setting instance.
    329330                 */
    330                 return do_action( 'customize_update_' . $this->type, $value, $this );
     331                do_action( "customize_update_{$this->type}", $value, $this );
     332
     333                return has_action( "customize_update_{$this->type}" );
    331334        }
    332335    }
  • trunk/tests/phpunit/tests/customize/setting.php

    r32840 r34838  
    368368
    369369    /**
     370     * Test setting save method for custom type.
     371     *
     372     * @see WP_Customize_Setting::save()
     373     * @see WP_Customize_Setting::update()
     374     */
     375    function test_update_custom_type() {
     376        $type = 'custom';
     377        $name = 'foo';
     378        $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
     379        $this->manager->add_setting( $setting );
     380        add_action( 'customize_update_custom', array( $this, 'handle_customize_update_custom_foo_action' ), 10, 2 );
     381        add_action( 'customize_save_foo', array( $this, 'handle_customize_save_custom_foo_action' ), 10, 2 );
     382
     383        // Try saving before value set.
     384        $this->assertTrue( 0 === did_action( 'customize_update_custom' ) );
     385        $this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
     386        $this->assertFalse( $setting->save() );
     387        $this->assertTrue( 0 === did_action( 'customize_update_custom' ) );
     388        $this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
     389
     390        // Try setting post value without user as admin.
     391        $this->manager->set_post_value( $setting->id, 'hello world' );
     392        $this->assertFalse( $setting->save() );
     393        $this->assertTrue( 0 === did_action( 'customize_update_custom' ) );
     394        $this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
     395
     396        // Satisfy all requirements for save to happen.
     397        wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
     398        $this->assertTrue( false !== $setting->save() );
     399        $this->assertTrue( 1 === did_action( 'customize_update_custom' ) );
     400        $this->assertTrue( 1 === did_action( 'customize_save_foo' ) );
     401    }
     402
     403    /**
     404     * Check customize_update_custom action.
     405     *
     406     * @see Tests_WP_Customize_Setting::test_update_custom_type()
     407     * @param mixed $value
     408     * @param WP_Customize_Setting $setting
     409     */
     410    function handle_customize_update_custom_foo_action( $value, $setting = null ) {
     411        $this->assertEquals( 'hello world', $value );
     412        $this->assertInstanceOf( 'WP_Customize_Setting', $setting );
     413    }
     414
     415    /**
     416     * Check customize_save_foo action.
     417     *
     418     * @see Tests_WP_Customize_Setting::test_update_custom_type()
     419     * @param WP_Customize_Setting $setting
     420     */
     421    function handle_customize_save_custom_foo_action( $setting ) {
     422        $this->assertInstanceOf( 'WP_Customize_Setting', $setting );
     423        $this->assertEquals( 'custom', $setting->type );
     424        $this->assertEquals( 'foo', $setting->id );
     425    }
     426
     427    /**
    370428     * Ensure that is_current_blog_previewed returns the expected values.
    371429     *
Note: See TracChangeset for help on using the changeset viewer.