wiki.techinc.nl/tests/phpunit/unit/includes/user/TempUser/RealTempUserConfigTest.php
Thalia 578f5ea339 RealTempUserConfig: Ensure match pattern is available when known
Why:

* When temporary accounts are known, the match patterns need to
  be available. If no match patterns are set, the match pattern
  is set to an array containing the generate pattern. Therefore
  the generate pattern must also be available if temporary
  accounts are known.
* This was not the case before this commit, and certain settings
  caused an error to be thrown from isTempName.

What:

* Use the generate pattern for the match patterns if temp accounts
  are known and no match patterns are set.
* Update the tests to use a generate pattern but no match pattern
  (the same as the default config).

Change-Id: Iadfbed40b14378a9309e8d7afab6064e25480514
2024-07-19 15:08:15 +01:00

106 lines
3.4 KiB
PHP

<?php
namespace MediaWiki\Tests\Unit\User\TempUser;
use BadMethodCallException;
use MediaWiki\Tests\MockDatabase;
use MediaWiki\User\TempUser\RealTempUserConfig;
use MediaWikiUnitTestCase;
use Wikimedia\Rdbms\IExpression;
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 );
}
/** @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' ) );
}
}