MediaWiki  1.33.0
SessionInfoTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
6 
13 
14  public function testBasics() {
15  $anonInfo = UserInfo::newAnonymous();
16  $userInfo = UserInfo::newFromName( 'UTSysop', true );
17  $unverifiedUserInfo = UserInfo::newFromName( 'UTSysop', false );
18 
19  try {
21  $this->fail( 'Expected exception not thrown', 'priority < min' );
22  } catch ( \InvalidArgumentException $ex ) {
23  $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority < min' );
24  }
25 
26  try {
28  $this->fail( 'Expected exception not thrown', 'priority > max' );
29  } catch ( \InvalidArgumentException $ex ) {
30  $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority > max' );
31  }
32 
33  try {
34  new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'id' => 'ABC?' ] );
35  $this->fail( 'Expected exception not thrown', 'bad session ID' );
36  } catch ( \InvalidArgumentException $ex ) {
37  $this->assertSame( 'Invalid session ID', $ex->getMessage(), 'bad session ID' );
38  }
39 
40  try {
41  new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'userInfo' => new \stdClass ] );
42  $this->fail( 'Expected exception not thrown', 'bad userInfo' );
43  } catch ( \InvalidArgumentException $ex ) {
44  $this->assertSame( 'Invalid userInfo', $ex->getMessage(), 'bad userInfo' );
45  }
46 
47  try {
49  $this->fail( 'Expected exception not thrown', 'no provider, no id' );
50  } catch ( \InvalidArgumentException $ex ) {
51  $this->assertSame( 'Must supply an ID when no provider is given', $ex->getMessage(),
52  'no provider, no id' );
53  }
54 
55  try {
56  new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'copyFrom' => new \stdClass ] );
57  $this->fail( 'Expected exception not thrown', 'bad copyFrom' );
58  } catch ( \InvalidArgumentException $ex ) {
59  $this->assertSame( 'Invalid copyFrom', $ex->getMessage(),
60  'bad copyFrom' );
61  }
62 
63  $manager = new SessionManager();
64  $provider = $this->getMockBuilder( SessionProvider::class )
65  ->setMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
66  ->getMockForAbstractClass();
67  $provider->setManager( $manager );
68  $provider->expects( $this->any() )->method( 'persistsSessionId' )
69  ->will( $this->returnValue( true ) );
70  $provider->expects( $this->any() )->method( 'canChangeUser' )
71  ->will( $this->returnValue( true ) );
72  $provider->expects( $this->any() )->method( '__toString' )
73  ->will( $this->returnValue( 'Mock' ) );
74 
75  $provider2 = $this->getMockBuilder( SessionProvider::class )
76  ->setMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
77  ->getMockForAbstractClass();
78  $provider2->setManager( $manager );
79  $provider2->expects( $this->any() )->method( 'persistsSessionId' )
80  ->will( $this->returnValue( true ) );
81  $provider2->expects( $this->any() )->method( 'canChangeUser' )
82  ->will( $this->returnValue( true ) );
83  $provider2->expects( $this->any() )->method( '__toString' )
84  ->will( $this->returnValue( 'Mock2' ) );
85 
86  try {
88  'provider' => $provider,
89  'userInfo' => $anonInfo,
90  'metadata' => 'foo',
91  ] );
92  $this->fail( 'Expected exception not thrown', 'bad metadata' );
93  } catch ( \InvalidArgumentException $ex ) {
94  $this->assertSame( 'Invalid metadata', $ex->getMessage(), 'bad metadata' );
95  }
96 
97  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
98  'provider' => $provider,
99  'userInfo' => $anonInfo
100  ] );
101  $this->assertSame( $provider, $info->getProvider() );
102  $this->assertNotNull( $info->getId() );
103  $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
104  $this->assertSame( $anonInfo, $info->getUserInfo() );
105  $this->assertTrue( $info->isIdSafe() );
106  $this->assertFalse( $info->forceUse() );
107  $this->assertFalse( $info->wasPersisted() );
108  $this->assertFalse( $info->wasRemembered() );
109  $this->assertFalse( $info->forceHTTPS() );
110  $this->assertNull( $info->getProviderMetadata() );
111 
112  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
113  'provider' => $provider,
114  'userInfo' => $unverifiedUserInfo,
115  'metadata' => [ 'Foo' ],
116  ] );
117  $this->assertSame( $provider, $info->getProvider() );
118  $this->assertNotNull( $info->getId() );
119  $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
120  $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
121  $this->assertTrue( $info->isIdSafe() );
122  $this->assertFalse( $info->forceUse() );
123  $this->assertFalse( $info->wasPersisted() );
124  $this->assertFalse( $info->wasRemembered() );
125  $this->assertFalse( $info->forceHTTPS() );
126  $this->assertSame( [ 'Foo' ], $info->getProviderMetadata() );
127 
128  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
129  'provider' => $provider,
130  'userInfo' => $userInfo
131  ] );
132  $this->assertSame( $provider, $info->getProvider() );
133  $this->assertNotNull( $info->getId() );
134  $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
135  $this->assertSame( $userInfo, $info->getUserInfo() );
136  $this->assertTrue( $info->isIdSafe() );
137  $this->assertFalse( $info->forceUse() );
138  $this->assertFalse( $info->wasPersisted() );
139  $this->assertTrue( $info->wasRemembered() );
140  $this->assertFalse( $info->forceHTTPS() );
141  $this->assertNull( $info->getProviderMetadata() );
142 
143  $id = $manager->generateSessionId();
144 
145  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
146  'provider' => $provider,
147  'id' => $id,
148  'persisted' => true,
149  'userInfo' => $anonInfo
150  ] );
151  $this->assertSame( $provider, $info->getProvider() );
152  $this->assertSame( $id, $info->getId() );
153  $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
154  $this->assertSame( $anonInfo, $info->getUserInfo() );
155  $this->assertFalse( $info->isIdSafe() );
156  $this->assertFalse( $info->forceUse() );
157  $this->assertTrue( $info->wasPersisted() );
158  $this->assertFalse( $info->wasRemembered() );
159  $this->assertFalse( $info->forceHTTPS() );
160  $this->assertNull( $info->getProviderMetadata() );
161 
162  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
163  'provider' => $provider,
164  'id' => $id,
165  'userInfo' => $userInfo
166  ] );
167  $this->assertSame( $provider, $info->getProvider() );
168  $this->assertSame( $id, $info->getId() );
169  $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
170  $this->assertSame( $userInfo, $info->getUserInfo() );
171  $this->assertFalse( $info->isIdSafe() );
172  $this->assertFalse( $info->forceUse() );
173  $this->assertFalse( $info->wasPersisted() );
174  $this->assertTrue( $info->wasRemembered() );
175  $this->assertFalse( $info->forceHTTPS() );
176  $this->assertNull( $info->getProviderMetadata() );
177 
178  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
179  'id' => $id,
180  'persisted' => true,
181  'userInfo' => $userInfo,
182  'metadata' => [ 'Foo' ],
183  ] );
184  $this->assertSame( $id, $info->getId() );
185  $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
186  $this->assertSame( $userInfo, $info->getUserInfo() );
187  $this->assertFalse( $info->isIdSafe() );
188  $this->assertFalse( $info->forceUse() );
189  $this->assertTrue( $info->wasPersisted() );
190  $this->assertFalse( $info->wasRemembered() );
191  $this->assertFalse( $info->forceHTTPS() );
192  $this->assertNull( $info->getProviderMetadata() );
193 
194  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
195  'id' => $id,
196  'remembered' => true,
197  'userInfo' => $userInfo,
198  ] );
199  $this->assertFalse( $info->wasRemembered(), 'no provider' );
200 
201  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
202  'provider' => $provider,
203  'id' => $id,
204  'remembered' => true,
205  ] );
206  $this->assertFalse( $info->wasRemembered(), 'no user' );
207 
208  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
209  'provider' => $provider,
210  'id' => $id,
211  'remembered' => true,
212  'userInfo' => $anonInfo,
213  ] );
214  $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
215 
216  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
217  'provider' => $provider,
218  'id' => $id,
219  'remembered' => true,
220  'userInfo' => $unverifiedUserInfo,
221  ] );
222  $this->assertFalse( $info->wasRemembered(), 'unverified user' );
223 
224  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
225  'provider' => $provider,
226  'id' => $id,
227  'remembered' => false,
228  'userInfo' => $userInfo,
229  ] );
230  $this->assertFalse( $info->wasRemembered(), 'specific override' );
231 
232  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
233  'id' => $id,
234  'idIsSafe' => true,
235  ] );
236  $this->assertSame( $id, $info->getId() );
237  $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
238  $this->assertTrue( $info->isIdSafe() );
239 
240  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
241  'id' => $id,
242  'forceUse' => true,
243  ] );
244  $this->assertFalse( $info->forceUse(), 'no provider' );
245 
246  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
247  'provider' => $provider,
248  'forceUse' => true,
249  ] );
250  $this->assertFalse( $info->forceUse(), 'no id' );
251 
252  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
253  'provider' => $provider,
254  'id' => $id,
255  'forceUse' => true,
256  ] );
257  $this->assertTrue( $info->forceUse(), 'correct use' );
258 
259  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
260  'id' => $id,
261  'forceHTTPS' => 1,
262  ] );
263  $this->assertTrue( $info->forceHTTPS() );
264 
265  $fromInfo = new SessionInfo( SessionInfo::MIN_PRIORITY, [
266  'id' => $id . 'A',
267  'provider' => $provider,
268  'userInfo' => $userInfo,
269  'idIsSafe' => true,
270  'forceUse' => true,
271  'persisted' => true,
272  'remembered' => true,
273  'forceHTTPS' => true,
274  'metadata' => [ 'foo!' ],
275  ] );
276  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 4, [
277  'copyFrom' => $fromInfo,
278  ] );
279  $this->assertSame( $id . 'A', $info->getId() );
280  $this->assertSame( SessionInfo::MIN_PRIORITY + 4, $info->getPriority() );
281  $this->assertSame( $provider, $info->getProvider() );
282  $this->assertSame( $userInfo, $info->getUserInfo() );
283  $this->assertTrue( $info->isIdSafe() );
284  $this->assertTrue( $info->forceUse() );
285  $this->assertTrue( $info->wasPersisted() );
286  $this->assertTrue( $info->wasRemembered() );
287  $this->assertTrue( $info->forceHTTPS() );
288  $this->assertSame( [ 'foo!' ], $info->getProviderMetadata() );
289 
290  $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 4, [
291  'id' => $id . 'X',
292  'provider' => $provider2,
293  'userInfo' => $unverifiedUserInfo,
294  'idIsSafe' => false,
295  'forceUse' => false,
296  'persisted' => false,
297  'remembered' => false,
298  'forceHTTPS' => false,
299  'metadata' => null,
300  'copyFrom' => $fromInfo,
301  ] );
302  $this->assertSame( $id . 'X', $info->getId() );
303  $this->assertSame( SessionInfo::MIN_PRIORITY + 4, $info->getPriority() );
304  $this->assertSame( $provider2, $info->getProvider() );
305  $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
306  $this->assertFalse( $info->isIdSafe() );
307  $this->assertFalse( $info->forceUse() );
308  $this->assertFalse( $info->wasPersisted() );
309  $this->assertFalse( $info->wasRemembered() );
310  $this->assertFalse( $info->forceHTTPS() );
311  $this->assertNull( $info->getProviderMetadata() );
312 
313  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
314  'id' => $id,
315  ] );
316  $this->assertSame(
317  '[' . SessionInfo::MIN_PRIORITY . "]null<null>$id",
318  (string)$info,
319  'toString'
320  );
321 
322  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
323  'provider' => $provider,
324  'id' => $id,
325  'persisted' => true,
326  'userInfo' => $userInfo
327  ] );
328  $this->assertSame(
329  '[' . SessionInfo::MIN_PRIORITY . "]Mock<+:{$userInfo->getId()}:UTSysop>$id",
330  (string)$info,
331  'toString'
332  );
333 
334  $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
335  'provider' => $provider,
336  'id' => $id,
337  'persisted' => true,
338  'userInfo' => $unverifiedUserInfo
339  ] );
340  $this->assertSame(
341  '[' . SessionInfo::MIN_PRIORITY . "]Mock<-:{$userInfo->getId()}:UTSysop>$id",
342  (string)$info,
343  'toString'
344  );
345  }
346 
347  public function testCompare() {
348  $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
349  $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, [ 'id' => $id ] );
350  $info2 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, [ 'id' => $id ] );
351 
352  $this->assertTrue( SessionInfo::compare( $info1, $info2 ) < 0, '<' );
353  $this->assertTrue( SessionInfo::compare( $info2, $info1 ) > 0, '>' );
354  $this->assertTrue( SessionInfo::compare( $info1, $info1 ) === 0, '==' );
355  }
356 }
MediaWiki\Session\UserInfo\newAnonymous
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition: UserInfo.php:75
MediaWiki\Session\SessionInfo\compare
static compare( $a, $b)
Compare two SessionInfo objects by priority.
Definition: SessionInfo.php:293
MediaWiki\Session\SessionInfoTest\testBasics
testBasics()
Definition: SessionInfoTest.php:14
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
MediaWiki\Session\SessionInfoTest\testCompare
testCompare()
Definition: SessionInfoTest.php:347
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
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
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\SessionInfo\MAX_PRIORITY
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:39
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
MediaWiki\Session\UserInfo\newFromName
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition: UserInfo.php:103
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:50
MediaWiki\Session\SessionInfoTest
Session Database MediaWiki\Session\SessionInfo.
Definition: SessionInfoTest.php:12
MediaWiki\Session\SessionInfo
Value object returned by SessionProvider.
Definition: SessionInfo.php:34
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWiki\Session\SessionInfo\MIN_PRIORITY
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36