r101024 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101023‎ | r101024 | r101025 >
Date:18:46, 27 October 2011
Author:ialex
Status:ok
Tags:
Comment:
Fix for r100905:
* Make tests work again
* Added ApiTestCase::doApiRequest() and related to pass it to the context and removed override of $wgUser where possible
* Fix ApiLogin to get the correct User object (i.e. the logged in one)
* Fix ApiBase to feed a RequestContext to setContext() so that ApiLogin can call setUser()
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiLogin.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/api/ApiBlockTest.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/api/ApiTestCase.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/api/ApiUploadTest.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php
@@ -20,7 +20,7 @@
2121 }
2222 }
2323
24 - protected function doApiRequest( $params, $unused = null, $appendModule = false ) {
 24+ protected function doApiRequest( $params, $unused = null, $appendModule = false, $user = null ) {
2525 $sessionId = session_id();
2626 session_write_close();
2727
Index: trunk/phase3/tests/phpunit/includes/api/ApiUploadTest.php
@@ -79,14 +79,11 @@
8080 * @depends testLogin
8181 */
8282 public function testUploadMissingParams( $session ) {
83 - global $wgUser;
84 - $wgUser = self::$users['uploader']->user;
85 -
8683 $exception = false;
8784 try {
8885 $this->doApiRequestWithToken( array(
8986 'action' => 'upload',
90 - ), $session );
 87+ ), $session, self::$users['uploader']->user );
9188 } catch ( UsageException $e ) {
9289 $exception = true;
9390 $this->assertEquals( "One of the parameters filekey, file, url, statuskey is required",
@@ -100,9 +97,6 @@
10198 * @depends testLogin
10299 */
103100 public function testUpload( $session ) {
104 - global $wgUser;
105 - $wgUser = self::$users['uploader']->user;
106 -
107101 $extension = 'png';
108102 $mimeType = 'image/png';
109103
@@ -136,7 +130,8 @@
137131
138132 $exception = false;
139133 try {
140 - list( $result, , ) = $this->doApiRequestWithToken( $params, $session );
 134+ list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
 135+ self::$users['uploader']->user );
141136 } catch ( UsageException $e ) {
142137 $exception = true;
143138 }
@@ -156,9 +151,6 @@
157152 * @depends testLogin
158153 */
159154 public function testUploadZeroLength( $session ) {
160 - global $wgUser;
161 - $wgUser = self::$users['uploader']->user;
162 -
163155 $mimeType = 'image/png';
164156
165157 $filePath = tempnam( wfTempDir(), "" );
@@ -180,7 +172,7 @@
181173
182174 $exception = false;
183175 try {
184 - $this->doApiRequestWithToken( $params, $session );
 176+ $this->doApiRequestWithToken( $params, $session, self::$users['uploader']->user );
185177 } catch ( UsageException $e ) {
186178 $this->assertContains( 'The file you submitted was empty', $e->getMessage() );
187179 $exception = true;
@@ -197,9 +189,6 @@
198190 * @depends testLogin
199191 */
200192 public function testUploadSameFileName( $session ) {
201 - global $wgUser;
202 - $wgUser = self::$users['uploader']->user;
203 -
204193 $extension = 'png';
205194 $mimeType = 'image/png';
206195
@@ -234,7 +223,8 @@
235224
236225 $exception = false;
237226 try {
238 - list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session );
 227+ list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
 228+ self::$users['uploader']->user );
239229 } catch ( UsageException $e ) {
240230 $exception = true;
241231 }
@@ -250,7 +240,8 @@
251241
252242 $exception = false;
253243 try {
254 - list( $result, , ) = $this->doApiRequestWithToken( $params, $session );
 244+ list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
 245+ self::$users['uploader']->user );
255246 } catch ( UsageException $e ) {
256247 $exception = true;
257248 }
@@ -271,9 +262,6 @@
272263 * @depends testLogin
273264 */
274265 public function testUploadSameContent( $session ) {
275 - global $wgUser;
276 - $wgUser = self::$users['uploader']->user;
277 -
278266 $extension = 'png';
279267 $mimeType = 'image/png';
280268
@@ -309,7 +297,8 @@
310298
311299 $exception = false;
312300 try {
313 - list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session );
 301+ list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session,
 302+ self::$users['uploader']->user );
314303 } catch ( UsageException $e ) {
315304 $exception = true;
316305 }
@@ -334,7 +323,8 @@
335324
336325 $exception = false;
337326 try {
338 - list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session );
 327+ list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session,
 328+ self::$users['uploader']->user );
339329 } catch ( UsageException $e ) {
340330 $exception = true;
341331 }
@@ -356,7 +346,7 @@
357347 */
358348 public function testUploadStash( $session ) {
359349 global $wgUser;
360 - $wgUser = self::$users['uploader']->user;
 350+ $wgUser = self::$users['uploader']->user; // @todo FIXME: still used somewhere
361351
362352 $extension = 'png';
363353 $mimeType = 'image/png';
@@ -391,7 +381,8 @@
392382
393383 $exception = false;
394384 try {
395 - list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session );
 385+ list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session,
 386+ self::$users['uploader']->user );
396387 } catch ( UsageException $e ) {
397388 $exception = true;
398389 }
@@ -419,7 +410,8 @@
420411 $this->clearFakeUploads();
421412 $exception = false;
422413 try {
423 - list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session );
 414+ list( $result, $request, $session ) = $this->doApiRequestWithToken( $params, $session,
 415+ self::$users['uploader']->user );
424416 } catch ( UsageException $e ) {
425417 $exception = true;
426418 }
Index: trunk/phase3/tests/phpunit/includes/api/ApiBlockTest.php
@@ -47,7 +47,7 @@
4848 'action' => 'block',
4949 'user' => 'UTApiBlockee',
5050 'reason' => 'Some reason',
51 - 'token' => $pageinfo['blocktoken'] ), $data );
 51+ 'token' => $pageinfo['blocktoken'] ), $data, false, self::$users['sysop']->user );
5252
5353 $block = Block::newFromTarget('UTApiBlockee');
5454
Index: trunk/phase3/tests/phpunit/includes/api/ApiTestCase.php
@@ -43,12 +43,12 @@
4444
4545 }
4646
47 - protected function doApiRequest( $params, $session = null, $appendModule = false ) {
 47+ protected function doApiRequest( $params, $session = null, $appendModule = false, $user = null ) {
4848 if ( is_null( $session ) ) {
4949 $session = array();
5050 }
5151
52 - $context = $this->apiContext->newTestContext( $params, $session );
 52+ $context = $this->apiContext->newTestContext( $params, $session, $user );
5353 $module = new ApiMain( $context, true );
5454 $module->execute();
5555
@@ -71,13 +71,13 @@
7272 * @param $params: key-value API params
7373 * @param $session: session array
7474 */
75 - protected function doApiRequestWithToken( $params, $session ) {
 75+ protected function doApiRequestWithToken( $params, $session, $user = null ) {
7676 if ( $session['wsToken'] ) {
7777 // add edit token to fake session
7878 $session['wsEditToken'] = $session['wsToken'];
7979 // add token to request parameters
8080 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
81 - return $this->doApiRequest( $params, $session );
 81+ return $this->doApiRequest( $params, $session, false, $user );
8282 } else {
8383 throw new Exception( "request data not in right format" );
8484 }
@@ -102,12 +102,11 @@
103103 }
104104
105105 protected function getTokenList( $user ) {
106 - $GLOBALS['wgUser'] = $user->user;
107106 $data = $this->doApiRequest( array(
108107 'action' => 'query',
109108 'titles' => 'Main Page',
110109 'intoken' => 'edit|delete|protect|move|block|unblock',
111 - 'prop' => 'info' ) );
 110+ 'prop' => 'info' ), false, $user->user );
112111 return $data;
113112 }
114113 }
@@ -156,11 +155,15 @@
157156 *
158157 * @param $params Array key-value API params
159158 * @param $session Array session data
 159+ * @param $user User or null
160160 * @return DerivativeContext
161161 */
162 - public function newTestContext( $params, $session ) {
 162+ public function newTestContext( $params, $session, $user = null ) {
163163 $context = new DerivativeContext( $this );
164164 $context->setRequest( new FauxRequest( $params, true, $session ) );
 165+ if ( $user !== null ) {
 166+ $context->setUser( $user );
 167+ }
165168 return $context;
166169 }
167170 }
Index: trunk/phase3/includes/api/ApiLogin.php
@@ -74,15 +74,16 @@
7575 $context->setRequest( $req );*/
7676 $loginForm = new LoginForm();
7777 $loginForm->setContext( $context );
78 - $user = $this->getUser();
7978
8079 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
8180
8281 $authRes = $loginForm->authenticateUserData();
8382 switch ( $authRes ) {
8483 case LoginForm::SUCCESS:
 84+ $user = $context->getUser();
 85+ $this->getContext()->setUser( $user );
8586 $user->setOption( 'rememberpassword', 1 );
86 - $user->setCookies( $this->getMain()->getRequest() );
 87+ $user->setCookies( $this->getRequest() );
8788
8889 // Run hooks.
8990 // @todo FIXME: Split back and frontend from this hook.
Index: trunk/phase3/includes/api/ApiBase.php
@@ -74,7 +74,7 @@
7575 $this->mModulePrefix = $modulePrefix;
7676
7777 if ( !$this->isMain() ) {
78 - $this->setContext( $this->mMainModule );
 78+ $this->setContext( $mainModule->getContext() );
7979 }
8080 }
8181

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r100905Adding context to the API...johnduhart23:27, 26 October 2011

Status & tagging log