wiki.techinc.nl/tests/phpunit/integration/includes/user/TempUser/TempUserTestTrait.php
Kosta Harlan 6efd008f33 TempAccounts: Rate limit acquisition of temp account names
Why:

- We don't want to allow unlimited acquisition of temp account names.
  These should be rate limited in similar way to how we limit the
  creation of temp accounts

What:

- Provide a TempAccountNameAcquisitionThrottle, and use it in the
  acquireName() method
- Set a default that is 10 times the limit of
  TempAccountNameCreationThrottle

Depends-On: If660aad1d0f04f366414084aff3f88484a19d416
Bug: T343101
Change-Id: I99d5973498a89ac227847de5837c0a8e895c28fb
2024-04-23 13:33:57 +00:00

71 lines
2.3 KiB
PHP

<?php
namespace MediaWiki\Tests\User\TempUser;
use MediaWiki\MainConfigNames;
/**
* Helper trait for defining the temporary user configuration settings for tests.
* This can only be used for test classes that extend MediaWikiIntegrationTestCase.
*
* @stable to use
* @since 1.42
*/
trait TempUserTestTrait {
/**
* Loads configuration that enables the automatic creation of temporary accounts using the defaults
* for the generation pattern and match pattern.
*
* @param array $configOverrides Specify overrides to the default wgAutoCreateTempUser configuration
* setting (all values are the default except 'enabled' which is set to true).
* @since 1.42
*/
protected function enableAutoCreateTempUser( array $configOverrides = [] ): void {
$this->overrideConfigValue(
MainConfigNames::TempAccountNameAcquisitionThrottle,
[ 'count' => 0, 'seconds' => 86400 ]
);
$this->overrideConfigValue(
MainConfigNames::AutoCreateTempUser,
array_merge( [
'enabled' => true,
'expireAfterDays' => 365,
'notifyBeforeExpirationDays' => 10,
'actions' => [ 'edit' ],
'genPattern' => '~$1',
'reservedPattern' => '~$1',
'serialProvider' => [ 'type' => 'local', 'useYear' => true ],
'serialMapping' => [ 'type' => 'plain-numeric' ],
], $configOverrides )
);
$this->setGroupPermissions( '*', 'createaccount', true );
}
/**
* Disables the automatic creation of temporary accounts for the test.
*
* This is done to avoid exceptions when a test or the code being tested creates an actor for an IP address.
*
* @param ?string $reservedPattern The value for the reservedPattern key. Specify null to not include the key. Default is to not
* include the key.
* @since 1.42
*/
protected function disableAutoCreateTempUser( ?string $reservedPattern = null ): void {
$config = [ 'enabled' => false ];
if ( $reservedPattern !== null ) {
$config['reservedPattern'] = $reservedPattern;
}
$this->overrideConfigValue( MainConfigNames::AutoCreateTempUser, $config );
}
/**
* Defined to ensure that the class has the overrideConfigValue method that we can use.
*
* @see \MediaWikiIntegrationTestCase::overrideConfigValue
*
* @param string $key
* @param mixed $value
*/
abstract protected function overrideConfigValue( string $key, $value );
}