Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / TestUser.php
blob754568d0b23fc5dc75dca41445d72bbc978d776a
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 && $wgDBprefix !== MediaWikiTestCase::ORA_DB_PREFIX ) {
32 throw new MWException( "Can't create user on real database" );
36 public function __construct( $username, $realname = 'Real Name',
37 $email = 'sample@example.com', $groups = array()
38 ) {
39 $this->assertNotReal();
41 $this->username = $username;
42 $this->password = 'TestUser';
44 $this->user = User::newFromName( $this->username );
45 $this->user->load();
47 // In an ideal world we'd have a new wiki (or mock data store) for every single test.
48 // But for now, we just need to create or update the user with the desired properties.
49 // we particularly need the new password, since we just generated it randomly.
50 // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
51 if ( !$this->user->isLoggedIn() ) {
52 // create the user
53 $this->user = User::createNew(
54 $this->username, array(
55 "email" => $email,
56 "real_name" => $realname
60 if ( !$this->user ) {
61 throw new MWException( "Error creating TestUser " . $username );
65 // Update the user to use the password and other details
66 $change = $this->setPassword( $this->password ) ||
67 $this->setEmail( $email ) ||
68 $this->setRealName( $realname );
70 // Adjust groups by adding any missing ones and removing any extras
71 $currentGroups = $this->user->getGroups();
72 foreach ( array_diff( $groups, $currentGroups ) as $group ) {
73 $this->user->addGroup( $group );
75 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
76 $this->user->removeGroup( $group );
78 if ( $change ) {
79 $this->user->saveSettings();
83 /**
84 * @param string $realname
85 * @return bool
87 private function setRealName( $realname ) {
88 if ( $this->user->getRealName() !== $realname ) {
89 $this->user->setRealName( $realname );
90 return true;
93 return false;
96 /**
97 * @param string $email
98 * @return bool
100 private function setEmail( $email ) {
101 if ( $this->user->getEmail() !== $email ) {
102 $this->user->setEmail( $email );
103 return true;
106 return false;
110 * @param string $password
111 * @return bool
113 private function setPassword( $password ) {
114 $passwordFactory = $this->user->getPasswordFactory();
115 $oldDefaultType = $passwordFactory->getDefaultType();
117 // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
118 $passwordFactory->setDefaultType( 'A' );
119 $newPassword = $passwordFactory->newFromPlaintext( $password, $this->user->getPassword() );
121 $change = false;
122 if ( !$this->user->getPassword()->equals( $newPassword ) ) {
123 // Password changed
124 $this->user->setPassword( $password );
125 $change = true;
128 $passwordFactory->setDefaultType( $oldDefaultType );
130 return $change;
134 * @return User
136 public function getUser() {
137 return $this->user;
141 * @return string
143 public function getPassword() {
144 return $this->password;