Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / TestUser.php
blob6887ed175fcc5ab9507fa8932c42d033e0c28ea2
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 * @deprecated Since 1.25. Use TestUser::getUser()->getName()
10 * @private
11 * @var string
13 public $username;
15 /**
16 * @deprecated Since 1.25. Use TestUser::getPassword()
17 * @private
18 * @var string
20 public $password;
22 /**
23 * @deprecated Since 1.25. Use TestUser::getUser()
24 * @private
25 * @var User
27 public $user;
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" );
38 public function __construct( $username, $realname = 'Real Name',
39 $email = 'sample@example.com', $groups = array()
40 ) {
41 $this->assertNotReal();
43 $this->username = $username;
44 $this->password = 'TestUser';
46 $this->user = User::newFromName( $this->username );
47 $this->user->load();
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, array(
57 "email" => $email,
58 "real_name" => $realname
62 if ( !$this->user ) {
63 throw new MWException( "Error creating TestUser " . $username );
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 );
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 );
77 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
78 $this->user->removeGroup( $group );
80 if ( $change ) {
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 $passwordFactory = new PasswordFactory();
133 $passwordFactory->init( RequestContext::getMain()->getConfig() );
134 // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
135 $passwordFactory->setDefaultType( 'A' );
136 $pwhash = $passwordFactory->newFromPlaintext( $password );
137 wfGetDB( DB_MASTER )->update(
138 'user',
139 array( 'user_password' => $pwhash->toString() ),
140 array( 'user_id' => $user->getId() ),
141 __METHOD__
146 * @return User
148 public function getUser() {
149 return $this->user;
153 * @return string
155 public function getPassword() {
156 return $this->password;