mediawiki-extensions-AbuseF.../tests/phpunit/AbuseFilterCreateAccountTestTrait.php
Daimona Eaytoy 6c8a29698b Add test traits for uploads and account creation
Ideally, this might live in MediaWikiIntegrationTestCase. For the
createaccount one, AuthManager should also provide a method to log the
creation, because currently we are forced to copypaste that code here.

 - Add the missing tests for 'upload' in RCVariableGenerator, and adjust
the existing ones (delete file afterwards, more tablesUsed, use the
right extension).

 - Exclude from the coverage report a couple of lines which should
theoretically be unreachable. Escalate logging to WARN level, where it's
more likely to be spotted.

 - Remove an unused method (RCVariableGenerator::newFromID). This denies
   the need to maintain and cover it. We also don't want this generator
   to act as a factory.

Overall, this change brings the coverage for RCVariableGenerator to 100%

Bug: T201193
Change-Id: I425c3d9f6800f74eb6e4eda483b90cfb3bbbcb51
2020-10-04 13:16:58 +00:00

44 lines
1.4 KiB
PHP

<?php
/**
* 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
) : StatusValue {
$user = User::newFromName( $accountName );
// A creatable username must exist to be passed to $logEntry->setPerformer(),
// so create the account.
$user->addToDatabase();
$creator = $creator ?? $user;
$provider = new AbuseFilterPreAuthenticationProvider();
$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 );
return $status;
}
}