00001 <?php
00002
00003 require_once( "ApiSetup.php" );
00004
00005 class MockApi extends ApiBase {
00006 public function execute() {}
00007 public function getVersion() {}
00008
00009 public function __construct() {}
00010
00011 public function getAllowedParams() {
00012 $params = array(
00013 'filename' => null,
00014 'enablechunks' => false,
00015 'sessionkey' => null,
00016 );
00017 }
00018
00019
00020 }
00021
00022
00023 class ApiTest extends ApiSetup {
00024
00025 function setup() {
00026 parent::setup();
00027 }
00028
00029 function testRequireOnlyOneParameterDefault() {
00030 $mock = new MockApi();
00031
00032 $this->assertEquals(
00033 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
00034 "enablechunks" => false), "filename", "enablechunks"));
00035 }
00036
00040 function testRequireOnlyOneParameterZero() {
00041 $mock = new MockApi();
00042
00043 $this->assertEquals(
00044 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
00045 "enablechunks" => 0), "filename", "enablechunks"));
00046 }
00047
00051 function testRequireOnlyOneParameterTrue() {
00052 $mock = new MockApi();
00053
00054 $this->assertEquals(
00055 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
00056 "enablechunks" => true), "filename", "enablechunks"));
00057 }
00058
00059 function testApi() {
00060 if(!isset($wgServername) || !isset($wgServer)) {
00061 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
00062 'be set in LocalSettings.php');
00063 }
00064
00065 $resp = Http::get( self::$apiUrl . "?format=xml" );
00066
00067 libxml_use_internal_errors( true );
00068 $sxe = simplexml_load_string( $resp );
00069 $this->assertNotType( "bool", $sxe );
00070 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00071 }
00072
00073 function testApiLoginNoName() {
00074 if(!isset($wgServername) || !isset($wgServer)) {
00075 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
00076 'be set in LocalSettings.php');
00077 }
00078 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
00079 array( "postData" => array(
00080 "lgname" => "",
00081 "lgpassword" => self::$passWord ) ) );
00082 libxml_use_internal_errors( true );
00083 $sxe = simplexml_load_string( $resp );
00084 $this->assertNotType( "bool", $sxe );
00085 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00086 $a = $sxe->login[0]->attributes()->result;
00087 $this->assertEquals( ' result="NoName"', $a->asXML() );
00088 }
00089
00090 function testApiLoginBadPass() {
00091 if(!isset($wgServername) || !isset($wgServer)) {
00092 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
00093 'be set in LocalSettings.php');
00094 }
00095 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
00096 array( "postData" => array(
00097 "lgname" => self::$userName,
00098 "lgpassword" => "bad" ) ) );
00099 libxml_use_internal_errors( true );
00100 $sxe = simplexml_load_string( $resp );
00101 $this->assertNotType( "bool", $sxe );
00102 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00103 $a = $sxe->login[0]->attributes()->result;
00104 $this->assertEquals( ' result="WrongPass"', $a->asXML() );
00105 }
00106
00107 function testApiLoginGoodPass() {
00108 if(!isset($wgServername) || !isset($wgServer)) {
00109 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
00110 'be set in LocalSettings.php');
00111 }
00112 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
00113 array( "postData" => array(
00114 "lgname" => self::$userName,
00115 "lgpassword" => self::$passWord ) ) );
00116 libxml_use_internal_errors( true );
00117 $sxe = simplexml_load_string( $resp );
00118 $this->assertNotType( "bool", $sxe );
00119 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00120 $a = $sxe->login[0]->attributes()->result;
00121 $this->assertEquals( ' result="Success"', $a->asXML() );
00122 }
00123
00124 function testApiGotCookie() {
00125 global $wgScriptPath, $wgServerName;
00126
00127 if(!isset($wgServername) || !isset($wgServer)) {
00128 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
00129 'be set in LocalSettings.php');
00130 }
00131 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
00132 array( "method" => "POST",
00133 "postData" => array( "lgname" => self::$userName,
00134 "lgpassword" => self::$passWord ) ) );
00135 $req->execute();
00136 $cj = $req->getCookieJar();
00137 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
00138 $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
00139
00140
00141 return $cj;
00142 }
00143
00147 function testApiListPages(CookieJar $cj) {
00148 $this->markTestIncomplete("Not done with this yet");
00149
00150 if($wgServerName == "localhost" || $wgServer == "http://localhost") {
00151 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
00152 'be set in LocalSettings.php');
00153 }
00154 $req = HttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&".
00155 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
00156 $req->setCookieJar($cj);
00157 $req->execute();
00158 libxml_use_internal_errors( true );
00159 $sxe = simplexml_load_string( $req->getContent() );
00160 $this->assertNotType( "bool", $sxe );
00161 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00162 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
00163 }
00164 }