Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryUserInfoTest.php
blobf4799ad2343c8ccef7b5c77089dc136c76745d2a
1 <?php
3 namespace MediaWiki\Tests\Api\Query;
5 use MediaWiki\Request\FauxRequest;
6 use MediaWiki\Tests\Api\ApiTestCase;
7 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
8 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
9 use MediaWiki\Utils\MWTimestamp;
11 /**
12 * @group API
13 * @group Database
14 * @group medium
15 * @covers \MediaWiki\Api\ApiQueryUserInfo
17 class ApiQueryUserInfoTest extends ApiTestCase {
19 use TempUserTestTrait;
20 use MockAuthorityTrait;
22 /**
23 * @covers \MediaWiki\Api\ApiQueryUserInfo::getLatestContributionTime
25 public function testTimestamp() {
26 $clock = MWTimestamp::convert( TS_UNIX, '20100101000000' );
27 MWTimestamp::setFakeTime( static function () use ( &$clock ) {
28 return $clock += 1000;
29 } );
31 $params = [
32 'action' => 'query',
33 'meta' => 'userinfo',
34 'uiprop' => 'latestcontrib',
37 $page = $this->getNonexistingTestPage();
38 $performer = $this->getTestUser()->getAuthority();
40 $apiResult = $this->doApiRequest( $params, null, false, $performer );
41 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
42 $this->assertArrayHasKey( 'query', $apiResult[0] );
43 $this->assertArrayHasKey( 'userinfo', $apiResult[0]['query'] );
44 $this->assertArrayNotHasKey( 'latestcontrib', $apiResult[0]['query']['userinfo'] );
46 $status = $this->editPage( $page, 'one' );
47 $this->assertStatusOK( $status );
48 $status = $this->editPage( $page, 'two' );
49 $this->assertStatusOK( $status );
51 $revisionTimestamp = MWTimestamp::convert( TS_ISO_8601, $page->getTimestamp() );
53 $apiResult = $this->doApiRequest( $params, null, false, $performer );
54 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
55 $this->assertArrayHasKey( 'query', $apiResult[0] );
56 $this->assertArrayHasKey( 'userinfo', $apiResult[0]['query'] );
57 $this->assertArrayHasKey( 'latestcontrib', $apiResult[0]['query']['userinfo'] );
58 $queryTimestamp = $apiResult[0]['query']['userinfo']['latestcontrib'];
59 $this->assertSame( $revisionTimestamp, $queryTimestamp );
62 public function testCanCreateAccount() {
63 $params = [
64 'action' => 'query',
65 'meta' => 'userinfo',
66 'uiprop' => 'cancreateaccount',
68 $user = $this->getTestUser()->getUser();
69 $apiResult = $this->doApiRequest( $params, null, false, $user );
70 $this->assertArrayHasKey( 'query', $apiResult[0] );
71 $this->assertArrayHasKey( 'userinfo', $apiResult[0]['query'] );
72 $this->assertArrayHasKey( 'cancreateaccount', $apiResult[0]['query']['userinfo'] );
73 $this->assertTrue( $apiResult[0]['query']['userinfo']['cancreateaccount'] );
74 $this->assertArrayNotHasKey( 'cancreateaccounterror', $apiResult[0]['query']['userinfo'] );
76 $user = $this->getMutableTestUser()->getUser();
77 $status = $this->getServiceContainer()->getBlockUserFactory()->newBlockUser(
78 $user,
79 $this->getTestSysop()->getUser(),
80 'infinity',
81 '',
82 [ 'isCreateAccountBlocked' => true ]
83 )->placeBlock();
84 if ( !$status->isGood() ) {
85 $this->fail( $status->getWikiText( false, false, 'en' ) );
87 $apiResult = $this->doApiRequest( $params, null, false, $user );
88 $this->assertArrayHasKey( 'query', $apiResult[0] );
89 $this->assertArrayHasKey( 'userinfo', $apiResult[0]['query'] );
90 $this->assertArrayHasKey( 'cancreateaccount', $apiResult[0]['query']['userinfo'] );
91 $this->assertFalse( $apiResult[0]['query']['userinfo']['cancreateaccount'] );
92 $this->assertArrayHasKey( 'cancreateaccounterror', $apiResult[0]['query']['userinfo'] );
95 public function testTempFlag() {
96 $this->enableAutoCreateTempUser();
97 $params = [
98 'action' => 'query',
99 'meta' => 'userinfo',
101 $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
102 $apiResult = $this->doApiRequest( $params, null, false, $user );
104 // Verify that the temp flag is set.
105 $this->assertArrayHasKey( 'query', $apiResult[0] );
106 $this->assertArrayHasKey( 'userinfo', $apiResult[0]['query'] );
107 $this->assertArrayHasKey( 'temp', $apiResult[0]['query']['userinfo'] );
108 $this->assertTrue( $apiResult[0]['query']['userinfo']['temp'] );
110 // Verify that the name is correct
111 $this->assertArrayHasKey( 'name', $apiResult[0]['query']['userinfo'] );
112 $this->assertSame( $user->getName(), $apiResult[0]['query']['userinfo']['name'] );
114 // Verify that the user ID is correct
115 $this->assertArrayHasKey( 'id', $apiResult[0]['query']['userinfo'] );
116 $this->assertSame( $user->getId(), $apiResult[0]['query']['userinfo']['id'] );
119 public function testAnonFlag() {
120 $this->disableAutoCreateTempUser();
121 $params = [
122 'action' => 'query',
123 'meta' => 'userinfo',
125 $user = $this->mockAnonUltimateAuthority();
126 $apiResult = $this->doApiRequest( $params, null, false, $user );
128 // Verify that the temp flag is not set.
129 $this->assertArrayHasKey( 'query', $apiResult[0] );
130 $this->assertArrayHasKey( 'userinfo', $apiResult[0]['query'] );
131 $this->assertArrayNotHasKey( 'temp', $apiResult[0]['query']['userinfo'] );
133 // Verify that the anon flag is set.
134 $this->assertArrayHasKey( 'anon', $apiResult[0]['query']['userinfo'] );
135 $this->assertTrue( $apiResult[0]['query']['userinfo']['anon'] );