Merge "Mocha tests: Support language links to en-x-piglatin"
[mediawiki.git] / tests / phpunit / includes / user / ExternalUserNamesTest.php
blobcdc741321c18fec8528854b9183b2b0b564d958f
1 <?php
3 use MediaWiki\Tests\Unit\DummyServicesTrait;
4 use MediaWiki\Title\Title;
5 use MediaWiki\User\ExternalUserNames;
7 /**
8 * @covers \MediaWiki\User\ExternalUserNames
9 * @group Database
11 class ExternalUserNamesTest extends MediaWikiIntegrationTestCase {
12 use DummyServicesTrait;
14 public static function provideGetUserLinkTitle() {
15 return [
17 'Valid user name from known import source',
18 'valid:>User1',
19 Title::makeTitle( NS_MAIN, ':User:User1', '', 'valid' )
22 'Valid user name that looks like an import source, from known import source',
23 'valid:valid:>User1',
24 Title::makeTitle( NS_MAIN, 'valid::User:User1', '', 'valid' )
27 'Local IP address',
28 '127.0.0.1',
29 Title::makeTitle( NS_SPECIAL, 'Contributions/127.0.0.1', '', '' )
32 'Valid user name from unknown import source',
33 'invalid:>User1',
34 null
37 'Corrupt local user name with linebreak',
38 "Foo\nBar",
39 null
42 'Corrupt local user name with terminal underscore',
43 'Barf_',
44 null
47 'Corrupt local user name with initial lowercase',
48 'abcd',
49 null
52 'Corrupt local user name with slash',
53 'For/Bar',
54 null
57 'Corrupt local user name with octothorpe',
58 'For#Bar',
59 null
64 /**
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 );
74 $this->assertEquals(
75 $expected,
76 ExternalUserNames::getUserLinkTitle( $username ),
77 $caseDescription
81 public static function provideApplyPrefix() {
82 return [
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' ],
92 /**
93 * @covers \MediaWiki\User\ExternalUserNames::applyPrefix
94 * @dataProvider provideApplyPrefix
96 public function testApplyPrefix( $username, $prefix, $expected ) {
97 $externalUserNames = new ExternalUserNames( $prefix, true );
99 $this->assertSame(
100 $expected,
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() {
123 return [
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 );
137 $this->assertSame(
138 $expected,
139 $externalUserNames->addPrefix( $username )
143 public static function provideIsExternal() {
144 return [
145 [ 'User1', false ],
146 [ '>User1', true ],
147 [ 'prefix>User1', true ],
148 [ 'prefix:>User1', true ],
153 * @covers \MediaWiki\User\ExternalUserNames::isExternal
154 * @dataProvider provideIsExternal
156 public function testIsExternal( $username, $expected ) {
157 $this->assertSame(
158 $expected,
159 ExternalUserNames::isExternal( $username )
163 public static function provideGetLocal() {
164 return [
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 ) {
177 $this->assertSame(
178 $expected,
179 ExternalUserNames::getLocal( $username )