Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions tests/phpunit/tests/user/wpSetCurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,34 @@ 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.
*
* @ticket 64628
*
* @dataProvider data_should_not_switch_to_same_user_type_equivalency
*/
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(), "Current user's ID should match the ID of the user switched to." );

$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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return array[] Data provider.
* @return array<string, array{ type_function: string }> Data provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter It's a little mixed but currently it seems common just to add the simplified docs for the data provider to avoid the need to double up docs with the @params in the function above it. For example c4d8047#diff-09a2e7959959d6a08ef07b1849e0178b853f7ef86f7885bbf9be2ed261145b24R4215

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly, but in new tests I've tried to be as explicit about the return types as possible. This makes it easier to understand at a glance what a data provider is expected to return, as opposed to analyzing the pattern in the data being returned. Granted, the data returned here is quite simple.

*/
public function data_should_not_switch_to_same_user_type_equivalency(): array {
return array(
'integer' => array( 'type_function' => 'intval' ),
'string' => array( 'type_function' => 'strval' ),
);
}
}
Loading