X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/8989532d3de45b196373107c7a812a68ac0ff2d9..d75ce11339b35963b5f8c3d53190819c1c025716:/maintenance/tests/ApiTest.php diff --git a/maintenance/tests/ApiTest.php b/maintenance/tests/ApiTest.php new file mode 100644 index 00000000..d098b1a2 --- /dev/null +++ b/maintenance/tests/ApiTest.php @@ -0,0 +1,164 @@ + null, + 'enablechunks' => false, + 'sessionkey' => null, + ); + } + + +} + + +class ApiTest extends ApiSetup { + + function setup() { + parent::setup(); + } + + function testRequireOnlyOneParameterDefault() { + $mock = new MockApi(); + + $this->assertEquals( + null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt", + "enablechunks" => false), "filename", "enablechunks")); + } + + /** + * @expectedException UsageException + */ + function testRequireOnlyOneParameterZero() { + $mock = new MockApi(); + + $this->assertEquals( + null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt", + "enablechunks" => 0), "filename", "enablechunks")); + } + + /** + * @expectedException UsageException + */ + function testRequireOnlyOneParameterTrue() { + $mock = new MockApi(); + + $this->assertEquals( + null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt", + "enablechunks" => true), "filename", "enablechunks")); + } + + function testApi() { + if(!isset($wgServername) || !isset($wgServer)) { + $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. + 'be set in LocalSettings.php'); + } + /* 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() { + if(!isset($wgServername) || !isset($wgServer)) { + $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. + 'be set in LocalSettings.php'); + } + $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() { + if(!isset($wgServername) || !isset($wgServer)) { + $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. + 'be set in LocalSettings.php'); + } + $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() { + if(!isset($wgServername) || !isset($wgServer)) { + $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. + 'be set in LocalSettings.php'); + } + $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; + + if(!isset($wgServername) || !isset($wgServer)) { + $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. + 'be set in LocalSettings.php'); + } + $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 ) ); + + + return $cj; + } + + /** + * @depends testApiGotCookie + */ + function testApiListPages(CookieJar $cj) { + $this->markTestIncomplete("Not done with this yet"); + + if($wgServerName == "localhost" || $wgServer == "http://localhost") { + $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. + 'be set in LocalSettings.php'); + } + $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(); + } +}