Rename JsonUnserial… to JsonDeserial…
[mediawiki.git] / tests / phpunit / includes / api / ApiCheckTokenTest.php
blob2301524a962ef6890de6a6d8f7a5eaf0b0197608
1 <?php
3 namespace MediaWiki\Tests\Api;
5 use MediaWiki\Session\Token;
7 /**
8 * @group API
9 * @group medium
10 * @covers \ApiCheckToken
12 class ApiCheckTokenTest extends ApiTestCase {
14 /**
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( [
20 'action' => 'query',
21 'meta' => 'tokens',
22 ] );
24 $data = $this->doApiRequest( [
25 'action' => 'checktoken',
26 'type' => 'csrf',
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'] );
34 /**
35 * Test result of checking invalid token
37 public function testCheckTokenInvalid() {
38 $session = [];
39 $data = $this->doApiRequest( [
40 'action' => 'checktoken',
41 'type' => 'csrf',
42 'token' => 'invalid_token',
43 ], $session );
45 $this->assertEquals( 'invalid', $data[0]['checktoken']['result'] );
48 /**
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( [
54 'action' => 'query',
55 'meta' => 'tokens',
56 ] );
58 $data = $this->doApiRequest( [
59 'action' => 'checktoken',
60 'type' => 'csrf',
61 'token' => $tokens[0]['query']['tokens']['csrftoken'],
62 'maxtokenage' => -1,
63 ], $tokens[1]->getSessionArray() );
65 $this->assertEquals( 'expired', $data[0]['checktoken']['result'] );
66 $this->assertArrayHasKey( 'generated', $data[0]['checktoken'] );
69 /**
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( [
75 'action' => 'query',
76 'meta' => 'tokens',
77 ] );
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',
85 'type' => 'csrf',
86 'token' => $token,
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'] );