Merge "Provide direction hinting in the personal toolbar"
[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 like password if we log in via the API
5 */
6 class TestUser {
7 public $username;
8 public $password;
9 public $email;
10 public $groups;
11 public $user;
12
13 public function __construct( $username, $realname = 'Real Name', $email = 'sample@example.com', $groups = array() ) {
14 $this->username = $username;
15 $this->realname = $realname;
16 $this->email = $email;
17 $this->groups = $groups;
18
19 // don't allow user to hardcode or select passwords -- people sometimes run tests
20 // on live wikis. Sometimes we create sysop users in these tests. A sysop user with
21 // a known password would be a Bad Thing.
22 $this->password = User::randomPassword();
23
24 $this->user = User::newFromName( $this->username );
25 $this->user->load();
26
27 // In an ideal world we'd have a new wiki (or mock data store) for every single test.
28 // But for now, we just need to create or update the user with the desired properties.
29 // we particularly need the new password, since we just generated it randomly.
30 // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
31 if ( !$this->user->getID() ) {
32 // create the user
33 $this->user = User::createNew(
34 $this->username, array(
35 "email" => $this->email,
36 "real_name" => $this->realname
37 )
38 );
39 if ( !$this->user ) {
40 throw new Exception( "error creating user" );
41 }
42 }
43
44 // update the user to use the new random password and other details
45 $this->user->setPassword( $this->password );
46 $this->user->setEmail( $this->email );
47 $this->user->setRealName( $this->realname );
48
49 // Adjust groups by adding any missing ones and removing any extras
50 $currentGroups = $this->user->getGroups();
51 foreach ( array_diff( $this->groups, $currentGroups ) as $group ) {
52 $this->user->addGroup( $group );
53 }
54 foreach ( array_diff( $currentGroups, $this->groups ) as $group ) {
55 $this->user->removeGroup( $group );
56 }
57 $this->user->saveSettings();
58 }
59 }