| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\User\User; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class TestUserRegistry { |
| 9 |
|
| 10 |
|
| 11 |
private static $testUsers = []; |
| 12 |
|
| 13 |
|
| 14 |
private static $counter = 0; |
| 15 |
|
| 16 |
|
| 17 |
private static $randInt; |
| 18 |
|
| 19 |
public static function getNextId() { |
| 20 |
if ( !self::$randInt ) { |
| 21 |
self::$randInt = mt_rand( 1, 0xFFFFFF ); |
| 22 |
} |
| 23 |
return sprintf( '%06x.%03x', self::$randInt, ++self::$counter ); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static function getMutableTestUser( $testName, $groups = [], $userPrefix = null ) { |
| 38 |
$id = self::getNextId(); |
| 39 |
$testUserName = "$testName $id"; |
| 40 |
$userPrefix ??= "TestUser"; |
| 41 |
$testUser = new TestUser( |
| 42 |
"$userPrefix $testName $id", |
| 43 |
"Name $id", |
| 44 |
"$id@mediawiki.test", |
| 45 |
(array)$groups |
| 46 |
); |
| 47 |
$testUser->getUser()->clearInstanceCache(); |
| 48 |
return $testUser; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function getImmutableTestUser( $groups = [] ) { |
| 64 |
$groups = array_unique( (array)$groups ); |
| 65 |
sort( $groups ); |
| 66 |
$key = implode( ',', $groups ); |
| 67 |
|
| 68 |
$testUser = self::$testUsers[$key] ?? false; |
| 69 |
|
| 70 |
if ( !$testUser || !$testUser->getUser()->isRegistered() ) { |
| 71 |
$id = self::getNextId(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if ( $groups === [ 'bureaucrat', 'sysop' ] ) { |
| 77 |
$username = 'UTSysop'; |
| 78 |
} else { |
| 79 |
$username = "TestUser $id"; |
| 80 |
} |
| 81 |
self::$testUsers[$key] = $testUser = new TestUser( |
| 82 |
$username, |
| 83 |
"Name $id", |
| 84 |
"$id@mediawiki.test", |
| 85 |
$groups |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
$testUser->getUser()->clearInstanceCache(); |
| 90 |
return self::$testUsers[$key]; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static function clear() { |
| 101 |
self::$testUsers = []; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static function clearInstanceCaches() { |
| 112 |
foreach ( self::$testUsers as $user ) { |
| 113 |
$user->getUser()->clearInstanceCache(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static function isMutable( User $user ) { |
| 125 |
foreach ( self::$testUsers as $key => $testUser ) { |
| 126 |
if ( $user === $testUser->getUser() ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |