3 namespace MediaWiki\Tests\Api\Query
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\Message\Message
;
7 use MediaWiki\Tests\Api\ApiTestCase
;
12 * @covers \MediaWiki\Api\ApiQueryTokens
14 class ApiQueryTokensTest
extends ApiTestCase
{
16 public function testGetCsrfToken() {
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() {
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
48 $this->overrideConfigValue( MainConfigNames
::APIMaxResultSize
, $size );
50 [ $result ] = $this->doApiRequest( [
53 'type' => 'csrf|patrol|watch',
57 $this->apiContext
->msg( 'apiwarn-truncatedresult', Message
::numParam( $size ) )
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'] );