3 use MediaWiki\Tests\Unit\DummyServicesTrait
;
4 use MediaWiki\Title\Title
;
5 use MediaWiki\User\ExternalUserNames
;
8 * @covers \MediaWiki\User\ExternalUserNames
11 class ExternalUserNamesTest
extends MediaWikiIntegrationTestCase
{
12 use DummyServicesTrait
;
14 public static function provideGetUserLinkTitle() {
17 'Valid user name from known import source',
19 Title
::makeTitle( NS_MAIN
, ':User:User1', '', 'valid' )
22 'Valid user name that looks like an import source, from known import source',
24 Title
::makeTitle( NS_MAIN
, 'valid::User:User1', '', 'valid' )
29 Title
::makeTitle( NS_SPECIAL
, 'Contributions/127.0.0.1', '', '' )
32 'Valid user name from unknown import source',
37 'Corrupt local user name with linebreak',
42 'Corrupt local user name with terminal underscore',
47 'Corrupt local user name with initial lowercase',
52 'Corrupt local user name with slash',
57 'Corrupt local user name with octothorpe',
65 * @covers \MediaWiki\User\ExternalUserNames::getUserLinkTitle
66 * @dataProvider provideGetUserLinkTitle
68 public function testGetUserLinkTitle( $caseDescription, $username, $expected ) {
69 $this->setContentLang( 'en' );
71 $interwikiLookup = $this->getDummyInterwikiLookup( [ 'valid' ] );
72 $this->setService( 'InterwikiLookup', $interwikiLookup );
76 ExternalUserNames
::getUserLinkTitle( $username ),
81 public static function provideApplyPrefix() {
83 [ 'User1', 'prefix', 'prefix>User1' ],
84 [ 'User1', 'prefix:>', 'prefix>User1' ],
85 [ 'User1', 'prefix:', 'prefix>User1' ],
86 [ 'user1', 'prefix', 'prefix>user1' ],
87 [ '0', 'prefix', 'prefix>0' ],
88 [ 'Unknown user', 'prefix', 'Unknown user' ],
93 * @covers \MediaWiki\User\ExternalUserNames::applyPrefix
94 * @dataProvider provideApplyPrefix
96 public function testApplyPrefix( $username, $prefix, $expected ) {
97 $externalUserNames = new ExternalUserNames( $prefix, true );
101 $externalUserNames->applyPrefix( $username )
106 * @covers \MediaWiki\User\ExternalUserNames::applyPrefix
108 public function testApplyPrefix_existingUser() {
109 $testName = $this->getTestUser()->getUser()->getName();
110 $testName2 = lcfirst( $testName );
111 $this->assertNotSame( $testName, $testName2 );
113 $externalUserNames = new ExternalUserNames( 'p', false );
114 $this->assertSame( "p>$testName", $externalUserNames->applyPrefix( $testName ) );
115 $this->assertSame( "p>$testName2", $externalUserNames->applyPrefix( $testName2 ) );
117 $externalUserNames = new ExternalUserNames( 'p', true );
118 $this->assertSame( $testName, $externalUserNames->applyPrefix( $testName ) );
119 $this->assertSame( $testName2, $externalUserNames->applyPrefix( $testName2 ) );
122 public static function provideAddPrefix() {
124 [ 'User1', 'prefix', 'prefix>User1' ],
125 [ 'User2', 'prefix2', 'prefix2>User2' ],
126 [ 'User3', 'prefix3', 'prefix3>User3' ],
131 * @covers \MediaWiki\User\ExternalUserNames::addPrefix
132 * @dataProvider provideAddPrefix
134 public function testAddPrefix( $username, $prefix, $expected ) {
135 $externalUserNames = new ExternalUserNames( $prefix, true );
139 $externalUserNames->addPrefix( $username )
143 public static function provideIsExternal() {
147 [ 'prefix>User1', true ],
148 [ 'prefix:>User1', true ],
153 * @covers \MediaWiki\User\ExternalUserNames::isExternal
154 * @dataProvider provideIsExternal
156 public function testIsExternal( $username, $expected ) {
159 ExternalUserNames
::isExternal( $username )
163 public static function provideGetLocal() {
165 [ 'User1', 'User1' ],
166 [ '>User2', 'User2' ],
167 [ 'prefix>User3', 'User3' ],
168 [ 'prefix:>User4', 'User4' ],
173 * @covers \MediaWiki\User\ExternalUserNames::getLocal
174 * @dataProvider provideGetLocal
176 public function testGetLocal( $username, $expected ) {
179 ExternalUserNames
::getLocal( $username )