Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / auth / LegacyHookPreAuthenticationProviderTest.php
blobb96455e09124c724929c24ccd20143ae1d18b9ca
1 <?php
3 namespace MediaWiki\Auth;
5 use MediaWiki\MediaWikiServices;
7 /**
8 * @group AuthManager
9 * @group Database
10 * @covers MediaWiki\Auth\LegacyHookPreAuthenticationProvider
12 class LegacyHookPreAuthenticationProviderTest extends \MediaWikiTestCase {
13 /**
14 * Get an instance of the provider
15 * @return LegacyHookPreAuthenticationProvider
17 protected function getProvider() {
18 $request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
19 $request->expects( $this->any() )->method( 'getIP' )->will( $this->returnValue( '127.0.0.42' ) );
21 $manager = new AuthManager(
22 $request,
23 MediaWikiServices::getInstance()->getMainConfig()
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;
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( ... ).
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" );
49 /**
50 * Unsets a hook
51 * @param string $hook
53 protected function unhook( $hook ) {
54 $this->mergeMwGlobalArrayValue( 'wgHooks', [
55 $hook => [],
56 ] );
59 // Stubs for hooks taking reference parameters
60 public function onLoginUserMigrated( $user, &$msg ) {
62 public function onAbortLogin( $user, $password, &$abort, &$msg ) {
64 public function onAbortNewAccount( $user, &$abortError, &$abortStatus ) {
66 public function onAbortAutoAccount( $user, &$abortError ) {
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
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;
96 $req->username = $username;
97 $reqs[get_class( $req )] = $req;
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;
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;
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 )
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 );
131 $abort = $abortForAbortLogin;
132 $msg = $msgForAbortLogin;
133 return false;
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 );
146 return true;
148 ) );
152 unset( $h, $h2 );
154 $status = $this->getProvider()->testForAuthentication( $reqs );
156 $this->unhook( 'LoginUserMigrated' );
157 $this->unhook( 'AbortLogin' );
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)' );
170 public static function provideTestForAuthentication() {
171 return [
172 'No valid requests' => [
173 null, null, null, null, null, null
175 'No hook errors' => [
176 'User', 'PaSsWoRd', null, null, null, null
178 'No hook errors, no password' => [
179 'User', null, null, null, null, null
181 'LoginUserMigrated no message' => [
182 'User', 'PaSsWoRd', false, null, null, 'login-migrated-generic'
184 'LoginUserMigrated with message' => [
185 'User', 'PaSsWoRd', 'LUM-abort', null, null, 'LUM-abort'
187 'LoginUserMigrated with message and params' => [
188 'User', 'PaSsWoRd', [ 'LUM-abort', 'foo' ], null, null, 'LUM-abort', [ 'foo' ]
190 'AbortLogin, SUCCESS' => [
191 'User', 'PaSsWoRd', null, \LoginForm::SUCCESS, null, null
193 'AbortLogin, NEED_TOKEN, no message' => [
194 'User', 'PaSsWoRd', null, \LoginForm::NEED_TOKEN, null, 'nocookiesforlogin'
196 'AbortLogin, NEED_TOKEN, with message' => [
197 'User', 'PaSsWoRd', null, \LoginForm::NEED_TOKEN, 'needtoken', 'needtoken'
199 'AbortLogin, WRONG_TOKEN, no message' => [
200 'User', 'PaSsWoRd', null, \LoginForm::WRONG_TOKEN, null, 'sessionfailure'
202 'AbortLogin, WRONG_TOKEN, with message' => [
203 'User', 'PaSsWoRd', null, \LoginForm::WRONG_TOKEN, 'wrongtoken', 'wrongtoken'
205 'AbortLogin, ILLEGAL, no message' => [
206 'User', 'PaSsWoRd', null, \LoginForm::ILLEGAL, null, 'noname'
208 'AbortLogin, ILLEGAL, with message' => [
209 'User', 'PaSsWoRd', null, \LoginForm::ILLEGAL, 'badname', 'badname'
211 'AbortLogin, NO_NAME, no message' => [
212 'User', 'PaSsWoRd', null, \LoginForm::NO_NAME, null, 'noname'
214 'AbortLogin, NO_NAME, with message' => [
215 'User', 'PaSsWoRd', null, \LoginForm::NO_NAME, 'badname', 'badname'
217 'AbortLogin, WRONG_PASS, no message' => [
218 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PASS, null, 'wrongpassword'
220 'AbortLogin, WRONG_PASS, with message' => [
221 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PASS, 'badpass', 'badpass'
223 'AbortLogin, WRONG_PLUGIN_PASS, no message' => [
224 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PLUGIN_PASS, null, 'wrongpassword'
226 'AbortLogin, WRONG_PLUGIN_PASS, with message' => [
227 'User', 'PaSsWoRd', null, \LoginForm::WRONG_PLUGIN_PASS, 'badpass', 'badpass'
229 'AbortLogin, NOT_EXISTS, no message' => [
230 "User'", 'A', null, \LoginForm::NOT_EXISTS, null, 'nosuchusershort', [ 'User&#39;' ]
232 'AbortLogin, NOT_EXISTS, with message' => [
233 "User'", 'A', null, \LoginForm::NOT_EXISTS, 'badname', 'badname', [ 'User&#39;' ]
235 'AbortLogin, EMPTY_PASS, no message' => [
236 'User', 'PaSsWoRd', null, \LoginForm::EMPTY_PASS, null, 'wrongpasswordempty'
238 'AbortLogin, EMPTY_PASS, with message' => [
239 'User', 'PaSsWoRd', null, \LoginForm::EMPTY_PASS, 'badpass', 'badpass'
241 'AbortLogin, RESET_PASS, no message' => [
242 'User', 'PaSsWoRd', null, \LoginForm::RESET_PASS, null, 'resetpass_announce'
244 'AbortLogin, RESET_PASS, with message' => [
245 'User', 'PaSsWoRd', null, \LoginForm::RESET_PASS, 'resetpass', 'resetpass'
247 'AbortLogin, THROTTLED, no message' => [
248 'User', 'PaSsWoRd', null, \LoginForm::THROTTLED, null, 'login-throttled',
249 [ \Message::durationParam( 42 ) ]
251 'AbortLogin, THROTTLED, with message' => [
252 'User', 'PaSsWoRd', null, \LoginForm::THROTTLED, 't', 't',
253 [ \Message::durationParam( 42 ) ]
255 'AbortLogin, USER_BLOCKED, no message' => [
256 "User'", 'P', null, \LoginForm::USER_BLOCKED, null, 'login-userblocked', [ 'User&#39;' ]
258 'AbortLogin, USER_BLOCKED, with message' => [
259 "User'", 'P', null, \LoginForm::USER_BLOCKED, 'blocked', 'blocked', [ 'User&#39;' ]
261 'AbortLogin, ABORTED, no message' => [
262 "User'", 'P', null, \LoginForm::ABORTED, null, 'login-abort-generic', [ 'User&#39;' ]
264 'AbortLogin, ABORTED, with message' => [
265 "User'", 'P', null, \LoginForm::ABORTED, 'aborted', 'aborted', [ 'User&#39;' ]
267 'AbortLogin, USER_MIGRATED, no message' => [
268 'User', 'P', null, \LoginForm::USER_MIGRATED, null, 'login-migrated-generic'
270 'AbortLogin, USER_MIGRATED, with message' => [
271 'User', 'P', null, \LoginForm::USER_MIGRATED, 'migrated', 'migrated'
273 'AbortLogin, USER_MIGRATED, with message and params' => [
274 'User', 'P', null, \LoginForm::USER_MIGRATED, [ 'migrated', 'foo' ],
275 'migrated', [ 'foo' ]
281 * @dataProvider provideTestForAccountCreation
282 * @param string $msg
283 * @param Status|null $status
284 * @param StatusValue Result
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 )
291 $this->assertInstanceOf( 'User', $user );
292 $this->assertSame( 'User', $user->getName() );
293 $error = $msg;
294 $abortStatus = $status;
295 return $error === null && $status === null;
296 } ) );
298 $user = \User::newFromName( 'User' );
299 $creator = \User::newFromName( 'UTSysop' );
300 $ret = $this->getProvider()->testForAccountCreation( $user, $creator, [] );
302 $this->unhook( 'AbortNewAccount' );
304 $this->assertEquals( $result, $ret );
307 public static function provideTestForAccountCreation() {
308 return [
309 'No hook errors' => [
310 null, null, \StatusValue::newGood()
312 'AbortNewAccount, old style' => [
313 'foobar', null, \StatusValue::newFatal(
314 \Message::newFromKey( 'createaccount-hook-aborted' )->rawParams( 'foobar' )
317 'AbortNewAccount, new style' => [
318 'foobar',
319 \Status::newFatal( 'aborted!', 'param' ),
320 \StatusValue::newFatal( 'aborted!', 'param' )
326 * @dataProvider provideTestUserForCreation
327 * @param string|null $error
328 * @param string|null $failMsg
330 public function testTestUserForCreation( $error, $failMsg ) {
331 $testUser = self::getTestUser()->getUser();
332 $provider = $this->getProvider();
333 $options = [ 'flags' => \User::READ_LOCKING, 'creating' => true ];
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
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)' );
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' );
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' ],