assertType() is deprecated, switching usages tp assertInternalType() and assertInstan...
[lhc/web/wiklou.git] / 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 /**
21 * @group Database
22 * @group Destructive
23 */
24 class ApiTest extends ApiTestSetup {
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->assertNotInternalType( "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::$user->password,
83 ) );
84 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
85 }
86
87 function testApiLoginBadPass() {
88 global $wgServer;
89
90 $user = self::$user;
91
92 if ( !isset( $wgServer ) ) {
93 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
94 }
95 $ret = $this->doApiRequest( array(
96 "action" => "login",
97 "lgname" => $user->userName,
98 "lgpassword" => "bad",
99 )
100 );
101
102 $result = $ret[0];
103
104 $this->assertNotInternalType( "bool", $result );
105 $a = $result["login"]["result"];
106 $this->assertEquals( "NeedToken", $a );
107
108 $token = $result["login"]["token"];
109
110 $ret = $this->doApiRequest( array(
111 "action" => "login",
112 "lgtoken" => $token,
113 "lgname" => $user->userName,
114 "lgpassword" => "bad",
115 )
116 );
117
118 $result = $ret[0];
119
120 $this->assertNotInternalType( "bool", $result );
121 $a = $result["login"]["result"];
122
123 $this->assertEquals( "WrongPass", $a );
124 }
125
126 function testApiLoginGoodPass() {
127 global $wgServer;
128
129 if ( !isset( $wgServer ) ) {
130 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
131 }
132
133 $user = self::$user;
134
135 $ret = $this->doApiRequest( array(
136 "action" => "login",
137 "lgname" => $user->userName,
138 "lgpassword" => $user->password,
139 )
140 );
141
142 $result = $ret[0];
143 $this->assertNotInternalType( "bool", $result );
144 $this->assertNotInternalType( "null", $result["login"] );
145
146 $a = $result["login"]["result"];
147 $this->assertEquals( "NeedToken", $a );
148 $token = $result["login"]["token"];
149
150 $ret = $this->doApiRequest( array(
151 "action" => "login",
152 "lgtoken" => $token,
153 "lgname" => $user->userName,
154 "lgpassword" => $user->password,
155 )
156 );
157
158 $result = $ret[0];
159
160 $this->assertNotInternalType( "bool", $result );
161 $a = $result["login"]["result"];
162
163 $this->assertEquals( "Success", $a );
164 }
165
166 function testApiGotCookie() {
167 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
168
169 global $wgServer, $wgScriptPath;
170
171 if ( !isset( $wgServer ) ) {
172 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
173 }
174 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
175 array( "method" => "POST",
176 "postData" => array(
177 "lgname" => self::$user->userName,
178 "lgpassword" => self::$user->password ) ) );
179 $req->execute();
180
181 libxml_use_internal_errors( true );
182 $sxe = simplexml_load_string( $req->getContent() );
183 $this->assertNotInternalType( "bool", $sxe );
184 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
185 $this->assertNotInternalType( "null", $sxe->login[0] );
186
187 $a = $sxe->login[0]->attributes()->result[0];
188 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
189 $token = (string)$sxe->login[0]->attributes()->token;
190
191 $req->setData( array(
192 "lgtoken" => $token,
193 "lgname" => self::$user->userName,
194 "lgpassword" => self::$user->password ) );
195 $req->execute();
196
197 $cj = $req->getCookieJar();
198 $serverName = parse_url( $wgServer, PHP_URL_HOST );
199 $this->assertNotEquals( false, $serverName );
200 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
201 $this->assertNotEquals( '', $serializedCookie );
202 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$user->userName . '; .*Token=/', $serializedCookie );
203
204 return $cj;
205 }
206
207 /**
208 * @depends testApiGotCookie
209 */
210 function testApiListPages( CookieJar $cj ) {
211 $this->markTestIncomplete( "Not done with this yet" );
212 global $wgServer;
213
214 if ( $wgServer == "http://localhost" ) {
215 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
216 }
217 $req = MWHttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
218 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
219 $req->setCookieJar( $cj );
220 $req->execute();
221 libxml_use_internal_errors( true );
222 $sxe = simplexml_load_string( $req->getContent() );
223 $this->assertNotInternalType( "bool", $sxe );
224 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
225 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
226 }
227 }