r62175 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62174‎ | r62175 | r62176 >
Date:08:37, 9 February 2010
Author:mah
Status:resolved (Comments)
Tags:
Comment:
* Fix up ApiTest a bit, cookie handling works
* Start upload chunks testing
* found some problems with messages
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiUpload.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromChunks.php (modified) (history)
  • /trunk/phase3/maintenance/tests/ApiTest.php (modified) (history)
  • /trunk/phase3/maintenance/tests/UploadFromChunksTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/ApiTest.php
@@ -61,14 +61,32 @@
6262 global $wgScriptPath, $wgServerName;
6363
6464 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
65 - array( "method" => "POST",
66 - "postData" => array(
67 - "lgname" => self::$userName,
68 - "lgpassword" => self::$passWord ) ) );
 65+ array( "method" => "POST",
 66+ "postData" => array( "lgname" => self::$userName,
 67+ "lgpassword" => self::$passWord ) ) );
6968 $req->execute();
7069 $cj = $req->getCookieJar();
71 - $this->markTestIncomplete("Need to make sure cookie/domain handling is correct");
7270 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
7371 $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
 72+
 73+
 74+ return $cj;
7475 }
 76+
 77+ /**
 78+ * @depends testApiGotCookie
 79+ */
 80+ function testApiListPages(CookieJar $cj) {
 81+ $this->markTestIncomplete("Not done with this yet");
 82+
 83+ $req = HttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&".
 84+ "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
 85+ $req->setCookieJar($cj);
 86+ $req->execute();
 87+ libxml_use_internal_errors( true );
 88+ $sxe = simplexml_load_string( $req->getContent() );
 89+ $this->assertNotType( "bool", $sxe );
 90+ $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
 91+ $a = $sxe->query[0]->pages[0]->page[0]->attributes();
 92+ }
7593 }
Index: trunk/phase3/maintenance/tests/UploadFromChunksTest.php
@@ -0,0 +1,99 @@
 2+<?php
 3+
 4+require_once("ApiSetup.php");
 5+
 6+class UploadFromChunksTest extends ApiSetup {
 7+
 8+ function setUp() {
 9+ global $wgEnableUploads;
 10+
 11+ $wgEnableUploads=true;
 12+ ini_set('file_loads', true);
 13+ }
 14+
 15+ function testGetEditToken() {
 16+ }
 17+
 18+ function testInitFromSessionKey() {
 19+
 20+ }
 21+
 22+ function testInitialize() {
 23+ }
 24+
 25+ function testSetupChunkSession() {
 26+ }
 27+
 28+
 29+ function makeChunk() {
 30+ $file = tempnam( wfTempDir(), "" );
 31+ $fh = fopen($file, "w");
 32+ if($fh == false) {
 33+ $this->markTestIncomplete("Couldn't open $file!\n");
 34+ return;
 35+ }
 36+ fwrite($fh, "123");
 37+ fclose($fh);
 38+
 39+ $_FILES['chunk']['tmp_name'] = $file;
 40+ $_FILES['chunk']['size'] = 3;
 41+ $_FILES['chunk']['error'] = null;
 42+ $_FILES['chunk']['name'] = "test.txt";
 43+ }
 44+
 45+ function cleanChunk() {
 46+ unlink($_FILES['chunk']['tmp_name']);
 47+ }
 48+
 49+ /**
 50+ * @expectedException UsageException
 51+ */
 52+ function testPerformUploadInitError() {
 53+ global $wgUser;
 54+
 55+ $wgUser = User::newFromId(1);
 56+ $token = $wgUser->editToken();
 57+ $this->makeChunk();
 58+
 59+ $req = new FauxRequest(
 60+ array('action' => 'upload',
 61+ 'enablechunks' => '1',
 62+ 'filename' => 'test.txt',
 63+ 'token' => $token,
 64+ ));
 65+ $module = new ApiMain($req, true);
 66+ $module->execute();
 67+ }
 68+
 69+ function testPerformUploadInitSuccess() {
 70+ global $wgUser;
 71+
 72+ $wgUser = User::newFromId(1);
 73+ $token = $wgUser->editToken();
 74+ $this->makeChunk();
 75+
 76+ $req = new FauxRequest(
 77+ array('action' => 'upload',
 78+ 'enablechunks' => '1',
 79+ 'filename' => 'test.txt',
 80+ 'token' => $token,
 81+ ));
 82+ $module = new ApiMain($req, true);
 83+ $module->execute();
 84+ }
 85+
 86+ function testAppendToUploadFile() {
 87+ }
 88+
 89+ function testAppendChunk() {
 90+ }
 91+
 92+ function testPeformUploadChunk() {
 93+ }
 94+
 95+ function testPeformUploadDone() {
 96+ }
 97+
 98+
 99+
 100+}
Property changes on: trunk/phase3/maintenance/tests/UploadFromChunksTest.php
___________________________________________________________________
Name: svn:eol-syle
1101 + native
Index: trunk/phase3/includes/upload/UploadFromChunks.php
@@ -51,7 +51,6 @@
5252 } else if ( $this->sessionKey && $done ) {
5353 $this->chunkMode = self::DONE;
5454 }
55 -
5655 if ( $this->chunkMode == self::CHUNK || $this->chunkMode == self::DONE ) {
5756 $this->mTempPath = $path;
5857 $this->fileSize += $fileSize;
@@ -90,8 +89,7 @@
9190 protected function initFromSessionKey( $sessionKey, $sessionData ) {
9291 // testing against null because we don't want to cause obscure
9392 // bugs when $sessionKey is full of "0"
94 - if ( $sessionKey !== null ) {
95 - $this->status = Status::newFromFatal( 'import-token-mismatch' );
 93+ if ( $sessionKey === null ) {
9694 return;
9795 }
9896 $this->sessionKey = $sessionKey;
@@ -106,7 +104,7 @@
107105 $this->repoPath = $sessionData[$this->sessionKey]['repoPath'];
108106 $this->mDesiredDestName = $sessionData[$this->sessionKey]['mDesiredDestName'];
109107 } else {
110 - $this->status = Status::newFromFatal( 'Missing session data.' );
 108+ $this->status = Status::newFatal( 'invalid-session-key' );
111109 }
112110 }
113111
Index: trunk/phase3/includes/api/ApiBase.php
@@ -5,7 +5,7 @@
66 *
77 * API for MediaWiki 1.8+
88 *
9 - * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 9+ * Copyright (C) 2006, 2010 Yuri Astrakhan <Firstname><Lastname>@gmail.com
1010 *
1111 * This program is free software; you can redistribute it and/or modify
1212 * it under the terms of the GNU General Public License as published by
@@ -342,7 +342,7 @@
343343 } else
344344 return false;
345345 }
346 -
 346+
347347 /**
348348 * Callback for preg_replace_callback() call in makeHelpMsg().
349349 * Replaces a source file name with a link to ViewVC
@@ -353,14 +353,14 @@
354354 $file = $wgAutoloadLocalClasses[get_class( $this )];
355355 else if ( isset( $wgAutoloadClasses[get_class( $this )] ) )
356356 $file = $wgAutoloadClasses[get_class( $this )];
357 -
 357+
358358 // Do some guesswork here
359359 $path = strstr( $file, 'includes/api/' );
360360 if ( $path === false )
361361 $path = strstr( $file, 'extensions/' );
362362 else
363363 $path = 'phase3/' . $path;
364 -
 364+
365365 // Get the filename from $matches[2] instead of $file
366366 // If they're not the same file, they're assumed to be in the
367367 // same directory
@@ -409,7 +409,7 @@
410410 protected function getParamDescription() {
411411 return false;
412412 }
413 -
 413+
414414 /**
415415 * Get final list of parameters, after hooks have had a chance to
416416 * tweak it as needed.
@@ -472,7 +472,7 @@
473473 $paramSettings = $params[$paramName];
474474 return $this->getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
475475 }
476 -
 476+
477477 /**
478478 * Die if none or more than one of a certain set of parameters is set
479479 * @param $params array of parameter names
@@ -480,7 +480,7 @@
481481 public function requireOnlyOneParameter( $params ) {
482482 $required = func_get_args();
483483 array_shift( $required );
484 -
 484+
485485 $intersection = array_intersect( array_keys( array_filter( $params,
486486 create_function( '$x', 'return !is_null($x);' )
487487 ) ), $required );
@@ -721,7 +721,7 @@
722722 }
723723 }
724724 }
725 -
 725+
726726 /**
727727 * Truncate an array to a certain length.
728728 * @param $arr array Array to truncate
@@ -885,6 +885,8 @@
886886 // uploadMsgs
887887 'invalid-session-key' => array( 'code' => 'invalid-session-key', 'info' => 'Not a valid session key' ),
888888 'nouploadmodule' => array( 'code' => 'nouploadmodule', 'info' => 'No upload module set' ),
 889+ 'uploaddisabled' => array( 'code' => 'uploaddisabled', 'info' => 'Uploads are not enabled. Make sure $wgEnableUploads is set to true in LocalSettings.php and the PHP ini setting file_uploads is true' ),
 890+ 'chunked-error' => array( 'code' => 'chunked-error', 'info' => 'There was a problem initializing the chunked upload.' ),
889891 );
890892
891893 /**
@@ -904,7 +906,7 @@
905907 $parsed = $this->parseMsg( $error );
906908 $this->dieUsage( $parsed['info'], $parsed['code'] );
907909 }
908 -
 910+
909911 /**
910912 * Return the error message related to a certain array
911913 * @param $error array Element of a getUserPermissionsErrors()-style array
Index: trunk/phase3/includes/api/ApiUpload.php
@@ -3,7 +3,7 @@
44 * Created on Aug 21, 2008
55 * API for MediaWiki 1.8+
66 *
7 - * Copyright (C) 2008 - 2009 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
 7+ * Copyright (C) 2008 - 2010 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
88 *
99 * This program is free software; you can redistribute it and/or modify
1010 * it under the terms of the GNU General Public License as published by
@@ -93,8 +93,7 @@
9494 );
9595
9696 if ( !$this->mUpload->status->isOK() ) {
97 - return $this->dieUsageMsg( $this->mUpload->status->getWikiText(),
98 - 'chunked-error' );
 97+ return $this->dieUsageMsg( $this->mUpload->status->getErrorsArray() );
9998 }
10099 } elseif ( isset( $this->mParams['file'] ) ) {
101100 $this->mUpload = new UploadFromFile();

Follow-up revisions

RevisionCommit summaryAuthorDate
r62806follow up r62231, r61779, r62175...mah02:15, 22 February 2010

Comments

#Comment by Tim Starling (talk | contribs)   01:12, 16 February 2010

Don't pretend to return a value from a function that never returns (like dieUsageMsg), it's confusing. You can either leave the return out, or put it in a separate statement.

+					return $this->dieUsageMsg( $this->mUpload->status->getErrorsArray() );

The format of the array returned by Status::getErrorsArray() is not the same as the one expected by dieUsageMsg(), so this is broken.

#Comment by MarkAHershberger (talk | contribs)   03:16, 22 February 2010

fixed in r62806

Status & tagging log