Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / session / UserInfoTest.php
blobc38edd694a666f505bc82cc44dc652254b9ead59
1 <?php
3 namespace MediaWiki\Session;
5 use MediaWikiTestCase;
6 use User;
8 /**
9 * @group Session
10 * @group Database
11 * @covers MediaWiki\Session\UserInfo
13 class UserInfoTest extends MediaWikiTestCase {
15 public function testNewAnonymous() {
16 $userinfo = UserInfo::newAnonymous();
18 $this->assertTrue( $userinfo->isAnon() );
19 $this->assertTrue( $userinfo->isVerified() );
20 $this->assertSame( 0, $userinfo->getId() );
21 $this->assertSame( null, $userinfo->getName() );
22 $this->assertSame( '', $userinfo->getToken() );
23 $this->assertNotNull( $userinfo->getUser() );
24 $this->assertSame( $userinfo, $userinfo->verified() );
25 $this->assertSame( '<anon>', (string)$userinfo );
28 public function testNewFromId() {
29 $id = wfGetDB( DB_MASTER )->selectField( 'user', 'MAX(user_id)' ) + 1;
30 try {
31 UserInfo::newFromId( $id );
32 $this->fail( 'Expected exception not thrown' );
33 } catch ( \InvalidArgumentException $ex ) {
34 $this->assertSame( 'Invalid ID', $ex->getMessage() );
37 $user = User::newFromName( 'UTSysop' );
38 $userinfo = UserInfo::newFromId( $user->getId() );
39 $this->assertFalse( $userinfo->isAnon() );
40 $this->assertFalse( $userinfo->isVerified() );
41 $this->assertSame( $user->getId(), $userinfo->getId() );
42 $this->assertSame( $user->getName(), $userinfo->getName() );
43 $this->assertSame( $user->getToken( true ), $userinfo->getToken() );
44 $this->assertInstanceOf( 'User', $userinfo->getUser() );
45 $userinfo2 = $userinfo->verified();
46 $this->assertNotSame( $userinfo2, $userinfo );
47 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
49 $this->assertFalse( $userinfo2->isAnon() );
50 $this->assertTrue( $userinfo2->isVerified() );
51 $this->assertSame( $user->getId(), $userinfo2->getId() );
52 $this->assertSame( $user->getName(), $userinfo2->getName() );
53 $this->assertSame( $user->getToken( true ), $userinfo2->getToken() );
54 $this->assertInstanceOf( 'User', $userinfo2->getUser() );
55 $this->assertSame( $userinfo2, $userinfo2->verified() );
56 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
58 $userinfo = UserInfo::newFromId( $user->getId(), true );
59 $this->assertTrue( $userinfo->isVerified() );
60 $this->assertSame( $userinfo, $userinfo->verified() );
63 public function testNewFromName() {
64 try {
65 UserInfo::newFromName( '<bad name>' );
66 $this->fail( 'Expected exception not thrown' );
67 } catch ( \InvalidArgumentException $ex ) {
68 $this->assertSame( 'Invalid user name', $ex->getMessage() );
71 // User name that exists
72 $user = User::newFromName( 'UTSysop' );
73 $userinfo = UserInfo::newFromName( $user->getName() );
74 $this->assertFalse( $userinfo->isAnon() );
75 $this->assertFalse( $userinfo->isVerified() );
76 $this->assertSame( $user->getId(), $userinfo->getId() );
77 $this->assertSame( $user->getName(), $userinfo->getName() );
78 $this->assertSame( $user->getToken( true ), $userinfo->getToken() );
79 $this->assertInstanceOf( 'User', $userinfo->getUser() );
80 $userinfo2 = $userinfo->verified();
81 $this->assertNotSame( $userinfo2, $userinfo );
82 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
84 $this->assertFalse( $userinfo2->isAnon() );
85 $this->assertTrue( $userinfo2->isVerified() );
86 $this->assertSame( $user->getId(), $userinfo2->getId() );
87 $this->assertSame( $user->getName(), $userinfo2->getName() );
88 $this->assertSame( $user->getToken( true ), $userinfo2->getToken() );
89 $this->assertInstanceOf( 'User', $userinfo2->getUser() );
90 $this->assertSame( $userinfo2, $userinfo2->verified() );
91 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
93 $userinfo = UserInfo::newFromName( $user->getName(), true );
94 $this->assertTrue( $userinfo->isVerified() );
95 $this->assertSame( $userinfo, $userinfo->verified() );
97 // User name that does not exist should still be non-anon
98 $user = User::newFromName( 'DoesNotExist' );
99 $this->assertSame( 0, $user->getId(), 'sanity check' );
100 $userinfo = UserInfo::newFromName( $user->getName() );
101 $this->assertFalse( $userinfo->isAnon() );
102 $this->assertFalse( $userinfo->isVerified() );
103 $this->assertSame( $user->getId(), $userinfo->getId() );
104 $this->assertSame( $user->getName(), $userinfo->getName() );
105 $this->assertSame( '', $userinfo->getToken() );
106 $this->assertInstanceOf( 'User', $userinfo->getUser() );
107 $userinfo2 = $userinfo->verified();
108 $this->assertNotSame( $userinfo2, $userinfo );
109 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
111 $this->assertFalse( $userinfo2->isAnon() );
112 $this->assertTrue( $userinfo2->isVerified() );
113 $this->assertSame( $user->getId(), $userinfo2->getId() );
114 $this->assertSame( $user->getName(), $userinfo2->getName() );
115 $this->assertSame( '', $userinfo2->getToken() );
116 $this->assertInstanceOf( 'User', $userinfo2->getUser() );
117 $this->assertSame( $userinfo2, $userinfo2->verified() );
118 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
120 $userinfo = UserInfo::newFromName( $user->getName(), true );
121 $this->assertTrue( $userinfo->isVerified() );
122 $this->assertSame( $userinfo, $userinfo->verified() );
125 public function testNewFromUser() {
126 // User that exists
127 $user = User::newFromName( 'UTSysop' );
128 $userinfo = UserInfo::newFromUser( $user );
129 $this->assertFalse( $userinfo->isAnon() );
130 $this->assertFalse( $userinfo->isVerified() );
131 $this->assertSame( $user->getId(), $userinfo->getId() );
132 $this->assertSame( $user->getName(), $userinfo->getName() );
133 $this->assertSame( $user->getToken( true ), $userinfo->getToken() );
134 $this->assertSame( $user, $userinfo->getUser() );
135 $userinfo2 = $userinfo->verified();
136 $this->assertNotSame( $userinfo2, $userinfo );
137 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
139 $this->assertFalse( $userinfo2->isAnon() );
140 $this->assertTrue( $userinfo2->isVerified() );
141 $this->assertSame( $user->getId(), $userinfo2->getId() );
142 $this->assertSame( $user->getName(), $userinfo2->getName() );
143 $this->assertSame( $user->getToken( true ), $userinfo2->getToken() );
144 $this->assertSame( $user, $userinfo2->getUser() );
145 $this->assertSame( $userinfo2, $userinfo2->verified() );
146 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
148 $userinfo = UserInfo::newFromUser( $user, true );
149 $this->assertTrue( $userinfo->isVerified() );
150 $this->assertSame( $userinfo, $userinfo->verified() );
152 // User name that does not exist should still be non-anon
153 $user = User::newFromName( 'DoesNotExist' );
154 $this->assertSame( 0, $user->getId(), 'sanity check' );
155 $userinfo = UserInfo::newFromUser( $user );
156 $this->assertFalse( $userinfo->isAnon() );
157 $this->assertFalse( $userinfo->isVerified() );
158 $this->assertSame( $user->getId(), $userinfo->getId() );
159 $this->assertSame( $user->getName(), $userinfo->getName() );
160 $this->assertSame( '', $userinfo->getToken() );
161 $this->assertSame( $user, $userinfo->getUser() );
162 $userinfo2 = $userinfo->verified();
163 $this->assertNotSame( $userinfo2, $userinfo );
164 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
166 $this->assertFalse( $userinfo2->isAnon() );
167 $this->assertTrue( $userinfo2->isVerified() );
168 $this->assertSame( $user->getId(), $userinfo2->getId() );
169 $this->assertSame( $user->getName(), $userinfo2->getName() );
170 $this->assertSame( '', $userinfo2->getToken() );
171 $this->assertSame( $user, $userinfo2->getUser() );
172 $this->assertSame( $userinfo2, $userinfo2->verified() );
173 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
175 $userinfo = UserInfo::newFromUser( $user, true );
176 $this->assertTrue( $userinfo->isVerified() );
177 $this->assertSame( $userinfo, $userinfo->verified() );
179 // Anonymous user gives anon
180 $userinfo = UserInfo::newFromUser( new User, false );
181 $this->assertTrue( $userinfo->isVerified() );
182 $this->assertSame( 0, $userinfo->getId() );
183 $this->assertSame( null, $userinfo->getName() );