Merge "mediawiki.util: Use RegExp.test() instead of String.search()"
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / AuthenticationResponseTest.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 /**
6 * @group AuthManager
7 * @covers MediaWiki\Auth\AuthenticationResponse
8 */
9 class AuthenticationResponseTest extends \MediaWikiTestCase {
10 protected function setUp() {
11 global $wgDisableAuthManager;
12
13 parent::setUp();
14 if ( $wgDisableAuthManager ) {
15 $this->markTestSkipped( '$wgDisableAuthManager is set' );
16 }
17 }
18
19 /**
20 * @dataProvider provideConstructors
21 * @param string $constructor
22 * @param array $args
23 * @param array|Exception $expect
24 */
25 public function testConstructors( $constructor, $args, $expect ) {
26 if ( is_array( $expect ) ) {
27 $res = new AuthenticationResponse();
28 foreach ( $expect as $field => $value ) {
29 $res->$field = $value;
30 }
31 $ret = call_user_func_array( "MediaWiki\\Auth\\AuthenticationResponse::$constructor", $args );
32 $this->assertEquals( $res, $ret );
33 } else {
34 try {
35 call_user_func_array( "MediaWiki\\Auth\\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 ] ],
64
65 [ 'newRestart', [ $msg ], [
66 'status' => AuthenticationResponse::RESTART,
67 'message' => $msg,
68 ] ],
69
70 [ 'newAbstain', [], [
71 'status' => AuthenticationResponse::ABSTAIN,
72 ] ],
73
74 [ 'newUI', [ [ $req ], $msg ], [
75 'status' => AuthenticationResponse::UI,
76 'neededRequests' => [ $req ],
77 'message' => $msg,
78 ] ],
79 [ 'newUI', [ [], $msg ],
80 new \InvalidArgumentException( '$reqs may not be empty' )
81 ],
82
83 [ 'newRedirect', [ [ $req ], 'http://example.org/redir' ], [
84 'status' => AuthenticationResponse::REDIRECT,
85 'neededRequests' => [ $req ],
86 'redirectTarget' => 'http://example.org/redir',
87 ] ],
88 [
89 'newRedirect',
90 [ [ $req ], 'http://example.org/redir', [ 'foo' => 'bar' ] ],
91 [
92 'status' => AuthenticationResponse::REDIRECT,
93 'neededRequests' => [ $req ],
94 'redirectTarget' => 'http://example.org/redir',
95 'redirectApiData' => [ 'foo' => 'bar' ],
96 ]
97 ],
98 [ 'newRedirect', [ [], 'http://example.org/redir' ],
99 new \InvalidArgumentException( '$reqs may not be empty' )
100 ],
101 ];
102 }
103
104 }