summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-api-base/tests/Unit/MediawikiApiTest.php
blob: a55f67396ffa0c0cc2c94f141df010f033299aff (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php

namespace Mediawiki\Api\Test\Unit;

use Mediawiki\Api\ApiUser;
use Mediawiki\Api\MediawikiApi;
use Mediawiki\Api\SimpleRequest;
use Mediawiki\Api\UsageException;
use PHPUnit_Framework_TestCase;
use stdClass;

/**
 * @author Addshore
 *
 * @covers Mediawiki\Api\MediawikiApi
 */
class MediawikiApiTest extends PHPUnit_Framework_TestCase {

	public function provideValidConstruction() {
		return [
			[ 'localhost' ],
			[ 'http://en.wikipedia.org/w/api.php' ],
			[ '127.0.0.1/foo/bar/wwwwwwwww/api.php' ],
		];
	}

	/**
	 * @dataProvider provideValidConstruction
	 */
	public function testValidConstruction( $apiLocation ) {
		new MediawikiApi( $apiLocation );
		$this->assertTrue( true );
	}

	public function provideInvalidConstruction() {
		return [
			[ null ],
			[ 12345678 ],
			[ [] ],
			[ new stdClass() ],
		];
	}

	/**
	 * @dataProvider provideInvalidConstruction
	 */
	public function testInvalidConstruction( $apiLocation ) {
		$this->setExpectedException( 'InvalidArgumentException' );
		new MediawikiApi( $apiLocation );
	}

	private function getMockClient() {
		return $this->getMock( 'GuzzleHttp\ClientInterface' );
	}

	private function getMockResponse( $responseValue ) {
		$mock = $this->getMock( 'Psr\Http\Message\ResponseInterface' );
		$mock->expects( $this->any() )
			->method( 'getBody' )
			->will( $this->returnValue( json_encode( $responseValue ) ) );
		return $mock;
	}

	private function getExpectedRequestOpts( $params, $paramsLocation ) {
		return [
			$paramsLocation => array_merge( $params, [ 'format' => 'json' ] ),
			'headers' => [ 'User-Agent' => 'addwiki-mediawiki-client' ],
		];
	}

	public function testGetRequestThrowsUsageExceptionOnError() {
		$client = $this->getMockClient();
		$client->expects( $this->once() )
			->method( 'request' )
			->will( $this->returnValue(
				$this->getMockResponse( [ 'error' => [
					'code' => 'imacode',
					'info' => 'imamsg',
				] ] )
			) );
		$api = new MediawikiApi( '', $client );

		try{
			$api->getRequest( new SimpleRequest( 'foo' ) );
			$this->fail( 'No Usage Exception Thrown' );
		}
		catch ( UsageException $e ) {
			$this->assertEquals( 'imacode', $e->getApiCode() );
			$this->assertEquals( 'imamsg', $e->getRawMessage() );
		}
	}

	public function testPostRequestThrowsUsageExceptionOnError() {
		$client = $this->getMockClient();
		$client->expects( $this->once() )
			->method( 'request' )
			->will( $this->returnValue(
				$this->getMockResponse( [ 'error' => [
					'code' => 'imacode',
					'info' => 'imamsg',
				] ] )
			) );
		$api = new MediawikiApi( '', $client );

		try{
			$api->postRequest( new SimpleRequest( 'foo' ) );
			$this->fail( 'No Usage Exception Thrown' );
		}
		catch ( UsageException $e ) {
			$this->assertSame( 'imacode', $e->getApiCode() );
			$this->assertSame( 'imamsg', $e->getRawMessage() );
		}
	}

	/**
	 * @dataProvider provideActionsParamsResults
	 */
	public function testGetActionReturnsResult( $expectedResult, $action, $params = [] ) {
		$client = $this->getMockClient();
		$params = array_merge( [ 'action' => $action ], $params );
		$client->expects( $this->once() )
			->method( 'request' )
			->with( 'GET', null, $this->getExpectedRequestOpts( $params, 'query' ) )
			->will( $this->returnValue( $this->getMockResponse( $expectedResult ) ) );
		$api = new MediawikiApi( '', $client );

		$result = $api->getRequest( new SimpleRequest( $action, $params ) );

		$this->assertEquals( $expectedResult, $result );
	}

	/**
	 * @dataProvider provideActionsParamsResults
	 */
	public function testPostActionReturnsResult( $expectedResult, $action, $params = [] ) {
		$client = $this->getMockClient();
		$params = array_merge( [ 'action' => $action ], $params );
		$client->expects( $this->once() )
			->method( 'request' )
			->with( 'POST', null, $this->getExpectedRequestOpts( $params, 'form_params' ) )
			->will( $this->returnValue( $this->getMockResponse( $expectedResult ) ) );
		$api = new MediawikiApi( '', $client );

		$result = $api->postRequest( new SimpleRequest( $action, $params ) );

		$this->assertEquals( $expectedResult, $result );
	}

	private function getNullFilePointer() {
		if ( !file_exists( '/dev/null' ) ) {
			// windows
			return fopen( 'NUL', 'r' );
		}
		return fopen( '/dev/null', 'r' );
	}

	public function testPostActionWithFileReturnsResult() {
		$dummyFile = $this->getNullFilePointer();
		$params = [
			'filename' => 'foo.jpg',
			'file' => $dummyFile,
		];
		$client = $this->getMockClient();
		$client->expects( $this->once() )->method( 'request' )->with(
				'POST',
				null,
				[
					'multipart' => [
						[ 'name' => 'action', 'contents' => 'upload' ],
						[ 'name' => 'filename', 'contents' => 'foo.jpg' ],
						[ 'name' => 'file', 'contents' => $dummyFile ],
						[ 'name' => 'format', 'contents' => 'json' ],
					],
					'headers' => [ 'User-Agent' => 'addwiki-mediawiki-client' ],
				]
			)->will( $this->returnValue( $this->getMockResponse( [ 'success ' => 1 ] ) ) );
		$api = new MediawikiApi( '', $client );

		$result = $api->postRequest( new SimpleRequest( 'upload', $params ) );

		$this->assertEquals( [ 'success ' => 1 ], $result );
	}

	public function provideActionsParamsResults() {
		return [
			[ [ 'key' => 'value' ], 'logout' ],
			[ [ 'key' => 'value' ], 'logout', [ 'param1' => 'v1' ] ],
			[ [ 'key' => 'value', 'key2' => 1212, [] ], 'logout' ],
		];
	}

	public function testGoodLoginSequence() {
		$client = $this->getMockClient();
		$user = new ApiUser( 'U1', 'P1' );
		$eq1 = [
			'action' => 'login',
			'lgname' => 'U1',
			'lgpassword' => 'P1',
		];
		$client->expects( $this->at( 0 ) )
			->method( 'request' )
			->with( 'POST', null, $this->getExpectedRequestOpts( $eq1, 'form_params' ) )
			->will( $this->returnValue( $this->getMockResponse( [ 'login' => [
				'result' => 'NeedToken',
				'token' => 'IamLoginTK',
			] ] ) ) );
		$params = array_merge( $eq1, [ 'lgtoken' => 'IamLoginTK' ] );
		$response = $this->getMockResponse( [ 'login' => [ 'result' => 'Success' ] ] );
		$client->expects( $this->at( 1 ) )
			->method( 'request' )
			->with( 'POST', null, $this->getExpectedRequestOpts( $params, 'form_params' ) )
			->will( $this->returnValue( $response ) );
		$api = new MediawikiApi( '', $client );

		$this->assertTrue( $api->login( $user ) );
		$this->assertSame( 'U1', $api->isLoggedin() );
	}

	public function testBadLoginSequence() {
		$client = $this->getMockClient();
		$user = new ApiUser( 'U1', 'P1' );
		$eq1 = [
			'action' => 'login',
			'lgname' => 'U1',
			'lgpassword' => 'P1',
		];
		$client->expects( $this->at( 0 ) )
			->method( 'request' )
			->with( 'POST', null, $this->getExpectedRequestOpts( $eq1, 'form_params' ) )
			->will( $this->returnValue( $this->getMockResponse( [ 'login' => [
				'result' => 'NeedToken',
				'token' => 'IamLoginTK',
			] ] ) ) );
		$params = array_merge( $eq1, [ 'lgtoken' => 'IamLoginTK' ] );
		$response = $this->getMockResponse( [ 'login' => [ 'result' => 'BADTOKENorsmthin' ] ] );
		$client->expects( $this->at( 1 ) )
			->method( 'request' )
			->with( 'POST', null, $this->getExpectedRequestOpts( $params, 'form_params' ) )
			->will( $this->returnValue( $response ) );
		$api = new MediawikiApi( '', $client );

		$this->setExpectedException( 'Mediawiki\Api\UsageException' );
		$api->login( $user );
	}

	public function testLogout() {
		$client = $this->getMockClient();
		$client->expects( $this->at( 0 ) )
			->method( 'request' )
			->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) )
			->will( $this->returnValue( $this->getMockResponse( [] ) ) );
		$api = new MediawikiApi( '', $client );

		$this->assertTrue( $api->logout() );
	}

	public function testLogoutOnFailure() {
		$client = $this->getMockClient();
		$client->expects( $this->at( 0 ) )
			->method( 'request' )
			->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) )
			->will( $this->returnValue( $this->getMockResponse( null ) ) );
		$api = new MediawikiApi( '', $client );

		$this->assertFalse( $api->logout() );
	}

	/**
	 * @dataProvider provideVersions
	 */
	public function testGetVersion( $apiValue, $expectedVersion ) {
		$client = $this->getMockClient();
		$params = [ 'action' => 'query', 'meta' => 'siteinfo', 'continue' => '' ];
		$client->expects( $this->exactly( 1 ) )
			->method( 'request' )
			->with( 'GET', null, $this->getExpectedRequestOpts( $params, 'query' ) )
			->will( $this->returnValue( $this->getMockResponse( [
				'query' => [
					'general' => [
						'generator' => $apiValue,
					],
				],
			] ) ) );
		$api = new MediawikiApi( '', $client );
		$this->assertEquals( $expectedVersion, $api->getVersion() );
	}

	public function provideVersions() {
		return [
			[ 'MediaWiki 1.25wmf13', '1.25' ],
			[ 'MediaWiki 1.24.1', '1.24.1' ],
			[ 'MediaWiki 1.19', '1.19' ],
			[ 'MediaWiki 1.0.0', '1.0.0' ],
		];
	}
}