| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\User\TempUser; |
| 4 |
|
| 5 |
use MediaWiki\User\TempUser\Pattern; |
| 6 |
use PHPUnit\Framework\TestCase; |
| 7 |
use RuntimeException; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class PatternTest extends TestCase { |
| 13 |
public function testInvalid() { |
| 14 |
$this->expectException( RuntimeException::class ); |
| 15 |
$pattern = new Pattern( 'test', 'test' ); |
| 16 |
$pattern->isMatch( 'test' ); |
| 17 |
} |
| 18 |
|
| 19 |
public static function provideIsMatch() { |
| 20 |
return [ |
| 21 |
'prefix mismatch' => [ |
| 22 |
'pattern' => '*$1', |
| 23 |
'name' => 'Test', |
| 24 |
'expected' => false, |
| 25 |
], |
| 26 |
'prefix match' => [ |
| 27 |
'pattern' => '*$1', |
| 28 |
'name' => '*Some user', |
| 29 |
'expected' => true, |
| 30 |
], |
| 31 |
'suffix only match' => [ |
| 32 |
'pattern' => '$1*', |
| 33 |
'name' => 'Some user*', |
| 34 |
'expected' => true, |
| 35 |
], |
| 36 |
'suffix only mismatch' => [ |
| 37 |
'pattern' => '$1*', |
| 38 |
'name' => 'Some user', |
| 39 |
'expected' => false, |
| 40 |
], |
| 41 |
'prefix and suffix match' => [ |
| 42 |
'pattern' => '*$1*', |
| 43 |
'name' => '*Unregistered 123*', |
| 44 |
'expected' => true, |
| 45 |
], |
| 46 |
'prefix and suffix mismatch' => [ |
| 47 |
'pattern' => '*$1*', |
| 48 |
'name' => 'Unregistered 123*', |
| 49 |
'expected' => false, |
| 50 |
], |
| 51 |
'prefix and suffix zero length match' => [ |
| 52 |
'pattern' => '*$1*', |
| 53 |
'name' => '**', |
| 54 |
'expected' => true, |
| 55 |
], |
| 56 |
'prefix and suffix overlapping' => [ |
| 57 |
'pattern' => '*$1*', |
| 58 |
'name' => '*', |
| 59 |
'expected' => false, |
| 60 |
], |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public function testIsMatch( $stringPattern, $name, $expected ) { |
| 66 |
$pattern = new Pattern( 'test', $stringPattern ); |
| 67 |
$this->assertSame( $expected, $pattern->isMatch( $name ) ); |
| 68 |
} |
| 69 |
|
| 70 |
public static function provideGenerate() { |
| 71 |
return [ |
| 72 |
'prefix' => [ |
| 73 |
'pattern' => 'x$1', |
| 74 |
'serial' => 'y', |
| 75 |
'expected' => 'xy', |
| 76 |
], |
| 77 |
'suffix' => [ |
| 78 |
'pattern' => '$1x', |
| 79 |
'serial' => 'y', |
| 80 |
'expected' => 'yx', |
| 81 |
], |
| 82 |
'both' => [ |
| 83 |
'pattern' => '*Unregistered $1*', |
| 84 |
'serial' => '123', |
| 85 |
'expected' => '*Unregistered 123*' |
| 86 |
] |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
public function testGenerate( $stringPattern, $serial, $expected ) { |
| 92 |
$pattern = new Pattern( 'test', $stringPattern ); |
| 93 |
$this->assertSame( $expected, $pattern->generate( $serial ) ); |
| 94 |
} |
| 95 |
|
| 96 |
public function testGenerateWithYear() { |
| 97 |
$pattern = new Pattern( 'test', '*$1*' ); |
| 98 |
$this->assertSame( |
| 99 |
'*2000-123*', |
| 100 |
$pattern->generate( '123', '2000' ) |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
public function testToString() { |
| 105 |
$pattern = new Pattern( 'test', '*$1' ); |
| 106 |
$this->assertSame( '*$1', (string)$pattern ); |
| 107 |
} |
| 108 |
} |