MediaWiki  REL1_24
TestUser.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class TestUser {
00008     public $username;
00009     public $password;
00010     public $email;
00011     public $groups;
00012     public $user;
00013 
00014     public function __construct( $username, $realname = 'Real Name',
00015         $email = '[email protected]', $groups = array()
00016     ) {
00017         $this->username = $username;
00018         $this->realname = $realname;
00019         $this->email = $email;
00020         $this->groups = $groups;
00021 
00022         // don't allow user to hardcode or select passwords -- people sometimes run tests
00023         // on live wikis. Sometimes we create sysop users in these tests. A sysop user with
00024         // a known password would be a Bad Thing.
00025         $this->password = User::randomPassword();
00026 
00027         $this->user = User::newFromName( $this->username );
00028         $this->user->load();
00029 
00030         // In an ideal world we'd have a new wiki (or mock data store) for every single test.
00031         // But for now, we just need to create or update the user with the desired properties.
00032         // we particularly need the new password, since we just generated it randomly.
00033         // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
00034         if ( !$this->user->getID() ) {
00035             // create the user
00036             $this->user = User::createNew(
00037                 $this->username, array(
00038                     "email" => $this->email,
00039                     "real_name" => $this->realname
00040                 )
00041             );
00042             if ( !$this->user ) {
00043                 throw new Exception( "error creating user" );
00044             }
00045         }
00046 
00047         // update the user to use the new random password and other details
00048         $this->user->setPassword( $this->password );
00049         $this->user->setEmail( $this->email );
00050         $this->user->setRealName( $this->realname );
00051 
00052         // Adjust groups by adding any missing ones and removing any extras
00053         $currentGroups = $this->user->getGroups();
00054         foreach ( array_diff( $this->groups, $currentGroups ) as $group ) {
00055             $this->user->addGroup( $group );
00056         }
00057         foreach ( array_diff( $currentGroups, $this->groups ) as $group ) {
00058             $this->user->removeGroup( $group );
00059         }
00060         $this->user->saveSettings();
00061     }
00062 }