wiki.techinc.nl/tests/phpunit/integration/includes/user/TempUser/TempUserTestTrait.php
Kosta Harlan 499277a67a
[temp accounts] Set expiration to 90 days
Why:

- We are unlikely to see good faith editing patterns with temp accounts
  that would require a full year

What:

- Set the default expiry for temp accounts to 90 days

Bug: T359653
Change-Id: Iae9dd0f73aceecfc9935b2b6019b035b1057eeb3
2024-06-20 10:43:23 +01:00

80 lines
2.5 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 {
/**
* Array of default configuration to use in tests.
*
* @return array
*/
private function getTempAccountConfigTestDefaults(): array {
return [
'expireAfterDays' => 90,
'notifyBeforeExpirationDays' => 10,
'actions' => [ 'edit' ],
'genPattern' => '~$1',
'reservedPattern' => '~$1',
'serialProvider' => [ 'type' => 'local', 'useYear' => true ],
'serialMapping' => [ 'type' => 'plain-numeric' ],
];
}
/**
* 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 {
$configOverrides['enabled'] = true;
$this->overrideConfigValue(
MainConfigNames::TempAccountNameAcquisitionThrottle,
[ 'count' => 0, 'seconds' => 86400 ]
);
$this->overrideConfigValue(
MainConfigNames::AutoCreateTempUser,
array_merge( $this->getTempAccountConfigTestDefaults(), $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 array $configOverrides Specify overrides to the default wgAutoCreateTempUser configuration
* setting (all values are the default except 'enabled' which is set to false).
* @since 1.42
*/
protected function disableAutoCreateTempUser( array $configOverrides = [] ): void {
$configOverrides['enabled'] = false;
$this->overrideConfigValue(
MainConfigNames::AutoCreateTempUser,
array_merge( $this->getTempAccountConfigTestDefaults(), $configOverrides )
);
}
/**
* 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 );
}