MediaWiki  REL1_22
ApiTestCase.php
Go to the documentation of this file.
00001 <?php
00002 
00003 abstract class ApiTestCase extends MediaWikiLangTestCase {
00004     protected static $apiUrl;
00005 
00009     protected $apiContext;
00010 
00011     protected function setUp() {
00012         global $wgServer;
00013 
00014         parent::setUp();
00015         self::$apiUrl = $wgServer . wfScript( 'api' );
00016 
00017         ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
00018 
00019         self::$users = array(
00020             'sysop' => new TestUser(
00021                 'Apitestsysop',
00022                 'Api Test Sysop',
00023                 '[email protected]',
00024                 array( 'sysop' )
00025             ),
00026             'uploader' => new TestUser(
00027                 'Apitestuser',
00028                 'Api Test User',
00029                 '[email protected]',
00030                 array()
00031             )
00032         );
00033 
00034         $this->setMwGlobals( array(
00035             'wgMemc' => new EmptyBagOStuff(),
00036             'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
00037             'wgRequest' => new FauxRequest( array() ),
00038             'wgUser' => self::$users['sysop']->user,
00039         ) );
00040 
00041         $this->apiContext = new ApiTestContext();
00042     }
00043 
00052     protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
00053         $title = Title::newFromText( $pageName, $defaultNs );
00054         $page = WikiPage::factory( $title );
00055 
00056         return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
00057     }
00058 
00075     protected function doApiRequest( array $params, array $session = null, $appendModule = false, User $user = null ) {
00076         global $wgRequest, $wgUser;
00077 
00078         if ( is_null( $session ) ) {
00079             // re-use existing global session by default
00080             $session = $wgRequest->getSessionArray();
00081         }
00082 
00083         // set up global environment
00084         if ( $user ) {
00085             $wgUser = $user;
00086         }
00087 
00088         $wgRequest = new FauxRequest( $params, true, $session );
00089         RequestContext::getMain()->setRequest( $wgRequest );
00090 
00091         // set up local environment
00092         $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
00093 
00094         $module = new ApiMain( $context, true );
00095 
00096         // run it!
00097         $module->execute();
00098 
00099         // construct result
00100         $results = array(
00101             $module->getResultData(),
00102             $context->getRequest(),
00103             $context->getRequest()->getSessionArray()
00104         );
00105 
00106         if ( $appendModule ) {
00107             $results[] = $module;
00108         }
00109 
00110         return $results;
00111     }
00112 
00123     protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
00124         global $wgRequest;
00125 
00126         if ( $session === null ) {
00127             $session = $wgRequest->getSessionArray();
00128         }
00129 
00130         if ( $session['wsToken'] ) {
00131             // add edit token to fake session
00132             $session['wsEditToken'] = $session['wsToken'];
00133             // add token to request parameters
00134             $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
00135 
00136             return $this->doApiRequest( $params, $session, false, $user );
00137         } else {
00138             throw new Exception( "request data not in right format" );
00139         }
00140     }
00141 
00142     protected function doLogin( $user = 'sysop' ) {
00143         if ( !array_key_exists( $user, self::$users ) ) {
00144             throw new MWException( "Can not log in to undefined user $user" );
00145         }
00146 
00147         $data = $this->doApiRequest( array(
00148             'action' => 'login',
00149             'lgname' => self::$users[ $user ]->username,
00150             'lgpassword' => self::$users[ $user ]->password ) );
00151 
00152         $token = $data[0]['login']['token'];
00153 
00154         $data = $this->doApiRequest(
00155             array(
00156                 'action' => 'login',
00157                 'lgtoken' => $token,
00158                 'lgname' => self::$users[ $user ]->username,
00159                 'lgpassword' => self::$users[ $user ]->password,
00160             ),
00161             $data[2]
00162         );
00163 
00164         return $data;
00165     }
00166 
00167     protected function getTokenList( $user, $session = null ) {
00168         $data = $this->doApiRequest( array(
00169             'action' => 'tokens',
00170             'type' => 'edit|delete|protect|move|block|unblock|watch'
00171         ), $session, false, $user->user );
00172 
00173         if ( !array_key_exists( 'tokens', $data[0] ) ) {
00174             throw new MWException( 'Api failed to return a token list' );
00175         }
00176 
00177         return $data[0]['tokens'];
00178     }
00179 
00180     public function testApiTestGroup() {
00181         $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
00182         $constraint = PHPUnit_Framework_Assert::logicalOr(
00183             $this->contains( 'medium' ),
00184             $this->contains( 'large' )
00185         );
00186         $this->assertThat( $groups, $constraint,
00187             'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
00188         );
00189     }
00190 }
00191 
00192 class UserWrapper {
00193     public $userName;
00194     public $password;
00195     public $user;
00196 
00197     public function __construct( $userName, $password, $group = '' ) {
00198         $this->userName = $userName;
00199         $this->password = $password;
00200 
00201         $this->user = User::newFromName( $this->userName );
00202         if ( !$this->user->getID() ) {
00203             $this->user = User::createNew( $this->userName, array(
00204                 "email" => "[email protected]",
00205                 "real_name" => "Test User" ) );
00206         }
00207         $this->user->setPassword( $this->password );
00208 
00209         if ( $group !== '' ) {
00210             $this->user->addGroup( $group );
00211         }
00212         $this->user->saveSettings();
00213     }
00214 }
00215 
00216 class MockApi extends ApiBase {
00217     public function execute() {
00218     }
00219 
00220     public function getVersion() {
00221     }
00222 
00223     public function __construct() {
00224     }
00225 
00226     public function getAllowedParams() {
00227         return array(
00228             'filename' => null,
00229             'enablechunks' => false,
00230             'sessionkey' => null,
00231         );
00232     }
00233 }
00234 
00235 class ApiTestContext extends RequestContext {
00236 
00244     public function newTestContext( WebRequest $request, User $user = null ) {
00245         $context = new DerivativeContext( $this );
00246         $context->setRequest( $request );
00247         if ( $user !== null ) {
00248             $context->setUser( $user );
00249         }
00250 
00251         return $context;
00252     }
00253 }