Merge "[debug] Remove BC code from AvroFormatter"
[lhc/web/wiklou.git] / tests / phpunit / includes / session / SessionInfoTest.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use Psr\Log\LogLevel;
6 use MediaWikiTestCase;
7
8 /**
9 * @group Session
10 * @group Database
11 * @covers MediaWiki\Session\SessionInfo
12 */
13 class SessionInfoTest extends MediaWikiTestCase {
14
15 public function testBasics() {
16 $anonInfo = UserInfo::newAnonymous();
17 $userInfo = UserInfo::newFromName( 'UTSysop', true );
18 $unverifiedUserInfo = UserInfo::newFromName( 'UTSysop', false );
19
20 try {
21 new SessionInfo( SessionInfo::MIN_PRIORITY - 1, array() );
22 $this->fail( 'Expected exception not thrown', 'priority < min' );
23 } catch ( \InvalidArgumentException $ex ) {
24 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority < min' );
25 }
26
27 try {
28 new SessionInfo( SessionInfo::MAX_PRIORITY + 1, array() );
29 $this->fail( 'Expected exception not thrown', 'priority > max' );
30 } catch ( \InvalidArgumentException $ex ) {
31 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority > max' );
32 }
33
34 try {
35 new SessionInfo( SessionInfo::MIN_PRIORITY, array( 'id' => 'ABC?' ) );
36 $this->fail( 'Expected exception not thrown', 'bad session ID' );
37 } catch ( \InvalidArgumentException $ex ) {
38 $this->assertSame( 'Invalid session ID', $ex->getMessage(), 'bad session ID' );
39 }
40
41 try {
42 new SessionInfo( SessionInfo::MIN_PRIORITY, array( 'userInfo' => new \stdClass ) );
43 $this->fail( 'Expected exception not thrown', 'bad userInfo' );
44 } catch ( \InvalidArgumentException $ex ) {
45 $this->assertSame( 'Invalid userInfo', $ex->getMessage(), 'bad userInfo' );
46 }
47
48 try {
49 new SessionInfo( SessionInfo::MIN_PRIORITY, array() );
50 $this->fail( 'Expected exception not thrown', 'no provider, no id' );
51 } catch ( \InvalidArgumentException $ex ) {
52 $this->assertSame( 'Must supply an ID when no provider is given', $ex->getMessage(),
53 'no provider, no id' );
54 }
55
56 try {
57 new SessionInfo( SessionInfo::MIN_PRIORITY, array( 'copyFrom' => new \stdClass ) );
58 $this->fail( 'Expected exception not thrown', 'bad copyFrom' );
59 } catch ( \InvalidArgumentException $ex ) {
60 $this->assertSame( 'Invalid copyFrom', $ex->getMessage(),
61 'bad copyFrom' );
62 }
63
64 $manager = new SessionManager();
65 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
66 ->setMethods( array( 'persistsSessionId', 'canChangeUser', '__toString' ) )
67 ->getMockForAbstractClass();
68 $provider->setManager( $manager );
69 $provider->expects( $this->any() )->method( 'persistsSessionId' )
70 ->will( $this->returnValue( true ) );
71 $provider->expects( $this->any() )->method( 'canChangeUser' )
72 ->will( $this->returnValue( true ) );
73 $provider->expects( $this->any() )->method( '__toString' )
74 ->will( $this->returnValue( 'Mock' ) );
75
76 $provider2 = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
77 ->setMethods( array( 'persistsSessionId', 'canChangeUser', '__toString' ) )
78 ->getMockForAbstractClass();
79 $provider2->setManager( $manager );
80 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
81 ->will( $this->returnValue( true ) );
82 $provider2->expects( $this->any() )->method( 'canChangeUser' )
83 ->will( $this->returnValue( true ) );
84 $provider2->expects( $this->any() )->method( '__toString' )
85 ->will( $this->returnValue( 'Mock2' ) );
86
87 try {
88 new SessionInfo( SessionInfo::MIN_PRIORITY, array(
89 'provider' => $provider,
90 'userInfo' => $anonInfo,
91 'metadata' => 'foo',
92 ) );
93 $this->fail( 'Expected exception not thrown', 'bad metadata' );
94 } catch ( \InvalidArgumentException $ex ) {
95 $this->assertSame( 'Invalid metadata', $ex->getMessage(), 'bad metadata' );
96 }
97
98 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
99 'provider' => $provider,
100 'userInfo' => $anonInfo
101 ) );
102 $this->assertSame( $provider, $info->getProvider() );
103 $this->assertNotNull( $info->getId() );
104 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
105 $this->assertSame( $anonInfo, $info->getUserInfo() );
106 $this->assertTrue( $info->isIdSafe() );
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, array(
113 'provider' => $provider,
114 'userInfo' => $unverifiedUserInfo,
115 'metadata' => array( '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->wasPersisted() );
123 $this->assertFalse( $info->wasRemembered() );
124 $this->assertFalse( $info->forceHTTPS() );
125 $this->assertSame( array( 'Foo' ), $info->getProviderMetadata() );
126
127 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
128 'provider' => $provider,
129 'userInfo' => $userInfo
130 ) );
131 $this->assertSame( $provider, $info->getProvider() );
132 $this->assertNotNull( $info->getId() );
133 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
134 $this->assertSame( $userInfo, $info->getUserInfo() );
135 $this->assertTrue( $info->isIdSafe() );
136 $this->assertFalse( $info->wasPersisted() );
137 $this->assertTrue( $info->wasRemembered() );
138 $this->assertFalse( $info->forceHTTPS() );
139 $this->assertNull( $info->getProviderMetadata() );
140
141 $id = $manager->generateSessionId();
142
143 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
144 'provider' => $provider,
145 'id' => $id,
146 'persisted' => true,
147 'userInfo' => $anonInfo
148 ) );
149 $this->assertSame( $provider, $info->getProvider() );
150 $this->assertSame( $id, $info->getId() );
151 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
152 $this->assertSame( $anonInfo, $info->getUserInfo() );
153 $this->assertFalse( $info->isIdSafe() );
154 $this->assertTrue( $info->wasPersisted() );
155 $this->assertFalse( $info->wasRemembered() );
156 $this->assertFalse( $info->forceHTTPS() );
157 $this->assertNull( $info->getProviderMetadata() );
158
159 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
160 'provider' => $provider,
161 'id' => $id,
162 'userInfo' => $userInfo
163 ) );
164 $this->assertSame( $provider, $info->getProvider() );
165 $this->assertSame( $id, $info->getId() );
166 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
167 $this->assertSame( $userInfo, $info->getUserInfo() );
168 $this->assertFalse( $info->isIdSafe() );
169 $this->assertFalse( $info->wasPersisted() );
170 $this->assertTrue( $info->wasRemembered() );
171 $this->assertFalse( $info->forceHTTPS() );
172 $this->assertNull( $info->getProviderMetadata() );
173
174 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
175 'id' => $id,
176 'persisted' => true,
177 'userInfo' => $userInfo,
178 'metadata' => array( 'Foo' ),
179 ) );
180 $this->assertSame( $id, $info->getId() );
181 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
182 $this->assertSame( $userInfo, $info->getUserInfo() );
183 $this->assertFalse( $info->isIdSafe() );
184 $this->assertTrue( $info->wasPersisted() );
185 $this->assertFalse( $info->wasRemembered() );
186 $this->assertFalse( $info->forceHTTPS() );
187 $this->assertNull( $info->getProviderMetadata() );
188
189 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
190 'id' => $id,
191 'remembered' => true,
192 'userInfo' => $userInfo,
193 ) );
194 $this->assertFalse( $info->wasRemembered(), 'no provider' );
195
196 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
197 'provider' => $provider,
198 'id' => $id,
199 'remembered' => true,
200 ) );
201 $this->assertFalse( $info->wasRemembered(), 'no user' );
202
203 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
204 'provider' => $provider,
205 'id' => $id,
206 'remembered' => true,
207 'userInfo' => $anonInfo,
208 ) );
209 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
210
211 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
212 'provider' => $provider,
213 'id' => $id,
214 'remembered' => true,
215 'userInfo' => $unverifiedUserInfo,
216 ) );
217 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
218
219 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
220 'provider' => $provider,
221 'id' => $id,
222 'remembered' => false,
223 'userInfo' => $userInfo,
224 ) );
225 $this->assertFalse( $info->wasRemembered(), 'specific override' );
226
227 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, array(
228 'id' => $id,
229 'idIsSafe' => true,
230 ) );
231 $this->assertSame( $id, $info->getId() );
232 $this->assertSame( SessionInfo::MIN_PRIORITY + 5, $info->getPriority() );
233 $this->assertTrue( $info->isIdSafe() );
234
235 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
236 'id' => $id,
237 'forceHTTPS' => 1,
238 ) );
239 $this->assertTrue( $info->forceHTTPS() );
240
241 $fromInfo = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
242 'id' => $id . 'A',
243 'provider' => $provider,
244 'userInfo' => $userInfo,
245 'idIsSafe' => true,
246 'persisted' => true,
247 'remembered' => true,
248 'forceHTTPS' => true,
249 'metadata' => array( 'foo!' ),
250 ) );
251 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 4, array(
252 'copyFrom' => $fromInfo,
253 ) );
254 $this->assertSame( $id . 'A', $info->getId() );
255 $this->assertSame( SessionInfo::MIN_PRIORITY + 4, $info->getPriority() );
256 $this->assertSame( $provider, $info->getProvider() );
257 $this->assertSame( $userInfo, $info->getUserInfo() );
258 $this->assertTrue( $info->isIdSafe() );
259 $this->assertTrue( $info->wasPersisted() );
260 $this->assertTrue( $info->wasRemembered() );
261 $this->assertTrue( $info->forceHTTPS() );
262 $this->assertSame( array( 'foo!' ), $info->getProviderMetadata() );
263
264 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 4, array(
265 'id' => $id . 'X',
266 'provider' => $provider2,
267 'userInfo' => $unverifiedUserInfo,
268 'idIsSafe' => false,
269 'persisted' => false,
270 'remembered' => false,
271 'forceHTTPS' => false,
272 'metadata' => null,
273 'copyFrom' => $fromInfo,
274 ) );
275 $this->assertSame( $id . 'X', $info->getId() );
276 $this->assertSame( SessionInfo::MIN_PRIORITY + 4, $info->getPriority() );
277 $this->assertSame( $provider2, $info->getProvider() );
278 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
279 $this->assertFalse( $info->isIdSafe() );
280 $this->assertFalse( $info->wasPersisted() );
281 $this->assertFalse( $info->wasRemembered() );
282 $this->assertFalse( $info->forceHTTPS() );
283 $this->assertNull( $info->getProviderMetadata() );
284
285 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
286 'id' => $id,
287 ) );
288 $this->assertSame(
289 '[' . SessionInfo::MIN_PRIORITY . "]null<null>$id",
290 (string)$info,
291 'toString'
292 );
293
294 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
295 'provider' => $provider,
296 'id' => $id,
297 'persisted' => true,
298 'userInfo' => $userInfo
299 ) );
300 $this->assertSame(
301 '[' . SessionInfo::MIN_PRIORITY . "]Mock<+:{$userInfo->getId()}:UTSysop>$id",
302 (string)$info,
303 'toString'
304 );
305
306 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
307 'provider' => $provider,
308 'id' => $id,
309 'persisted' => true,
310 'userInfo' => $unverifiedUserInfo
311 ) );
312 $this->assertSame(
313 '[' . SessionInfo::MIN_PRIORITY . "]Mock<-:{$userInfo->getId()}:UTSysop>$id",
314 (string)$info,
315 'toString'
316 );
317 }
318
319 public function testCompare() {
320 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
321 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, array( 'id' => $id ) );
322 $info2 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, array( 'id' => $id ) );
323
324 $this->assertTrue( SessionInfo::compare( $info1, $info2 ) < 0, '<' );
325 $this->assertTrue( SessionInfo::compare( $info2, $info1 ) > 0, '>' );
326 $this->assertTrue( SessionInfo::compare( $info1, $info1 ) === 0, '==' );
327 }
328 }