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