Why: * The temporary account configuration settings, which are controlled via wgAutoCreateTempUser have many options and so defining the config for a test is difficult without keeping a private array for the default array value. * Providing methods to easily disable and enable temporary account creation in a one-line call is useful to fix existing tests for temporary account creation. What: * Add the trait named TempUserTestTrait which provides two methods that disable and enable temporary account autocreation. * This trait is designed for use in integration tests as it modifies configuration values. * The ::enableAutoCreateTempUser method optionally takes an array that can be used to override the default wgAutoCreateTempUser configuration. * The ::disableAutoCreateTempUser method optionally takes a value for the reservedPattern key as this is still used if the 'enabled' key is set to false. Bug: T352693 Change-Id: I21c5c7d61370b9ce96059341e0744abc61545e09
65 lines
2 KiB
PHP
65 lines
2 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::AutoCreateTempUser,
|
|
array_merge( [
|
|
'enabled' => true,
|
|
'expireAfterDays' => null,
|
|
'actions' => [ 'edit' ],
|
|
'genPattern' => '*Unregistered $1',
|
|
'matchPattern' => '*$1',
|
|
'serialProvider' => [ 'type' => 'local' ],
|
|
'serialMapping' => [ 'type' => 'plain-numeric' ],
|
|
], $configOverrides )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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 );
|
|
}
|