3 namespace MediaWiki\Tests\Api
;
5 use MediaWiki\Session\Token
;
10 * @covers \ApiCheckToken
12 class ApiCheckTokenTest
extends ApiTestCase
{
15 * Test result of checking previously queried token (should be valid)
17 public function testCheckTokenValid() {
18 // Query token which will be checked later
19 $tokens = $this->doApiRequest( [
24 $data = $this->doApiRequest( [
25 'action' => 'checktoken',
27 'token' => $tokens[0]['query']['tokens']['csrftoken'],
28 ], $tokens[1]->getSessionArray() );
30 $this->assertEquals( 'valid', $data[0]['checktoken']['result'] );
31 $this->assertArrayHasKey( 'generated', $data[0]['checktoken'] );
35 * Test result of checking invalid token
37 public function testCheckTokenInvalid() {
39 $data = $this->doApiRequest( [
40 'action' => 'checktoken',
42 'token' => 'invalid_token',
45 $this->assertEquals( 'invalid', $data[0]['checktoken']['result'] );
49 * Test result of checking token with negative max age (should be expired)
51 public function testCheckTokenExpired() {
52 // Query token which will be checked later
53 $tokens = $this->doApiRequest( [
58 $data = $this->doApiRequest( [
59 'action' => 'checktoken',
61 'token' => $tokens[0]['query']['tokens']['csrftoken'],
63 ], $tokens[1]->getSessionArray() );
65 $this->assertEquals( 'expired', $data[0]['checktoken']['result'] );
66 $this->assertArrayHasKey( 'generated', $data[0]['checktoken'] );
70 * Test if using token with incorrect suffix will produce a warning
72 public function testCheckTokenSuffixWarning() {
73 // Query token which will be checked later
74 $tokens = $this->doApiRequest( [
79 // Get token and change the suffix
80 $token = $tokens[0]['query']['tokens']['csrftoken'];
81 $token = substr( $token, 0, -strlen( Token
::SUFFIX
) ) . urldecode( Token
::SUFFIX
);
83 $data = $this->doApiRequest( [
84 'action' => 'checktoken',
87 'errorformat' => 'raw',
88 ], $tokens[1]->getSessionArray() );
90 $this->assertEquals( 'invalid', $data[0]['checktoken']['result'] );
91 $this->assertArrayHasKey( 'warnings', $data[0] );
92 $this->assertCount( 1, $data[0]['warnings'] );
93 $this->assertEquals( 'checktoken', $data[0]['warnings'][0]['module'] );
94 $this->assertEquals( 'checktoken-percentencoding', $data[0]['warnings'][0]['code'] );