Merge "jquery.ui.datepicker: Add translations in de-AT and de-CH"
[lhc/web/wiklou.git] / tests / phpunit / includes / session / SessionProviderTest.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use MediaWikiTestCase;
6
7 /**
8 * @group Session
9 * @group Database
10 * @covers MediaWiki\Session\SessionProvider
11 */
12 class SessionProviderTest extends MediaWikiTestCase {
13
14 public function testBasics() {
15 $manager = new SessionManager();
16 $logger = new \TestLogger();
17 $config = new \HashConfig();
18
19 $provider = $this->getMockForAbstractClass( SessionProvider::class );
20 $priv = \TestingAccessWrapper::newFromObject( $provider );
21
22 $provider->setConfig( $config );
23 $this->assertSame( $config, $priv->config );
24 $provider->setLogger( $logger );
25 $this->assertSame( $logger, $priv->logger );
26 $provider->setManager( $manager );
27 $this->assertSame( $manager, $priv->manager );
28 $this->assertSame( $manager, $provider->getManager() );
29
30 $this->assertSame( [], $provider->getVaryHeaders() );
31 $this->assertSame( [], $provider->getVaryCookies() );
32 $this->assertSame( null, $provider->suggestLoginUsername( new \FauxRequest ) );
33
34 $this->assertSame( get_class( $provider ), (string)$provider );
35
36 $this->assertNull( $provider->whyNoSession() );
37
38 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
39 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
40 'provider' => $provider,
41 ] );
42 $metadata = [ 'foo' ];
43 $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest, $metadata ) );
44 $this->assertSame( [ 'foo' ], $metadata );
45 }
46
47 /**
48 * @dataProvider provideNewSessionInfo
49 * @param bool $persistId Return value for ->persistsSessionId()
50 * @param bool $persistUser Return value for ->persistsSessionUser()
51 * @param bool $ok Whether a SessionInfo is provided
52 */
53 public function testNewSessionInfo( $persistId, $persistUser, $ok ) {
54 $manager = new SessionManager();
55
56 $provider = $this->getMockBuilder( SessionProvider::class )
57 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
58 ->getMockForAbstractClass();
59 $provider->expects( $this->any() )->method( 'persistsSessionId' )
60 ->will( $this->returnValue( $persistId ) );
61 $provider->expects( $this->any() )->method( 'canChangeUser' )
62 ->will( $this->returnValue( $persistUser ) );
63 $provider->setManager( $manager );
64
65 if ( $ok ) {
66 $info = $provider->newSessionInfo();
67 $this->assertNotNull( $info );
68 $this->assertFalse( $info->wasPersisted() );
69 $this->assertTrue( $info->isIdSafe() );
70
71 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
72 $info = $provider->newSessionInfo( $id );
73 $this->assertNotNull( $info );
74 $this->assertSame( $id, $info->getId() );
75 $this->assertFalse( $info->wasPersisted() );
76 $this->assertTrue( $info->isIdSafe() );
77 } else {
78 $this->assertNull( $provider->newSessionInfo() );
79 }
80 }
81
82 public function testMergeMetadata() {
83 $provider = $this->getMockBuilder( SessionProvider::class )
84 ->getMockForAbstractClass();
85
86 try {
87 $provider->mergeMetadata(
88 [ 'foo' => 1, 'baz' => 3 ],
89 [ 'bar' => 2, 'baz' => '3' ]
90 );
91 $this->fail( 'Expected exception not thrown' );
92 } catch ( MetadataMergeException $ex ) {
93 $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
94 $this->assertSame(
95 [ 'old_value' => 3, 'new_value' => '3' ], $ex->getContext() );
96 }
97
98 $res = $provider->mergeMetadata(
99 [ 'foo' => 1, 'baz' => 3 ],
100 [ 'bar' => 2, 'baz' => 3 ]
101 );
102 $this->assertSame( [ 'bar' => 2, 'baz' => 3 ], $res );
103 }
104
105 public static function provideNewSessionInfo() {
106 return [
107 [ false, false, false ],
108 [ true, false, false ],
109 [ false, true, false ],
110 [ true, true, true ],
111 ];
112 }
113
114 public function testImmutableSessions() {
115 $provider = $this->getMockBuilder( SessionProvider::class )
116 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
117 ->getMockForAbstractClass();
118 $provider->expects( $this->any() )->method( 'canChangeUser' )
119 ->will( $this->returnValue( true ) );
120 $provider->preventSessionsForUser( 'Foo' );
121
122 $provider = $this->getMockBuilder( SessionProvider::class )
123 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
124 ->getMockForAbstractClass();
125 $provider->expects( $this->any() )->method( 'canChangeUser' )
126 ->will( $this->returnValue( false ) );
127 try {
128 $provider->preventSessionsForUser( 'Foo' );
129 $this->fail( 'Expected exception not thrown' );
130 } catch ( \BadMethodCallException $ex ) {
131 $this->assertSame(
132 'MediaWiki\\Session\\SessionProvider::preventSessionsForUser must be implmented ' .
133 'when canChangeUser() is false',
134 $ex->getMessage()
135 );
136 }
137
138 }
139
140 public function testHashToSessionId() {
141 $config = new \HashConfig( [
142 'SecretKey' => 'Shhh!',
143 ] );
144
145 $provider = $this->getMockForAbstractClass( SessionProvider::class,
146 [], 'MockSessionProvider' );
147 $provider->setConfig( $config );
148 $priv = \TestingAccessWrapper::newFromObject( $provider );
149
150 $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
151 $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
152 $priv->hashToSessionId( 'foobar', 'secret' ) );
153
154 try {
155 $priv->hashToSessionId( [] );
156 $this->fail( 'Expected exception not thrown' );
157 } catch ( \InvalidArgumentException $ex ) {
158 $this->assertSame(
159 '$data must be a string, array was passed',
160 $ex->getMessage()
161 );
162 }
163 try {
164 $priv->hashToSessionId( '', false );
165 $this->fail( 'Expected exception not thrown' );
166 } catch ( \InvalidArgumentException $ex ) {
167 $this->assertSame(
168 '$key must be a string or null, boolean was passed',
169 $ex->getMessage()
170 );
171 }
172 }
173
174 public function testDescribe() {
175 $provider = $this->getMockForAbstractClass( SessionProvider::class,
176 [], 'MockSessionProvider' );
177
178 $this->assertSame(
179 'MockSessionProvider sessions',
180 $provider->describe( \Language::factory( 'en' ) )
181 );
182 }
183
184 public function testGetAllowedUserRights() {
185 $provider = $this->getMockForAbstractClass( SessionProvider::class );
186 $backend = TestUtils::getDummySessionBackend();
187
188 try {
189 $provider->getAllowedUserRights( $backend );
190 $this->fail( 'Expected exception not thrown' );
191 } catch ( \InvalidArgumentException $ex ) {
192 $this->assertSame(
193 'Backend\'s provider isn\'t $this',
194 $ex->getMessage()
195 );
196 }
197
198 \TestingAccessWrapper::newFromObject( $backend )->provider = $provider;
199 $this->assertNull( $provider->getAllowedUserRights( $backend ) );
200 }
201
202 }