| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 4 |
use MediaWiki\Title\Title; |
| 5 |
use MediaWiki\User\ExternalUserNames; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class ExternalUserNamesTest extends MediaWikiIntegrationTestCase { |
| 12 |
use DummyServicesTrait; |
| 13 |
|
| 14 |
public static function provideGetUserLinkTitle() { |
| 15 |
return [ |
| 16 |
[ |
| 17 |
'Valid user name from known import source', |
| 18 |
'valid:>User1', |
| 19 |
Title::makeTitle( NS_MAIN, ':User:User1', '', 'valid' ) |
| 20 |
], |
| 21 |
[ |
| 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' ) |
| 25 |
], |
| 26 |
[ |
| 27 |
'Local IP address', |
| 28 |
'127.0.0.1', |
| 29 |
Title::makeTitle( NS_SPECIAL, 'Contributions/127.0.0.1', '', '' ) |
| 30 |
], |
| 31 |
[ |
| 32 |
'Valid user name from unknown import source', |
| 33 |
'invalid:>User1', |
| 34 |
null |
| 35 |
], |
| 36 |
[ |
| 37 |
'Corrupt local user name with linebreak', |
| 38 |
"Foo\nBar", |
| 39 |
null |
| 40 |
], |
| 41 |
[ |
| 42 |
'Corrupt local user name with terminal underscore', |
| 43 |
'Barf_', |
| 44 |
null |
| 45 |
], |
| 46 |
[ |
| 47 |
'Corrupt local user name with initial lowercase', |
| 48 |
'abcd', |
| 49 |
null |
| 50 |
], |
| 51 |
[ |
| 52 |
'Corrupt local user name with slash', |
| 53 |
'For/Bar', |
| 54 |
null |
| 55 |
], |
| 56 |
[ |
| 57 |
'Corrupt local user name with octothorpe', |
| 58 |
'For#Bar', |
| 59 |
null |
| 60 |
], |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function testGetUserLinkTitle( $caseDescription, $username, $expected ) { |
| 69 |
$this->setContentLang( 'en' ); |
| 70 |
|
| 71 |
$interwikiLookup = $this->getDummyInterwikiLookup( [ 'valid' ] ); |
| 72 |
$this->setService( 'InterwikiLookup', $interwikiLookup ); |
| 73 |
|
| 74 |
$this->assertEquals( |
| 75 |
$expected, |
| 76 |
ExternalUserNames::getUserLinkTitle( $username ), |
| 77 |
$caseDescription |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 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' ], |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function testApplyPrefix( $username, $prefix, $expected ) { |
| 97 |
$externalUserNames = new ExternalUserNames( $prefix, true ); |
| 98 |
|
| 99 |
$this->assertSame( |
| 100 |
$expected, |
| 101 |
$externalUserNames->applyPrefix( $username ) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function testApplyPrefix_existingUser() { |
| 109 |
$testName = $this->getTestUser()->getUser()->getName(); |
| 110 |
$testName2 = lcfirst( $testName ); |
| 111 |
$this->assertNotSame( $testName, $testName2 ); |
| 112 |
|
| 113 |
$externalUserNames = new ExternalUserNames( 'p', false ); |
| 114 |
$this->assertSame( "p>$testName", $externalUserNames->applyPrefix( $testName ) ); |
| 115 |
$this->assertSame( "p>$testName2", $externalUserNames->applyPrefix( $testName2 ) ); |
| 116 |
|
| 117 |
$externalUserNames = new ExternalUserNames( 'p', true ); |
| 118 |
$this->assertSame( $testName, $externalUserNames->applyPrefix( $testName ) ); |
| 119 |
$this->assertSame( $testName2, $externalUserNames->applyPrefix( $testName2 ) ); |
| 120 |
} |
| 121 |
|
| 122 |
public static function provideAddPrefix() { |
| 123 |
return [ |
| 124 |
[ 'User1', 'prefix', 'prefix>User1' ], |
| 125 |
[ 'User2', 'prefix2', 'prefix2>User2' ], |
| 126 |
[ 'User3', 'prefix3', 'prefix3>User3' ], |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function testAddPrefix( $username, $prefix, $expected ) { |
| 135 |
$externalUserNames = new ExternalUserNames( $prefix, true ); |
| 136 |
|
| 137 |
$this->assertSame( |
| 138 |
$expected, |
| 139 |
$externalUserNames->addPrefix( $username ) |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
public static function provideIsExternal() { |
| 144 |
return [ |
| 145 |
[ 'User1', false ], |
| 146 |
[ '>User1', true ], |
| 147 |
[ 'prefix>User1', true ], |
| 148 |
[ 'prefix:>User1', true ], |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function testIsExternal( $username, $expected ) { |
| 157 |
$this->assertSame( |
| 158 |
$expected, |
| 159 |
ExternalUserNames::isExternal( $username ) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
public static function provideGetLocal() { |
| 164 |
return [ |
| 165 |
[ 'User1', 'User1' ], |
| 166 |
[ '>User2', 'User2' ], |
| 167 |
[ 'prefix>User3', 'User3' ], |
| 168 |
[ 'prefix:>User4', 'User4' ], |
| 169 |
]; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function testGetLocal( $username, $expected ) { |
| 177 |
$this->assertSame( |
| 178 |
$expected, |
| 179 |
ExternalUserNames::getLocal( $username ) |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
} |