File: UserNameUtilsTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (366 lines) | stat: -rw-r--r-- 10,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php

use MediaWiki\MainConfigNames;
use MediaWiki\Tests\Unit\DummyServicesTrait;
use MediaWiki\User\TempUser\RealTempUserConfig;
use MediaWiki\User\UserRigorOptions;
use Psr\Log\LogLevel;

/**
 * @covers \MediaWiki\User\UserNameUtils
 * @author DannyS712
 */
class UserNameUtilsTest extends MediaWikiUnitTestCase {
	use DummyServicesTrait;

	/**
	 * @dataProvider provideIsValid
	 * @covers \MediaWiki\User\UserNameUtils::isValid
	 */
	public function testIsValid( string $name, bool $result ) {
		$this->assertSame(
			$result,
			$this->getDummyUserNameUtils()->isValid( $name )
		);
	}

	public static function provideIsValid() {
		return [
			'Empty string' => [ '', false ],
			'Blank space' => [ ' ', false ],
			'Starts with small letter' => [ 'abcd', false ],
			'Contains slash' => [ 'Ab/cd', false ],
			'Whitespace' => [ 'Ab cd', true ],
			'IP' => [ '192.168.1.1', false ],
			'IP dash range' => [ '111.222.333.444-555.666.777.888', false ],
			'IP range' => [ '116.17.184.5/32', false ],
			'IPv6 range' => [ '::e:f:2001/96', false ],
			'Reserved Namespace' => [ 'User:Abcd', false ],
			'Starts with Numbers' => [ '12abcd232', true ],
			'Start with ? mark' => [ '?abcd', true ],
			'Start with #' => [ '#abcd', false ],
			' Mixed scripts' => [ 'Abcdകഖഗഘ', true ],
			'ZWNJ- Format control character' => [ 'ജോസ്‌തോമസ്', false ],
			' Ideographic space' => [ 'Ab cd', false ],
			'Looks too much like an IPv4 address (1)' => [ '300.300.300.300', false ],
			'Looks too much like an IPv4 address (2)' => [ '302.113.311.900', false ],
			'Reserved for usage by UseMod for cloaked logged-out users' => [ '203.0.113.xxx', false ],
			'Disallowed characters' => [ "\u{E000}", false ],
		];
	}

	/**
	 * @dataProvider provideIsUsable
	 * @covers \MediaWiki\User\UserNameUtils::isUsable
	 */
	public function testIsUsable( string $name, bool $result ) {
		$utils = $this->getDummyUserNameUtils( [
			MainConfigNames::ReservedUsernames => [
				'MediaWiki default',
				'msg:reserved-user'
			],
			'textFormatter' => $this->getDummyTextFormatter(),
		] );
		$this->assertSame(
			$result,
			$utils->isUsable( $name )
		);
	}

	public static function provideIsUsable() {
		return [
			'Only valid user names are creatable' => [ '', false ],
			'Reserved names cannot be used' => [ 'MediaWiki default', false ],
			'Names can also be reserved via msg: ' => [ 'reserved-user', false ],
			'User names with no issues can be used' => [ 'FooBar', true ],
			'Reserved temp users' => [ '!Unregistered 1234', false ],
			'Real temp users' => [ '*Unregistered 1234', true ],
		];
	}

	public function testIsUsableForTemporaryAccountWhenFeatureIsKnownButDisabled() {
		$utils = $this->getDummyUserNameUtils( [
			'tempUserConfig' => new RealTempUserConfig( [
				'enabled' => false,
				'known' => true,
				'expireAfterDays' => null,
				'actions' => [ 'edit' ],
				'serialProvider' => [ 'type' => 'local' ],
				'serialMapping' => [ 'type' => 'plain-numeric' ],
				'reservedPattern' => '!$1',
				'matchPattern' => '*$1',
				'genPattern' => '*Unregistered $1'
			] ),
		] );
		$this->assertFalse(
			$utils->isUsable( '*Unregistered 1234' ),
			'::isUsable should return false for a valid temporary account ' .
			'when TempUserConfig::isEnabled is false and TempUserConfig::isKnown is true'
		);
	}

	/**
	 * @covers \MediaWiki\User\UserNameUtils::isCreatable
	 */
	public function testIsCreatable() {
		$logger = new TestLogger( true, static function ( $message ) {
			$message = str_replace(
				'MediaWiki\\User\\UserNameUtils::isCreatable: ',
				'',
				$message
			);
			return $message;
		} );
		$utils = $this->getDummyUserNameUtils( [
			'logger' => $logger,
		] );

		$longUserName = str_repeat( 'q', 1000 );
		$this->assertFalse(
			$utils->isCreatable( $longUserName ),
			'longUserName is too long'
		);
		$this->assertSame( [
			[ LogLevel::DEBUG, "'$longUserName' uncreatable due to length" ],
		], $logger->getBuffer() );
		$logger->clearBuffer();

		$atUserName = 'Foo@Bar';
		$this->assertFalse(
			$utils->isCreatable( $atUserName ),
			'User name contains invalid character'
		);
		$this->assertSame( [
			[ LogLevel::DEBUG, "'$atUserName' uncreatable due to wgInvalidUsernameCharacters" ],
		], $logger->getBuffer() );
		$logger->clearBuffer();

		$this->assertTrue(
			$utils->isCreatable( 'FooBar' ),
			'User names with no issues can be created'
		);

		$tempUserName = '*Unregistered 1234';
		$this->assertFalse(
			$utils->isCreatable( $tempUserName ),
			'UI account creation with the temp user prefix needs to be prevented'
		);
	}

	/**
	 * @dataProvider provideGetCanonical
	 * @covers \MediaWiki\User\UserNameUtils::getCanonical
	 */
	public function testGetCanonical( string $name, array $expectedArray ) {
		$utils = $this->getDummyUserNameUtils();
		foreach ( $expectedArray as $validate => $expected ) {
			$this->assertSame(
				$expected,
				$utils->getCanonical( $name, $validate ),
				"Validating '$name' with level '$validate' should be '$expected'"
			);
		}
	}

	public static function provideGetCanonical() {
		return [
			'Normal name' => [
				'Normal name',
				[
					UserRigorOptions::RIGOR_CREATABLE => 'Normal name',
					UserRigorOptions::RIGOR_USABLE => 'Normal name',
					UserRigorOptions::RIGOR_VALID => 'Normal name',
					UserRigorOptions::RIGOR_NONE => 'Normal name'
				]
			],
			'Leading space' => [
				' Leading space',
				[ UserRigorOptions::RIGOR_CREATABLE => 'Leading space' ]
			],
			'Trailing space' => [
				'Trailing space ',
				[ UserRigorOptions::RIGOR_CREATABLE => 'Trailing space' ]
			],
			'Subject namespace prefix' => [
				'User:Username',
				[
					UserRigorOptions::RIGOR_NONE => 'Username'
				]
			],
			'Talk namespace prefix' => [
				'Talk:Username',
				[
					UserRigorOptions::RIGOR_CREATABLE => false,
					UserRigorOptions::RIGOR_USABLE => false,
					UserRigorOptions::RIGOR_VALID => false,
					UserRigorOptions::RIGOR_NONE => 'Talk:Username'
				]
			],
			'With hash' => [
				'name with # hash',
				[
					UserRigorOptions::RIGOR_CREATABLE => false,
					UserRigorOptions::RIGOR_USABLE => false
				]
			],
			'Multi spaces' => [
				'Multi  spaces',
				[
					UserRigorOptions::RIGOR_CREATABLE => 'Multi spaces',
					UserRigorOptions::RIGOR_USABLE => 'Multi spaces'
				]
			],
			'Lowercase' => [
				'lowercase',
				[ UserRigorOptions::RIGOR_CREATABLE => 'Lowercase' ]
			],
			'Invalid character' => [
				'in[]valid',
				[
					UserRigorOptions::RIGOR_CREATABLE => false,
					UserRigorOptions::RIGOR_USABLE => false,
					UserRigorOptions::RIGOR_VALID => false,
					UserRigorOptions::RIGOR_NONE => 'In[]valid'
				]
			],
			'With slash' => [
				'with / slash',
				[
					UserRigorOptions::RIGOR_CREATABLE => false,
					UserRigorOptions::RIGOR_USABLE => false,
					UserRigorOptions::RIGOR_VALID => false,
					UserRigorOptions::RIGOR_NONE => 'With / slash'
				]
			],
			// 'interwiki' is a valid interwiki prefix, per configuration of the
			// title formatter in DummyServicesTrait::getDummyUserNameUtils()
			'Interwiki prefix in username' => [
				'interwiki:Username',
				[
					UserRigorOptions::RIGOR_CREATABLE => false,
					UserRigorOptions::RIGOR_USABLE => false,
					UserRigorOptions::RIGOR_VALID => false,
					UserRigorOptions::RIGOR_NONE => 'Interwiki:Username'
				]
			],
		];
	}

	/**
	 * @covers \MediaWiki\User\UserNameUtils::getCanonical
	 */
	public function testGetCanonical_bad() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage( 'Invalid parameter value for validation' );
		$this->getDummyUserNameUtils()->getCanonical( 'ValidName', 'InvalidValidationValue' );
	}

	/**
	 * @dataProvider provideIPs
	 * @covers \MediaWiki\User\UserNameUtils::isIP
	 */
	public function testIsIP( string $value, bool $result ) {
		$utils = $this->getDummyUserNameUtils();
		$this->assertSame(
			$result,
			$utils->isIP( $value )
		);
	}

	public static function provideIPs() {
		return [
			'Empty string' => [ '', false ],
			'Blank space' => [ ' ', false ],
			'IPv4 private 10/8 (1)' => [ '10.0.0.0', true ],
			'IPv4 private 10/8 (2)' => [ '10.255.255.255', true ],
			'IPv4 private 192.168/16' => [ '192.168.1.1', true ],
			'IPv4 example' => [ '203.0.113.0', true ],
			'IPv6 example' => [ '2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff', true ],
			// Not valid IPs but classified as such by MediaWiki for negated asserting
			// of whether this might be the identifier of a logged-out user or whether
			// to allow usernames like it.
			'Looks too much like an IPv4 address' => [ '300.300.300.300', true ],
			'Assigned by UseMod to cloaked logged-out users' => [ '203.0.113.xxx', true ],
			'Does not accept IPv4 ranges' => [ '74.24.52.13/20', false ],
			'Does not accept IPv6 ranges' => [ 'fc:100:a:d:1:e:ac:0/24', false ],
		];
	}

	/**
	 * @dataProvider provideIPRanges
	 * @covers \MediaWiki\User\UserNameUtils::isValidIPRange
	 */
	public function testIsValidIPRange( $value, $result ) {
		$utils = $this->getDummyUserNameUtils();
		$this->assertSame(
			$result,
			$utils->isValidIPRange( $value )
		);
	}

	public static function provideIPRanges() {
		return [
			[ '116.17.184.5/32', true ],
			[ '0.17.184.5/30', true ],
			[ '16.17.184.1/24', true ],
			[ '30.242.52.14/1', true ],
			[ '10.232.52.13/8', true ],
			[ '30.242.52.14/0', true ],
			[ '::e:f:2001/96', true ],
			[ '::c:f:2001/128', true ],
			[ '::10:f:2001/70', true ],
			[ '::fe:f:2001/1', true ],
			[ '::6d:f:2001/8', true ],
			[ '::fe:f:2001/0', true ],
			[ '116.17.184.5/33', false ],
			[ '0.17.184.5/130', false ],
			[ '16.17.184.1/-1', false ],
			[ '10.232.52.13/*', false ],
			[ '7.232.52.13/ab', false ],
			[ '11.232.52.13/', false ],
			[ '::e:f:2001/129', false ],
			[ '::c:f:2001/228', false ],
			[ '::10:f:2001/-1', false ],
			[ '::6d:f:2001/*', false ],
			[ '::86:f:2001/ab', false ],
			[ '::23:f:2001/', false ]
		];
	}

	/**
	 * @dataProvider provideIPv4DashRanges
	 * @covers \MediaWiki\User\UserNameUtils::isLikeIPv4DashRange
	 */
	public function testIsLikeIPv4DashRange( $value, $result ) {
		$utils = $this->getDummyUserNameUtils();
		$this->assertSame(
			$result,
			$utils->isLikeIPv4DashRange( $value )
		);
	}

	public static function provideIPv4DashRanges() {
		return [
			[ '1.2.3.4-5.6.7.8', true ],
			[ '111.222.333.444-555.666.777.888', true ],
			[ '128.128.128.0-128.128.128.1', true ],
			[ '255.255.255.50-255.255.255.254', true ],
			[ 'User 1.2.3.4-5.6.7.8', false ],
			[ 'A string', false ]
		];
	}

	public function testIsTemp() {
		$utils = $this->getDummyUserNameUtils();
		$this->assertFalse( $utils->isTemp( 'Some user' ) );
		$this->assertTrue( $utils->isTemp( '*Unregistered 1234' ) );
		$this->assertTrue( $utils->isTemp( '*1234' ) );
	}

	public function testGetTempPlaceholder() {
		$utils = $this->getDummyUserNameUtils();
		$name = $utils->getTempPlaceholder();
		$this->assertSame( '*Unregistered *', $name );
	}

}