Why: * The default value of wgAutoCreateTempUser has not changed since the decision to use a different prefix for temporary accounts (T332805). * The default needs to be updated to reduce the number of overrides in operations/mediawiki-config and also to make the development experience more consistent with what is happening on WMF production. What: * Update the wgAutoCreateTempUser default in the following ways: ** Set expireAfterDays as 365 ** Set notifyBeforeExpirationDays as 10 ** Set genPattern and reservedPattern to '~$1' ** Set matchPattern to null, which will mean that the genPattern is used as the value. * Update RealTempUserConfig::getPlaceholderName to add the year to the placeholder name so that if the match pattern includes the first digit of the year, then the placeholder name still is considered a valid temporary account username. * Replace modifications of the wgAutoCreateTempUser config in integration tests with a use of the TempUserTestTrait to make the code cleaner and make it easier to find tests that relies on the values in wgAutoCreateTempUser. * Update multiple tests to handle the new defaults for the config. Bug: T359335 Change-Id: Ifa5a0123cd915bdb7c87e473c51fb93321622f12
87 lines
2 KiB
PHP
87 lines
2 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\Api;
|
|
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
|
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
|
|
use MediaWiki\User\TempUser\TempUserCreator;
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
|
|
|
/**
|
|
* @group API
|
|
* @group Database
|
|
* @group medium
|
|
*
|
|
* @covers \ApiAcquireTempUserName
|
|
*/
|
|
class ApiAcquireTempUserNameTest extends ApiTestCase {
|
|
use MockAuthorityTrait;
|
|
use TempUserTestTrait;
|
|
|
|
public function testExecuteDiesWhenNotEnabled() {
|
|
$this->disableAutoCreateTempUser();
|
|
$this->expectApiErrorCode( 'tempuserdisabled' );
|
|
|
|
$this->doApiRequestWithToken( [
|
|
"action" => "acquiretempusername",
|
|
] );
|
|
}
|
|
|
|
public function testExecuteDiesWhenUserIsRegistered() {
|
|
$this->enableAutoCreateTempUser();
|
|
$this->expectApiErrorCode( 'alreadyregistered' );
|
|
|
|
$this->doApiRequestWithToken(
|
|
[
|
|
'action' => 'acquiretempusername',
|
|
],
|
|
null,
|
|
$this->mockRegisteredUltimateAuthority()
|
|
);
|
|
}
|
|
|
|
public function testExecuteDiesWhenNameCannotBeAcquired() {
|
|
$mockTempUserCreator = $this->createMock( TempUserCreator::class );
|
|
$mockTempUserCreator->method( 'isEnabled' )
|
|
->willReturn( true );
|
|
$mockTempUserCreator->method( 'acquireAndStashName' )
|
|
->willReturn( null );
|
|
$this->overrideMwServices(
|
|
null,
|
|
[
|
|
'TempUserCreator' => static function () use ( $mockTempUserCreator ) {
|
|
return $mockTempUserCreator;
|
|
}
|
|
]
|
|
);
|
|
$this->expectApiErrorCode( 'tempuseracquirefailed' );
|
|
|
|
$this->doApiRequestWithToken(
|
|
[
|
|
'action' => 'acquiretempusername',
|
|
],
|
|
null,
|
|
$this->mockAnonUltimateAuthority()
|
|
);
|
|
}
|
|
|
|
public function testExecuteForSuccessfulCall() {
|
|
ConvertibleTimestamp::setFakeTime( '20240405060708' );
|
|
$this->enableAutoCreateTempUser( [
|
|
'genPattern' => '~$1',
|
|
] );
|
|
|
|
$this->assertArrayEquals(
|
|
[ 'acquiretempusername' => '~2024-1' ],
|
|
$this->doApiRequestWithToken(
|
|
[
|
|
'action' => 'acquiretempusername',
|
|
],
|
|
null,
|
|
$this->mockAnonUltimateAuthority()
|
|
)[0],
|
|
true,
|
|
true
|
|
);
|
|
}
|
|
}
|