TableDiffFormatter: Don't repeatedly call array_shift()
[lhc/web/wiklou.git] / tests / phpunit / includes / session / SessionInfoTest.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use MediaWikiTestCase;
6
7 /**
8 * @group Session
9 * @group Database
10 * @covers MediaWiki\Session\SessionInfo
11 */
12 class SessionInfoTest extends MediaWikiTestCase {
13
14 public function testBasics() {
15 $anonInfo = UserInfo::newAnonymous();
16 $userInfo = UserInfo::newFromName( 'UTSysop', true );
17 $unverifiedUserInfo = UserInfo::newFromName( 'UTSysop', false );
18
19 try {
20 new SessionInfo( SessionInfo::MIN_PRIORITY - 1, [] );
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 {
27 new SessionInfo( SessionInfo::MAX_PRIORITY + 1, [] );
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 {
48 new SessionInfo( SessionInfo::MIN_PRIORITY, [] );
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( 'MediaWiki\\Session\\SessionProvider' )
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( 'MediaWiki\\Session\\SessionProvider' )
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 {
87 new SessionInfo( SessionInfo::MIN_PRIORITY, [
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->wasPersisted() );
107 $this->assertFalse( $info->wasRemembered() );
108 $this->assertFalse( $info->forceHTTPS() );
109 $this->assertNull( $info->getProviderMetadata() );
110
111 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
112 'provider' => $provider,
113 'userInfo' => $unverifiedUserInfo,
114 'metadata' => [ 'Foo' ],
115 ] );
116 $this->assertSame( $provider, $info->getProvider() );
117 $this->assertNotNull( $info->getId() );
118 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
119 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
120 $this->assertTrue( $info->isIdSafe() );
121 $this->assertFalse( $info->wasPersisted() );
122 $this->assertFalse( $info->wasRemembered() );
123 $this->assertFalse( $info->forceHTTPS() );
124 $this->assertSame( [ 'Foo' ], $info->getProviderMetadata() );
125
126 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
127 'provider' => $provider,
128 'userInfo' => $userInfo
129 ] );
130 $this->assertSame( $provider, $info->getProvider() );
131 $this->assertNotNull( $info->getId() );
132 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
133 $this->assertSame( $userInfo, $info->getUserInfo() );
134 $this->assertTrue( $info->isIdSafe() );
135 $this->assertFalse( $info->wasPersisted() );
136 $this->assertTrue( $info->wasRemembered() );
137 $this->assertFalse( $info->forceHTTPS() );
138 $this->assertNull( $info->getProviderMetadata() );
139
140 $id = $manager->generateSessionId();
141
142 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
143 'provider' => $provider,
144 'id' => $id,
145 'persisted' => true,
146 'userInfo' => $anonInfo
147 ] );
148 $this->assertSame( $provider, $info->getProvider() );
149 $this->assertSame( $id, $info->getId() );
150 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
151 $this->assertSame( $anonInfo, $info->getUserInfo() );
152 $this->assertFalse( $info->isIdSafe() );
153 $this->assertTrue( $info->wasPersisted() );
154 $this->assertFalse( $info->wasRemembered() );
155 $this->assertFalse( $info->forceHTTPS() );
156 $this->assertNull( $info->getProviderMetadata() );
157
158 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
159 'provider' => $provider,
160 'id' => $id,
161 'userInfo' => $userInfo
162 ] );
163 $this->assertSame( $provider, $info->getProvider() );
164 $this->assertSame( $id, $info->getId() );
165 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
166 $this->assertSame( $userInfo, $info->getUserInfo() );
167 $this->assertFalse( $info->isIdSafe() );
168 $this->assertFalse( $info->wasPersisted() );
169 $this->assertTrue( $info->wasRemembered() );
170 $this->assertFalse( $info->forceHTTPS() );
171 $this->assertNull( $info->getProviderMetadata() );
172
173 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
174 'id' => $id,
175 'persisted' => true,
176 'userInfo' => $userInfo,
177 'metadata' => [ 'Foo' ],
178 ] );
179 $this->assertSame( $id, $info->getId() );
180 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
181 $this->assertSame( $userInfo, $info->getUserInfo() );
182 $this->assertFalse( $info->isIdSafe() );
183 $this->assertTrue( $info->wasPersisted() );
184 $this->assertFalse( $info->wasRemembered() );
185 $this->assertFalse( $info->forceHTTPS() );
186 $this->assertNull( $info->getProviderMetadata() );
187
188 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
189 'id' => $id,
190 'remembered' => true,
191 'userInfo' => $userInfo,
192 ] );
193 $this->assertFalse( $info->wasRemembered(), 'no provider' );
194
195 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
196 'provider' => $provider,
197 'id' => $id,
198 'remembered' => true,
199 ] );
200 $this->assertFalse( $info->wasRemembered(), 'no user' );
201
202 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
203 'provider' => $provider,
204 'id' => $id,
205 'remembered' => true,
206 'userInfo' => $anonInfo,
207 ] );
208 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
209
210 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
211 'provider' => $provider,
212 'id' => $id,
213 'remembered' => true,
214 'userInfo' => $unverifiedUserInfo,
215 ] );
216 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
217
218 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
219 'provider' => $provider,
220 'id' => $id,
221 'remembered' => false,
222 'userInfo' => $userInfo,
223 ] );
224 $this->assertFalse( $info->wasRemembered(), 'specific override' );
225
226 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
227 'id' => $id,
228 'idIsSafe' => true,
229 ] );
230 $this->assertSame( $id, $info->getId() );
231 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
232 $this->assertTrue( $info->isIdSafe() );
233
234 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
235 'id' => $id,
236 'forceHTTPS' => 1,
237 ] );
238 $this->assertTrue( $info->forceHTTPS() );
239
240 $fromInfo = new SessionInfo( SessionInfo::MIN_PRIORITY, [
241 'id' => $id . 'A',
242 'provider' => $provider,
243 'userInfo' => $userInfo,
244 'idIsSafe' => true,
245 'persisted' => true,
246 'remembered' => true,
247 'forceHTTPS' => true,
248 'metadata' => [ 'foo!' ],
249 ] );
250 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 4, [
251 'copyFrom' => $fromInfo,
252 ] );
253 $this->assertSame( $id . 'A', $info->getId() );
254 $this->assertSame( SessionInfo::MIN_PRIORITY + 4, $info->getPriority() );
255 $this->assertSame( $provider, $info->getProvider() );
256 $this->assertSame( $userInfo, $info->getUserInfo() );
257 $this->assertTrue( $info->isIdSafe() );
258 $this->assertTrue( $info->wasPersisted() );
259 $this->assertTrue( $info->wasRemembered() );
260 $this->assertTrue( $info->forceHTTPS() );
261 $this->assertSame( [ 'foo!' ], $info->getProviderMetadata() );
262
263 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 4, [
264 'id' => $id . 'X',
265 'provider' => $provider2,
266 'userInfo' => $unverifiedUserInfo,
267 'idIsSafe' => false,
268 'persisted' => false,
269 'remembered' => false,
270 'forceHTTPS' => false,
271 'metadata' => null,
272 'copyFrom' => $fromInfo,
273 ] );
274 $this->assertSame( $id . 'X', $info->getId() );
275 $this->assertSame( SessionInfo::MIN_PRIORITY + 4, $info->getPriority() );
276 $this->assertSame( $provider2, $info->getProvider() );
277 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
278 $this->assertFalse( $info->isIdSafe() );
279 $this->assertFalse( $info->wasPersisted() );
280 $this->assertFalse( $info->wasRemembered() );
281 $this->assertFalse( $info->forceHTTPS() );
282 $this->assertNull( $info->getProviderMetadata() );
283
284 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
285 'id' => $id,
286 ] );
287 $this->assertSame(
288 '[' . SessionInfo::MIN_PRIORITY . "]null<null>$id",
289 (string)$info,
290 'toString'
291 );
292
293 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
294 'provider' => $provider,
295 'id' => $id,
296 'persisted' => true,
297 'userInfo' => $userInfo
298 ] );
299 $this->assertSame(
300 '[' . SessionInfo::MIN_PRIORITY . "]Mock<+:{$userInfo->getId()}:UTSysop>$id",
301 (string)$info,
302 'toString'
303 );
304
305 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
306 'provider' => $provider,
307 'id' => $id,
308 'persisted' => true,
309 'userInfo' => $unverifiedUserInfo
310 ] );
311 $this->assertSame(
312 '[' . SessionInfo::MIN_PRIORITY . "]Mock<-:{$userInfo->getId()}:UTSysop>$id",
313 (string)$info,
314 'toString'
315 );
316 }
317
318 public function testCompare() {
319 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
320 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, [ 'id' => $id ] );
321 $info2 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, [ 'id' => $id ] );
322
323 $this->assertTrue( SessionInfo::compare( $info1, $info2 ) < 0, '<' );
324 $this->assertTrue( SessionInfo::compare( $info2, $info1 ) > 0, '>' );
325 $this->assertTrue( SessionInfo::compare( $info1, $info1 ) === 0, '==' );
326 }
327 }