MediaWiki REL1_33
TestUserRegistry.php
Go to the documentation of this file.
1<?php
2
7
9 private static $testUsers = [];
10
12 private static $counter = 0;
13
15 private static $randInt;
16
17 public static function getNextId() {
18 if ( !self::$randInt ) {
19 self::$randInt = mt_rand( 1, 0xFFFFFF );
20 }
21 return sprintf( '%06x.%03x', self::$randInt, ++self::$counter );
22 }
23
34 public static function getMutableTestUser( $testName, $groups = [] ) {
35 $id = self::getNextId();
36 $password = wfRandomString( 20 );
37 $testUser = new TestUser(
38 "TestUser $testName $id", // username
39 "Name $id", // real name
40 "$id@mediawiki.test", // e-mail
41 (array)$groups, // groups
42 $password // password
43 );
44 $testUser->getUser()->clearInstanceCache();
45 return $testUser;
46 }
47
60 public static function getImmutableTestUser( $groups = [] ) {
61 $groups = array_unique( (array)$groups );
62 sort( $groups );
63 $key = implode( ',', $groups );
64
65 $testUser = self::$testUsers[$key] ?? false;
66
67 if ( !$testUser || !$testUser->getUser()->isLoggedIn() ) {
68 $id = self::getNextId();
69 // Hack! If this is the primary sysop account, make the username
70 // be 'UTSysop', for back-compat, and for the sake of PHPUnit data
71 // provider methods, which are executed before the test database
72 // is set up. See T136348.
73 if ( $groups === [ 'bureaucrat', 'sysop' ] ) {
74 $username = 'UTSysop';
75 $password = 'UTSysopPassword';
76 } else {
77 $username = "TestUser $id";
78 $password = wfRandomString( 20 );
79 }
80 self::$testUsers[$key] = $testUser = new TestUser(
81 $username, // username
82 "Name $id", // real name
83 "$id@mediawiki.test", // e-mail
84 $groups, // groups
85 $password // password
86 );
87 }
88
89 $testUser->getUser()->clearInstanceCache();
90 return self::$testUsers[$key];
91 }
92
105 public static function clear() {
106 self::$testUsers = [];
107 }
108
115 public static function isMutable( User $user ) {
116 foreach ( self::$testUsers as $key => $testUser ) {
117 if ( $user === $testUser->getUser() ) {
118 return false;
119 }
120 }
121 return true;
122 }
123}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
static int $randInt
Random int, included in IDs.
static clear()
Clear the registry.
static getMutableTestUser( $testName, $groups=[])
Get a TestUser object that the caller may modify.
static isMutable(User $user)
static getImmutableTestUser( $groups=[])
Get a TestUser object that the caller may not modify.
static TestUser[] $testUsers
(group key => TestUser)
static int $counter
Count of users that have been generated.
Wraps the user object, so we can also retain full access to properties like password if we log in via...
Definition TestUser.php:9
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:782
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))