1<?php
2
3namespace MediaWiki\Auth;
4
5/**
6 * @group AuthManager
7 * @covers \MediaWiki\Auth\TemporaryPasswordAuthenticationRequest
8 */
9class TemporaryPasswordAuthenticationRequestTest extends AuthenticationRequestTestCase {
10
11	protected function getInstance( array $args = [] ) {
12		$ret = new TemporaryPasswordAuthenticationRequest;
13		$ret->action = $args[0];
14		return $ret;
15	}
16
17	public static function provideGetFieldInfo() {
18		return [
19			[ [ AuthManager::ACTION_CREATE ] ],
20			[ [ AuthManager::ACTION_CHANGE ] ],
21			[ [ AuthManager::ACTION_REMOVE ] ],
22		];
23	}
24
25	public function testNewRandom() {
26		global $wgPasswordPolicy;
27
28		$policy = $wgPasswordPolicy;
29		unset( $policy['policies'] );
30		$policy['policies']['default'] = [
31			'MinimalPasswordLength' => 1,
32			'MinimumPasswordLengthToLogin' => 1,
33		];
34
35		$this->setMwGlobals( [
36			'wgMinimalPasswordLength' => 10,
37			'wgPasswordPolicy' => $policy,
38		] );
39
40		$ret1 = TemporaryPasswordAuthenticationRequest::newRandom();
41		$ret2 = TemporaryPasswordAuthenticationRequest::newRandom();
42		$this->assertEquals( 10, strlen( $ret1->password ) );
43		$this->assertEquals( 10, strlen( $ret2->password ) );
44		$this->assertNotSame( $ret1->password, $ret2->password );
45
46		$policy['policies']['default']['MinimalPasswordLength'] = 15;
47		$this->setMwGlobals( 'wgPasswordPolicy', $policy );
48		$ret = TemporaryPasswordAuthenticationRequest::newRandom();
49		$this->assertEquals( 15, strlen( $ret->password ) );
50
51		$policy['policies']['default']['MinimalPasswordLength'] = [ 'value' => 20 ];
52		$this->setMwGlobals( 'wgPasswordPolicy', $policy );
53		$ret = TemporaryPasswordAuthenticationRequest::newRandom();
54		$this->assertEquals( 20, strlen( $ret->password ) );
55	}
56
57	public function testNewInvalid() {
58		$ret = TemporaryPasswordAuthenticationRequest::newInvalid();
59		$this->assertNull( $ret->password );
60	}
61
62	public function provideLoadFromSubmission() {
63		return [
64			'Empty request' => [
65				[ AuthManager::ACTION_REMOVE ],
66				[],
67				false,
68			],
69			'Create, empty request' => [
70				[ AuthManager::ACTION_CREATE ],
71				[],
72				false,
73			],
74			'Create, mailpassword set' => [
75				[ AuthManager::ACTION_CREATE ],
76				[ 'mailpassword' => 1 ],
77				[ 'mailpassword' => true, 'action' => AuthManager::ACTION_CREATE ],
78			],
79		];
80	}
81
82	public function testDescribeCredentials() {
83		$req = new TemporaryPasswordAuthenticationRequest;
84		$req->action = AuthManager::ACTION_LOGIN;
85		$req->username = 'UTSysop';
86		$ret = $req->describeCredentials();
87		$this->assertIsArray( $ret );
88		$this->assertArrayHasKey( 'provider', $ret );
89		$this->assertInstanceOf( \Message::class, $ret['provider'] );
90		$this->assertSame( 'authmanager-provider-temporarypassword', $ret['provider']->getKey() );
91		$this->assertArrayHasKey( 'account', $ret );
92		$this->assertInstanceOf( \Message::class, $ret['account'] );
93		$this->assertSame( [ 'UTSysop' ], $ret['account']->getParams() );
94	}
95}
96