wiki.techinc.nl/tests/phpunit/unit/includes/user/TempUser/PatternTest.php
Kosta Harlan 2ed2467a7a
ApiQuerySiteinfo: Use TempUserConfig class
Why:

- Using TempUserConfig is more flexible as it doesn't require us to know
  about the internal details of the AutoCreateTempUser config object

What:

- Use TempUserConfig service instead of loading the AutoCreateTempUser
  object
- Remove properties from ApiQuerySiteInfo that don't already have
  getters. If someone has a need for more this information to be exposed
  in ApiQuerySiteInfo, they can be added in a separate patch
  - This seems compatible with the goals of T335532 which was about
    making it possible to discover via the API if an action would result
    in a temp account
- Implement __toString for Pattern, so that it can be used in
  ApiQuerySiteInfo

Bug: T335532
Change-Id: Ica84b3e9b9865b8b83a9e9e513c99cd2e47661c9
2024-06-13 22:11:27 +02:00

108 lines
2.4 KiB
PHP

<?php
namespace MediaWiki\Tests\User\TempUser;
use MediaWiki\User\TempUser\Pattern;
use PHPUnit\Framework\TestCase;
use RuntimeException;
/**
* @covers \MediaWiki\User\TempUser\Pattern
*/
class PatternTest extends TestCase {
public function testInvalid() {
$this->expectException( RuntimeException::class );
$pattern = new Pattern( 'test', 'test' );
$pattern->isMatch( 'test' );
}
public static function provideIsMatch() {
return [
'prefix mismatch' => [
'pattern' => '*$1',
'name' => 'Test',
'expected' => false,
],
'prefix match' => [
'pattern' => '*$1',
'name' => '*Some user',
'expected' => true,
],
'suffix only match' => [
'pattern' => '$1*',
'name' => 'Some user*',
'expected' => true,
],
'suffix only mismatch' => [
'pattern' => '$1*',
'name' => 'Some user',
'expected' => false,
],
'prefix and suffix match' => [
'pattern' => '*$1*',
'name' => '*Unregistered 123*',
'expected' => true,
],
'prefix and suffix mismatch' => [
'pattern' => '*$1*',
'name' => 'Unregistered 123*',
'expected' => false,
],
'prefix and suffix zero length match' => [
'pattern' => '*$1*',
'name' => '**',
'expected' => true,
],
'prefix and suffix overlapping' => [
'pattern' => '*$1*',
'name' => '*',
'expected' => false,
],
];
}
/** @dataProvider provideIsMatch */
public function testIsMatch( $stringPattern, $name, $expected ) {
$pattern = new Pattern( 'test', $stringPattern );
$this->assertSame( $expected, $pattern->isMatch( $name ) );
}
public static function provideGenerate() {
return [
'prefix' => [
'pattern' => 'x$1',
'serial' => 'y',
'expected' => 'xy',
],
'suffix' => [
'pattern' => '$1x',
'serial' => 'y',
'expected' => 'yx',
],
'both' => [
'pattern' => '*Unregistered $1*',
'serial' => '123',
'expected' => '*Unregistered 123*'
]
];
}
/** @dataProvider provideGenerate */
public function testGenerate( $stringPattern, $serial, $expected ) {
$pattern = new Pattern( 'test', $stringPattern );
$this->assertSame( $expected, $pattern->generate( $serial ) );
}
public function testGenerateWithYear() {
$pattern = new Pattern( 'test', '*$1*' );
$this->assertSame(
'*2000-123*',
$pattern->generate( '123', '2000' )
);
}
public function testToString() {
$pattern = new Pattern( 'test', '*$1' );
$this->assertSame( '*$1', (string)$pattern );
}
}