config('user.settings')->set('register', USER_REGISTER_ADMINISTRATORS_ONLY)->save(); // Create a new user account and log in. $account = $this->drupalCreateUser(['change own username']); $this->drupalLogin($account); // Change own username. $edit = []; $edit['name'] = $this->randomMachineName(); $this->drupalPostForm('user/' . $account->id() . '/edit', $edit, t('Save')); // Log out. $this->drupalLogout(); // Set the new name on the user account and attempt to log back in. $account->name = $edit['name']; $this->drupalLogin($account); } }