MediaWiki  master
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 = "password_for_test_user_id_{$id}";
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 = "password_for_test_user_id_{$id}";
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 }
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
static getImmutableTestUser( $groups=[])
Get a TestUser object that the caller may not modify.
static clear()
Clear the registry.
static getMutableTestUser( $testName, $groups=[])
Get a TestUser object that the caller may modify.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:780
static int $randInt
Random int, included in IDs.
Wraps the user object, so we can also retain full access to properties like password if we log in via...
Definition: TestUser.php:9
static isMutable(User $user)
static TestUser [] $testUsers
(group key => TestUser)
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473
static int $counter
Count of users that have been generated.