wiki.techinc.nl/tests/phpunit/includes/auth/TemporaryPasswordAuthenticationRequestTest.php
mainframe98 7299aed7a5 tests: Allow overridable data providers to be static
This uses some reflection to identify if the data provider is static or
not. If it isn't, a deprecation notice is emitted. This doesn't fail the
tests, but is still printed in the output.

To facilitate this, the relevant abstract method has been uncommented,
as PHP does not like it when function signatures do not match up.

This approach means that tests in extensions or skins do not immediately
break when making data providers static. Instead, they can do so at
their own pace.

Bug: T332865
Change-Id: I5ff35ad0e894f0a27beae00257dc1fc599ad518d
2023-05-16 16:28:43 +01:00

97 lines
2.9 KiB
PHP

<?php
namespace MediaWiki\Auth;
use MediaWiki\MainConfigNames;
/**
* @group AuthManager
* @covers \MediaWiki\Auth\TemporaryPasswordAuthenticationRequest
*/
class TemporaryPasswordAuthenticationRequestTest extends AuthenticationRequestTestCase {
protected function getInstance( array $args = [] ) {
$ret = new TemporaryPasswordAuthenticationRequest;
$ret->action = $args[0];
return $ret;
}
public static function provideGetFieldInfo() {
return [
[ [ AuthManager::ACTION_CREATE ] ],
[ [ AuthManager::ACTION_CHANGE ] ],
[ [ AuthManager::ACTION_REMOVE ] ],
];
}
public function testNewRandom() {
global $wgPasswordPolicy;
$policy = $wgPasswordPolicy;
unset( $policy['policies'] );
$policy['policies']['default'] = [
'MinimalPasswordLength' => 1,
'MinimumPasswordLengthToLogin' => 1,
];
$this->overrideConfigValues( [
MainConfigNames::MinimalPasswordLength => 10,
MainConfigNames::PasswordPolicy => $policy,
] );
$ret1 = TemporaryPasswordAuthenticationRequest::newRandom();
$ret2 = TemporaryPasswordAuthenticationRequest::newRandom();
$this->assertEquals( 10, strlen( $ret1->password ) );
$this->assertEquals( 10, strlen( $ret2->password ) );
$this->assertNotSame( $ret1->password, $ret2->password );
$policy['policies']['default']['MinimalPasswordLength'] = 15;
$this->overrideConfigValue( MainConfigNames::PasswordPolicy, $policy );
$ret = TemporaryPasswordAuthenticationRequest::newRandom();
$this->assertEquals( 15, strlen( $ret->password ) );
$policy['policies']['default']['MinimalPasswordLength'] = [ 'value' => 20 ];
$this->overrideConfigValue( MainConfigNames::PasswordPolicy, $policy );
$ret = TemporaryPasswordAuthenticationRequest::newRandom();
$this->assertEquals( 20, strlen( $ret->password ) );
}
public function testNewInvalid() {
$ret = TemporaryPasswordAuthenticationRequest::newInvalid();
$this->assertNull( $ret->password );
}
public static function provideLoadFromSubmission() {
return [
'Empty request' => [
[ AuthManager::ACTION_REMOVE ],
[],
false,
],
'Create, empty request' => [
[ AuthManager::ACTION_CREATE ],
[],
false,
],
'Create, mailpassword set' => [
[ AuthManager::ACTION_CREATE ],
[ 'mailpassword' => 1 ],
[ 'mailpassword' => true, 'action' => AuthManager::ACTION_CREATE ],
],
];
}
public function testDescribeCredentials() {
$req = new TemporaryPasswordAuthenticationRequest;
$req->action = AuthManager::ACTION_LOGIN;
$req->username = 'UTSysop';
$ret = $req->describeCredentials();
$this->assertIsArray( $ret );
$this->assertArrayHasKey( 'provider', $ret );
$this->assertInstanceOf( \Message::class, $ret['provider'] );
$this->assertSame( 'authmanager-provider-temporarypassword', $ret['provider']->getKey() );
$this->assertArrayHasKey( 'account', $ret );
$this->assertInstanceOf( \Message::class, $ret['account'] );
$this->assertSame( [ 'UTSysop' ], $ret['account']->getParams() );
}
}