Make session persist between calls to doApiRequest
authordaniel <daniel.kinzler@wikimedia.de>
Fri, 22 Jun 2012 20:42:42 +0000 (22:42 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Fri, 22 Jun 2012 21:22:34 +0000 (23:22 +0200)
Make sure the global session data in $wgRequest is used for doApiRequest
per default, and return it's content among with the request's results.

Previously, an empty session was used per default, and the local context's
session data would get out of sync with $wgRequest.

This change allows for the following assumptions to hold in test cases:

* within the same function, changes to the session made by one api call
  will be visible to subsequent api calls.

* the session data returned by doApiRequest is the actual status of the
  session as manipulated by the api call. This session data can be passed
  to subsequent api calls.

Note that the session data is still reset for every call to a test
function.

Change-Id: Ia20cf0ccfcdca736dd5da3444b14fbdd1c5def46

tests/phpunit/includes/api/ApiTestCase.php
tests/phpunit/includes/upload/UploadFromUrlTest.php

index 23739bf..f81bb6d 100644 (file)
@@ -43,18 +43,31 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
 
        }
 
-       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(),
@@ -71,11 +84,11 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
         * 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'];
@@ -100,17 +113,17 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
                        '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;
        }
 }
@@ -157,14 +170,13 @@ class ApiTestContext extends RequestContext {
        /**
         * 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 );
                }
index c6498fa..f66c387 100644 (file)
@@ -20,7 +20,7 @@ class UploadFromUrlTest extends ApiTestCase {
                }
        }
 
-       protected function doApiRequest( $params, $unused = null, $appendModule = false, $user = null ) {
+       protected function doApiRequest( Array $params, Array $unused = null, $appendModule = false, User $user = null ) {
                $sessionId = session_id();
                session_write_close();