Thanks link and other links should be styled consistently with other links
[mediawiki.git] / tests / phpunit / includes / TestUser.php
blobc5dac9212d79270afe8367715ad56f79c712a161
1 <?php
3 use MediaWiki\MediaWikiServices;
4 use MediaWiki\Permissions\Authority;
5 use MediaWiki\User\User;
6 use MediaWiki\User\UserIdentity;
7 use MediaWiki\User\UserIdentityValue;
9 /**
10 * Wraps the user object, so we can also retain full access to properties
11 * like password if we log in via the API.
13 class TestUser {
14 /**
15 * @var string
17 private $username;
19 /**
20 * @var string
22 private $password;
24 /**
25 * @var User
27 private $user;
29 private function assertNotReal() {
30 global $wgDBprefix;
31 if (
32 $wgDBprefix !== MediaWikiIntegrationTestCase::DB_PREFIX &&
33 $wgDBprefix !== ParserTestRunner::DB_PREFIX
34 ) {
35 throw new RuntimeException( "Can't create user on real database" );
39 public function __construct( $username, $realname = 'Real Name',
40 $email = 'sample@example.com', $groups = []
41 ) {
42 $this->assertNotReal();
44 $this->username = $username;
45 $this->password = 'TestUser';
47 $this->user = User::newFromName( $this->username );
48 $this->user->load();
50 // In an ideal world we'd have a new wiki (or mock data store) for every single test.
51 // But for now, we just need to create or update the user with the desired properties.
52 // we particularly need the new password, since we just generated it randomly.
53 // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
54 if ( !$this->user->isRegistered() ) {
55 // create the user
56 $this->user = User::createNew(
57 $this->username, [
58 "email" => $email,
59 "real_name" => $realname
63 if ( !$this->user ) {
64 throw new RuntimeException( "Error creating TestUser " . $username );
68 // Update the user to use the password and other details
69 $this->setPassword( $this->password );
70 $change = $this->setEmail( $email ) ||
71 $this->setRealName( $realname );
73 // Adjust groups by adding any missing ones and removing any extras
74 $userGroupManager = MediaWikiServices::getInstance()->getUserGroupManager();
75 $currentGroups = $userGroupManager->getUserGroups( $this->user );
76 $userGroupManager->addUserToMultipleGroups( $this->user, array_diff( $groups, $currentGroups ) );
77 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
78 $userGroupManager->removeUserFromGroup( $this->user, $group );
80 if ( $change ) {
81 // Disable CAS check before saving. The User object may have been initialized from cached
82 // information that may be out of whack with the database during testing. If tests were
83 // perfectly isolated, this would not happen. But if it does happen, let's just ignore the
84 // inconsistency, and just write the data we want - during testing, we are not worried
85 // about data loss.
86 $this->user->mTouched = '';
87 $this->user->saveSettings();
91 /**
92 * @param string $realname
93 * @return bool
95 private function setRealName( $realname ) {
96 if ( $this->user->getRealName() !== $realname ) {
97 $this->user->setRealName( $realname );
98 return true;
101 return false;
105 * @param string $email
106 * @return bool
108 private function setEmail( string $email ) {
109 if ( $this->user->getEmail() !== $email ) {
110 $this->user->setEmail( $email );
111 return true;
114 return false;
118 * @param string $password
120 private function setPassword( $password ) {
121 self::setPasswordForUser( $this->user, $password );
125 * Set the password on a testing user
127 * This assumes we're still using the generic AuthManager config from
128 * PHPUnitMaintClass::finalSetup(), and just sets the password in the
129 * database directly.
130 * @param User $user
131 * @param string $password
133 public static function setPasswordForUser( User $user, $password ) {
134 if ( !$user->getId() ) {
135 throw new InvalidArgumentException( "Passed User has not been added to the database yet!" );
138 $services = MediaWikiServices::getInstance();
140 $dbw = $services->getConnectionProvider()->getPrimaryDatabase();
141 $row = $dbw->newSelectQueryBuilder()
142 ->select( [ 'user_password' ] )
143 ->from( 'user' )
144 ->where( [ 'user_id' => $user->getId() ] )
145 ->caller( __METHOD__ )->fetchRow();
146 if ( !$row ) {
147 throw new RuntimeException( "Passed User has an ID but is not in the database?" );
150 $passwordFactory = $services->getPasswordFactory();
151 if ( !$passwordFactory->newFromCiphertext( $row->user_password )->verify( $password ) ) {
152 $passwordHash = $passwordFactory->newFromPlaintext( $password );
153 $dbw->newUpdateQueryBuilder()
154 ->update( 'user' )
155 ->set( [ 'user_password' => $passwordHash->toString() ] )
156 ->where( [ 'user_id' => $user->getId() ] )
157 ->caller( __METHOD__ )->execute();
162 * @since 1.25
163 * @return User
165 public function getUser() {
166 return $this->user;
170 * @since 1.39
171 * @return Authority
173 public function getAuthority(): Authority {
174 return $this->user;
178 * @since 1.36
179 * @return UserIdentity
181 public function getUserIdentity(): UserIdentity {
182 return new UserIdentityValue( $this->user->getId(), $this->user->getName() );
186 * @since 1.25
187 * @return string
189 public function getPassword() {
190 return $this->password;