Add preliminary API tests. Next step: UploadChunks API testing.
authorMark A. Hershberger <mah@users.mediawiki.org>
Wed, 3 Feb 2010 07:45:05 +0000 (07:45 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Wed, 3 Feb 2010 07:45:05 +0000 (07:45 +0000)
includes/HttpFunctions.php
tests/MediaWikiAPITest.php [new file with mode: 0644]
tests/MediaWikiAPI_TestCase.php [new file with mode: 0644]

index a0c1a90..551206e 100644 (file)
@@ -257,7 +257,9 @@ class HttpRequest {
                $list = array();
 
                if( $this->cookieJar ) {
-                       $this->reqHeaders['Cookie'] = $this->cookieJar->serializeToHttpRequest();
+                       $this->reqHeaders['Cookie'] =
+                               $this->cookieJar->serializeToHttpRequest($this->parsedURL['path'],
+                                                                                                                $this->parsedURL['host']);
                }
                foreach($this->reqHeaders as $name => $value) {
                        $list[] = "$name: $value";
@@ -553,6 +555,12 @@ class CookieJar {
                                $attr[strtolower( $parts[0] )] = true;
                        }
                }
+
+               if( !isset( $attr['domain'] ) ) {
+                       $attr['domain'] = $domain;
+               } else {
+                       /* FIXME: Check that domain is valid */
+               }
                $this->setCookie( $name, $value, $attr );
        }
 }
diff --git a/tests/MediaWikiAPITest.php b/tests/MediaWikiAPITest.php
new file mode 100644 (file)
index 0000000..95ca074
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+
+require_once("MediaWikiAPI_TestCase.php");
+
+class MediaWikiAPITest extends MediaWikiAPI_TestCase {
+
+       function setup() {
+               parent::setup();
+       }
+
+       function testApi() {
+               /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
+               $resp = Http::get(self::$apiUrl."?format=xml");
+
+               libxml_use_internal_errors(true);
+               $sxe = simplexml_load_string($resp);
+               $this->assertNotType("bool", $sxe);
+               $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
+       }
+
+       function testApiLoginNoName() {
+               $resp = Http::post(self::$apiUrl."?action=login&format=xml",
+                                                  array("postData" => array(
+                                                               "lgname" => "",
+                                                               "lgpassword" => self::$passWord)));
+               libxml_use_internal_errors(true);
+               $sxe = simplexml_load_string($resp);
+               $this->assertNotType("bool", $sxe);
+               $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
+               $a = $sxe->login[0]->attributes()->result;
+               $this->assertEquals( ' result="NoName"', $a->asXML() );
+       }
+
+       function testApiLoginBadPass() {
+               $resp = Http::post(self::$apiUrl."?action=login&format=xml",
+                                                  array("postData" => array(
+                                                               "lgname" => self::$userName,
+                                                               "lgpassword" => "bad")));
+               libxml_use_internal_errors(true);
+               $sxe = simplexml_load_string($resp);
+               $this->assertNotType("bool", $sxe);
+               $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
+               $a = $sxe->login[0]->attributes()->result;
+               $this->assertEquals( ' result="WrongPass"', $a->asXML() );
+       }
+
+       function testApiLoginGoodPass() {
+               $resp = Http::post(self::$apiUrl."?action=login&format=xml",
+                                                  array("postData" => array(
+                                                               "lgname" => self::$userName,
+                                                               "lgpassword" => self::$passWord)));
+               libxml_use_internal_errors(true);
+               $sxe = simplexml_load_string($resp);
+               $this->assertNotType("bool", $sxe);
+               $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
+               $a = $sxe->login[0]->attributes()->result;
+               $this->assertEquals( ' result="Success"', $a->asXML() );
+       }
+
+       function testApiGotCookie() {
+               global $wgScriptPath, $wgServerName;
+
+               $req = HttpRequest::factory(self::$apiUrl."?action=login&format=xml",
+                                                                       array("method" => "POST",
+                                                                                 "postData" => array(
+                                                                                         "lgname" => self::$userName,
+                                                                                         "lgpassword" => self::$passWord)));
+               $req->execute();
+               $cj = $req->getCookieJar();
+
+               $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName='.self::$userName.'; .*Token=/',
+                                                        $cj->serializeToHttpRequest($wgScriptPath, $wgServerName) );
+       }
+}
diff --git a/tests/MediaWikiAPI_TestCase.php b/tests/MediaWikiAPI_TestCase.php
new file mode 100644 (file)
index 0000000..e00e79b
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+abstract class MediaWikiAPI_TestCase extends PHPUnit_Framework_TestCase {
+       protected static $userName;
+       protected static $passWord;
+       protected static $user;
+       protected static $apiUrl;
+
+       function setup() {
+               global $wgServerName, $wgServer, $wgContLang, $wgAuth, $wgScriptPath,
+                       $wgScriptExtension, $wgMemc;
+
+               if($wgServerName == "localhost" || $wgServer == "http://localhost") {
+                       $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
+                                                                         'be set in LocalSettings.php');
+               }
+               self::$apiUrl = $wgServer.$wgScriptPath."/api".$wgScriptExtension;
+
+               $wgMemc = new FakeMemCachedClient;
+               $wgContLang = Language::factory( 'en' );
+               $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
+               self::setupUser();
+       }
+
+       static function setupUser() {
+               if ( self::$user == NULL ) {
+                       self::$userName = "Useruser";
+                       self::$passWord = User::randomPassword();
+
+                       self::$user = User::newFromName(self::$userName);
+                       if ( !self::$user->getID() ) {
+                               self::$user = User::createNew(self::$userName, array(
+                                       "password" => self::$passWord,
+                                       "email" => "test@example.com",
+                                       "real_name" => "Test User"));
+                       } else {
+                               self::$user->setPassword(self::$passWord);
+                       }
+                       self::$user->saveSettings();
+               }
+       }
+}