Merge "Add Special:Login and Special:Logout as aliases."
[lhc/web/wiklou.git] / tests / phpunit / includes / TestUser.php
1 <?php
2
3 /* Wraps the user object, so we can also retain full access to properties like password if we log in via the API */
4 class TestUser {
5 public $username;
6 public $password;
7 public $email;
8 public $groups;
9 public $user;
10
11 function __construct( $username, $realname = 'Real Name', $email = 'sample@example.com', $groups = array() ) {
12 $this->username = $username;
13 $this->realname = $realname;
14 $this->email = $email;
15 $this->groups = $groups;
16
17 // don't allow user to hardcode or select passwords -- people sometimes run tests
18 // on live wikis. Sometimes we create sysop users in these tests. A sysop user with
19 // a known password would be a Bad Thing.
20 $this->password = User::randomPassword();
21
22 $this->user = User::newFromName( $this->username );
23 $this->user->load();
24
25 // In an ideal world we'd have a new wiki (or mock data store) for every single test.
26 // But for now, we just need to create or update the user with the desired properties.
27 // we particularly need the new password, since we just generated it randomly.
28 // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
29 if ( !$this->user->getID() ) {
30 // create the user
31 $this->user = User::createNew(
32 $this->username, array(
33 "email" => $this->email,
34 "real_name" => $this->realname
35 )
36 );
37 if ( !$this->user ) {
38 throw new Exception( "error creating user" );
39 }
40 }
41
42 // update the user to use the new random password and other details
43 $this->user->setPassword( $this->password );
44 $this->user->setEmail( $this->email );
45 $this->user->setRealName( $this->realname );
46 // remove all groups, replace with any groups specified
47 foreach ( $this->user->getGroups() as $group ) {
48 $this->user->removeGroup( $group );
49 }
50 if ( count( $this->groups ) ) {
51 foreach ( $this->groups as $group ) {
52 $this->user->addGroup( $group );
53 }
54 }
55 $this->user->saveSettings();
56 }
57 }