MediaWiki  1.30.0
TestUser.php
Go to the documentation of this file.
1 <?php
2 
7 class TestUser {
11  private $username;
12 
16  private $password;
17 
21  private $user;
22 
23  private function assertNotReal() {
27  ) {
28  throw new MWException( "Can't create user on real database" );
29  }
30  }
31 
32  public function __construct( $username, $realname = 'Real Name',
33  $email = 'sample@example.com', $groups = []
34  ) {
35  $this->assertNotReal();
36 
37  $this->username = $username;
38  $this->password = 'TestUser';
39 
40  $this->user = User::newFromName( $this->username );
41  $this->user->load();
42 
43  // In an ideal world we'd have a new wiki (or mock data store) for every single test.
44  // But for now, we just need to create or update the user with the desired properties.
45  // we particularly need the new password, since we just generated it randomly.
46  // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
47  if ( !$this->user->isLoggedIn() ) {
48  // create the user
49  $this->user = User::createNew(
50  $this->username, [
51  "email" => $email,
52  "real_name" => $realname
53  ]
54  );
55 
56  if ( !$this->user ) {
57  throw new MWException( "Error creating TestUser " . $username );
58  }
59  }
60 
61  // Update the user to use the password and other details
62  $this->setPassword( $this->password );
63  $change = $this->setEmail( $email ) ||
64  $this->setRealName( $realname );
65 
66  // Adjust groups by adding any missing ones and removing any extras
67  $currentGroups = $this->user->getGroups();
68  foreach ( array_diff( $groups, $currentGroups ) as $group ) {
69  $this->user->addGroup( $group );
70  }
71  foreach ( array_diff( $currentGroups, $groups ) as $group ) {
72  $this->user->removeGroup( $group );
73  }
74  if ( $change ) {
75  // Disable CAS check before saving. The User object may have been initialized from cached
76  // information that may be out of whack with the database during testing. If tests were
77  // perfectly isolated, this would not happen. But if it does happen, let's just ignore the
78  // inconsistency, and just write the data we want - during testing, we are not worried
79  // about data loss.
80  $this->user->mTouched = '';
81  $this->user->saveSettings();
82  }
83  }
84 
89  private function setRealName( $realname ) {
90  if ( $this->user->getRealName() !== $realname ) {
91  $this->user->setRealName( $realname );
92  return true;
93  }
94 
95  return false;
96  }
97 
102  private function setEmail( $email ) {
103  if ( $this->user->getEmail() !== $email ) {
104  $this->user->setEmail( $email );
105  return true;
106  }
107 
108  return false;
109  }
110 
114  private function setPassword( $password ) {
116  }
117 
127  public static function setPasswordForUser( User $user, $password ) {
128  if ( !$user->getId() ) {
129  throw new MWException( "Passed User has not been added to the database yet!" );
130  }
131 
132  $dbw = wfGetDB( DB_MASTER );
133  $row = $dbw->selectRow(
134  'user',
135  [ 'user_password' ],
136  [ 'user_id' => $user->getId() ],
137  __METHOD__
138  );
139  if ( !$row ) {
140  throw new MWException( "Passed User has an ID but is not in the database?" );
141  }
142 
143  $passwordFactory = new PasswordFactory();
144  $passwordFactory->init( RequestContext::getMain()->getConfig() );
145  if ( !$passwordFactory->newFromCiphertext( $row->user_password )->equals( $password ) ) {
146  $passwordHash = $passwordFactory->newFromPlaintext( $password );
147  $dbw->update(
148  'user',
149  [ 'user_password' => $passwordHash->toString() ],
150  [ 'user_id' => $user->getId() ],
151  __METHOD__
152  );
153  }
154  }
155 
160  public function getUser() {
161  return $this->user;
162  }
163 
168  public function getPassword() {
169  return $this->password;
170  }
171 }
User\getId
getId()
Get the user's ID.
Definition: User.php:2224
TestUser\setPassword
setPassword( $password)
Definition: TestUser.php:114
TestUser\$password
string $password
Definition: TestUser.php:16
TestUser\assertNotReal
assertNotReal()
Definition: TestUser.php:23
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:550
$wgDBprefix
$wgDBprefix
Table name prefix.
Definition: DefaultSettings.php:1849
php
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
User\createNew
static createNew( $name, $params=[])
Add a user to the database, return the user object.
Definition: User.php:4106
TestUser
Wraps the user object, so we can also retain full access to properties like password if we log in via...
Definition: TestUser.php:7
MWException
MediaWiki exception.
Definition: MWException.php:26
user
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 and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
TestUser\__construct
__construct( $username, $realname='Real Name', $email='sample @example.com', $groups=[])
Definition: TestUser.php:32
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_MASTER
const DB_MASTER
Definition: defines.php:26
TestUser\$username
string $username
Definition: TestUser.php:11
TestUser\$user
User $user
Definition: TestUser.php:21
TestUser\setPasswordForUser
static setPasswordForUser(User $user, $password)
Set the password on a testing user.
Definition: TestUser.php:127
TestUser\getPassword
getPassword()
Definition: TestUser.php:168
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:470
TestUser\getUser
getUser()
Definition: TestUser.php:160
MediaWikiTestCase\DB_PREFIX
const DB_PREFIX
Table name prefixes.
Definition: MediaWikiTestCase.php:104
TestUser\setEmail
setEmail( $email)
Definition: TestUser.php:102
MediaWikiTestCase\ORA_DB_PREFIX
const ORA_DB_PREFIX
Definition: MediaWikiTestCase.php:105
as
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
PasswordFactory
Factory class for creating and checking Password objects.
Definition: PasswordFactory.php:28
TestUser\setRealName
setRealName( $realname)
Definition: TestUser.php:89
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51