From f8305d2e42caf0fb97ed6824934831f5c0aa61ca Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 24 Nov 2026 11:08:57 +0530 Subject: [PATCH 1/2] Tests: Cover the invalid user setting fallback in `wp_default_editor()`. When the user's `editor` setting is not `tinymce`, `html`, or `test`, `wp_default_editor()` falls back to `user_can_richedit()`. The existing tests do not reach this branch. This adds a data-driven test for both results of `user_can_richedit()`. See #65819. Co-Authored-By: Claude Opus 5.5 --- .../phpunit/tests/general/wpDefaultEditor.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/phpunit/tests/general/wpDefaultEditor.php b/tests/phpunit/tests/general/wpDefaultEditor.php index 6235a8b613f72..6980bbc19137a 100644 --- a/tests/phpunit/tests/general/wpDefaultEditor.php +++ b/tests/phpunit/tests/general/wpDefaultEditor.php @@ -61,4 +61,50 @@ static function () { $this->assertSame( 'html', wp_default_editor() ); } + + /** + * Tests that wp_default_editor() falls back to user_can_richedit() for an invalid user setting. + * + * @ticket 65819 + * + * @dataProvider data_wp_default_editor_falls_back_for_invalid_user_setting + * + * @param bool $can_richedit Value returned by user_can_richedit(). + * @param string $expected Expected editor. + */ + public function test_wp_default_editor_falls_back_for_invalid_user_setting( $can_richedit, $expected ) { + $user_id = self::factory()->user->create(); + wp_set_current_user( $user_id ); + + add_filter( 'user_can_richedit', $can_richedit ? '__return_true' : '__return_false' ); + + $orig_updated_user_settings = $GLOBALS['_updated_user_settings'] ?? null; + unset( $GLOBALS['_updated_user_settings'] ); + + $_COOKIE[ 'wp-settings-' . $user_id ] = 'editor=invalid'; + + try { + $this->assertSame( $expected, wp_default_editor() ); + } finally { + unset( $_COOKIE[ 'wp-settings-' . $user_id ] ); + + if ( null !== $orig_updated_user_settings ) { + $GLOBALS['_updated_user_settings'] = $orig_updated_user_settings; + } else { + unset( $GLOBALS['_updated_user_settings'] ); + } + } + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_wp_default_editor_falls_back_for_invalid_user_setting() { + return array( + 'rich editing available' => array( true, 'tinymce' ), + 'rich editing unavailable' => array( false, 'html' ), + ); + } } From 8a4e3d608af09cbdddbf90c571d80cfffb1ec04f Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 25 Nov 2026 21:17:11 +0530 Subject: [PATCH 2/2] Tests: Move the invalid setting cases into the `wp_default_editor()` user setting test. Add the two invalid setting cases to `test_wp_default_editor_respects_user_setting()` as data provider rows next to the valid `html` case, so the cookie and `$_updated_user_settings` cleanup lives in one place. See #66757. Co-Authored-By: Claude Opus 5.5 --- .../phpunit/tests/general/wpDefaultEditor.php | 77 +++++++------------ 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/tests/phpunit/tests/general/wpDefaultEditor.php b/tests/phpunit/tests/general/wpDefaultEditor.php index 6980bbc19137a..88a0688c223e6 100644 --- a/tests/phpunit/tests/general/wpDefaultEditor.php +++ b/tests/phpunit/tests/general/wpDefaultEditor.php @@ -20,59 +20,19 @@ public function test_wp_default_editor_default() { } /** - * Tests that wp_default_editor() returns user editor setting when set. + * Tests that wp_default_editor() respects the user editor setting. * - * @ticket 65819 - */ - public function test_wp_default_editor_respects_user_setting() { - $user_id = self::factory()->user->create(); - wp_set_current_user( $user_id ); - - $orig_updated_user_settings = $GLOBALS['_updated_user_settings'] ?? null; - unset( $GLOBALS['_updated_user_settings'] ); - - $_COOKIE[ 'wp-settings-' . $user_id ] = 'editor=html'; - - try { - $this->assertSame( 'html', wp_default_editor() ); - } finally { - unset( $_COOKIE[ 'wp-settings-' . $user_id ] ); - - if ( null !== $orig_updated_user_settings ) { - $GLOBALS['_updated_user_settings'] = $orig_updated_user_settings; - } else { - unset( $GLOBALS['_updated_user_settings'] ); - } - } - } - - /** - * Tests that the `wp_default_editor` filter modifies the default editor. - * - * @ticket 65819 - */ - public function test_wp_default_editor_filter() { - add_filter( - 'wp_default_editor', - static function () { - return 'html'; - } - ); - - $this->assertSame( 'html', wp_default_editor() ); - } - - /** - * Tests that wp_default_editor() falls back to user_can_richedit() for an invalid user setting. + * An invalid setting falls back to user_can_richedit(). * * @ticket 65819 * - * @dataProvider data_wp_default_editor_falls_back_for_invalid_user_setting + * @dataProvider data_wp_default_editor_respects_user_setting * + * @param string $setting The user editor setting. * @param bool $can_richedit Value returned by user_can_richedit(). * @param string $expected Expected editor. */ - public function test_wp_default_editor_falls_back_for_invalid_user_setting( $can_richedit, $expected ) { + public function test_wp_default_editor_respects_user_setting( $setting, $can_richedit, $expected ) { $user_id = self::factory()->user->create(); wp_set_current_user( $user_id ); @@ -81,7 +41,7 @@ public function test_wp_default_editor_falls_back_for_invalid_user_setting( $can $orig_updated_user_settings = $GLOBALS['_updated_user_settings'] ?? null; unset( $GLOBALS['_updated_user_settings'] ); - $_COOKIE[ 'wp-settings-' . $user_id ] = 'editor=invalid'; + $_COOKIE[ 'wp-settings-' . $user_id ] = 'editor=' . $setting; try { $this->assertSame( $expected, wp_default_editor() ); @@ -99,12 +59,29 @@ public function test_wp_default_editor_falls_back_for_invalid_user_setting( $can /** * Data provider. * - * @return array[] + * @return array */ - public static function data_wp_default_editor_falls_back_for_invalid_user_setting() { + public static function data_wp_default_editor_respects_user_setting() { return array( - 'rich editing available' => array( true, 'tinymce' ), - 'rich editing unavailable' => array( false, 'html' ), + 'valid html setting' => array( 'html', true, 'html' ), + 'invalid setting, rich editing available' => array( 'invalid', true, 'tinymce' ), + 'invalid setting, rich editing unavailable' => array( 'invalid', false, 'html' ), ); } + + /** + * Tests that the `wp_default_editor` filter modifies the default editor. + * + * @ticket 65819 + */ + public function test_wp_default_editor_filter() { + add_filter( + 'wp_default_editor', + static function () { + return 'html'; + } + ); + + $this->assertSame( 'html', wp_default_editor() ); + } }