Followup r100905, fixed the ApiTests
[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@sample.com',
30 array( 'sysop' )
31 ),
32 'uploader' => new ApiTestUser(
33 'Apitestuser',
34 'Api Test User',
35 'api_test_user@sample.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 ) {
47 if ( is_null( $session ) ) {
48 $session = array();
49 }
50
51 $context = $this->apiContext->newTestContext( $params, $session );
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 */
74 protected function doApiRequestWithToken( $params, $session ) {
75 if ( $session['wsToken'] ) {
76 // add edit token to fake session
77 $session['wsEditToken'] = $session['wsToken'];
78 // add token to request parameters
79 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
80 return $this->doApiRequest( $params, $session );
81 } else {
82 throw new Exception( "request data not in right format" );
83 }
84 }
85
86 protected function doLogin() {
87 $data = $this->doApiRequest( array(
88 'action' => 'login',
89 'lgname' => self::$users['sysop']->username,
90 'lgpassword' => self::$users['sysop']->password ) );
91
92 $token = $data[0]['login']['token'];
93
94 $data = $this->doApiRequest( array(
95 'action' => 'login',
96 'lgtoken' => $token,
97 'lgname' => self::$users['sysop']->username,
98 'lgpassword' => self::$users['sysop']->password
99 ), $data );
100
101 return $data;
102 }
103
104 protected function getTokenList( $user ) {
105 $GLOBALS['wgUser'] = $user->user;
106 $data = $this->doApiRequest( array(
107 'action' => 'query',
108 'titles' => 'Main Page',
109 'intoken' => 'edit|delete|protect|move|block|unblock',
110 'prop' => 'info' ) );
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 * @return DerivativeContext
160 */
161 public function newTestContext( $params, $session ) {
162 $context = new DerivativeContext( $this );
163 $context->setRequest( new FauxRequest( $params, true, $session ) );
164 return $context;
165 }
166 }