MediaWiki  1.32.5
ApiLogoutTest.php
Go to the documentation of this file.
1 <?php
2 
10 class ApiLogoutTest extends ApiTestCase {
11 
12  protected function setUp() {
13  global $wgRequest, $wgUser;
14 
15  parent::setUp();
16 
17  // Link the user to the Session properly so User::doLogout() doesn't complain.
18  $wgRequest->getSession()->setUser( $wgUser );
19  $wgUser = User::newFromSession( $wgRequest );
20  $this->apiContext->setUser( $wgUser );
21  }
22 
23  public function testUserLogoutBadToken() {
24  global $wgUser;
25 
26  $this->setExpectedApiException( 'apierror-badtoken' );
27 
28  try {
29  $token = 'invalid token';
30  $this->doUserLogout( $token );
31  } finally {
32  $this->assertTrue( $wgUser->isLoggedIn(), 'not logged out' );
33  }
34  }
35 
36  public function testUserLogout() {
37  global $wgUser;
38 
39  $this->assertTrue( $wgUser->isLoggedIn(), 'sanity check' );
40  $token = $this->getUserCsrfTokenFromApi();
41  $this->doUserLogout( $token );
42  $this->assertFalse( $wgUser->isLoggedIn() );
43  }
44 
45  public function testUserLogoutWithWebToken() {
46  global $wgUser, $wgRequest;
47 
48  $this->assertTrue( $wgUser->isLoggedIn(), 'sanity check' );
49 
50  // Logic copied from SkinTemplate.
51  $token = $wgUser->getEditToken( 'logoutToken', $wgRequest );
52 
53  $this->doUserLogout( $token );
54  $this->assertFalse( $wgUser->isLoggedIn() );
55  }
56 
57  private function getUserCsrfTokenFromApi() {
58  $retToken = $this->doApiRequest( [
59  'action' => 'query',
60  'meta' => 'tokens',
61  'type' => 'csrf'
62  ] );
63 
64  $this->assertArrayNotHasKey( 'warnings', $retToken );
65 
66  return $retToken[0]['query']['tokens']['csrftoken'];
67  }
68 
69  private function doUserLogout( $logoutToken ) {
70  return $this->doApiRequest( [
71  'action' => 'logout',
72  'token' => $logoutToken
73  ] );
74  }
75 }
ApiLogoutTest\testUserLogoutWithWebToken
testUserLogoutWithWebToken()
Definition: ApiLogoutTest.php:45
User\newFromSession
static newFromSession(WebRequest $request=null)
Create a new user object using data from session.
Definition: User.php:756
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:63
ApiLogoutTest\getUserCsrfTokenFromApi
getUserCsrfTokenFromApi()
Definition: ApiLogoutTest.php:57
ApiLogoutTest\testUserLogout
testUserLogout()
Definition: ApiLogoutTest.php:36
ApiLogoutTest
API Database medium.
Definition: ApiLogoutTest.php:10
ApiTestCase
Definition: ApiTestCase.php:5
ApiLogoutTest\testUserLogoutBadToken
testUserLogoutBadToken()
Definition: ApiLogoutTest.php:23
ApiLogoutTest\setUp
setUp()
Definition: ApiLogoutTest.php:12
ApiTestCase\setExpectedApiException
setExpectedApiException( $msg, $code=null, array $data=null, $httpCode=0)
Expect an ApiUsageException to be thrown with the given parameters, which are the same as ApiUsageExc...
Definition: ApiTestCase.php:217
ApiLogoutTest\doUserLogout
doUserLogout( $logoutToken)
Definition: ApiLogoutTest.php:69
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:747