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;
|
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 User|null $creator Defaults to the newly created user
|
|
|
|
* @param bool $autocreate
|
|
|
|
* @return StatusValue
|
|
|
|
*/
|
|
|
|
protected function createAccount(
|
|
|
|
string $accountName,
|
|
|
|
User $creator = null,
|
|
|
|
bool $autocreate = false
|
2021-07-21 18:51:12 +00:00
|
|
|
): StatusValue {
|
2022-04-09 15:07:45 +00:00
|
|
|
$user = MediaWikiServices::getInstance()->getUserFactory()->newFromName( $accountName );
|
2020-09-16 09:15:38 +00:00
|
|
|
// A creatable username must exist to be passed to $logEntry->setPerformer(),
|
|
|
|
// so create the account.
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
|
|
|
$creator = $creator ?? $user;
|
|
|
|
|
2022-04-09 15:07:45 +00:00
|
|
|
$provider = new AbuseFilterPreAuthenticationProvider(
|
|
|
|
AbuseFilterServices::getVariableGeneratorFactory(),
|
|
|
|
AbuseFilterServices::getFilterRunnerFactory(),
|
|
|
|
new NullStatsdDataFactory(),
|
|
|
|
MediaWikiServices::getInstance()->getUserFactory()
|
|
|
|
);
|
2020-09-16 09:15:38 +00:00
|
|
|
$status = $provider->testForAccountCreation( $user, $creator, [] );
|
|
|
|
|
|
|
|
// 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( Title::makeTitle( NS_USER, $accountName ) );
|
|
|
|
$logEntry->setComment( 'Fooobarcomment' );
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
'4::userid' => $user->getId(),
|
|
|
|
] );
|
|
|
|
$logid = $logEntry->insert();
|
|
|
|
$logEntry->publish( $logid );
|
2022-06-28 20:23:42 +00:00
|
|
|
$status->value = $logid;
|
2020-09-16 09:15:38 +00:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
}
|