3 namespace MediaWiki\Tests\Auth
;
5 use MediaWiki\Auth\AuthManager
;
6 use MediaWiki\Auth\TemporaryPasswordAuthenticationRequest
;
7 use MediaWiki\MainConfigNames
;
8 use MediaWiki\Message\Message
;
12 * @covers \MediaWiki\Auth\TemporaryPasswordAuthenticationRequest
14 class TemporaryPasswordAuthenticationRequestTest
extends AuthenticationRequestTestCase
{
16 protected function getInstance( array $args = [] ) {
17 $ret = new TemporaryPasswordAuthenticationRequest
;
18 $ret->action
= $args[0];
22 public static function provideGetFieldInfo() {
24 [ [ AuthManager
::ACTION_CREATE
] ],
25 [ [ AuthManager
::ACTION_CHANGE
] ],
26 [ [ AuthManager
::ACTION_REMOVE
] ],
30 public function testNewRandom() {
31 global $wgPasswordPolicy;
33 $policy = $wgPasswordPolicy;
34 unset( $policy['policies'] );
35 $policy['policies']['default'] = [
36 'MinimalPasswordLength' => 1,
37 'MinimumPasswordLengthToLogin' => 1,
40 $this->overrideConfigValues( [
41 MainConfigNames
::PasswordPolicy
=> $policy,
44 $ret1 = TemporaryPasswordAuthenticationRequest
::newRandom();
45 $ret2 = TemporaryPasswordAuthenticationRequest
::newRandom();
46 $this->assertEquals( 10, strlen( $ret1->password
) );
47 $this->assertEquals( 10, strlen( $ret2->password
) );
48 $this->assertNotSame( $ret1->password
, $ret2->password
);
50 $policy['policies']['default']['MinimalPasswordLength'] = 15;
51 $this->overrideConfigValue( MainConfigNames
::PasswordPolicy
, $policy );
52 $ret = TemporaryPasswordAuthenticationRequest
::newRandom();
53 $this->assertEquals( 15, strlen( $ret->password
) );
55 $policy['policies']['default']['MinimalPasswordLength'] = [ 'value' => 20 ];
56 $this->overrideConfigValue( MainConfigNames
::PasswordPolicy
, $policy );
57 $ret = TemporaryPasswordAuthenticationRequest
::newRandom();
58 $this->assertEquals( 20, strlen( $ret->password
) );
61 public function testNewInvalid() {
62 $ret = TemporaryPasswordAuthenticationRequest
::newInvalid();
63 $this->assertNull( $ret->password
);
66 public static function provideLoadFromSubmission() {
69 [ AuthManager
::ACTION_REMOVE
],
73 'Create, empty request' => [
74 [ AuthManager
::ACTION_CREATE
],
78 'Create, mailpassword set' => [
79 [ AuthManager
::ACTION_CREATE
],
80 [ 'mailpassword' => 1 ],
81 [ 'mailpassword' => true, 'action' => AuthManager
::ACTION_CREATE
],
86 public function testDescribeCredentials() {
87 $username = 'TestDescribeCredentials';
88 $req = new TemporaryPasswordAuthenticationRequest
;
89 $req->action
= AuthManager
::ACTION_LOGIN
;
90 $req->username
= $username;
91 $ret = $req->describeCredentials();
92 $this->assertIsArray( $ret );
93 $this->assertArrayHasKey( 'provider', $ret );
94 $this->assertInstanceOf( Message
::class, $ret['provider'] );
95 $this->assertSame( 'authmanager-provider-temporarypassword', $ret['provider']->getKey() );
96 $this->assertArrayHasKey( 'account', $ret );
97 $this->assertInstanceOf( Message
::class, $ret['account'] );
98 $this->assertSame( [ $username ], $ret['account']->getParams() );