Merge "SessionProvider::mergeMetadata: Log additional data"
[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( 'MediaWiki\\Session\\SessionProvider' );
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( array(), $provider->getVaryHeaders() );
31 $this->assertSame( array(), $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, array(
39 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
40 'provider' => $provider,
41 ) );
42 $metadata = array( 'foo' );
43 $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest, $metadata ) );
44 $this->assertSame( array( '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( 'MediaWiki\\Session\\SessionProvider' )
57 ->setMethods( array( '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( 'MediaWiki\\Session\\SessionProvider' )
84 ->getMockForAbstractClass();
85
86 try {
87 $provider->mergeMetadata(
88 array( 'foo' => 1, 'baz' => 3 ),
89 array( '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 array( 'foo' => 1, 'baz' => 3 ),
100 array( 'bar' => 2, 'baz' => 3 )
101 );
102 $this->assertSame( array( 'bar' => 2, 'baz' => 3 ), $res );
103 }
104
105 public static function provideNewSessionInfo() {
106 return array(
107 array( false, false, false ),
108 array( true, false, false ),
109 array( false, true, false ),
110 array( true, true, true ),
111 );
112 }
113
114 public function testImmutableSessions() {
115 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
116 ->setMethods( array( '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( 'MediaWiki\\Session\\SessionProvider' )
123 ->setMethods( array( '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 }
132
133 }
134
135 public function testHashToSessionId() {
136 $config = new \HashConfig( array(
137 'SecretKey' => 'Shhh!',
138 ) );
139
140 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
141 array(), 'MockSessionProvider' );
142 $provider->setConfig( $config );
143 $priv = \TestingAccessWrapper::newFromObject( $provider );
144
145 $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
146 $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
147 $priv->hashToSessionId( 'foobar', 'secret' ) );
148
149 try {
150 $priv->hashToSessionId( array() );
151 $this->fail( 'Expected exception not thrown' );
152 } catch ( \InvalidArgumentException $ex ) {
153 $this->assertSame(
154 '$data must be a string, array was passed',
155 $ex->getMessage()
156 );
157 }
158 try {
159 $priv->hashToSessionId( '', false );
160 $this->fail( 'Expected exception not thrown' );
161 } catch ( \InvalidArgumentException $ex ) {
162 $this->assertSame(
163 '$key must be a string or null, boolean was passed',
164 $ex->getMessage()
165 );
166 }
167 }
168
169 public function testDescribe() {
170 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
171 array(), 'MockSessionProvider' );
172
173 $this->assertSame(
174 'MockSessionProvider sessions',
175 $provider->describe( \Language::factory( 'en' ) )
176 );
177 }
178
179 public function testGetAllowedUserRights() {
180 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider' );
181 $backend = TestUtils::getDummySessionBackend();
182
183 try {
184 $provider->getAllowedUserRights( $backend );
185 $this->fail( 'Expected exception not thrown' );
186 } catch ( \InvalidArgumentException $ex ) {
187 $this->assertSame(
188 'Backend\'s provider isn\'t $this',
189 $ex->getMessage()
190 );
191 }
192
193 \TestingAccessWrapper::newFromObject( $backend )->provider = $provider;
194 $this->assertNull( $provider->getAllowedUserRights( $backend ) );
195 }
196
197 }