diff --git a/src/MediawikiApi.php b/src/MediawikiApi.php index 2ec9540..1591991 100644 --- a/src/MediawikiApi.php +++ b/src/MediawikiApi.php @@ -489,7 +489,9 @@ private function throwLoginUsageException( $result ) { */ public function logout() { $this->logger->log( LogLevel::DEBUG, 'Logging out' ); - $result = $this->postRequest( new SimpleRequest( 'logout' ) ); + $result = $this->postRequest( new SimpleRequest( 'logout', [ + 'token' => $this->getToken() + ] ) ); if ( $result === [] ) { $this->isLoggedIn = false; $this->clearTokens(); diff --git a/tests/Unit/MediawikiApiTest.php b/tests/Unit/MediawikiApiTest.php index 9dbc910..588f340 100644 --- a/tests/Unit/MediawikiApiTest.php +++ b/tests/Unit/MediawikiApiTest.php @@ -246,10 +246,29 @@ public function testBadLoginSequence() { public function testLogout() { $client = $this->getMockClient(); - $client->expects( $this->at( 0 ) ) - ->method( 'request' ) - ->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) ) - ->will( $this->returnValue( $this->getMockResponse( [] ) ) ); + $client->method( 'request' ) + ->withConsecutive( + [ 'POST', null, $this->getExpectedRequestOpts( [ + 'action' => 'query', + 'meta' => 'tokens', + 'type' => 'csrf', + 'continue' => '' + ], 'form_params' ) ], + [ 'POST', null, $this->getExpectedRequestOpts( [ + 'action' => 'logout', + 'token' => 'TKN-csrf' + ], 'form_params' ) ] + ) + ->willReturnOnConsecutiveCalls( + $this->returnValue( $this->getMockResponse( [ + 'query' => [ + 'tokens' => [ + 'csrf' => 'TKN-csrf', + ] + ] + ] ) ), + $this->returnValue( $this->getMockResponse( [] ) ) + ); $api = new MediawikiApi( '', $client ); $this->assertTrue( $api->logout() ); @@ -257,10 +276,29 @@ public function testLogout() { public function testLogoutOnFailure() { $client = $this->getMockClient(); - $client->expects( $this->at( 0 ) ) - ->method( 'request' ) - ->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) ) - ->will( $this->returnValue( $this->getMockResponse( null ) ) ); + $client->method( 'request' ) + ->withConsecutive( + [ 'POST', null, $this->getExpectedRequestOpts( [ + 'action' => 'query', + 'meta' => 'tokens', + 'type' => 'csrf', + 'continue' => '' + ], 'form_params' ) ], + [ 'POST', null, $this->getExpectedRequestOpts( [ + 'action' => 'logout', + 'token' => 'TKN-csrf' + ], 'form_params' ) ] + ) + ->willReturnOnConsecutiveCalls( + $this->returnValue( $this->getMockResponse( [ + 'query' => [ + 'tokens' => [ + 'csrf' => 'TKN-csrf', + ] + ] + ] ) ), + $this->returnValue( $this->getMockResponse( null ) ) + ); $api = new MediawikiApi( '', $client ); $this->assertFalse( $api->logout() );