Followup r91270: didn't need to be so paranoid about $_SESSION, and actually was...
[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 function setUp() {
11 global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer;
12
13 parent::setUp();
14 self::$apiUrl = $wgServer . wfScript( 'api' );
15 $wgMemc = new EmptyBagOStuff();
16 $wgContLang = Language::factory( 'en' );
17 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
18 $wgRequest = new FauxRequest( array() );
19
20 self::$users = array(
21 'sysop' => new ApiTestUser(
22 'Apitestsysop',
23 'Api Test Sysop',
24 'api_test_sysop@sample.com',
25 array( 'sysop' )
26 ),
27 'uploader' => new ApiTestUser(
28 'Apitestuser',
29 'Api Test User',
30 'api_test_user@sample.com',
31 array()
32 )
33 );
34
35 $wgUser = self::$users['sysop']->user;
36
37 }
38
39 protected function doApiRequest( $params, $session = null, $appendModule = false ) {
40 if ( is_null( $session ) ) {
41 $session = array();
42 }
43
44 $request = new FauxRequest( $params, true, $session );
45 $module = new ApiMain( $request, true );
46 $module->execute();
47
48 $results = array( $module->getResultData(), $request, $request->getSessionArray() );
49 if( $appendModule ) {
50 $results[] = $module;
51 }
52
53 return $results;
54 }
55
56 /**
57 * Add an edit token to the API request
58 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
59 * request, without actually requesting a "real" edit token
60 * @param $params: key-value API params
61 * @param $session: session array
62 */
63 protected function doApiRequestWithToken( $params, $session ) {
64 if ( $session['wsToken'] ) {
65 // add edit token to fake session
66 $session['wsEditToken'] = $session['wsToken'];
67 // add token to request parameters
68 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
69 return $this->doApiRequest( $params, $session );
70 } else {
71 throw new Exception( "request data not in right format" );
72 }
73 }
74
75 protected function doLogin() {
76 $data = $this->doApiRequest( array(
77 'action' => 'login',
78 'lgname' => self::$users['sysop']->username,
79 'lgpassword' => self::$users['sysop']->password ) );
80
81 $token = $data[0]['login']['token'];
82
83 $data = $this->doApiRequest( array(
84 'action' => 'login',
85 'lgtoken' => $token,
86 'lgname' => self::$users['sysop']->username,
87 'lgpassword' => self::$users['sysop']->password
88 ), $data );
89
90 return $data;
91 }
92
93 protected function getTokenList( $user ) {
94 $GLOBALS['wgUser'] = $user->user;
95 $data = $this->doApiRequest( array(
96 'action' => 'query',
97 'titles' => 'Main Page',
98 'intoken' => 'edit|delete|protect|move|block|unblock',
99 'prop' => 'info' ) );
100 return $data;
101 }
102 }
103
104 class UserWrapper {
105 public $userName, $password, $user;
106
107 public function __construct( $userName, $password, $group = '' ) {
108 $this->userName = $userName;
109 $this->password = $password;
110
111 $this->user = User::newFromName( $this->userName );
112 if ( !$this->user->getID() ) {
113 $this->user = User::createNew( $this->userName, array(
114 "email" => "test@example.com",
115 "real_name" => "Test User" ) );
116 }
117 $this->user->setPassword( $this->password );
118
119 if ( $group !== '' ) {
120 $this->user->addGroup( $group );
121 }
122 $this->user->saveSettings();
123 }
124 }
125
126 class MockApi extends ApiBase {
127 public function execute() { }
128 public function getVersion() { }
129
130 public function __construct() { }
131
132 public function getAllowedParams() {
133 return array(
134 'filename' => null,
135 'enablechunks' => false,
136 'sessionkey' => null,
137 );
138 }
139 }