3 namespace MediaWiki\Session
;
11 * @covers MediaWiki\Session\SessionInfo
13 class SessionInfoTest
extends MediaWikiTestCase
{
15 public function testBasics() {
16 $anonInfo = UserInfo
::newAnonymous();
17 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
18 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
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' );
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' );
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' );
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' );
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' );
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(),
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' ) );
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' ) );
88 new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
89 'provider' => $provider,
90 'userInfo' => $anonInfo,
93 $this->fail( 'Expected exception not thrown', 'bad metadata' );
94 } catch ( \InvalidArgumentException
$ex ) {
95 $this->assertSame( 'Invalid metadata', $ex->getMessage(), 'bad metadata' );
98 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
99 'provider' => $provider,
100 'userInfo' => $anonInfo
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() );
112 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
113 'provider' => $provider,
114 'userInfo' => $unverifiedUserInfo,
115 'metadata' => array( 'Foo' ),
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() );
127 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
128 'provider' => $provider,
129 'userInfo' => $userInfo
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() );
141 $id = $manager->generateSessionId();
143 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
144 'provider' => $provider,
147 'userInfo' => $anonInfo
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() );
159 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
160 'provider' => $provider,
162 'userInfo' => $userInfo
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() );
174 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
177 'userInfo' => $userInfo,
178 'metadata' => array( 'Foo' ),
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() );
189 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
191 'remembered' => true,
192 'userInfo' => $userInfo,
194 $this->assertFalse( $info->wasRemembered(), 'no provider' );
196 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
197 'provider' => $provider,
199 'remembered' => true,
201 $this->assertFalse( $info->wasRemembered(), 'no user' );
203 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
204 'provider' => $provider,
206 'remembered' => true,
207 'userInfo' => $anonInfo,
209 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
211 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
212 'provider' => $provider,
214 'remembered' => true,
215 'userInfo' => $unverifiedUserInfo,
217 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
219 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
220 'provider' => $provider,
222 'remembered' => false,
223 'userInfo' => $userInfo,
225 $this->assertFalse( $info->wasRemembered(), 'specific override' );
227 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, array(
231 $this->assertSame( $id, $info->getId() );
232 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
233 $this->assertTrue( $info->isIdSafe() );
235 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
239 $this->assertTrue( $info->forceHTTPS() );
241 $fromInfo = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
243 'provider' => $provider,
244 'userInfo' => $userInfo,
247 'remembered' => true,
248 'forceHTTPS' => true,
249 'metadata' => array( 'foo!' ),
251 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, array(
252 'copyFrom' => $fromInfo,
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() );
264 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, array(
266 'provider' => $provider2,
267 'userInfo' => $unverifiedUserInfo,
269 'persisted' => false,
270 'remembered' => false,
271 'forceHTTPS' => false,
273 'copyFrom' => $fromInfo,
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() );
285 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
289 '[' . SessionInfo
::MIN_PRIORITY
. "]null<null>$id",
294 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
295 'provider' => $provider,
298 'userInfo' => $userInfo
301 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<+:{$userInfo->getId()}:UTSysop>$id",
306 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
307 'provider' => $provider,
310 'userInfo' => $unverifiedUserInfo
313 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<-:{$userInfo->getId()}:UTSysop>$id",
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 ) );
324 $this->assertTrue( SessionInfo
::compare( $info1, $info2 ) < 0, '<' );
325 $this->assertTrue( SessionInfo
::compare( $info2, $info1 ) > 0, '>' );
326 $this->assertTrue( SessionInfo
::compare( $info1, $info1 ) === 0, '==' );