Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / session / UserInfoTest.php
blobaa2226e881fb31f89ca4cae731df9a53912034e6
1 <?php
3 namespace MediaWiki\Tests\Session;
5 use InvalidArgumentException;
6 use MediaWiki\Session\UserInfo;
7 use MediaWiki\User\User;
8 use MediaWikiIntegrationTestCase;
10 /**
11 * @group Session
12 * @group Database
13 * @covers \MediaWiki\Session\UserInfo
15 class UserInfoTest extends MediaWikiIntegrationTestCase {
17 public function testNewAnonymous() {
18 $userinfo = UserInfo::newAnonymous();
20 $this->assertTrue( $userinfo->isAnon() );
21 $this->assertTrue( $userinfo->isVerified() );
22 $this->assertSame( 0, $userinfo->getId() );
23 $this->assertSame( null, $userinfo->getName() );
24 $this->assertSame( '', $userinfo->getToken() );
25 $this->assertNotNull( $userinfo->getUser() );
26 $this->assertSame( $userinfo, $userinfo->verified() );
27 $this->assertSame( '<anon>', (string)$userinfo );
30 public function testNewFromId() {
31 $id = $this->getDb()->newSelectQueryBuilder()
32 ->select( 'MAX(user_id)' )
33 ->from( 'user' )
34 ->fetchField() + 1;
35 try {
36 UserInfo::newFromId( $id );
37 $this->fail( 'Expected exception not thrown' );
38 } catch ( InvalidArgumentException $ex ) {
39 $this->assertSame( 'Invalid ID', $ex->getMessage() );
42 $user = $this->getTestSysop()->getUser();
43 $userinfo = UserInfo::newFromId( $user->getId() );
44 $this->assertFalse( $userinfo->isAnon() );
45 $this->assertFalse( $userinfo->isVerified() );
46 $this->assertSame( $user->getId(), $userinfo->getId() );
47 $this->assertSame( $user->getName(), $userinfo->getName() );
48 $this->assertSame( $user->getToken( true ), $userinfo->getToken() );
49 $this->assertInstanceOf( User::class, $userinfo->getUser() );
50 $userinfo2 = $userinfo->verified();
51 $this->assertNotSame( $userinfo2, $userinfo );
52 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
54 $this->assertFalse( $userinfo2->isAnon() );
55 $this->assertTrue( $userinfo2->isVerified() );
56 $this->assertSame( $user->getId(), $userinfo2->getId() );
57 $this->assertSame( $user->getName(), $userinfo2->getName() );
58 $this->assertSame( $user->getToken( true ), $userinfo2->getToken() );
59 $this->assertInstanceOf( User::class, $userinfo2->getUser() );
60 $this->assertSame( $userinfo2, $userinfo2->verified() );
61 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
63 $userinfo = UserInfo::newFromId( $user->getId(), true );
64 $this->assertTrue( $userinfo->isVerified() );
65 $this->assertSame( $userinfo, $userinfo->verified() );
68 public function testNewFromName() {
69 try {
70 UserInfo::newFromName( '<bad name>' );
71 $this->fail( 'Expected exception not thrown' );
72 } catch ( InvalidArgumentException $ex ) {
73 $this->assertSame( 'Invalid user name', $ex->getMessage() );
76 // User name that exists
77 $user = $this->getTestSysop()->getUser();
78 $userinfo = UserInfo::newFromName( $user->getName() );
79 $this->assertFalse( $userinfo->isAnon() );
80 $this->assertFalse( $userinfo->isVerified() );
81 $this->assertSame( $user->getId(), $userinfo->getId() );
82 $this->assertSame( $user->getName(), $userinfo->getName() );
83 $this->assertSame( $user->getToken( true ), $userinfo->getToken() );
84 $this->assertInstanceOf( User::class, $userinfo->getUser() );
85 $userinfo2 = $userinfo->verified();
86 $this->assertNotSame( $userinfo2, $userinfo );
87 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
89 $this->assertFalse( $userinfo2->isAnon() );
90 $this->assertTrue( $userinfo2->isVerified() );
91 $this->assertSame( $user->getId(), $userinfo2->getId() );
92 $this->assertSame( $user->getName(), $userinfo2->getName() );
93 $this->assertSame( $user->getToken( true ), $userinfo2->getToken() );
94 $this->assertInstanceOf( User::class, $userinfo2->getUser() );
95 $this->assertSame( $userinfo2, $userinfo2->verified() );
96 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
98 $userinfo = UserInfo::newFromName( $user->getName(), true );
99 $this->assertTrue( $userinfo->isVerified() );
100 $this->assertSame( $userinfo, $userinfo->verified() );
102 // User name that does not exist should still be non-anon
103 $user = User::newFromName( 'DoesNotExist' );
104 $this->assertSame( 0, $user->getId(), 'User id is 0' );
105 $userinfo = UserInfo::newFromName( $user->getName() );
106 $this->assertFalse( $userinfo->isAnon() );
107 $this->assertFalse( $userinfo->isVerified() );
108 $this->assertSame( $user->getId(), $userinfo->getId() );
109 $this->assertSame( $user->getName(), $userinfo->getName() );
110 $this->assertSame( '', $userinfo->getToken() );
111 $this->assertInstanceOf( User::class, $userinfo->getUser() );
112 $userinfo2 = $userinfo->verified();
113 $this->assertNotSame( $userinfo2, $userinfo );
114 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
116 $this->assertFalse( $userinfo2->isAnon() );
117 $this->assertTrue( $userinfo2->isVerified() );
118 $this->assertSame( $user->getId(), $userinfo2->getId() );
119 $this->assertSame( $user->getName(), $userinfo2->getName() );
120 $this->assertSame( '', $userinfo2->getToken() );
121 $this->assertInstanceOf( User::class, $userinfo2->getUser() );
122 $this->assertSame( $userinfo2, $userinfo2->verified() );
123 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
125 $userinfo = UserInfo::newFromName( $user->getName(), true );
126 $this->assertTrue( $userinfo->isVerified() );
127 $this->assertSame( $userinfo, $userinfo->verified() );
130 public function testNewFromUser() {
131 // User that exists
132 $user = $this->getTestSysop()->getUser();
133 $userinfo = UserInfo::newFromUser( $user );
134 $this->assertFalse( $userinfo->isAnon() );
135 $this->assertFalse( $userinfo->isVerified() );
136 $this->assertSame( $user->getId(), $userinfo->getId() );
137 $this->assertSame( $user->getName(), $userinfo->getName() );
138 $this->assertSame( $user->getToken( true ), $userinfo->getToken() );
139 $this->assertSame( $user, $userinfo->getUser() );
140 $userinfo2 = $userinfo->verified();
141 $this->assertNotSame( $userinfo2, $userinfo );
142 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
144 $this->assertFalse( $userinfo2->isAnon() );
145 $this->assertTrue( $userinfo2->isVerified() );
146 $this->assertSame( $user->getId(), $userinfo2->getId() );
147 $this->assertSame( $user->getName(), $userinfo2->getName() );
148 $this->assertSame( $user->getToken( true ), $userinfo2->getToken() );
149 $this->assertSame( $user, $userinfo2->getUser() );
150 $this->assertSame( $userinfo2, $userinfo2->verified() );
151 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
153 $userinfo = UserInfo::newFromUser( $user, true );
154 $this->assertTrue( $userinfo->isVerified() );
155 $this->assertSame( $userinfo, $userinfo->verified() );
157 // User name that does not exist should still be non-anon
158 $user = User::newFromName( 'DoesNotExist' );
159 $this->assertSame( 0, $user->getId() );
160 $userinfo = UserInfo::newFromUser( $user );
161 $this->assertFalse( $userinfo->isAnon() );
162 $this->assertFalse( $userinfo->isVerified() );
163 $this->assertSame( $user->getId(), $userinfo->getId() );
164 $this->assertSame( $user->getName(), $userinfo->getName() );
165 $this->assertSame( '', $userinfo->getToken() );
166 $this->assertSame( $user, $userinfo->getUser() );
167 $userinfo2 = $userinfo->verified();
168 $this->assertNotSame( $userinfo2, $userinfo );
169 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
171 $this->assertFalse( $userinfo2->isAnon() );
172 $this->assertTrue( $userinfo2->isVerified() );
173 $this->assertSame( $user->getId(), $userinfo2->getId() );
174 $this->assertSame( $user->getName(), $userinfo2->getName() );
175 $this->assertSame( '', $userinfo2->getToken() );
176 $this->assertSame( $user, $userinfo2->getUser() );
177 $this->assertSame( $userinfo2, $userinfo2->verified() );
178 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
180 $userinfo = UserInfo::newFromUser( $user, true );
181 $this->assertTrue( $userinfo->isVerified() );
182 $this->assertSame( $userinfo, $userinfo->verified() );
184 // Anonymous user gives anon
185 $userinfo = UserInfo::newFromUser( new User, false );
186 $this->assertTrue( $userinfo->isVerified() );
187 $this->assertSame( 0, $userinfo->getId() );
188 $this->assertSame( null, $userinfo->getName() );