From 15fec3ab7f0c4c4ea49494b537b57f98b82a8dff Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Tue, 9 Feb 2010 08:37:38 +0000 Subject: [PATCH] * Fix up ApiTest a bit, cookie handling works * Start upload chunks testing * found some problems with messages --- includes/api/ApiBase.php | 20 +++-- includes/api/ApiUpload.php | 5 +- includes/upload/UploadFromChunks.php | 6 +- maintenance/tests/ApiTest.php | 28 ++++-- maintenance/tests/UploadFromChunksTest.php | 99 ++++++++++++++++++++++ 5 files changed, 137 insertions(+), 21 deletions(-) create mode 100644 maintenance/tests/UploadFromChunksTest.php diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 1455d1b3c8..de5b72cc66 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -5,7 +5,7 @@ * * API for MediaWiki 1.8+ * - * Copyright (C) 2006 Yuri Astrakhan @gmail.com + * Copyright (C) 2006, 2010 Yuri Astrakhan @gmail.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -342,7 +342,7 @@ abstract class ApiBase { } else return false; } - + /** * Callback for preg_replace_callback() call in makeHelpMsg(). * Replaces a source file name with a link to ViewVC @@ -353,14 +353,14 @@ abstract class ApiBase { $file = $wgAutoloadLocalClasses[get_class( $this )]; else if ( isset( $wgAutoloadClasses[get_class( $this )] ) ) $file = $wgAutoloadClasses[get_class( $this )]; - + // Do some guesswork here $path = strstr( $file, 'includes/api/' ); if ( $path === false ) $path = strstr( $file, 'extensions/' ); else $path = 'phase3/' . $path; - + // Get the filename from $matches[2] instead of $file // If they're not the same file, they're assumed to be in the // same directory @@ -409,7 +409,7 @@ abstract class ApiBase { protected function getParamDescription() { return false; } - + /** * Get final list of parameters, after hooks have had a chance to * tweak it as needed. @@ -472,7 +472,7 @@ abstract class ApiBase { $paramSettings = $params[$paramName]; return $this->getParameterFromSettings( $paramName, $paramSettings, $parseLimit ); } - + /** * Die if none or more than one of a certain set of parameters is set * @param $params array of parameter names @@ -480,7 +480,7 @@ abstract class ApiBase { public function requireOnlyOneParameter( $params ) { $required = func_get_args(); array_shift( $required ); - + $intersection = array_intersect( array_keys( array_filter( $params, create_function( '$x', 'return !is_null($x);' ) ) ), $required ); @@ -721,7 +721,7 @@ abstract class ApiBase { } } } - + /** * Truncate an array to a certain length. * @param $arr array Array to truncate @@ -885,6 +885,8 @@ abstract class ApiBase { // uploadMsgs 'invalid-session-key' => array( 'code' => 'invalid-session-key', 'info' => 'Not a valid session key' ), 'nouploadmodule' => array( 'code' => 'nouploadmodule', 'info' => 'No upload module set' ), + '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' ), + 'chunked-error' => array( 'code' => 'chunked-error', 'info' => 'There was a problem initializing the chunked upload.' ), ); /** @@ -904,7 +906,7 @@ abstract class ApiBase { $parsed = $this->parseMsg( $error ); $this->dieUsage( $parsed['info'], $parsed['code'] ); } - + /** * Return the error message related to a certain array * @param $error array Element of a getUserPermissionsErrors()-style array diff --git a/includes/api/ApiUpload.php b/includes/api/ApiUpload.php index c5df5e5163..03d8b4d7f5 100644 --- a/includes/api/ApiUpload.php +++ b/includes/api/ApiUpload.php @@ -3,7 +3,7 @@ * Created on Aug 21, 2008 * API for MediaWiki 1.8+ * - * Copyright (C) 2008 - 2009 Bryan Tong Minh + * Copyright (C) 2008 - 2010 Bryan Tong Minh * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -93,8 +93,7 @@ class ApiUpload extends ApiBase { ); if ( !$this->mUpload->status->isOK() ) { - return $this->dieUsageMsg( $this->mUpload->status->getWikiText(), - 'chunked-error' ); + return $this->dieUsageMsg( $this->mUpload->status->getErrorsArray() ); } } elseif ( isset( $this->mParams['file'] ) ) { $this->mUpload = new UploadFromFile(); diff --git a/includes/upload/UploadFromChunks.php b/includes/upload/UploadFromChunks.php index c0a8912137..713ba13bee 100644 --- a/includes/upload/UploadFromChunks.php +++ b/includes/upload/UploadFromChunks.php @@ -51,7 +51,6 @@ class UploadFromChunks extends UploadBase { } else if ( $this->sessionKey && $done ) { $this->chunkMode = self::DONE; } - if ( $this->chunkMode == self::CHUNK || $this->chunkMode == self::DONE ) { $this->mTempPath = $path; $this->fileSize += $fileSize; @@ -90,8 +89,7 @@ class UploadFromChunks extends UploadBase { protected function initFromSessionKey( $sessionKey, $sessionData ) { // testing against null because we don't want to cause obscure // bugs when $sessionKey is full of "0" - if ( $sessionKey !== null ) { - $this->status = Status::newFromFatal( 'import-token-mismatch' ); + if ( $sessionKey === null ) { return; } $this->sessionKey = $sessionKey; @@ -106,7 +104,7 @@ class UploadFromChunks extends UploadBase { $this->repoPath = $sessionData[$this->sessionKey]['repoPath']; $this->mDesiredDestName = $sessionData[$this->sessionKey]['mDesiredDestName']; } else { - $this->status = Status::newFromFatal( 'Missing session data.' ); + $this->status = Status::newFatal( 'invalid-session-key' ); } } diff --git a/maintenance/tests/ApiTest.php b/maintenance/tests/ApiTest.php index 8e45fb3e8e..04f9c5b9b4 100644 --- a/maintenance/tests/ApiTest.php +++ b/maintenance/tests/ApiTest.php @@ -61,14 +61,32 @@ class ApiTest extends ApiSetup { global $wgScriptPath, $wgServerName; $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml", - array( "method" => "POST", - "postData" => array( - "lgname" => self::$userName, - "lgpassword" => self::$passWord ) ) ); + array( "method" => "POST", + "postData" => array( "lgname" => self::$userName, + "lgpassword" => self::$passWord ) ) ); $req->execute(); $cj = $req->getCookieJar(); - $this->markTestIncomplete("Need to make sure cookie/domain handling is correct"); $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/', $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) ); + + + return $cj; + } + + /** + * @depends testApiGotCookie + */ + function testApiListPages(CookieJar $cj) { + $this->markTestIncomplete("Not done with this yet"); + + $req = HttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&". + "titles=Main%20Page&rvprop=timestamp|user|comment|content" ); + $req->setCookieJar($cj); + $req->execute(); + libxml_use_internal_errors( true ); + $sxe = simplexml_load_string( $req->getContent() ); + $this->assertNotType( "bool", $sxe ); + $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); + $a = $sxe->query[0]->pages[0]->page[0]->attributes(); } } diff --git a/maintenance/tests/UploadFromChunksTest.php b/maintenance/tests/UploadFromChunksTest.php new file mode 100644 index 0000000000..09a212ac83 --- /dev/null +++ b/maintenance/tests/UploadFromChunksTest.php @@ -0,0 +1,99 @@ +markTestIncomplete("Couldn't open $file!\n"); + return; + } + fwrite($fh, "123"); + fclose($fh); + + $_FILES['chunk']['tmp_name'] = $file; + $_FILES['chunk']['size'] = 3; + $_FILES['chunk']['error'] = null; + $_FILES['chunk']['name'] = "test.txt"; + } + + function cleanChunk() { + unlink($_FILES['chunk']['tmp_name']); + } + + /** + * @expectedException UsageException + */ + function testPerformUploadInitError() { + global $wgUser; + + $wgUser = User::newFromId(1); + $token = $wgUser->editToken(); + $this->makeChunk(); + + $req = new FauxRequest( + array('action' => 'upload', + 'enablechunks' => '1', + 'filename' => 'test.txt', + 'token' => $token, + )); + $module = new ApiMain($req, true); + $module->execute(); + } + + function testPerformUploadInitSuccess() { + global $wgUser; + + $wgUser = User::newFromId(1); + $token = $wgUser->editToken(); + $this->makeChunk(); + + $req = new FauxRequest( + array('action' => 'upload', + 'enablechunks' => '1', + 'filename' => 'test.txt', + 'token' => $token, + )); + $module = new ApiMain($req, true); + $module->execute(); + } + + function testAppendToUploadFile() { + } + + function testAppendChunk() { + } + + function testPeformUploadChunk() { + } + + function testPeformUploadDone() { + } + + + +} -- 2.20.1