Merge "mw.Upload.BookletLayout: Don't explode when the API call fails with 'exception'"
[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 ( \UnexpectedValueException $ex ) {
93 $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
94 }
95
96 $res = $provider->mergeMetadata(
97 array( 'foo' => 1, 'baz' => 3 ),
98 array( 'bar' => 2, 'baz' => 3 )
99 );
100 $this->assertSame( array( 'bar' => 2, 'baz' => 3 ), $res );
101 }
102
103 public static function provideNewSessionInfo() {
104 return array(
105 array( false, false, false ),
106 array( true, false, false ),
107 array( false, true, false ),
108 array( true, true, true ),
109 );
110 }
111
112 public function testImmutableSessions() {
113 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
114 ->setMethods( array( 'canChangeUser', 'persistsSessionId' ) )
115 ->getMockForAbstractClass();
116 $provider->expects( $this->any() )->method( 'canChangeUser' )
117 ->will( $this->returnValue( true ) );
118 $provider->preventSessionsForUser( 'Foo' );
119
120 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
121 ->setMethods( array( 'canChangeUser', 'persistsSessionId' ) )
122 ->getMockForAbstractClass();
123 $provider->expects( $this->any() )->method( 'canChangeUser' )
124 ->will( $this->returnValue( false ) );
125 try {
126 $provider->preventSessionsForUser( 'Foo' );
127 $this->fail( 'Expected exception not thrown' );
128 } catch ( \BadMethodCallException $ex ) {
129 }
130
131 }
132
133 public function testHashToSessionId() {
134 $config = new \HashConfig( array(
135 'SecretKey' => 'Shhh!',
136 ) );
137
138 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
139 array(), 'MockSessionProvider' );
140 $provider->setConfig( $config );
141 $priv = \TestingAccessWrapper::newFromObject( $provider );
142
143 $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
144 $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
145 $priv->hashToSessionId( 'foobar', 'secret' ) );
146
147 try {
148 $priv->hashToSessionId( array() );
149 $this->fail( 'Expected exception not thrown' );
150 } catch ( \InvalidArgumentException $ex ) {
151 $this->assertSame(
152 '$data must be a string, array was passed',
153 $ex->getMessage()
154 );
155 }
156 try {
157 $priv->hashToSessionId( '', false );
158 $this->fail( 'Expected exception not thrown' );
159 } catch ( \InvalidArgumentException $ex ) {
160 $this->assertSame(
161 '$key must be a string or null, boolean was passed',
162 $ex->getMessage()
163 );
164 }
165 }
166
167 public function testDescribe() {
168 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
169 array(), 'MockSessionProvider' );
170
171 $this->assertSame(
172 'MockSessionProvider sessions',
173 $provider->describe( \Language::factory( 'en' ) )
174 );
175 }
176
177 public function testGetAllowedUserRights() {
178 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider' );
179 $backend = TestUtils::getDummySessionBackend();
180
181 try {
182 $provider->getAllowedUserRights( $backend );
183 $this->fail( 'Expected exception not thrown' );
184 } catch ( \InvalidArgumentException $ex ) {
185 $this->assertSame(
186 'Backend\'s provider isn\'t $this',
187 $ex->getMessage()
188 );
189 }
190
191 \TestingAccessWrapper::newFromObject( $backend )->provider = $provider;
192 $this->assertNull( $provider->getAllowedUserRights( $backend ) );
193 }
194
195 }