4b838ce73da0844d05c4b272b1f2e49a4ec7ee4d
[lhc/web/wiklou.git] / maintenance / tests / ApiTest.php
1 <?php
2
3 require_once( "ApiSetup.php" );
4
5 class MockApi extends ApiBase {
6 public function execute() {}
7 public function getVersion() {}
8
9 public function __construct() {}
10
11 public function getAllowedParams() {
12 $params = array(
13 'filename' => null,
14 'enablechunks' => false,
15 'sessionkey' => null,
16 );
17 }
18
19
20 }
21
22
23 class ApiTest extends ApiSetup {
24
25 function setup() {
26 parent::setup();
27 }
28
29 function testRequireOnlyOneParameterDefault() {
30 $mock = new MockApi();
31
32 $this->assertEquals(
33 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
34 "enablechunks" => false), "filename", "enablechunks"));
35 }
36
37 /**
38 * @expectedException UsageException
39 */
40 function testRequireOnlyOneParameterZero() {
41 $mock = new MockApi();
42
43 $this->assertEquals(
44 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
45 "enablechunks" => 0), "filename", "enablechunks"));
46 }
47
48 /**
49 * @expectedException UsageException
50 */
51 function testRequireOnlyOneParameterTrue() {
52 $mock = new MockApi();
53
54 $this->assertEquals(
55 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
56 "enablechunks" => true), "filename", "enablechunks"));
57 }
58
59 function testApi() {
60 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
61
62 if($wgDBprefix === "parsertest_" || ($wgDBtype == 'oracle' && $wgDBprefix === 'pt_'))
63 $this->markTestSkipped("This test can't (yet?) be run with the parser tests");
64 if(!isset($wgServerName) || !isset($wgServer)) {
65 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
66 'be set in LocalSettings.php');
67 }
68 /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
69 $resp = Http::get( self::$apiUrl . "?format=xml" );
70
71 libxml_use_internal_errors( true );
72 $sxe = simplexml_load_string( $resp );
73 $this->assertNotType( "bool", $sxe );
74 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
75 }
76
77 function testApiLoginNoName() {
78 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
79
80 if($wgDBprefix === "parsertest_" || ($wgDBtype == 'oracle' && $wgDBprefix === 'pt_'))
81 $this->markTestSkipped("This test can't (yet?) be run with the parser tests");
82 if(!isset($wgServerName) || !isset($wgServer)) {
83 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
84 'be set in LocalSettings.php');
85 }
86 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
87 array( "postData" => array(
88 "lgname" => "",
89 "lgpassword" => self::$passWord ) ) );
90 libxml_use_internal_errors( true );
91 $sxe = simplexml_load_string( $resp );
92 $this->assertNotType( "bool", $sxe );
93 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
94 $a = $sxe->login[0]->attributes()->result;
95 $this->assertEquals( ' result="NoName"', $a->asXML() );
96 }
97
98 function testApiLoginBadPass() {
99 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
100
101 if($wgDBprefix === "parsertest_" || ($wgDBtype == 'oracle' && $wgDBprefix === 'pt_'))
102 $this->markTestSkipped("This test can't (yet?) be run with the parser tests");
103 if(!isset($wgServerName) || !isset($wgServer)) {
104 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
105 'be set in LocalSettings.php');
106 }
107 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
108 array( "postData" => array(
109 "lgname" => self::$userName,
110 "lgpassword" => "bad" ) ) );
111 libxml_use_internal_errors( true );
112 $sxe = simplexml_load_string( $resp );
113 $this->assertNotType( "bool", $sxe );
114 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
115 $a = $sxe->login[0]->attributes()->result;
116 $this->assertEquals( ' result="WrongPass"', $a->asXML() );
117 }
118
119 function testApiLoginGoodPass() {
120 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
121
122 if($wgDBprefix === "parsertest_" || ($wgDBtype == 'oracle' && $wgDBprefix === 'pt_'))
123 $this->markTestSkipped("This test can't (yet?) be run with the parser tests");
124 if(!isset($wgServerName) || !isset($wgServer)) {
125 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
126 'be set in LocalSettings.php');
127 }
128 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
129 array( "postData" => array(
130 "lgname" => self::$userName,
131 "lgpassword" => self::$passWord ) ) );
132 libxml_use_internal_errors( true );
133 $sxe = simplexml_load_string( $resp );
134 $this->assertNotType( "bool", $sxe );
135 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
136 $a = $sxe->login[0]->attributes()->result;
137 $this->assertEquals( ' result="Success"', $a->asXML() );
138 }
139
140 function testApiGotCookie() {
141 global $wgServerName, $wgServer, $wgScriptPath, $wgDBprefix, $wgDBtype;
142
143 if($wgDBprefix === "parsertest_" || ($wgDBtype == 'oracle' && $wgDBprefix === 'pt_'))
144 $this->markTestSkipped("This test can't (yet?) be run with the parser tests");
145 if(!isset($wgServerName) || !isset($wgServer)) {
146 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
147 'be set in LocalSettings.php');
148 }
149 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
150 array( "method" => "POST",
151 "postData" => array( "lgname" => self::$userName,
152 "lgpassword" => self::$passWord ) ) );
153 $req->execute();
154 $cj = $req->getCookieJar();
155 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
156 $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
157
158
159 return $cj;
160 }
161
162 /**
163 * @depends testApiGotCookie
164 */
165 function testApiListPages(CookieJar $cj) {
166 $this->markTestIncomplete("Not done with this yet");
167 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
168
169 if($wgDBprefix === "parsertest_" || ($wgDBtype == 'oracle' && $wgDBprefix === 'pt_'))
170 $this->markTestSkipped("This test can't (yet?) be run with the parser tests");
171 if($wgServerName == "localhost" || $wgServer == "http://localhost") {
172 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
173 'be set in LocalSettings.php');
174 }
175 $req = HttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&".
176 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
177 $req->setCookieJar($cj);
178 $req->execute();
179 libxml_use_internal_errors( true );
180 $sxe = simplexml_load_string( $req->getContent() );
181 $this->assertNotType( "bool", $sxe );
182 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
183 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
184 }
185 }