Debian Codegraph @

tests/phpunit/unit/includes/user/UserNameUtilsTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Tests\Unit\DummyServicesTrait;
5 use MediaWiki\User\TempUser\RealTempUserConfig;
6 use MediaWiki\User\UserRigorOptions;
7 use Psr\Log\LogLevel;
8
9 /**
10 * @covers \MediaWiki\User\UserNameUtils
11 * @author DannyS712
12 */
13 class UserNameUtilsTest extends MediaWikiUnitTestCase {
14 use DummyServicesTrait;
15
16 /**
17 * @dataProvider provideIsValid
18 * @covers \MediaWiki\User\UserNameUtils::isValid
19 */
20 public function testIsValid( string $name, bool $result ) {
21 $this->assertSame(
22 $result,
23 $this->getDummyUserNameUtils()->isValid( $name )
24 );
25 }
26
27 public static function provideIsValid() {
28 return [
29 'Empty string' => [ '', false ],
30 'Blank space' => [ ' ', false ],
31 'Starts with small letter' => [ 'abcd', false ],
32 'Contains slash' => [ 'Ab/cd', false ],
33 'Whitespace' => [ 'Ab cd', true ],
34 'IP' => [ '192.168.1.1', false ],
35 'IP dash range' => [ '111.222.333.444-555.666.777.888', false ],
36 'IP range' => [ '116.17.184.5/32', false ],
37 'IPv6 range' => [ '::e:f:2001/96', false ],
38 'Reserved Namespace' => [ 'User:Abcd', false ],
39 'Starts with Numbers' => [ '12abcd232', true ],
40 'Start with ? mark' => [ '?abcd', true ],
41 'Start with #' => [ '#abcd', false ],
42 ' Mixed scripts' => [ 'Abcdകഖഗഘ', true ],
43 'ZWNJ- Format control character' => [ 'ജോസ്‌തോമസ്', false ],
44 ' Ideographic space' => [ 'Ab cd', false ],
45 'Looks too much like an IPv4 address (1)' => [ '300.300.300.300', false ],
46 'Looks too much like an IPv4 address (2)' => [ '302.113.311.900', false ],
47 'Reserved for usage by UseMod for cloaked logged-out users' => [ '203.0.113.xxx', false ],
48 'Disallowed characters' => [ "\u{E000}", false ],
49 ];
50 }
51
52 /**
53 * @dataProvider provideIsUsable
54 * @covers \MediaWiki\User\UserNameUtils::isUsable
55 */
56 public function testIsUsable( string $name, bool $result ) {
57 $utils = $this->getDummyUserNameUtils( [
58 MainConfigNames::ReservedUsernames => [
59 'MediaWiki default',
60 'msg:reserved-user'
61 ],
62 'textFormatter' => $this->getDummyTextFormatter(),
63 ] );
64 $this->assertSame(
65 $result,
66 $utils->isUsable( $name )
67 );
68 }
69
70 public static function provideIsUsable() {
71 return [
72 'Only valid user names are creatable' => [ '', false ],
73 'Reserved names cannot be used' => [ 'MediaWiki default', false ],
74 'Names can also be reserved via msg: ' => [ 'reserved-user', false ],
75 'User names with no issues can be used' => [ 'FooBar', true ],
76 'Reserved temp users' => [ '!Unregistered 1234', false ],
77 'Real temp users' => [ '*Unregistered 1234', true ],
78 ];
79 }
80
81 public function testIsUsableForTemporaryAccountWhenFeatureIsKnownButDisabled() {
82 $utils = $this->getDummyUserNameUtils( [
83 'tempUserConfig' => new RealTempUserConfig( [
84 'enabled' => false,
85 'known' => true,
86 'expireAfterDays' => null,
87 'actions' => [ 'edit' ],
88 'serialProvider' => [ 'type' => 'local' ],
89 'serialMapping' => [ 'type' => 'plain-numeric' ],
90 'reservedPattern' => '!$1',
91 'matchPattern' => '*$1',
92 'genPattern' => '*Unregistered $1'
93 ] ),
94 ] );
95 $this->assertFalse(
96 $utils->isUsable( '*Unregistered 1234' ),
97 '::isUsable should return false for a valid temporary account ' .
98 'when TempUserConfig::isEnabled is false and TempUserConfig::isKnown is true'
99 );
100 }
101
102 /**
103 * @covers \MediaWiki\User\UserNameUtils::isCreatable
104 */
105 public function testIsCreatable() {
106 $logger = new TestLogger( true, static function ( $message ) {
107 $message = str_replace(
108 'MediaWiki\\User\\UserNameUtils::isCreatable: ',
109 '',
110 $message
111 );
112 return $message;
113 } );
114 $utils = $this->getDummyUserNameUtils( [
115 'logger' => $logger,
116 ] );
117
118 $longUserName = str_repeat( 'q', 1000 );
119 $this->assertFalse(
120 $utils->isCreatable( $longUserName ),
121 'longUserName is too long'
122 );
123 $this->assertSame( [
124 [ LogLevel::DEBUG, "'$longUserName' uncreatable due to length" ],
125 ], $logger->getBuffer() );
126 $logger->clearBuffer();
127
128 $atUserName = 'Foo@Bar';
129 $this->assertFalse(
130 $utils->isCreatable( $atUserName ),
131 'User name contains invalid character'
132 );
133 $this->assertSame( [
134 [ LogLevel::DEBUG, "'$atUserName' uncreatable due to wgInvalidUsernameCharacters" ],
135 ], $logger->getBuffer() );
136 $logger->clearBuffer();
137
138 $this->assertTrue(
139 $utils->isCreatable( 'FooBar' ),
140 'User names with no issues can be created'
141 );
142
143 $tempUserName = '*Unregistered 1234';
144 $this->assertFalse(
145 $utils->isCreatable( $tempUserName ),
146 'UI account creation with the temp user prefix needs to be prevented'
147 );
148 }
149
150 /**
151 * @dataProvider provideGetCanonical
152 * @covers \MediaWiki\User\UserNameUtils::getCanonical
153 */
154 public function testGetCanonical( string $name, array $expectedArray ) {
155 $utils = $this->getDummyUserNameUtils();
156 foreach ( $expectedArray as $validate => $expected ) {
157 $this->assertSame(
158 $expected,
159 $utils->getCanonical( $name, $validate ),
160 "Validating '$name' with level '$validate' should be '$expected'"
161 );
162 }
163 }
164
165 public static function provideGetCanonical() {
166 return [
167 'Normal name' => [
168 'Normal name',
169 [
170 UserRigorOptions::RIGOR_CREATABLE => 'Normal name',
171 UserRigorOptions::RIGOR_USABLE => 'Normal name',
172 UserRigorOptions::RIGOR_VALID => 'Normal name',
173 UserRigorOptions::RIGOR_NONE => 'Normal name'
174 ]
175 ],
176 'Leading space' => [
177 ' Leading space',
178 [ UserRigorOptions::RIGOR_CREATABLE => 'Leading space' ]
179 ],
180 'Trailing space' => [
181 'Trailing space ',
182 [ UserRigorOptions::RIGOR_CREATABLE => 'Trailing space' ]
183 ],
184 'Subject namespace prefix' => [
185 'User:Username',
186 [
187 UserRigorOptions::RIGOR_NONE => 'Username'
188 ]
189 ],
190 'Talk namespace prefix' => [
191 'Talk:Username',
192 [
193 UserRigorOptions::RIGOR_CREATABLE => false,
194 UserRigorOptions::RIGOR_USABLE => false,
195 UserRigorOptions::RIGOR_VALID => false,
196 UserRigorOptions::RIGOR_NONE => 'Talk:Username'
197 ]
198 ],
199 'With hash' => [
200 'name with # hash',
201 [
202 UserRigorOptions::RIGOR_CREATABLE => false,
203 UserRigorOptions::RIGOR_USABLE => false
204 ]
205 ],
206 'Multi spaces' => [
207 'Multi spaces',
208 [
209 UserRigorOptions::RIGOR_CREATABLE => 'Multi spaces',
210 UserRigorOptions::RIGOR_USABLE => 'Multi spaces'
211 ]
212 ],
213 'Lowercase' => [
214 'lowercase',
215 [ UserRigorOptions::RIGOR_CREATABLE => 'Lowercase' ]
216 ],
217 'Invalid character' => [
218 'in[]valid',
219 [
220 UserRigorOptions::RIGOR_CREATABLE => false,
221 UserRigorOptions::RIGOR_USABLE => false,
222 UserRigorOptions::RIGOR_VALID => false,
223 UserRigorOptions::RIGOR_NONE => 'In[]valid'
224 ]
225 ],
226 'With slash' => [
227 'with / slash',
228 [
229 UserRigorOptions::RIGOR_CREATABLE => false,
230 UserRigorOptions::RIGOR_USABLE => false,
231 UserRigorOptions::RIGOR_VALID => false,
232 UserRigorOptions::RIGOR_NONE => 'With / slash'
233 ]
234 ],
235 // 'interwiki' is a valid interwiki prefix, per configuration of the
236 // title formatter in DummyServicesTrait::getDummyUserNameUtils()
237 'Interwiki prefix in username' => [
238 'interwiki:Username',
239 [
240 UserRigorOptions::RIGOR_CREATABLE => false,
241 UserRigorOptions::RIGOR_USABLE => false,
242 UserRigorOptions::RIGOR_VALID => false,
243 UserRigorOptions::RIGOR_NONE => 'Interwiki:Username'
244 ]
245 ],
246 ];
247 }
248
249 /**
250 * @covers \MediaWiki\User\UserNameUtils::getCanonical
251 */
252 public function testGetCanonical_bad() {
253 $this->expectException( InvalidArgumentException::class );
254 $this->expectExceptionMessage( 'Invalid parameter value for validation' );
255 $this->getDummyUserNameUtils()->getCanonical( 'ValidName', 'InvalidValidationValue' );
256 }
257
258 /**
259 * @dataProvider provideIPs
260 * @covers \MediaWiki\User\UserNameUtils::isIP
261 */
262 public function testIsIP( string $value, bool $result ) {
263 $utils = $this->getDummyUserNameUtils();
264 $this->assertSame(
265 $result,
266 $utils->isIP( $value )
267 );
268 }
269
270 public static function provideIPs() {
271 return [
272 'Empty string' => [ '', false ],
273 'Blank space' => [ ' ', false ],
274 'IPv4 private 10/8 (1)' => [ '10.0.0.0', true ],
275 'IPv4 private 10/8 (2)' => [ '10.255.255.255', true ],
276 'IPv4 private 192.168/16' => [ '192.168.1.1', true ],
277 'IPv4 example' => [ '203.0.113.0', true ],
278 'IPv6 example' => [ '2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff', true ],
279 // Not valid IPs but classified as such by MediaWiki for negated asserting
280 // of whether this might be the identifier of a logged-out user or whether
281 // to allow usernames like it.
282 'Looks too much like an IPv4 address' => [ '300.300.300.300', true ],
283 'Assigned by UseMod to cloaked logged-out users' => [ '203.0.113.xxx', true ],
284 'Does not accept IPv4 ranges' => [ '74.24.52.13/20', false ],
285 'Does not accept IPv6 ranges' => [ 'fc:100:a:d:1:e:ac:0/24', false ],
286 ];
287 }
288
289 /**
290 * @dataProvider provideIPRanges
291 * @covers \MediaWiki\User\UserNameUtils::isValidIPRange
292 */
293 public function testIsValidIPRange( $value, $result ) {
294 $utils = $this->getDummyUserNameUtils();
295 $this->assertSame(
296 $result,
297 $utils->isValidIPRange( $value )
298 );
299 }
300
301 public static function provideIPRanges() {
302 return [
303 [ '116.17.184.5/32', true ],
304 [ '0.17.184.5/30', true ],
305 [ '16.17.184.1/24', true ],
306 [ '30.242.52.14/1', true ],
307 [ '10.232.52.13/8', true ],
308 [ '30.242.52.14/0', true ],
309 [ '::e:f:2001/96', true ],
310 [ '::c:f:2001/128', true ],
311 [ '::10:f:2001/70', true ],
312 [ '::fe:f:2001/1', true ],
313 [ '::6d:f:2001/8', true ],
314 [ '::fe:f:2001/0', true ],
315 [ '116.17.184.5/33', false ],
316 [ '0.17.184.5/130', false ],
317 [ '16.17.184.1/-1', false ],
318 [ '10.232.52.13/*', false ],
319 [ '7.232.52.13/ab', false ],
320 [ '11.232.52.13/', false ],
321 [ '::e:f:2001/129', false ],
322 [ '::c:f:2001/228', false ],
323 [ '::10:f:2001/-1', false ],
324 [ '::6d:f:2001/*', false ],
325 [ '::86:f:2001/ab', false ],
326 [ '::23:f:2001/', false ]
327 ];
328 }
329
330 /**
331 * @dataProvider provideIPv4DashRanges
332 * @covers \MediaWiki\User\UserNameUtils::isLikeIPv4DashRange
333 */
334 public function testIsLikeIPv4DashRange( $value, $result ) {
335 $utils = $this->getDummyUserNameUtils();
336 $this->assertSame(
337 $result,
338 $utils->isLikeIPv4DashRange( $value )
339 );
340 }
341
342 public static function provideIPv4DashRanges() {
343 return [
344 [ '1.2.3.4-5.6.7.8', true ],
345 [ '111.222.333.444-555.666.777.888', true ],
346 [ '128.128.128.0-128.128.128.1', true ],
347 [ '255.255.255.50-255.255.255.254', true ],
348 [ 'User 1.2.3.4-5.6.7.8', false ],
349 [ 'A string', false ]
350 ];
351 }
352
353 public function testIsTemp() {
354 $utils = $this->getDummyUserNameUtils();
355 $this->assertFalse( $utils->isTemp( 'Some user' ) );
356 $this->assertTrue( $utils->isTemp( '*Unregistered 1234' ) );
357 $this->assertTrue( $utils->isTemp( '*1234' ) );
358 }
359
360 public function testGetTempPlaceholder() {
361 $utils = $this->getDummyUserNameUtils();
362 $name = $utils->getTempPlaceholder();
363 $this->assertSame( '*Unregistered *', $name );
364 }
365
366 }