test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / TestUser.php
blob2fb0f499c51abee911209839487e225273da25a4
1 <?php
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;
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;
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();
22 $this->user = User::newFromName( $this->username );
23 $this->user->load();
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
37 if ( !$this->user ) {
38 throw new Exception( "error creating user" );
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 );
50 if ( count( $this->groups ) ) {
51 foreach ( $this->groups as $group ) {
52 $this->user->addGroup( $group );
55 $this->user->saveSettings();