MediaWiki  1.30.2
UserInfoTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
6 use 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 {
31  UserInfo::newFromId( $id );
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', $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', $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', $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', $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', $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', $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 }
MediaWiki\Session\UserInfo\newAnonymous
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition: UserInfo.php:74
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
MediaWiki\Session\UserInfoTest
Session Database MediaWiki\Session\UserInfo.
Definition: UserInfoTest.php:13
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:550
User
User
Definition: All_system_messages.txt:425
MediaWiki\Session\UserInfo\newFromUser
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition: UserInfo.php:116
MediaWiki\Session\UserInfoTest\testNewFromUser
testNewFromUser()
Definition: UserInfoTest.php:125
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
DB_MASTER
const DB_MASTER
Definition: defines.php:26
MediaWiki\Session\UserInfo\newFromName
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition: UserInfo.php:102
MediaWiki\Session\UserInfoTest\testNewFromName
testNewFromName()
Definition: UserInfoTest.php:63
MediaWiki\Session\UserInfoTest\testNewFromId
testNewFromId()
Definition: UserInfoTest.php:28
MediaWiki\Session\UserInfoTest\testNewAnonymous
testNewAnonymous()
Definition: UserInfoTest.php:15
MediaWiki\Session\UserInfo\newFromId
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition: UserInfo.php:84
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1965
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51