Merge "Return early in ApiLogout if user not logged in"

This commit is contained in:
jenkins-bot 2024-09-09 17:34:51 +00:00 committed by Gerrit Code Review
commit e6d0f05427
2 changed files with 21 additions and 0 deletions

View file

@ -51,6 +51,13 @@ class ApiLogout extends ApiBase {
}
$user = $this->getUser();
if ( $user->isAnon() ) {
// Cannot logout a anon user, so add a warning and return early.
$this->addWarning( 'apierror-mustbeloggedin-generic', 'notloggedin' );
return;
}
$oldName = $user->getName();
$user->logout();

View file

@ -35,6 +35,20 @@ class ApiLogoutTest extends ApiTestCase {
}
}
public function testUserLogoutAlreadyLoggedOut() {
$user = $this->getServiceContainer()->getUserFactory()->newAnonymous( '1.2.3.4' );
$this->assertFalse( $user->isRegistered() );
$token = $this->getUserCsrfTokenFromApi( $user );
$response = $this->doUserLogout( $token, $user )[0];
$this->assertFalse( $user->isRegistered() );
$this->assertArrayEquals(
[ 'warnings' => [ 'logout' => [ 'warnings' => 'You must be logged in.' ] ] ],
$response
);
}
public function testUserLogout() {
$user = $this->getTestSysop()->getUser();