Skip to content

Commit

Permalink
Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Jan 16, 2025
1 parent 36cdf50 commit 545870b
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/phpunit/tests/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,36 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
public function set_up() {
parent::set_up();

add_action( 'set_auth_cookie', array( $this, 'action_set_auth_cookie' ), 10, 6 );

Check failure on line 66 in tests/phpunit/tests/user.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 space after comma in argument list; 6 found
add_action( 'set_logged_in_cookie', array( $this, 'action_set_logged_in_cookie' ), 10 );
add_action( 'clear_auth_cookie', array( $this, 'action_clear_auth_cookie' ) );

Check failure on line 68 in tests/phpunit/tests/user.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 space after comma in argument list; 4 found

$_COOKIE = [];

Check failure on line 70 in tests/phpunit/tests/user.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Short array syntax is not allowed
$this->author = clone self::$_author;
}

final public function action_set_auth_cookie(
string $cookie,
int $expire,
int $expiration,
int $user_id,
string $scheme,
string $token
): void {
$_COOKIE[ SECURE_AUTH_COOKIE ] = $cookie;
$_COOKIE[ AUTH_COOKIE ] = $cookie;
}

final public function action_set_logged_in_cookie( string $cookie ): void {
$_COOKIE[ LOGGED_IN_COOKIE ] = $cookie;
}

final public function action_clear_auth_cookie(): void {
unset( $_COOKIE[ LOGGED_IN_COOKIE ] );
unset( $_COOKIE[ SECURE_AUTH_COOKIE ] );
unset( $_COOKIE[ AUTH_COOKIE ] );
}

public function test_get_users_of_blog() {
// Add one of each user role.
$nusers = array(
Expand Down Expand Up @@ -1122,6 +1149,42 @@ public function test_changing_password_invalidates_password_reset_key() {
$this->assertEmpty( $user->user_activation_key );
}

/**
* @ticket 61366
* @dataProvider data_remember_user
*/
public function test_changing_own_password_retains_current_session( bool $remember ) {
$user = $this->author;
$manager = WP_Session_Tokens::get_instance( $user->ID );
$expiry = $remember ? ( 2 * WEEK_IN_SECONDS ) : ( 2 * DAY_IN_SECONDS );
$token = $manager->create( time() + $expiry );
$pass = $user->user_pass;

wp_set_current_user( $user->ID );
wp_set_auth_cookie( $user->ID, $remember, '', $token );

$userdata = array(
'ID' => $user->ID,
'user_pass' => 'my_new_password',
);
$updated = wp_update_user( $userdata, $manager );
$valid = wp_validate_auth_cookie();
$cookie = wp_parse_auth_cookie();

$this->assertNotWPError( $updated );
$this->assertNotSame( $pass, get_userdata( $user->ID )->user_pass );
$this->assertSame( $user->ID, $valid );
$this->assertSame( $token, $cookie['token'] );
$this->assertCount( 1, $manager->get_all() );
}

public function data_remember_user() {
return array(
array( true ),
array( false ),
);
}

public function test_search_users_login() {
$users = get_users(
array(
Expand Down

0 comments on commit 545870b

Please sign in to comment.