4 * Wraps the user object, so we can also retain full access to properties like password if we log in via the API
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;
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();
24 $this->user
= User
::newFromName( $this->username
);
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() ) {
33 $this->user
= User
::createNew(
34 $this->username
, array(
35 "email" => $this->email
,
36 "real_name" => $this->realname
40 throw new Exception( "error creating user" );
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 // remove all groups, replace with any groups specified
49 foreach ( $this->user
->getGroups() as $group ) {
50 $this->user
->removeGroup( $group );
52 if ( count( $this->groups
) ) {
53 foreach ( $this->groups
as $group ) {
54 $this->user
->addGroup( $group );
57 $this->user
->saveSettings();