PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mediawiki-1.21.2/tests/phpunit/includes/api/ApiTestCase.php

https://gitlab.com/mcepl/dumpathome
PHP | 239 lines | 150 code | 38 blank | 51 comment | 9 complexity | c0b24071d5a5ee41f568948f61b27eac MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. abstract class ApiTestCase extends MediaWikiLangTestCase {
  3. protected static $apiUrl;
  4. /**
  5. * @var ApiTestContext
  6. */
  7. protected $apiContext;
  8. protected function setUp() {
  9. global $wgServer;
  10. parent::setUp();
  11. self::$apiUrl = $wgServer . wfScript( 'api' );
  12. ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
  13. self::$users = array(
  14. 'sysop' => new TestUser(
  15. 'Apitestsysop',
  16. 'Api Test Sysop',
  17. 'api_test_sysop@example.com',
  18. array( 'sysop' )
  19. ),
  20. 'uploader' => new TestUser(
  21. 'Apitestuser',
  22. 'Api Test User',
  23. 'api_test_user@example.com',
  24. array()
  25. )
  26. );
  27. $this->setMwGlobals( array(
  28. 'wgMemc' => new EmptyBagOStuff(),
  29. 'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
  30. 'wgRequest' => new FauxRequest( array() ),
  31. 'wgUser' => self::$users['sysop']->user,
  32. ) );
  33. $this->apiContext = new ApiTestContext();
  34. }
  35. /**
  36. * Edits or creates a page/revision
  37. * @param $pageName string page title
  38. * @param $text string content of the page
  39. * @param $summary string optional summary string for the revision
  40. * @param $defaultNs int optional namespace id
  41. * @return array as returned by WikiPage::doEditContent()
  42. */
  43. protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
  44. $title = Title::newFromText( $pageName, $defaultNs );
  45. $page = WikiPage::factory( $title );
  46. return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
  47. }
  48. /**
  49. * Does the API request and returns the result.
  50. *
  51. * The returned value is an array containing
  52. * - the result data (array)
  53. * - the request (WebRequest)
  54. * - the session data of the request (array)
  55. * - if $appendModule is true, the Api module $module
  56. *
  57. * @param array $params
  58. * @param array|null $session
  59. * @param bool $appendModule
  60. * @param User|null $user
  61. *
  62. * @return array
  63. */
  64. protected function doApiRequest( array $params, array $session = null, $appendModule = false, User $user = null ) {
  65. global $wgRequest, $wgUser;
  66. if ( is_null( $session ) ) {
  67. // re-use existing global session by default
  68. $session = $wgRequest->getSessionArray();
  69. }
  70. // set up global environment
  71. if ( $user ) {
  72. $wgUser = $user;
  73. }
  74. $wgRequest = new FauxRequest( $params, true, $session );
  75. RequestContext::getMain()->setRequest( $wgRequest );
  76. // set up local environment
  77. $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
  78. $module = new ApiMain( $context, true );
  79. // run it!
  80. $module->execute();
  81. // construct result
  82. $results = array(
  83. $module->getResultData(),
  84. $context->getRequest(),
  85. $context->getRequest()->getSessionArray()
  86. );
  87. if ( $appendModule ) {
  88. $results[] = $module;
  89. }
  90. return $results;
  91. }
  92. /**
  93. * Add an edit token to the API request
  94. * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
  95. * request, without actually requesting a "real" edit token
  96. * @param $params Array: key-value API params
  97. * @param $session Array|null: session array
  98. * @param $user User|null A User object for the context
  99. * @return result of the API call
  100. * @throws Exception in case wsToken is not set in the session
  101. */
  102. protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
  103. global $wgRequest;
  104. if ( $session === null ) {
  105. $session = $wgRequest->getSessionArray();
  106. }
  107. if ( $session['wsToken'] ) {
  108. // add edit token to fake session
  109. $session['wsEditToken'] = $session['wsToken'];
  110. // add token to request parameters
  111. $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
  112. return $this->doApiRequest( $params, $session, false, $user );
  113. } else {
  114. throw new Exception( "request data not in right format" );
  115. }
  116. }
  117. protected function doLogin() {
  118. $data = $this->doApiRequest( array(
  119. 'action' => 'login',
  120. 'lgname' => self::$users['sysop']->username,
  121. 'lgpassword' => self::$users['sysop']->password ) );
  122. $token = $data[0]['login']['token'];
  123. $data = $this->doApiRequest(
  124. array(
  125. 'action' => 'login',
  126. 'lgtoken' => $token,
  127. 'lgname' => self::$users['sysop']->username,
  128. 'lgpassword' => self::$users['sysop']->password,
  129. ),
  130. $data[2]
  131. );
  132. return $data;
  133. }
  134. protected function getTokenList( $user, $session = null ) {
  135. $data = $this->doApiRequest( array(
  136. 'action' => 'query',
  137. 'titles' => 'Main Page',
  138. 'intoken' => 'edit|delete|protect|move|block|unblock|watch',
  139. 'prop' => 'info' ), $session, false, $user->user );
  140. return $data;
  141. }
  142. public function testApiTestGroup() {
  143. $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
  144. $constraint = PHPUnit_Framework_Assert::logicalOr(
  145. $this->contains( 'medium' ),
  146. $this->contains( 'large' )
  147. );
  148. $this->assertThat( $groups, $constraint,
  149. 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
  150. );
  151. }
  152. }
  153. class UserWrapper {
  154. public $userName;
  155. public $password;
  156. public $user;
  157. public function __construct( $userName, $password, $group = '' ) {
  158. $this->userName = $userName;
  159. $this->password = $password;
  160. $this->user = User::newFromName( $this->userName );
  161. if ( !$this->user->getID() ) {
  162. $this->user = User::createNew( $this->userName, array(
  163. "email" => "test@example.com",
  164. "real_name" => "Test User" ) );
  165. }
  166. $this->user->setPassword( $this->password );
  167. if ( $group !== '' ) {
  168. $this->user->addGroup( $group );
  169. }
  170. $this->user->saveSettings();
  171. }
  172. }
  173. class MockApi extends ApiBase {
  174. public function execute() {}
  175. public function getVersion() {}
  176. public function __construct() {}
  177. public function getAllowedParams() {
  178. return array(
  179. 'filename' => null,
  180. 'enablechunks' => false,
  181. 'sessionkey' => null,
  182. );
  183. }
  184. }
  185. class ApiTestContext extends RequestContext {
  186. /**
  187. * Returns a DerivativeContext with the request variables in place
  188. *
  189. * @param $request WebRequest request object including parameters and session
  190. * @param $user User or null
  191. * @return DerivativeContext
  192. */
  193. public function newTestContext( WebRequest $request, User $user = null ) {
  194. $context = new DerivativeContext( $this );
  195. $context->setRequest( $request );
  196. if ( $user !== null ) {
  197. $context->setUser( $user );
  198. }
  199. return $context;
  200. }
  201. }