summaryrefslogtreecommitdiff
path: root/maintenance/tests/ApiTest.php
blob: d098b1a2a9466178988aa95eede99404a95ab929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

require_once( "ApiSetup.php" );

class MockApi extends ApiBase {
	public function execute() {}
	public function getVersion() {}

	public function __construct() {}

	public function getAllowedParams() {
		$params = array(
			'filename' => 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();
	}
}