Merge "Remove unused messages"
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / LegacyHookPreAuthenticationProviderTest.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 use MediaWiki\MediaWikiServices;
6
7 /**
8 * @group AuthManager
9 * @group Database
10 * @covers MediaWiki\Auth\LegacyHookPreAuthenticationProvider
11 */
12 class LegacyHookPreAuthenticationProviderTest extends \MediaWikiTestCase {
13 /**
14 * Get an instance of the provider
15 * @return LegacyHookPreAuthenticationProvider
16 */
17 protected function getProvider() {
18 $request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
19 $request->expects( $this->any() )->method( 'getIP' )->will( $this->returnValue( '127.0.0.42' ) );
20
21 $manager = new AuthManager(
22 $request,
23 MediaWikiServices::getInstance()->getMainConfig()
24 );
25
26 $provider = new LegacyHookPreAuthenticationProvider();
27 $provider->setManager( $manager );
28 $provider->setLogger( new \Psr\Log\NullLogger() );
29 $provider->setConfig( new \HashConfig( [
30 'PasswordAttemptThrottle' => [ 'count' => 23, 'seconds' => 42 ],
31 ] ) );
32 return $provider;
33 }
34
35 /**
36 * Sets a mock on a hook
37 * @param string $hook
38 * @param object $expect From $this->once(), $this->never(), etc.
39 * @return object $mock->expects( $expect )->method( ... ).
40 */
41 protected function hook( $hook, $expect ) {
42 $mock = $this->getMock( __CLASS__, [ "on$hook" ] );
43 $this->mergeMwGlobalArrayValue( 'wgHooks', [
44 $hook => [ $mock ],
45 ] );
46 return $mock->expects( $expect )->method( "on$hook" );
47 }
48
49 /**
50 * Unsets a hook
51 * @param string $hook
52 */
53 protected function unhook( $hook ) {
54 $this->mergeMwGlobalArrayValue( 'wgHooks', [
55 $hook => [],
56 ] );
57 }
58
59 // Stubs for hooks taking reference parameters
60 public function onLoginUserMigrated( $user, &$msg ) {
61 }
62 public function onAbortLogin( $user, $password, &$abort, &$msg ) {
63 }
64 public function onAbortNewAccount( $user, &$abortError, &$abortStatus ) {
65 }
66 public function onAbortAutoAccount( $user, &$abortError ) {
67 }
68
69 /**
70 * @dataProvider provideTestForAuthentication
71 * @param string|null $username
72 * @param string|null $password
73 * @param string|null $msgForLoginUserMigrated
74 * @param int|null $abortForAbortLogin
75 * @param string|null $msgForAbortLogin
76 * @param string|null $failMsg
77 * @param array $failParams
78 */
79 public function testTestForAuthentication(
80 $username, $password,
81 $msgForLoginUserMigrated, $abortForAbortLogin, $msgForAbortLogin,
82 $failMsg, $failParams = []
83 ) {
84 $reqs = [];
85 if ( $username === null ) {
86 $this->hook( 'LoginUserMigrated', $this->never() );
87 $this->hook( 'AbortLogin', $this->never() );
88 } else {
89 if ( $password === null ) {
90 $req = $this->getMockForAbstractClass( AuthenticationRequest::class );
91 } else {
92 $req = new PasswordAuthenticationRequest();
93 $req->action = AuthManager::ACTION_LOGIN;
94 $req->password = $password;
95 }
96 $req->username = $username;
97 $reqs[get_class( $req )] = $req;
98
99 $h = $this->hook( 'LoginUserMigrated', $this->once() );
100 if ( $msgForLoginUserMigrated !== null ) {
101 $h->will( $this->returnCallback(
102 function ( $user, &$msg ) use ( $username, $msgForLoginUserMigrated ) {
103 $this->assertInstanceOf( 'User', $user );
104 $this->assertSame( $username, $user->getName() );
105 $msg = $msgForLoginUserMigrated;
106 return false;
107 }
108 ) );
109 $this->hook( 'AbortLogin', $this->never() );
110 } else {
111 $h->will( $this->returnCallback(
112 function ( $user, &$msg ) use ( $username ) {
113 $this->assertInstanceOf( 'User', $user );
114 $this->assertSame( $username, $user->getName() );
115 return true;
116 }
117 ) );
118 $h2 = $this->hook( 'AbortLogin', $this->once() );
119 if ( $abortForAbortLogin !== null ) {
120 $h2->will( $this->returnCallback(
121 function ( $user, $pass, &$abort, &$msg )
122 use ( $username, $password, $abortForAbortLogin, $msgForAbortLogin )
123 {
124 $this->assertInstanceOf( 'User', $user );
125 $this->assertSame( $username, $user->getName() );
126 if ( $password !== null ) {
127 $this->assertSame( $password, $pass );
128 } else {
129 $this->assertInternalType( 'string', $pass );
130 }
131 $abort = $abortForAbortLogin;
132 $msg = $msgForAbortLogin;
133 return false;
134 }
135 ) );
136 } else {
137 $h2->will( $this->returnCallback(
138 function ( $user, $pass, &$abort, &$msg ) use ( $username, $password ) {
139 $this->assertInstanceOf( 'User', $user );
140 $this->assertSame( $username, $user->getName() );
141 if ( $password !== null ) {
142 $this->assertSame( $password, $pass );
143 } else {
144 $this->assertInternalType( 'string', $pass );
145 }
146 return true;
147 }
148 ) );
149 }
150 }
151 }
152 unset( $h, $h2 );
153
154 $status = $this->getProvider()->testForAuthentication( $reqs );
155
156 $this->unhook( 'LoginUserMigrated' );
157 $this->unhook( 'AbortLogin' );
158
159 if ( $failMsg === null ) {
160 $this->assertEquals( \StatusValue::newGood(), $status, 'should succeed' );
161 } else {
162 $this->assertInstanceOf( 'StatusValue', $status, 'should fail (type)' );
163 $this->assertFalse( $status->isOk(), 'should fail (ok)' );
164 $errors = $status->getErrors();
165 $this->assertEquals( $failMsg, $errors[0]['message'], 'should fail (message)' );
166 $this->assertEquals( $failParams, $errors[0]['params'], 'should fail (params)' );
167 }
168 }
169
170 public static function provideTestForAuthentication() {
171 return [
172 'No valid requests' => [
173 null, null, null, null, null, null
174 ],
175 'No hook errors' => [
176 'User', 'PaSsWoRd', null, null, null, null
177 ],
178 'No hook errors, no password' => [
179 'User', null, null, null, null, null
180 ],
181 'LoginUserMigrated no message' => [
182 'User', 'PaSsWoRd', false, null, null, 'login-migrated-generic'
183 ],
184 'LoginUserMigrated with message' => [
185 'User', 'PaSsWoRd', 'LUM-abort', null, null, 'LUM-abort'
186 ],
187 'LoginUserMigrated with message and params' => [
188 'User', 'PaSsWoRd', [ 'LUM-abort', 'foo' ], null, null, 'LUM-abort', [ 'foo' ]
189 ],
190 'AbortLogin, SUCCESS' => [
191 'User', 'PaSsWoRd', null, \LoginForm::SUCCESS, null, null
192 ],
193 'AbortLogin, NEED_TOKEN, no message' => [
194 'User', 'PaSsWoRd', null, \LoginForm::NEED_TOKEN, null, 'nocookiesforlogin'
195 ],
196 'AbortLogin, NEED_TOKEN, with message' => [
197 'User', 'PaSsWoRd', null, \LoginForm::NEED_TOKEN, 'needtoken', 'needtoken'
198 ],
199 'AbortLogin, WRONG_TOKEN, no message' => [
200 'User', 'PaSsWoRd', null, \LoginForm::WRONG_TOKEN, null, 'sessionfailure'
201 ],
202 'AbortLogin, WRONG_TOKEN, with message' => [
203 'User', 'PaSsWoRd', null, \LoginForm::WRONG_TOKEN, 'wrongtoken', 'wrongtoken'
204 ],
205 'AbortLogin, ILLEGAL, no message' => [
206 'User', 'PaSsWoRd', null, \LoginForm::ILLEGAL, null, 'noname'
207 ],
208 'AbortLogin, ILLEGAL, with message' => [
209 'User', 'PaSsWoRd', null, \LoginForm::ILLEGAL, 'badname', 'badname'
210 ],
211 'AbortLogin, NO_NAME, no message' => [
212 'User', 'PaSsWoRd', null, \LoginForm::NO_NAME, null, 'noname'
213 ],
214 'AbortLogin, NO_NAME, with message' => [
215 'User', 'PaSsWoRd', null, \LoginForm::NO_NAME, 'badname', 'badname'
216 ],
217 'AbortLogin, WRONG_PASS, no message' => [
218 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PASS, null, 'wrongpassword'
219 ],
220 'AbortLogin, WRONG_PASS, with message' => [
221 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PASS, 'badpass', 'badpass'
222 ],
223 'AbortLogin, WRONG_PLUGIN_PASS, no message' => [
224 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PLUGIN_PASS, null, 'wrongpassword'
225 ],
226 'AbortLogin, WRONG_PLUGIN_PASS, with message' => [
227 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PLUGIN_PASS, 'badpass', 'badpass'
228 ],
229 'AbortLogin, NOT_EXISTS, no message' => [
230 "User'", 'A', null, \LoginForm::NOT_EXISTS, null, 'nosuchusershort', [ 'User&#39;' ]
231 ],
232 'AbortLogin, NOT_EXISTS, with message' => [
233 "User'", 'A', null, \LoginForm::NOT_EXISTS, 'badname', 'badname', [ 'User&#39;' ]
234 ],
235 'AbortLogin, EMPTY_PASS, no message' => [
236 'User', 'PaSsWoRd', null, \LoginForm::EMPTY_PASS, null, 'wrongpasswordempty'
237 ],
238 'AbortLogin, EMPTY_PASS, with message' => [
239 'User', 'PaSsWoRd', null, \LoginForm::EMPTY_PASS, 'badpass', 'badpass'
240 ],
241 'AbortLogin, RESET_PASS, no message' => [
242 'User', 'PaSsWoRd', null, \LoginForm::RESET_PASS, null, 'resetpass_announce'
243 ],
244 'AbortLogin, RESET_PASS, with message' => [
245 'User', 'PaSsWoRd', null, \LoginForm::RESET_PASS, 'resetpass', 'resetpass'
246 ],
247 'AbortLogin, THROTTLED, no message' => [
248 'User', 'PaSsWoRd', null, \LoginForm::THROTTLED, null, 'login-throttled',
249 [ \Message::durationParam( 42 ) ]
250 ],
251 'AbortLogin, THROTTLED, with message' => [
252 'User', 'PaSsWoRd', null, \LoginForm::THROTTLED, 't', 't',
253 [ \Message::durationParam( 42 ) ]
254 ],
255 'AbortLogin, USER_BLOCKED, no message' => [
256 "User'", 'P', null, \LoginForm::USER_BLOCKED, null, 'login-userblocked', [ 'User&#39;' ]
257 ],
258 'AbortLogin, USER_BLOCKED, with message' => [
259 "User'", 'P', null, \LoginForm::USER_BLOCKED, 'blocked', 'blocked', [ 'User&#39;' ]
260 ],
261 'AbortLogin, ABORTED, no message' => [
262 "User'", 'P', null, \LoginForm::ABORTED, null, 'login-abort-generic', [ 'User&#39;' ]
263 ],
264 'AbortLogin, ABORTED, with message' => [
265 "User'", 'P', null, \LoginForm::ABORTED, 'aborted', 'aborted', [ 'User&#39;' ]
266 ],
267 'AbortLogin, USER_MIGRATED, no message' => [
268 'User', 'P', null, \LoginForm::USER_MIGRATED, null, 'login-migrated-generic'
269 ],
270 'AbortLogin, USER_MIGRATED, with message' => [
271 'User', 'P', null, \LoginForm::USER_MIGRATED, 'migrated', 'migrated'
272 ],
273 'AbortLogin, USER_MIGRATED, with message and params' => [
274 'User', 'P', null, \LoginForm::USER_MIGRATED, [ 'migrated', 'foo' ],
275 'migrated', [ 'foo' ]
276 ],
277 ];
278 }
279
280 /**
281 * @dataProvider provideTestForAccountCreation
282 * @param string $msg
283 * @param Status|null $status
284 * @param StatusValue Result
285 */
286 public function testTestForAccountCreation( $msg, $status, $result ) {
287 $this->hook( 'AbortNewAccount', $this->once() )
288 ->will( $this->returnCallback( function ( $user, &$error, &$abortStatus )
289 use ( $msg, $status )
290 {
291 $this->assertInstanceOf( 'User', $user );
292 $this->assertSame( 'User', $user->getName() );
293 $error = $msg;
294 $abortStatus = $status;
295 return $error === null && $status === null;
296 } ) );
297
298 $user = \User::newFromName( 'User' );
299 $creator = \User::newFromName( 'UTSysop' );
300 $ret = $this->getProvider()->testForAccountCreation( $user, $creator, [] );
301
302 $this->unhook( 'AbortNewAccount' );
303
304 $this->assertEquals( $result, $ret );
305 }
306
307 public static function provideTestForAccountCreation() {
308 return [
309 'No hook errors' => [
310 null, null, \StatusValue::newGood()
311 ],
312 'AbortNewAccount, old style' => [
313 'foobar', null, \StatusValue::newFatal(
314 \Message::newFromKey( 'createaccount-hook-aborted' )->rawParams( 'foobar' )
315 )
316 ],
317 'AbortNewAccount, new style' => [
318 'foobar',
319 \Status::newFatal( 'aborted!', 'param' ),
320 \StatusValue::newFatal( 'aborted!', 'param' )
321 ],
322 ];
323 }
324
325 /**
326 * @dataProvider provideTestUserForCreation
327 * @param string|null $error
328 * @param string|null $failMsg
329 */
330 public function testTestUserForCreation( $error, $failMsg ) {
331 $testUser = self::getTestUser()->getUser();
332 $provider = $this->getProvider();
333 $options = [ 'flags' => \User::READ_LOCKING, 'creating' => true ];
334
335 $this->hook( 'AbortNewAccount', $this->never() );
336 $this->hook( 'AbortAutoAccount', $this->once() )
337 ->will( $this->returnCallback( function ( $user, &$abortError ) use ( $testUser, $error ) {
338 $this->assertInstanceOf( 'User', $user );
339 $this->assertSame( $testUser->getName(), $user->getName() );
340 $abortError = $error;
341 return $error === null;
342 } ) );
343 $status = $provider->testUserForCreation(
344 $testUser, AuthManager::AUTOCREATE_SOURCE_SESSION, $options
345 );
346 $this->unhook( 'AbortNewAccount' );
347 $this->unhook( 'AbortAutoAccount' );
348 if ( $failMsg === null ) {
349 $this->assertEquals( \StatusValue::newGood(), $status, 'should succeed' );
350 } else {
351 $this->assertInstanceOf( 'StatusValue', $status, 'should fail (type)' );
352 $this->assertFalse( $status->isOk(), 'should fail (ok)' );
353 $errors = $status->getErrors();
354 $this->assertEquals( $failMsg, $errors[0]['message'], 'should fail (message)' );
355 }
356
357 $this->hook( 'AbortAutoAccount', $this->never() );
358 $this->hook( 'AbortNewAccount', $this->never() );
359 $status = $provider->testUserForCreation( $testUser, false, $options );
360 $this->unhook( 'AbortNewAccount' );
361 $this->unhook( 'AbortAutoAccount' );
362 $this->assertEquals( \StatusValue::newGood(), $status, 'should succeed' );
363 }
364
365 public static function provideTestUserForCreation() {
366 return [
367 'Success' => [ null, null ],
368 'Fail, no message' => [ false, 'login-abort-generic' ],
369 'Fail, with message' => [ 'fail', 'fail' ],
370 ];
371 }
372 }