Fix doxygen docs before REL1_19 branching
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTestCase.php
1 <?php
2
3 abstract class ApiTestCase extends MediaWikiLangTestCase {
4 /**
5 * @var Array of ApiTestUser
6 */
7 public static $users;
8 protected static $apiUrl;
9
10 /**
11 * @var ApiTestContext
12 */
13 protected $apiContext;
14
15 function setUp() {
16 global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer;
17
18 parent::setUp();
19 self::$apiUrl = $wgServer . wfScript( 'api' );
20 $wgMemc = new EmptyBagOStuff();
21 $wgContLang = Language::factory( 'en' );
22 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
23 $wgRequest = new FauxRequest( array() );
24
25 self::$users = array(
26 'sysop' => new ApiTestUser(
27 'Apitestsysop',
28 'Api Test Sysop',
29 'api_test_sysop@example.com',
30 array( 'sysop' )
31 ),
32 'uploader' => new ApiTestUser(
33 'Apitestuser',
34 'Api Test User',
35 'api_test_user@example.com',
36 array()
37 )
38 );
39
40 $wgUser = self::$users['sysop']->user;
41
42 $this->apiContext = new ApiTestContext();
43
44 }
45
46 protected function doApiRequest( $params, $session = null, $appendModule = false, $user = null ) {
47 if ( is_null( $session ) ) {
48 $session = array();
49 }
50
51 $context = $this->apiContext->newTestContext( $params, $session, $user );
52 $module = new ApiMain( $context, true );
53 $module->execute();
54
55 $results = array(
56 $module->getResultData(),
57 $context->getRequest(),
58 $context->getRequest()->getSessionArray()
59 );
60 if( $appendModule ) {
61 $results[] = $module;
62 }
63
64 return $results;
65 }
66
67 /**
68 * Add an edit token to the API request
69 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
70 * request, without actually requesting a "real" edit token
71 * @param $params: key-value API params
72 * @param $session: session array
73 * @param $user String|null A User object for the context
74 */
75 protected function doApiRequestWithToken( $params, $session, $user = null ) {
76 if ( $session['wsToken'] ) {
77 // add edit token to fake session
78 $session['wsEditToken'] = $session['wsToken'];
79 // add token to request parameters
80 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
81 return $this->doApiRequest( $params, $session, false, $user );
82 } else {
83 throw new Exception( "request data not in right format" );
84 }
85 }
86
87 protected function doLogin() {
88 $data = $this->doApiRequest( array(
89 'action' => 'login',
90 'lgname' => self::$users['sysop']->username,
91 'lgpassword' => self::$users['sysop']->password ) );
92
93 $token = $data[0]['login']['token'];
94
95 $data = $this->doApiRequest( array(
96 'action' => 'login',
97 'lgtoken' => $token,
98 'lgname' => self::$users['sysop']->username,
99 'lgpassword' => self::$users['sysop']->password
100 ), $data );
101
102 return $data;
103 }
104
105 protected function getTokenList( $user ) {
106 $data = $this->doApiRequest( array(
107 'action' => 'query',
108 'titles' => 'Main Page',
109 'intoken' => 'edit|delete|protect|move|block|unblock',
110 'prop' => 'info' ), false, $user->user );
111 return $data;
112 }
113 }
114
115 class UserWrapper {
116 public $userName, $password, $user;
117
118 public function __construct( $userName, $password, $group = '' ) {
119 $this->userName = $userName;
120 $this->password = $password;
121
122 $this->user = User::newFromName( $this->userName );
123 if ( !$this->user->getID() ) {
124 $this->user = User::createNew( $this->userName, array(
125 "email" => "test@example.com",
126 "real_name" => "Test User" ) );
127 }
128 $this->user->setPassword( $this->password );
129
130 if ( $group !== '' ) {
131 $this->user->addGroup( $group );
132 }
133 $this->user->saveSettings();
134 }
135 }
136
137 class MockApi extends ApiBase {
138 public function execute() { }
139 public function getVersion() { }
140
141 public function __construct() { }
142
143 public function getAllowedParams() {
144 return array(
145 'filename' => null,
146 'enablechunks' => false,
147 'sessionkey' => null,
148 );
149 }
150 }
151
152 class ApiTestContext extends RequestContext {
153
154 /**
155 * Returns a DerivativeContext with the request variables in place
156 *
157 * @param $params Array key-value API params
158 * @param $session Array session data
159 * @param $user User or null
160 * @return DerivativeContext
161 */
162 public function newTestContext( $params, $session, $user = null ) {
163 $context = new DerivativeContext( $this );
164 $context->setRequest( new FauxRequest( $params, true, $session ) );
165 if ( $user !== null ) {
166 $context->setUser( $user );
167 }
168 return $context;
169 }
170 }