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