MediaWiki REL1_33
UserInfoTest.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Session;
4
6use User;
7
14
15 public function testNewAnonymous() {
16 $userinfo = UserInfo::newAnonymous();
17
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 );
26 }
27
28 public function testNewFromId() {
29 $id = wfGetDB( DB_MASTER )->selectField( 'user', 'MAX(user_id)' ) + 1;
30 try {
32 $this->fail( 'Expected exception not thrown' );
33 } catch ( \InvalidArgumentException $ex ) {
34 $this->assertSame( 'Invalid ID', $ex->getMessage() );
35 }
36
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::class, $userinfo->getUser() );
45 $userinfo2 = $userinfo->verified();
46 $this->assertNotSame( $userinfo2, $userinfo );
47 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
48
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::class, $userinfo2->getUser() );
55 $this->assertSame( $userinfo2, $userinfo2->verified() );
56 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
57
58 $userinfo = UserInfo::newFromId( $user->getId(), true );
59 $this->assertTrue( $userinfo->isVerified() );
60 $this->assertSame( $userinfo, $userinfo->verified() );
61 }
62
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() );
69 }
70
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::class, $userinfo->getUser() );
80 $userinfo2 = $userinfo->verified();
81 $this->assertNotSame( $userinfo2, $userinfo );
82 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
83
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::class, $userinfo2->getUser() );
90 $this->assertSame( $userinfo2, $userinfo2->verified() );
91 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
92
93 $userinfo = UserInfo::newFromName( $user->getName(), true );
94 $this->assertTrue( $userinfo->isVerified() );
95 $this->assertSame( $userinfo, $userinfo->verified() );
96
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::class, $userinfo->getUser() );
107 $userinfo2 = $userinfo->verified();
108 $this->assertNotSame( $userinfo2, $userinfo );
109 $this->assertSame( "<-:{$user->getId()}:{$user->getName()}>", (string)$userinfo );
110
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::class, $userinfo2->getUser() );
117 $this->assertSame( $userinfo2, $userinfo2->verified() );
118 $this->assertSame( "<+:{$user->getId()}:{$user->getName()}>", (string)$userinfo2 );
119
120 $userinfo = UserInfo::newFromName( $user->getName(), true );
121 $this->assertTrue( $userinfo->isVerified() );
122 $this->assertSame( $userinfo, $userinfo->verified() );
123 }
124
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 );
138
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 );
147
148 $userinfo = UserInfo::newFromUser( $user, true );
149 $this->assertTrue( $userinfo->isVerified() );
150 $this->assertSame( $userinfo, $userinfo->verified() );
151
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 );
165
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 );
174
175 $userinfo = UserInfo::newFromUser( $user, true );
176 $this->assertTrue( $userinfo->isVerified() );
177 $this->assertSame( $userinfo, $userinfo->verified() );
178
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() );
184 }
185
186}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Session Database MediaWiki\Session\UserInfo.
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition UserInfo.php:103
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition UserInfo.php:75
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition UserInfo.php:117
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition UserInfo.php:85
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
const DB_MASTER
Definition defines.php:26