Merge "Improve some documentation of AuthManager's additions"
[lhc/web/wiklou.git] / tests / phpunit / includes / TestUser.php
1 <?php
2
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 * @deprecated Since 1.25. Use TestUser::getUser()->getName()
10 * @private
11 * @var string
12 */
13 public $username;
14
15 /**
16 * @deprecated Since 1.25. Use TestUser::getPassword()
17 * @private
18 * @var string
19 */
20 public $password;
21
22 /**
23 * @deprecated Since 1.25. Use TestUser::getUser()
24 * @private
25 * @var User
26 */
27 public $user;
28
29 private function assertNotReal() {
30 global $wgDBprefix;
31 if ( $wgDBprefix !== MediaWikiTestCase::DB_PREFIX &&
32 $wgDBprefix !== MediaWikiTestCase::ORA_DB_PREFIX
33 ) {
34 throw new MWException( "Can't create user on real database" );
35 }
36 }
37
38 public function __construct( $username, $realname = 'Real Name',
39 $email = 'sample@example.com', $groups = []
40 ) {
41 $this->assertNotReal();
42
43 $this->username = $username;
44 $this->password = 'TestUser';
45
46 $this->user = User::newFromName( $this->username );
47 $this->user->load();
48
49 // In an ideal world we'd have a new wiki (or mock data store) for every single test.
50 // But for now, we just need to create or update the user with the desired properties.
51 // we particularly need the new password, since we just generated it randomly.
52 // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
53 if ( !$this->user->isLoggedIn() ) {
54 // create the user
55 $this->user = User::createNew(
56 $this->username, [
57 "email" => $email,
58 "real_name" => $realname
59 ]
60 );
61
62 if ( !$this->user ) {
63 throw new MWException( "Error creating TestUser " . $username );
64 }
65 }
66
67 // Update the user to use the password and other details
68 $this->setPassword( $this->password );
69 $change = $this->setEmail( $email ) ||
70 $this->setRealName( $realname );
71
72 // Adjust groups by adding any missing ones and removing any extras
73 $currentGroups = $this->user->getGroups();
74 foreach ( array_diff( $groups, $currentGroups ) as $group ) {
75 $this->user->addGroup( $group );
76 }
77 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
78 $this->user->removeGroup( $group );
79 }
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();
88 }
89 }
90
91 /**
92 * @param string $realname
93 * @return bool
94 */
95 private function setRealName( $realname ) {
96 if ( $this->user->getRealName() !== $realname ) {
97 $this->user->setRealName( $realname );
98 return true;
99 }
100
101 return false;
102 }
103
104 /**
105 * @param string $email
106 * @return bool
107 */
108 private function setEmail( $email ) {
109 if ( $this->user->getEmail() !== $email ) {
110 $this->user->setEmail( $email );
111 return true;
112 }
113
114 return false;
115 }
116
117 /**
118 * @param string $password
119 */
120 private function setPassword( $password ) {
121 self::setPasswordForUser( $this->user, $password );
122 }
123
124 /**
125 * Set the password on a testing user
126 *
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
132 */
133 public static function setPasswordForUser( User $user, $password ) {
134 if ( !$user->getId() ) {
135 throw new MWException( "Passed User has not been added to the database yet!" );
136 }
137
138 $dbw = wfGetDB( DB_MASTER );
139 $row = $dbw->selectRow(
140 'user',
141 [ 'user_password' ],
142 [ 'user_id' => $user->getId() ],
143 __METHOD__
144 );
145 if ( !$row ) {
146 throw new MWException( "Passed User has an ID but is not in the database?" );
147 }
148
149 $passwordFactory = new PasswordFactory();
150 $passwordFactory->init( RequestContext::getMain()->getConfig() );
151 if ( !$passwordFactory->newFromCiphertext( $row->user_password )->equals( $password ) ) {
152 $passwordHash = $passwordFactory->newFromPlaintext( $password );
153 $dbw->update(
154 'user',
155 [ 'user_password' => $passwordHash->toString() ],
156 [ 'user_id' => $user->getId() ],
157 __METHOD__
158 );
159 }
160 }
161
162 /**
163 * @since 1.25
164 * @return User
165 */
166 public function getUser() {
167 return $this->user;
168 }
169
170 /**
171 * @since 1.25
172 * @return string
173 */
174 public function getPassword() {
175 return $this->password;
176 }
177 }