2020-09-16 09:15:38 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-03 11:12:16 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPreAuthenticationProvider;
|
2022-04-09 15:07:45 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-07-13 17:27:19 +00:00
|
|
|
use MediaWiki\Page\PageReferenceValue;
|
2021-01-03 11:12:16 +00:00
|
|
|
|
2020-09-16 09:15:38 +00:00
|
|
|
/**
|
|
|
|
* This trait can be used to create accounts in integration tests.
|
|
|
|
* NOTE: The implementing classes MUST extend MediaWikiIntegrationTestCase
|
|
|
|
* @todo This might be moved to MediaWikiIntegrationTestCase
|
|
|
|
*/
|
|
|
|
trait AbuseFilterCreateAccountTestTrait {
|
|
|
|
/**
|
|
|
|
* @param string $accountName
|
|
|
|
* @param bool $autocreate
|
2022-07-13 17:27:19 +00:00
|
|
|
* @param User|null $creator Defaults to the newly created user
|
2020-09-16 09:15:38 +00:00
|
|
|
* @return StatusValue
|
|
|
|
*/
|
|
|
|
protected function createAccount(
|
|
|
|
string $accountName,
|
2022-07-13 17:27:19 +00:00
|
|
|
bool $autocreate = false,
|
|
|
|
User $creator = null
|
2021-07-21 18:51:12 +00:00
|
|
|
): StatusValue {
|
2022-07-13 17:27:19 +00:00
|
|
|
$userFactory = MediaWikiServices::getInstance()->getUserFactory();
|
|
|
|
$user = $userFactory->newFromName( $accountName );
|
2023-06-12 08:27:03 +00:00
|
|
|
$creator ??= $user;
|
2020-09-16 09:15:38 +00:00
|
|
|
|
2022-04-09 15:07:45 +00:00
|
|
|
$provider = new AbuseFilterPreAuthenticationProvider(
|
|
|
|
AbuseFilterServices::getVariableGeneratorFactory(),
|
|
|
|
AbuseFilterServices::getFilterRunnerFactory(),
|
|
|
|
new NullStatsdDataFactory(),
|
2022-07-13 17:27:19 +00:00
|
|
|
$userFactory
|
2022-04-09 15:07:45 +00:00
|
|
|
);
|
2022-07-13 17:27:19 +00:00
|
|
|
if ( $autocreate ) {
|
|
|
|
$status = $provider->testUserForCreation( $user, $autocreate );
|
|
|
|
} else {
|
|
|
|
$status = $provider->testForAccountCreation( $user, $creator, [] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
// A creatable username must exist to be passed to $logEntry->setPerformer(),
|
|
|
|
// so create the account.
|
|
|
|
$user->addToDatabase();
|
2020-09-16 09:15:38 +00:00
|
|
|
|
2022-07-13 17:27:19 +00:00
|
|
|
// FIXME This is a bit hacky, but AuthManager doesn't expose any methods for logging
|
|
|
|
$subType = $autocreate ? 'autocreate' : 'create2';
|
|
|
|
$logEntry = new ManualLogEntry( 'newusers', $subType );
|
|
|
|
$logEntry->setPerformer( $creator );
|
|
|
|
$logEntry->setTarget( PageReferenceValue::localReference( NS_USER, $accountName ) );
|
|
|
|
$logEntry->setComment( 'Fooobarcomment' );
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
'4::userid' => $user->getId(),
|
|
|
|
] );
|
|
|
|
$logid = $logEntry->insert();
|
|
|
|
$logEntry->publish( $logid );
|
|
|
|
$status->value = $logid;
|
|
|
|
}
|
2020-09-16 09:15:38 +00:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
}
|