From 2465488f0dac336496d566987e7bd71d3bcf29f3 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 12 Feb 2026 10:57:29 +1100 Subject: [PATCH 1/5] =?UTF-8?q?Tests:=20Ensure=20user=20switching=20doesn?= =?UTF-8?q?=E2=80=99t=20occur=20for=20current=20user.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/phpunit/tests/user/wpSetCurrentUser.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/phpunit/tests/user/wpSetCurrentUser.php b/tests/phpunit/tests/user/wpSetCurrentUser.php index bebee961ececc..581d286e642ed 100644 --- a/tests/phpunit/tests/user/wpSetCurrentUser.php +++ b/tests/phpunit/tests/user/wpSetCurrentUser.php @@ -57,4 +57,32 @@ public function test_should_set_by_name_if_id_is_null() { $this->assertSame( $user, wp_get_current_user() ); $this->assertSame( self::$user_id2, get_current_user_id() ); } + + /** + * Ensure user switching doesn't occur for the same user, even if type is non-int. + * + * @dataProvider data_should_not_switch_to_same_user_type_equivalency + */ + public function test_should_not_switch_to_same_user_type_equivalency( $type_function ) { + wp_set_current_user( self::$user_id ); + $this->assertSame( self::$user_id, get_current_user_id() ); + + $action = new MockAction(); + add_action( 'set_current_user', array( $action, 'action' ) ); + + wp_set_current_user( $type_function( self::$user_id ) ); + $this->assertSame( 0, $action->get_call_count(), 'set_current_user should not be fired when switching to the same user.' ); + } + + /** + * Data provider for test_should_not_switch_to_same_user_type_equivalency. + * + * @return array[] Data provider. + */ + public function data_should_not_switch_to_same_user_type_equivalency() { + return array( + 'integer' => array( 'type_function' => 'intval' ), + 'string' => array( 'type_function' => 'strval' ), + ); + } } From 6cd41b99b9592ec81ec5c19209e3465aaa8c9d91 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 12 Feb 2026 11:16:18 +1100 Subject: [PATCH 2/5] Cast $id parameter to integer before comparison with current user ID. --- src/wp-includes/pluggable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 2dbdd584b385d..03593ea3e64e6 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -30,7 +30,7 @@ function wp_set_current_user( $id, $name = '' ) { // If `$id` matches the current user, there is nothing to do. if ( isset( $current_user ) && ( $current_user instanceof WP_User ) - && ( $id === $current_user->ID ) + && ( (int) $id === $current_user->ID ) && ( null !== $id ) ) { return $current_user; From af7ce0158fb64276be809f30ecb6829f9785d707 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 12 Feb 2026 12:06:28 +1100 Subject: [PATCH 3/5] Add ticket number to test. --- tests/phpunit/tests/user/wpSetCurrentUser.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/user/wpSetCurrentUser.php b/tests/phpunit/tests/user/wpSetCurrentUser.php index 581d286e642ed..ad4ff5f34ca21 100644 --- a/tests/phpunit/tests/user/wpSetCurrentUser.php +++ b/tests/phpunit/tests/user/wpSetCurrentUser.php @@ -61,6 +61,8 @@ public function test_should_set_by_name_if_id_is_null() { /** * Ensure user switching doesn't occur for the same user, even if type is non-int. * + * @ticket 64628 + * * @dataProvider data_should_not_switch_to_same_user_type_equivalency */ public function test_should_not_switch_to_same_user_type_equivalency( $type_function ) { From 080ffaa6f628b4ce8b6e7c6fcf44624f70a2d262 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Fri, 13 Feb 2026 07:46:24 +1100 Subject: [PATCH 4/5] Add type declarations to unit tests. Co-authored-by: Weston Ruter --- tests/phpunit/tests/user/wpSetCurrentUser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/user/wpSetCurrentUser.php b/tests/phpunit/tests/user/wpSetCurrentUser.php index ad4ff5f34ca21..45096acf36999 100644 --- a/tests/phpunit/tests/user/wpSetCurrentUser.php +++ b/tests/phpunit/tests/user/wpSetCurrentUser.php @@ -65,7 +65,7 @@ public function test_should_set_by_name_if_id_is_null() { * * @dataProvider data_should_not_switch_to_same_user_type_equivalency */ - public function test_should_not_switch_to_same_user_type_equivalency( $type_function ) { + public function test_should_not_switch_to_same_user_type_equivalency( string $type_function ) { wp_set_current_user( self::$user_id ); $this->assertSame( self::$user_id, get_current_user_id() ); @@ -81,7 +81,7 @@ public function test_should_not_switch_to_same_user_type_equivalency( $type_func * * @return array[] Data provider. */ - public function data_should_not_switch_to_same_user_type_equivalency() { + public function data_should_not_switch_to_same_user_type_equivalency(): array { return array( 'integer' => array( 'type_function' => 'intval' ), 'string' => array( 'type_function' => 'strval' ), From a1836bc84e29f1de1e96b3b6c218613cc145e3b8 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 13 Feb 2026 11:40:44 +1100 Subject: [PATCH 5/5] Add message param to assertion. --- tests/phpunit/tests/user/wpSetCurrentUser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/user/wpSetCurrentUser.php b/tests/phpunit/tests/user/wpSetCurrentUser.php index 45096acf36999..868b3bc71b38a 100644 --- a/tests/phpunit/tests/user/wpSetCurrentUser.php +++ b/tests/phpunit/tests/user/wpSetCurrentUser.php @@ -67,7 +67,7 @@ public function test_should_set_by_name_if_id_is_null() { */ public function test_should_not_switch_to_same_user_type_equivalency( string $type_function ) { wp_set_current_user( self::$user_id ); - $this->assertSame( self::$user_id, get_current_user_id() ); + $this->assertSame( self::$user_id, get_current_user_id(), "Current user's ID should match the ID of the user switched to." ); $action = new MockAction(); add_action( 'set_current_user', array( $action, 'action' ) );