| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Auth; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\Auth\AuthenticationRequest; |
| 8 |
use MediaWiki\Auth\AuthenticationResponse; |
| 9 |
use MediaWiki\Message\Message; |
| 10 |
use MediaWikiUnitTestCase; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class AuthenticationResponseTest extends MediaWikiUnitTestCase { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function testConstructors( $constructor, $args, $expect ) { |
| 25 |
if ( is_array( $expect ) ) { |
| 26 |
$res = new AuthenticationResponse(); |
| 27 |
$res->messageType = 'warning'; |
| 28 |
foreach ( $expect as $field => $value ) { |
| 29 |
$res->$field = $value; |
| 30 |
} |
| 31 |
$ret = AuthenticationResponse::$constructor( ...$args ); |
| 32 |
$this->assertEquals( $res, $ret ); |
| 33 |
} else { |
| 34 |
try { |
| 35 |
AuthenticationResponse::$constructor( ...$args ); |
| 36 |
$this->fail( 'Expected exception not thrown' ); |
| 37 |
} catch ( Exception $ex ) { |
| 38 |
$this->assertEquals( $expect, $ex ); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public function provideConstructors() { |
| 44 |
$req = $this->getMockForAbstractClass( AuthenticationRequest::class ); |
| 45 |
$msg = new Message( 'mainpage' ); |
| 46 |
|
| 47 |
return [ |
| 48 |
[ 'newPass', [], [ |
| 49 |
'status' => AuthenticationResponse::PASS, |
| 50 |
] ], |
| 51 |
[ 'newPass', [ 'name' ], [ |
| 52 |
'status' => AuthenticationResponse::PASS, |
| 53 |
'username' => 'name', |
| 54 |
] ], |
| 55 |
[ 'newPass', [ 'name', null ], [ |
| 56 |
'status' => AuthenticationResponse::PASS, |
| 57 |
'username' => 'name', |
| 58 |
] ], |
| 59 |
|
| 60 |
[ 'newFail', [ $msg ], [ |
| 61 |
'status' => AuthenticationResponse::FAIL, |
| 62 |
'message' => $msg, |
| 63 |
'messageType' => 'error', |
| 64 |
'failReasons' => [] |
| 65 |
] ], |
| 66 |
|
| 67 |
[ 'newRestart', [ $msg ], [ |
| 68 |
'status' => AuthenticationResponse::RESTART, |
| 69 |
'message' => $msg, |
| 70 |
] ], |
| 71 |
|
| 72 |
[ 'newAbstain', [], [ |
| 73 |
'status' => AuthenticationResponse::ABSTAIN, |
| 74 |
] ], |
| 75 |
|
| 76 |
[ 'newUI', [ [ $req ], $msg ], [ |
| 77 |
'status' => AuthenticationResponse::UI, |
| 78 |
'neededRequests' => [ $req ], |
| 79 |
'message' => $msg, |
| 80 |
'messageType' => 'warning', |
| 81 |
] ], |
| 82 |
|
| 83 |
[ 'newUI', [ [ $req ], $msg, 'warning' ], [ |
| 84 |
'status' => AuthenticationResponse::UI, |
| 85 |
'neededRequests' => [ $req ], |
| 86 |
'message' => $msg, |
| 87 |
'messageType' => 'warning', |
| 88 |
] ], |
| 89 |
|
| 90 |
[ 'newUI', [ [ $req ], $msg, 'error' ], [ |
| 91 |
'status' => AuthenticationResponse::UI, |
| 92 |
'neededRequests' => [ $req ], |
| 93 |
'message' => $msg, |
| 94 |
'messageType' => 'error', |
| 95 |
] ], |
| 96 |
[ 'newUI', [ [], $msg ], |
| 97 |
new InvalidArgumentException( '$reqs may not be empty' ) |
| 98 |
], |
| 99 |
|
| 100 |
[ 'newRedirect', [ [ $req ], 'http://example.org/redir' ], [ |
| 101 |
'status' => AuthenticationResponse::REDIRECT, |
| 102 |
'neededRequests' => [ $req ], |
| 103 |
'redirectTarget' => 'http://example.org/redir', |
| 104 |
] ], |
| 105 |
[ |
| 106 |
'newRedirect', |
| 107 |
[ [ $req ], 'http://example.org/redir', [ 'foo' => 'bar' ] ], |
| 108 |
[ |
| 109 |
'status' => AuthenticationResponse::REDIRECT, |
| 110 |
'neededRequests' => [ $req ], |
| 111 |
'redirectTarget' => 'http://example.org/redir', |
| 112 |
'redirectApiData' => [ 'foo' => 'bar' ], |
| 113 |
] |
| 114 |
], |
| 115 |
[ 'newRedirect', [ [], 'http://example.org/redir' ], |
| 116 |
new InvalidArgumentException( '$reqs may not be empty' ) |
| 117 |
], |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
} |