wiki.techinc.nl/tests/phpunit/unit/includes/user/TempUser/TempUserCreatorTest.php
Dreamy Jazz 7f588780a0 Support multiple matchPatterns in temp user autocreate config
Why:
* There is a need to update the generation and match pattern on
  WMF wikis to a new format that includes the year and starts with
  `~`. As such, the 'matchPattern' key needs to be updated.
* Removing the old 'matchPattern' from the wgAutoCreateTempUser
  config currently leaves existing temporary accounts as no longer
  recongnised as temporary accounts.
* Instead, the 'matchPattern' needs to be able to take an array of
  string patterns so that old patterns can still be used for matching.

What:
* Update the MainConfigSchama to indicate that 'matchPattern' in the
  wgAutoCreateTempUser config can be an array of strings.
* Add TempUserConfig::getMatchPatterns and deprecate TempUserConfig::
  getMatchPattern. This is needed because ::getMatchPattern was typed
  to only ever return one Pattern, which is no longer the case with this
  config change.
* Update the RealTempUserConfig to support multiple patterns defined in
  the 'matchPattern' key. The RealTempUserConfig::getMatchPattern method
  returns the pattern or first pattern if multiple are defined to allow
  time for existing usages of this deprecated method to be updated.
* Update the RealTempUserConfig to rely on other methods instead of checking
  object property values where possible (e.g. use ::isEnabled instead of
  checking $this->enabled) to allow easier unit testing.
* Update UserSelectQueryBuilder and ChangesListSpecialPage to use TempUserConfig
  ::getMatchPatterns instead of ::getMatchPattern.
* Update mediawiki.util/util.js to be able to parse the 'matchPattern' value
  when it is an array of strings.
* Update maintenance/userOptions.php to use ::getMatchPatterns instead of
  ::getMatchPattern.
* Add and update unit and integration tests for the new code, as well as
  expanding coverage over existing code that was indirectly affected.

Bug: T354619
Change-Id: I3763daefe4dc7c76370bd934fb20452591c9c762
2024-01-17 11:24:26 +00:00

52 lines
1.7 KiB
PHP

<?php
namespace MediaWiki\Tests\Unit\User\TempUser;
use MediaWiki\Permissions\Authority;
use MediaWiki\Tests\Unit\MockServiceDependenciesTrait;
use MediaWiki\User\TempUser\RealTempUserConfig;
use MediaWiki\User\TempUser\TempUserCreator;
use MediaWikiUnitTestCase;
/**
* @covers \MediaWiki\User\TempUser\TempUserCreator
*/
class TempUserCreatorTest extends MediaWikiUnitTestCase {
use MockServiceDependenciesTrait;
/** @dataProvider providePassThroughMethods */
public function testPassThroughMethods( $methodName, $mockArguments = [] ) {
// Tests methods in the TempUserCreator class that pass through to
// the TempUserConfig methods with the same name.
$mockTempUserConfig = $this->createMock( RealTempUserConfig::class );
$mockTempUserConfig->expects( $this->once() )
->method( $methodName )
->with( ...$mockArguments );
$objectUnderTest = $this->newServiceInstance( TempUserCreator::class, [
'config' => $mockTempUserConfig
] );
$objectUnderTest->$methodName( ...$mockArguments );
}
public static function providePassThroughMethods() {
return [
'isEnabled' => [ 'isEnabled' ],
'isAutoCreateAction' => [ 'isAutoCreateAction', [ 'mock-action' ] ],
'isTempName' => [ 'isTempName', [ 'mock-name' ] ],
'isReservedName' => [ 'isReservedName', [ 'mock-name' ] ],
'getPlaceholderName' => [ 'getPlaceholderName' ],
'getMatchPattern' => [ 'getMatchPattern' ],
'getMatchPatterns' => [ 'getMatchPatterns' ],
'getExpireAfterDays' => [ 'getExpireAfterDays' ],
'getNotifyBeforeExpirationDays' => [ 'getNotifyBeforeExpirationDays' ],
];
}
public function testShouldAutoCreate() {
$this->testPassThroughMethods(
'shouldAutocreate',
[ $this->createMock( Authority::class ), 'mock-action' ]
);
}
}