Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryTokensTest.php
blob932065ea71a234d50a7d9fc7d532bb6c70d2ef06
1 <?php
3 namespace MediaWiki\Tests\Api\Query;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Message\Message;
7 use MediaWiki\Tests\Api\ApiTestCase;
9 /**
10 * @group API
11 * @group medium
12 * @covers \MediaWiki\Api\ApiQueryTokens
14 class ApiQueryTokensTest extends ApiTestCase {
16 public function testGetCsrfToken() {
17 $params = [
18 'action' => 'query',
19 'meta' => 'tokens',
20 'type' => 'csrf',
23 $apiResult = $this->doApiRequest( $params );
24 $this->assertArrayHasKey( 'query', $apiResult[0] );
25 $this->assertArrayHasKey( 'tokens', $apiResult[0]['query'] );
26 $this->assertArrayHasKey( 'csrftoken', $apiResult[0]['query']['tokens'] );
27 $this->assertStringEndsWith( '+\\', $apiResult[0]['query']['tokens']['csrftoken'] );
30 public function testGetAllTokens() {
31 $params = [
32 'action' => 'query',
33 'meta' => 'tokens',
34 'type' => '*',
37 $apiResult = $this->doApiRequest( $params );
38 $this->assertArrayHasKey( 'query', $apiResult[0] );
39 $this->assertArrayHasKey( 'tokens', $apiResult[0]['query'] );
41 // MW core has 7 token types (createaccount, csrf, login, patrol, rollback, userrights, watch)
42 $this->assertGreaterThanOrEqual( 7, count( $apiResult[0]['query']['tokens'] ) );
45 public function testContinuation(): void {
46 // one token is 42 characters, so 100 is enough for 2 tokens but not 3
47 $size = 100;
48 $this->overrideConfigValue( MainConfigNames::APIMaxResultSize, $size );
50 [ $result ] = $this->doApiRequest( [
51 'action' => 'query',
52 'meta' => 'tokens',
53 'type' => 'csrf|patrol|watch',
54 ] );
56 $this->assertSame(
57 $this->apiContext->msg( 'apiwarn-truncatedresult', Message::numParam( $size ) )
58 ->text(),
59 $result['warnings']['result']['warnings']
62 $this->assertSame( [ 'csrftoken', 'patroltoken' ], array_keys( $result['query']['tokens'] ) );
63 $this->assertTrue( $result['batchcomplete'], 'batchcomplete should be true' );
64 $this->assertSame( [ 'type' => 'watch', 'continue' => '-||' ], $result['continue'] );