wiki.techinc.nl/tests/phpunit/includes/api/ApiLogoutTest.php
Bartosz Dziewoński 1048db795b API tests: Assert error codes, not error messages
Error messages are not guaranteed to be stable, and these tests
prevent us from improving them.

Error codes are supposed to be stable, so we should be asserting
that they do not change (especially since many of them are
dynamically generated by the dodgy code in ApiMessageTrait).

Introduce helpers assertApiErrorCode() and expectApiErrorCode()
to be used instead of the usual PHPUnit assertions/expectations
for this case.

Change-Id: I752f82f29bf5f9405ea117ebf9e5cf70335464ad
2023-04-26 19:36:29 +02:00

74 lines
1.7 KiB
PHP

<?php
/**
* @group API
* @group Database
* @group medium
*
* @covers ApiLogout
*/
class ApiLogoutTest extends ApiTestCase {
protected function setUp(): void {
global $wgRequest;
parent::setUp();
$user = $this->getTestSysop()->getUser();
$wgRequest->getSession()->setUser( $user );
$this->apiContext->setUser( $user );
}
public function testUserLogoutBadToken() {
$user = $this->getTestSysop()->getUser();
$this->expectApiErrorCode( 'badtoken' );
try {
$token = 'invalid token';
$this->doUserLogout( $token, $user );
} finally {
$this->assertTrue( $user->isRegistered(), 'not logged out' );
}
}
public function testUserLogout() {
$user = $this->getTestSysop()->getUser();
$this->assertTrue( $user->isRegistered() );
$token = $this->getUserCsrfTokenFromApi( $user );
$this->doUserLogout( $token, $user );
$this->assertFalse( $user->isRegistered() );
}
public function testUserLogoutWithWebToken() {
global $wgRequest;
$user = $this->getTestSysop()->getUser();
$this->assertTrue( $user->isRegistered() );
// Logic copied from SkinTemplate.
$token = $user->getEditToken( 'logoutToken', $wgRequest );
$this->doUserLogout( $token, $user );
$this->assertFalse( $user->isRegistered() );
}
private function getUserCsrfTokenFromApi( User $user ) {
$retToken = $this->doApiRequest( [
'action' => 'query',
'meta' => 'tokens',
'type' => 'csrf'
], null, false, $user );
$this->assertArrayNotHasKey( 'warnings', $retToken );
return $retToken[0]['query']['tokens']['csrftoken'];
}
private function doUserLogout( $logoutToken, User $user ) {
return $this->doApiRequest( [
'action' => 'logout',
'token' => $logoutToken
], null, false, $user );
}
}