MediaWiki REL1_33
TestUser.php
Go to the documentation of this file.
1<?php
2
4
9class TestUser {
13 private $username;
14
18 private $password;
19
23 private $user;
24
25 private function assertNotReal() {
26 global $wgDBprefix;
27 if ( $wgDBprefix !== MediaWikiTestCase::DB_PREFIX &&
28 $wgDBprefix !== MediaWikiTestCase::ORA_DB_PREFIX
29 ) {
30 throw new MWException( "Can't create user on real database" );
31 }
32 }
33
34 public function __construct( $username, $realname = 'Real Name',
35 $email = 'sample@example.com', $groups = []
36 ) {
37 $this->assertNotReal();
38
39 $this->username = $username;
40 $this->password = 'TestUser';
41
42 $this->user = User::newFromName( $this->username );
43 $this->user->load();
44
45 // In an ideal world we'd have a new wiki (or mock data store) for every single test.
46 // But for now, we just need to create or update the user with the desired properties.
47 // we particularly need the new password, since we just generated it randomly.
48 // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
49 if ( !$this->user->isLoggedIn() ) {
50 // create the user
51 $this->user = User::createNew(
52 $this->username, [
53 "email" => $email,
54 "real_name" => $realname
55 ]
56 );
57
58 if ( !$this->user ) {
59 throw new MWException( "Error creating TestUser " . $username );
60 }
61 }
62
63 // Update the user to use the password and other details
64 $this->setPassword( $this->password );
65 $change = $this->setEmail( $email ) ||
66 $this->setRealName( $realname );
67
68 // Adjust groups by adding any missing ones and removing any extras
69 $currentGroups = $this->user->getGroups();
70 foreach ( array_diff( $groups, $currentGroups ) as $group ) {
71 $this->user->addGroup( $group );
72 }
73 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
74 $this->user->removeGroup( $group );
75 }
76 if ( $change ) {
77 // Disable CAS check before saving. The User object may have been initialized from cached
78 // information that may be out of whack with the database during testing. If tests were
79 // perfectly isolated, this would not happen. But if it does happen, let's just ignore the
80 // inconsistency, and just write the data we want - during testing, we are not worried
81 // about data loss.
82 $this->user->mTouched = '';
83 $this->user->saveSettings();
84 }
85 }
86
91 private function setRealName( $realname ) {
92 if ( $this->user->getRealName() !== $realname ) {
93 $this->user->setRealName( $realname );
94 return true;
95 }
96
97 return false;
98 }
99
104 private function setEmail( $email ) {
105 if ( $this->user->getEmail() !== $email ) {
106 $this->user->setEmail( $email );
107 return true;
108 }
109
110 return false;
111 }
112
116 private function setPassword( $password ) {
117 self::setPasswordForUser( $this->user, $password );
118 }
119
129 public static function setPasswordForUser( User $user, $password ) {
130 if ( !$user->getId() ) {
131 throw new MWException( "Passed User has not been added to the database yet!" );
132 }
133
134 $dbw = wfGetDB( DB_MASTER );
135 $row = $dbw->selectRow(
136 'user',
137 [ 'user_password' ],
138 [ 'user_id' => $user->getId() ],
139 __METHOD__
140 );
141 if ( !$row ) {
142 throw new MWException( "Passed User has an ID but is not in the database?" );
143 }
144
145 $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
146 if ( !$passwordFactory->newFromCiphertext( $row->user_password )->verify( $password ) ) {
147 $passwordHash = $passwordFactory->newFromPlaintext( $password );
148 $dbw->update(
149 'user',
150 [ 'user_password' => $passwordHash->toString() ],
151 [ 'user_id' => $user->getId() ],
152 __METHOD__
153 );
154 }
155 }
156
161 public function getUser() {
162 return $this->user;
163 }
164
169 public function getPassword() {
170 return $this->password;
171 }
172}
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
$wgDBprefix
Table name prefix.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Wraps the user object, so we can also retain full access to properties like password if we log in via...
Definition TestUser.php:9
string $username
Definition TestUser.php:13
getPassword()
Definition TestUser.php:169
assertNotReal()
Definition TestUser.php:25
setPassword( $password)
Definition TestUser.php:116
User $user
Definition TestUser.php:23
static setPasswordForUser(User $user, $password)
Set the password on a testing user.
Definition TestUser.php:129
setRealName( $realname)
Definition TestUser.php:91
string $password
Definition TestUser.php:18
__construct( $username, $realname='Real Name', $email='sample @example.com', $groups=[])
Definition TestUser.php:34
setEmail( $email)
Definition TestUser.php:104
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
static createNew( $name, $params=[])
Add a user to the database, return the user object.
Definition User.php:4300
const DB_MASTER
Definition defines.php:26