}
- protected function doApiRequest( $params, $session = null, $appendModule = false, $user = null ) {
- if ( is_null( $session ) ) {
- # use global session by default
+ protected function doApiRequest( Array $params, Array $session = null, $appendModule = false, User $user = null ) {
+ global $wgRequest, $wgUser;
- global $wgRequest;
+ if ( is_null( $session ) ) {
+ # re-use existing global session by default
$session = $wgRequest->getSessionArray();
}
- $context = $this->apiContext->newTestContext( $params, $session, $user );
+ # set up global environment
+ if ( $user ) {
+ $wgUser = $user;
+ }
+
+ $wgRequest = new FauxRequest( $params, true, $session );
+ RequestContext::getMain()->setRequest( $wgRequest );
+
+ # set up local environment
+ $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
+
$module = new ApiMain( $context, true );
+
+ # run it!
$module->execute();
+ # construct result
$results = array(
$module->getResultData(),
$context->getRequest(),
* Add an edit token to the API request
* This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
* request, without actually requesting a "real" edit token
- * @param $params: key-value API params
- * @param $session: session array
- * @param $user String|null A User object for the context
+ * @param $params Array: key-value API params
+ * @param $session Array: session array
+ * @param $user User|null A User object for the context
*/
- protected function doApiRequestWithToken( $params, $session, $user = null ) {
+ protected function doApiRequestWithToken( Array $params, Array $session, User $user = null ) {
if ( $session['wsToken'] ) {
// add edit token to fake session
$session['wsEditToken'] = $session['wsToken'];
'lgtoken' => $token,
'lgname' => self::$users['sysop']->username,
'lgpassword' => self::$users['sysop']->password
- ), $data );
+ ), $data[2] );
return $data;
}
- protected function getTokenList( $user ) {
+ protected function getTokenList( $user, $session = null ) {
$data = $this->doApiRequest( array(
'action' => 'query',
'titles' => 'Main Page',
'intoken' => 'edit|delete|protect|move|block|unblock',
- 'prop' => 'info' ), false, $user->user );
+ 'prop' => 'info' ), $session, false, $user->user );
return $data;
}
}
/**
* Returns a DerivativeContext with the request variables in place
*
- * @param $params Array key-value API params
- * @param $session Array session data
+ * @param $request WebRequest request object including parameters and session
* @param $user User or null
* @return DerivativeContext
*/
- public function newTestContext( $params, $session, $user = null ) {
+ public function newTestContext( WebRequest $request, User $user = null ) {
$context = new DerivativeContext( $this );
- $context->setRequest( new FauxRequest( $params, true, $session ) );
+ $context->setRequest( $request );
if ( $user !== null ) {
$context->setUser( $user );
}