3 namespace MediaWiki\Tests\Auth
;
5 use MediaWiki\Auth\AuthManager
;
6 use MediaWiki\Auth\PasswordAuthenticationRequest
;
7 use MediaWiki\Message\Message
;
11 * @covers \MediaWiki\Auth\PasswordAuthenticationRequest
13 class PasswordAuthenticationRequestTest
extends AuthenticationRequestTestCase
{
15 protected function getInstance( array $args = [] ) {
16 $ret = new PasswordAuthenticationRequest();
17 $ret->action
= $args[0];
21 public static function provideGetFieldInfo() {
23 [ [ AuthManager
::ACTION_LOGIN
] ],
24 [ [ AuthManager
::ACTION_CREATE
] ],
25 [ [ AuthManager
::ACTION_CHANGE
] ],
26 [ [ AuthManager
::ACTION_REMOVE
] ],
30 public function testGetFieldInfo2() {
33 AuthManager
::ACTION_LOGIN
,
34 AuthManager
::ACTION_CREATE
,
35 AuthManager
::ACTION_CHANGE
,
36 AuthManager
::ACTION_REMOVE
,
38 $req = new PasswordAuthenticationRequest();
39 $req->action
= $action;
40 $info[$action] = $req->getFieldInfo();
43 $this->assertSame( [], $info[AuthManager
::ACTION_REMOVE
], 'No data needed to remove' );
45 $this->assertArrayNotHasKey( 'retype', $info[AuthManager
::ACTION_LOGIN
],
46 'No need to retype password on login' );
47 $this->assertArrayHasKey( 'retype', $info[AuthManager
::ACTION_CREATE
],
48 'Need to retype when creating new password' );
49 $this->assertArrayHasKey( 'retype', $info[AuthManager
::ACTION_CHANGE
],
50 'Need to retype when changing password' );
52 $this->assertNotEquals(
53 $info[AuthManager
::ACTION_LOGIN
]['password']['label'],
54 $info[AuthManager
::ACTION_CHANGE
]['password']['label'],
55 'Password field for change is differentiated from login'
57 $this->assertNotEquals(
58 $info[AuthManager
::ACTION_CREATE
]['password']['label'],
59 $info[AuthManager
::ACTION_CHANGE
]['password']['label'],
60 'Password field for change is differentiated from create'
62 $this->assertNotEquals(
63 $info[AuthManager
::ACTION_CREATE
]['retype']['label'],
64 $info[AuthManager
::ACTION_CHANGE
]['retype']['label'],
65 'Retype field for change is differentiated from create'
69 public static function provideLoadFromSubmission() {
71 'Empty request, login' => [
72 [ AuthManager
::ACTION_LOGIN
],
76 'Empty request, change' => [
77 [ AuthManager
::ACTION_CHANGE
],
81 'Empty request, remove' => [
82 [ AuthManager
::ACTION_REMOVE
],
86 'Username + password, login' => [
87 [ AuthManager
::ACTION_LOGIN
],
88 $data = [ 'username' => 'User', 'password' => 'Bar' ],
89 $data +
[ 'action' => AuthManager
::ACTION_LOGIN
],
91 'Username + password, change' => [
92 [ AuthManager
::ACTION_CHANGE
],
93 [ 'username' => 'User', 'password' => 'Bar' ],
96 'Username + password + retype' => [
97 [ AuthManager
::ACTION_CHANGE
],
98 [ 'username' => 'User', 'password' => 'Bar', 'retype' => 'baz' ],
99 [ 'password' => 'Bar', 'retype' => 'baz', 'action' => AuthManager
::ACTION_CHANGE
],
101 'Username empty, login' => [
102 [ AuthManager
::ACTION_LOGIN
],
103 [ 'username' => '', 'password' => 'Bar' ],
106 'Username empty, change' => [
107 [ AuthManager
::ACTION_CHANGE
],
108 [ 'username' => '', 'password' => 'Bar', 'retype' => 'baz' ],
109 [ 'password' => 'Bar', 'retype' => 'baz', 'action' => AuthManager
::ACTION_CHANGE
],
111 'Password empty, login' => [
112 [ AuthManager
::ACTION_LOGIN
],
113 [ 'username' => 'User', 'password' => '' ],
116 'Password empty, login, with retype' => [
117 [ AuthManager
::ACTION_LOGIN
],
118 [ 'username' => 'User', 'password' => '', 'retype' => 'baz' ],
122 [ AuthManager
::ACTION_CHANGE
],
123 [ 'username' => 'User', 'password' => 'Bar', 'retype' => '' ],
129 public function testDescribeCredentials() {
130 $username = 'TestDescribeCredentials';
131 $req = new PasswordAuthenticationRequest
;
132 $req->action
= AuthManager
::ACTION_LOGIN
;
133 $req->username
= $username;
134 $ret = $req->describeCredentials();
135 $this->assertIsArray( $ret );
136 $this->assertArrayHasKey( 'provider', $ret );
137 $this->assertInstanceOf( Message
::class, $ret['provider'] );
138 $this->assertSame( 'authmanager-provider-password', $ret['provider']->getKey() );
139 $this->assertArrayHasKey( 'account', $ret );
140 $this->assertInstanceOf( Message
::class, $ret['account'] );
141 $this->assertSame( [ $username ], $ret['account']->getParams() );