* Fix up ApiTest a bit, cookie handling works
authorMark A. Hershberger <mah@users.mediawiki.org>
Tue, 9 Feb 2010 08:37:38 +0000 (08:37 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Tue, 9 Feb 2010 08:37:38 +0000 (08:37 +0000)
* Start upload chunks testing
* found some problems with messages

includes/api/ApiBase.php
includes/api/ApiUpload.php
includes/upload/UploadFromChunks.php
maintenance/tests/ApiTest.php
maintenance/tests/UploadFromChunksTest.php [new file with mode: 0644]

index 1455d1b..de5b72c 100644 (file)
@@ -5,7 +5,7 @@
  *
  * API for MediaWiki 1.8+
  *
- * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Copyright (C) 2006, 2010 Yuri Astrakhan <Firstname><Lastname>@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
index c5df5e5..03d8b4d 100644 (file)
@@ -3,7 +3,7 @@
  * Created on Aug 21, 2008
  * API for MediaWiki 1.8+
  *
- * Copyright (C) 2008 - 2009 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
+ * Copyright (C) 2008 - 2010 Bryan Tong Minh <Bryan.TongMinh@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
@@ -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();
index c0a8912..713ba13 100644 (file)
@@ -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' );
                }
        }
 
index 8e45fb3..04f9c5b 100644 (file)
@@ -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 (file)
index 0000000..09a212a
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+
+require_once("ApiSetup.php");
+
+class UploadFromChunksTest extends ApiSetup {
+
+       function setUp() {
+               global $wgEnableUploads;
+
+               $wgEnableUploads=true;
+               ini_set('file_loads', true);
+       }
+
+       function testGetEditToken() {
+       }
+
+       function testInitFromSessionKey() {
+
+       }
+
+       function testInitialize() {
+       }
+
+       function testSetupChunkSession() {
+       }
+
+
+       function makeChunk() {
+               $file = tempnam( wfTempDir(), "" );
+               $fh = fopen($file, "w");
+               if($fh == false) {
+                       $this->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() {
+       }
+
+
+
+}