MediaWiki
REL1_19
|
00001 <?php 00002 00003 abstract class ApiTestCase extends MediaWikiLangTestCase { 00007 public static $users; 00008 protected static $apiUrl; 00009 00013 protected $apiContext; 00014 00015 function setUp() { 00016 global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer; 00017 00018 parent::setUp(); 00019 self::$apiUrl = $wgServer . wfScript( 'api' ); 00020 $wgMemc = new EmptyBagOStuff(); 00021 $wgContLang = Language::factory( 'en' ); 00022 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' ); 00023 $wgRequest = new FauxRequest( array() ); 00024 00025 self::$users = array( 00026 'sysop' => new ApiTestUser( 00027 'Apitestsysop', 00028 'Api Test Sysop', 00029 '[email protected]', 00030 array( 'sysop' ) 00031 ), 00032 'uploader' => new ApiTestUser( 00033 'Apitestuser', 00034 'Api Test User', 00035 '[email protected]', 00036 array() 00037 ) 00038 ); 00039 00040 $wgUser = self::$users['sysop']->user; 00041 00042 $this->apiContext = new ApiTestContext(); 00043 00044 } 00045 00046 protected function doApiRequest( $params, $session = null, $appendModule = false, $user = null ) { 00047 if ( is_null( $session ) ) { 00048 $session = array(); 00049 } 00050 00051 $context = $this->apiContext->newTestContext( $params, $session, $user ); 00052 $module = new ApiMain( $context, true ); 00053 $module->execute(); 00054 00055 $results = array( 00056 $module->getResultData(), 00057 $context->getRequest(), 00058 $context->getRequest()->getSessionArray() 00059 ); 00060 if( $appendModule ) { 00061 $results[] = $module; 00062 } 00063 00064 return $results; 00065 } 00066 00075 protected function doApiRequestWithToken( $params, $session, $user = null ) { 00076 if ( $session['wsToken'] ) { 00077 // add edit token to fake session 00078 $session['wsEditToken'] = $session['wsToken']; 00079 // add token to request parameters 00080 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX; 00081 return $this->doApiRequest( $params, $session, false, $user ); 00082 } else { 00083 throw new Exception( "request data not in right format" ); 00084 } 00085 } 00086 00087 protected function doLogin() { 00088 $data = $this->doApiRequest( array( 00089 'action' => 'login', 00090 'lgname' => self::$users['sysop']->username, 00091 'lgpassword' => self::$users['sysop']->password ) ); 00092 00093 $token = $data[0]['login']['token']; 00094 00095 $data = $this->doApiRequest( array( 00096 'action' => 'login', 00097 'lgtoken' => $token, 00098 'lgname' => self::$users['sysop']->username, 00099 'lgpassword' => self::$users['sysop']->password 00100 ), $data ); 00101 00102 return $data; 00103 } 00104 00105 protected function getTokenList( $user ) { 00106 $data = $this->doApiRequest( array( 00107 'action' => 'query', 00108 'titles' => 'Main Page', 00109 'intoken' => 'edit|delete|protect|move|block|unblock', 00110 'prop' => 'info' ), false, $user->user ); 00111 return $data; 00112 } 00113 } 00114 00115 class UserWrapper { 00116 public $userName, $password, $user; 00117 00118 public function __construct( $userName, $password, $group = '' ) { 00119 $this->userName = $userName; 00120 $this->password = $password; 00121 00122 $this->user = User::newFromName( $this->userName ); 00123 if ( !$this->user->getID() ) { 00124 $this->user = User::createNew( $this->userName, array( 00125 "email" => "[email protected]", 00126 "real_name" => "Test User" ) ); 00127 } 00128 $this->user->setPassword( $this->password ); 00129 00130 if ( $group !== '' ) { 00131 $this->user->addGroup( $group ); 00132 } 00133 $this->user->saveSettings(); 00134 } 00135 } 00136 00137 class MockApi extends ApiBase { 00138 public function execute() { } 00139 public function getVersion() { } 00140 00141 public function __construct() { } 00142 00143 public function getAllowedParams() { 00144 return array( 00145 'filename' => null, 00146 'enablechunks' => false, 00147 'sessionkey' => null, 00148 ); 00149 } 00150 } 00151 00152 class ApiTestContext extends RequestContext { 00153 00162 public function newTestContext( $params, $session, $user = null ) { 00163 $context = new DerivativeContext( $this ); 00164 $context->setRequest( new FauxRequest( $params, true, $session ) ); 00165 if ( $user !== null ) { 00166 $context->setUser( $user ); 00167 } 00168 return $context; 00169 } 00170 }