6579fe3194b317a30eadf533295d8a9b219a3808
[lhc/web/wiklou.git] / maintenance / tests / phpunit / includes / api / ApiTest.php
1 <?php
2
3 require_once( dirname( __FILE__ ) . '/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 return array(
13 'filename' => null,
14 'enablechunks' => false,
15 'sessionkey' => null,
16 );
17 }
18 }
19
20 class ApiTest extends ApiTestSetup {
21
22 function setup() {
23 parent::setup();
24 }
25
26 function testRequireOnlyOneParameterDefault() {
27 $mock = new MockApi();
28
29 $this->assertEquals(
30 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
31 "enablechunks" => false ), "filename", "enablechunks" ) );
32 }
33
34 /**
35 * @expectedException UsageException
36 */
37 function testRequireOnlyOneParameterZero() {
38 $mock = new MockApi();
39
40 $this->assertEquals(
41 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
42 "enablechunks" => 0 ), "filename", "enablechunks" ) );
43 }
44
45 /**
46 * @expectedException UsageException
47 */
48 function testRequireOnlyOneParameterTrue() {
49 $mock = new MockApi();
50
51 $this->assertEquals(
52 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
53 "enablechunks" => true ), "filename", "enablechunks" ) );
54 }
55
56 /**
57 * Test that the API will accept a FauxRequest and execute. The help action
58 * (default) throws a UsageException. Just validate we're getting proper XML
59 *
60 * @expectedException UsageException
61 */
62 function testApi() {
63 $api = new ApiMain(
64 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
65 );
66 $api->execute();
67 $api->getPrinter()->setBufferResult( true );
68 $api->printResult( false );
69 $resp = $api->getPrinter()->getBuffer();
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 /**
78 * Test result of attempted login with an empty username
79 */
80 function testApiLoginNoName() {
81 $data = $this->doApiRequest( array( 'action' => 'login',
82 'lgname' => '', 'lgpassword' => self::$passWord
83 ) );
84 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
85 }
86
87 function testApiLoginBadPass() {
88 global $wgServer;
89
90 if ( !isset( $wgServer ) ) {
91 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
92 }
93 $ret = $this->doApiRequest( array(
94 "action" => "login",
95 "lgname" => self::$userName,
96 "lgpassword" => "bad",
97 )
98 );
99
100 $result = $ret[0];
101
102 $this->assertNotType( "bool", $result );
103
104 $a = $result["login"]["result"];
105 $this->assertEquals( "NeedToken", $a );
106
107 $token = $result["login"]["token"];
108
109 $ret = $this->doApiRequest( array(
110 "action" => "login",
111 "lgtoken" => $token,
112 "lgname" => self::$userName,
113 "lgpassword" => "bad",
114 )
115 );
116
117 $result = $ret[0];
118
119 $this->assertNotType( "bool", $result );
120 $a = $result["login"]["result"];
121
122 $this->assertEquals( "NeedToken", $a );
123 }
124
125 function testApiLoginGoodPass() {
126 global $wgServer;
127
128 if ( !isset( $wgServer ) ) {
129 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
130 }
131
132 $ret = $this->doApiRequest( array(
133 "action" => "login",
134 "lgname" => self::$userName,
135 "lgpassword" => self::$passWord,
136 )
137 );
138
139 $result = $ret[0];
140 $this->assertNotType( "bool", $result );
141 $this->assertNotType( "null", $result["login"] );
142
143 $a = $result["login"]["result"];
144 $this->assertEquals( "NeedToken", $a );
145 $token = $result["login"]["token"];
146
147 $ret = $this->doApiRequest( array(
148 "action" => "login",
149 "lgtoken" => $token,
150 "lgname" => self::$userName,
151 "lgpassword" => self::$passWord,
152 )
153 );
154
155 $result = $ret[0];
156
157 $this->assertNotType( "bool", $result );
158 $a = $result["login"]["result"];
159
160 $this->assertEquals( "Success", $a );
161 }
162
163 function testApiGotCookie() {
164 global $wgServer, $wgScriptPath;
165
166 if ( !isset( $wgServer ) ) {
167 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
168 }
169 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
170 array( "method" => "POST",
171 "postData" => array(
172 "lgname" => self::$userName,
173 "lgpassword" => self::$passWord ) ) );
174 $req->execute();
175
176 libxml_use_internal_errors( true );
177 $sxe = simplexml_load_string( $req->getContent() );
178 $this->assertNotType( "bool", $sxe );
179 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
180 $this->assertNotType( "null", $sxe->login[0] );
181
182 $a = $sxe->login[0]->attributes()->result[0];
183 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
184 $token = (string)$sxe->login[0]->attributes()->token;
185
186 $req->setData( array(
187 "lgtoken" => $token,
188 "lgname" => self::$userName,
189 "lgpassword" => self::$passWord ) );
190 $req->execute();
191
192 $cj = $req->getCookieJar();
193 $serverName = parse_url( $wgServer, PHP_URL_HOST );
194 $this->assertNotEquals( false, $serverName );
195 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
196 $this->assertNotEquals( '', $serializedCookie );
197 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/', $serializedCookie );
198
199 return $cj;
200 }
201
202 /**
203 * @depends testApiGotCookie
204 */
205 function testApiListPages( CookieJar $cj ) {
206 $this->markTestIncomplete( "Not done with this yet" );
207 global $wgServer;
208
209 if ( $wgServer == "http://localhost" ) {
210 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
211 }
212 $req = HttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
213 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
214 $req->setCookieJar( $cj );
215 $req->execute();
216 libxml_use_internal_errors( true );
217 $sxe = simplexml_load_string( $req->getContent() );
218 $this->assertNotType( "bool", $sxe );
219 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
220 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
221 }
222 }