Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / TestUser.php
blob86f4ae789d53960d43ad0aa782a6111647e69635
1 <?php
3 /**
4 * Wraps the user object, so we can also retain full access to properties
5 * like password if we log in via the API.
6 */
7 class TestUser {
8 /**
9 * @var string
11 private $username;
13 /**
14 * @var string
16 private $password;
18 /**
19 * @var User
21 private $user;
23 private function assertNotReal() {
24 global $wgDBprefix;
25 if ( $wgDBprefix !== MediaWikiTestCase::DB_PREFIX &&
26 $wgDBprefix !== MediaWikiTestCase::ORA_DB_PREFIX
27 ) {
28 throw new MWException( "Can't create user on real database" );
32 public function __construct( $username, $realname = 'Real Name',
33 $email = 'sample@example.com', $groups = []
34 ) {
35 $this->assertNotReal();
37 $this->username = $username;
38 $this->password = 'TestUser';
40 $this->user = User::newFromName( $this->username );
41 $this->user->load();
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
56 if ( !$this->user ) {
57 throw new MWException( "Error creating TestUser " . $username );
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 );
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 );
71 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
72 $this->user->removeGroup( $group );
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();
85 /**
86 * @param string $realname
87 * @return bool
89 private function setRealName( $realname ) {
90 if ( $this->user->getRealName() !== $realname ) {
91 $this->user->setRealName( $realname );
92 return true;
95 return false;
98 /**
99 * @param string $email
100 * @return bool
102 private function setEmail( $email ) {
103 if ( $this->user->getEmail() !== $email ) {
104 $this->user->setEmail( $email );
105 return true;
108 return false;
112 * @param string $password
114 private function setPassword( $password ) {
115 self::setPasswordForUser( $this->user, $password );
119 * Set the password on a testing user
121 * This assumes we're still using the generic AuthManager config from
122 * PHPUnitMaintClass::finalSetup(), and just sets the password in the
123 * database directly.
124 * @param User $user
125 * @param string $password
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!" );
132 $dbw = wfGetDB( DB_MASTER );
133 $row = $dbw->selectRow(
134 'user',
135 [ 'user_password' ],
136 [ 'user_id' => $user->getId() ],
137 __METHOD__
139 if ( !$row ) {
140 throw new MWException( "Passed User has an ID but is not in the database?" );
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__
157 * @since 1.25
158 * @return User
160 public function getUser() {
161 return $this->user;
165 * @since 1.25
166 * @return string
168 public function getPassword() {
169 return $this->password;