From 84e475dd80049b3b1bd2329445fe7bc9b3fd9571 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 5 Oct 2015 21:57:32 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@34838 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-customize-setting.php | 11 ++-- tests/phpunit/tests/customize/setting.php | 58 +++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-customize-setting.php b/src/wp-includes/class-wp-customize-setting.php index 98f37f9c78..6630157549 100644 --- a/src/wp-includes/class-wp-customize-setting.php +++ b/src/wp-includes/class-wp-customize-setting.php @@ -304,12 +304,13 @@ class WP_Customize_Setting { * @since 3.4.0 * * @param mixed $value The value to update. - * @return mixed The result of saving the value. + * @return bool The result of saving the value. */ protected function update( $value ) { - switch( $this->type ) { + switch ( $this->type ) { case 'theme_mod' : - return $this->_update_theme_mod( $value ); + $this->_update_theme_mod( $value ); + return true; case 'option' : return $this->_update_option( $value ); @@ -327,7 +328,9 @@ class WP_Customize_Setting { * @param mixed $value Value of the setting. * @param WP_Customize_Setting $this WP_Customize_Setting instance. */ - return do_action( 'customize_update_' . $this->type, $value, $this ); + do_action( "customize_update_{$this->type}", $value, $this ); + + return has_action( "customize_update_{$this->type}" ); } } diff --git a/tests/phpunit/tests/customize/setting.php b/tests/phpunit/tests/customize/setting.php index 08bbc6541d..069a8ae1b0 100644 --- a/tests/phpunit/tests/customize/setting.php +++ b/tests/phpunit/tests/customize/setting.php @@ -366,6 +366,64 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase { $this->assertEquals( $default, $setting->value() ); } + /** + * Test setting save method for custom type. + * + * @see WP_Customize_Setting::save() + * @see WP_Customize_Setting::update() + */ + function test_update_custom_type() { + $type = 'custom'; + $name = 'foo'; + $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) ); + $this->manager->add_setting( $setting ); + add_action( 'customize_update_custom', array( $this, 'handle_customize_update_custom_foo_action' ), 10, 2 ); + add_action( 'customize_save_foo', array( $this, 'handle_customize_save_custom_foo_action' ), 10, 2 ); + + // Try saving before value set. + $this->assertTrue( 0 === did_action( 'customize_update_custom' ) ); + $this->assertTrue( 0 === did_action( 'customize_save_foo' ) ); + $this->assertFalse( $setting->save() ); + $this->assertTrue( 0 === did_action( 'customize_update_custom' ) ); + $this->assertTrue( 0 === did_action( 'customize_save_foo' ) ); + + // Try setting post value without user as admin. + $this->manager->set_post_value( $setting->id, 'hello world' ); + $this->assertFalse( $setting->save() ); + $this->assertTrue( 0 === did_action( 'customize_update_custom' ) ); + $this->assertTrue( 0 === did_action( 'customize_save_foo' ) ); + + // Satisfy all requirements for save to happen. + wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); + $this->assertTrue( false !== $setting->save() ); + $this->assertTrue( 1 === did_action( 'customize_update_custom' ) ); + $this->assertTrue( 1 === did_action( 'customize_save_foo' ) ); + } + + /** + * Check customize_update_custom action. + * + * @see Tests_WP_Customize_Setting::test_update_custom_type() + * @param mixed $value + * @param WP_Customize_Setting $setting + */ + function handle_customize_update_custom_foo_action( $value, $setting = null ) { + $this->assertEquals( 'hello world', $value ); + $this->assertInstanceOf( 'WP_Customize_Setting', $setting ); + } + + /** + * Check customize_save_foo action. + * + * @see Tests_WP_Customize_Setting::test_update_custom_type() + * @param WP_Customize_Setting $setting + */ + function handle_customize_save_custom_foo_action( $setting ) { + $this->assertInstanceOf( 'WP_Customize_Setting', $setting ); + $this->assertEquals( 'custom', $setting->type ); + $this->assertEquals( 'foo', $setting->id ); + } + /** * Ensure that is_current_blog_previewed returns the expected values. *