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