wiki.techinc.nl/tests/phpunit/unit/includes/user/TempUser/RealTempUserConfigTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.4 KiB
PHP
Raw Normal View History

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-10 23:40:27 +00:00
<?php
namespace MediaWiki\Tests\Unit\User\TempUser;
use BadMethodCallException;
use MediaWiki\Tests\MockDatabase;
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-10 23:40:27 +00:00
use MediaWiki\User\TempUser\RealTempUserConfig;
use MediaWikiUnitTestCase;
use Wikimedia\Rdbms\IExpression;
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-10 23:40:27 +00:00
use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\User\TempUser\RealTempUserConfig
*/
class RealTempUserConfigTest extends MediaWikiUnitTestCase {
/** @dataProvider provideMethodsThatThrowWhenTempUsersAreDisabled */
public function testMethodsThatThrowWhenTempUsersAreDisabled( $methodName ) {
$this->expectException( BadMethodCallException::class );
$objectUnderTest = $this->getMockBuilder( RealTempUserConfig::class )
->onlyMethods( [ 'isEnabled' ] )
->disableOriginalConstructor()
->getMock();
// Simulate that the AutoCreateTempUser config has 'enabled' as false.
$objectUnderTest->method( 'isEnabled' )
->willReturn( false );
$objectUnderTest->$methodName();
}
public static function provideMethodsThatThrowWhenTempUsersAreDisabled() {
return [
'getPlaceholderName' => [ 'getPlaceholderName' ],
'getMatchPatterns' => [ 'getMatchPatterns' ],
'getGeneratorPattern' => [ 'getGeneratorPattern' ],
];
}
public function testGetMatchConditionThrowsWhenTempUsersAreDisabled() {
$this->expectException( BadMethodCallException::class );
$objectUnderTest = $this->getMockBuilder( RealTempUserConfig::class )
->onlyMethods( [ 'isEnabled' ] )
->disableOriginalConstructor()
->getMock();
// Simulate that the AutoCreateTempUser config has 'enabled' as false.
$objectUnderTest->method( 'isEnabled' )
->willReturn( false );
$objectUnderTest->getMatchCondition( new MockDatabase, 'foo', IExpression::LIKE );
}
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-10 23:40:27 +00:00
/** @dataProvider provideIsEnabled */
public function testIsEnabled( $enabledValue ) {
// Get the object under test with the constructor disabled.
$objectUnderTest = $this->getMockBuilder( RealTempUserConfig::class )
->onlyMethods( [] )
->disableOriginalConstructor()
->getMock();
// Set $objectUnderTest->enabled to $enabledValue
$objectUnderTest = TestingAccessWrapper::newFromObject( $objectUnderTest );
$objectUnderTest->enabled = $enabledValue;
$this->assertSame( $enabledValue, $objectUnderTest->isEnabled() );
}
public static function provideIsEnabled() {
return [
'Auto creation is enabled' => [ true ],
'Auto creation is disabled' => [ false ],
];
}
public function testIsKnownWhenEnabledIsTrue() {
$realTempUserConfig = new RealTempUserConfig( [
'enabled' => true,
'actions' => [ 'edit' ],
'genPattern' => '',
'serialProvider' => '',
'serialMapping' => '',
'known' => false,
] );
$this->assertTrue( $realTempUserConfig->isKnown() );
}
public function testIsTempNameWhenKnownIsTrueAndEnabledIsFalse() {
$realTempUserConfig = new RealTempUserConfig( [
'enabled' => false,
'known' => true,
'actions' => [ 'edit' ],
'genPattern' => '~$1',
'serialProvider' => '',
'serialMapping' => '',
] );
$this->assertTrue( $realTempUserConfig->isTempName( '~2024-foo' ) );
}
public function testIsTempNameWhenKnownIsFalseAndEnabledIsFalse() {
$realTempUserConfig = new RealTempUserConfig( [
'enabled' => false,
'known' => false,
'actions' => [ 'edit' ],
'genPattern' => '',
'serialProvider' => '',
'serialMapping' => '',
'matchPattern' => [ '~$1' ],
] );
$this->assertFalse( $realTempUserConfig->isTempName( '~2024-foo' ) );
}
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-10 23:40:27 +00:00
}