2011-04-06 19:50:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* Wraps the user object, so we can also retain full access to properties like password if we log in via the API
|
|
|
|
|
*/
|
2012-09-11 00:07:18 +00:00
|
|
|
class TestUser {
|
2011-04-06 19:50:54 +00:00
|
|
|
public $username;
|
|
|
|
|
public $password;
|
|
|
|
|
public $email;
|
|
|
|
|
public $groups;
|
|
|
|
|
public $user;
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function __construct( $username, $realname = 'Real Name', $email = 'sample@example.com', $groups = array() ) {
|
2011-04-06 19:50:54 +00:00
|
|
|
$this->username = $username;
|
|
|
|
|
$this->realname = $realname;
|
|
|
|
|
$this->email = $email;
|
|
|
|
|
$this->groups = $groups;
|
|
|
|
|
|
|
|
|
|
// don't allow user to hardcode or select passwords -- people sometimes run tests
|
|
|
|
|
// on live wikis. Sometimes we create sysop users in these tests. A sysop user with
|
|
|
|
|
// a known password would be a Bad Thing.
|
|
|
|
|
$this->password = User::randomPassword();
|
|
|
|
|
|
|
|
|
|
$this->user = User::newFromName( $this->username );
|
|
|
|
|
$this->user->load();
|
|
|
|
|
|
|
|
|
|
// In an ideal world we'd have a new wiki (or mock data store) for every single test.
|
|
|
|
|
// But for now, we just need to create or update the user with the desired properties.
|
|
|
|
|
// we particularly need the new password, since we just generated it randomly.
|
|
|
|
|
// In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
|
|
|
|
|
if ( !$this->user->getID() ) {
|
|
|
|
|
// create the user
|
|
|
|
|
$this->user = User::createNew(
|
|
|
|
|
$this->username, array(
|
|
|
|
|
"email" => $this->email,
|
|
|
|
|
"real_name" => $this->realname
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
if ( !$this->user ) {
|
|
|
|
|
throw new Exception( "error creating user" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update the user to use the new random password and other details
|
|
|
|
|
$this->user->setPassword( $this->password );
|
|
|
|
|
$this->user->setEmail( $this->email );
|
|
|
|
|
$this->user->setRealName( $this->realname );
|
2013-12-05 17:41:24 +00:00
|
|
|
|
|
|
|
|
// Adjust groups by adding any missing ones and removing any extras
|
|
|
|
|
$currentGroups = $this->user->getGroups();
|
|
|
|
|
foreach ( array_diff( $this->groups, $currentGroups ) as $group ) {
|
|
|
|
|
$this->user->addGroup( $group );
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
2013-12-05 17:41:24 +00:00
|
|
|
foreach ( array_diff( $currentGroups, $this->groups ) as $group ) {
|
|
|
|
|
$this->user->removeGroup( $group );
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
$this->user->saveSettings();
|
|
|
|
|
}
|
|
|
|
|
}
|