summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/upload/UploadFromUrlTest.php
blob: f66c387b10dc231ffcabc5143863a525451458be (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php

/**
 * @group Broken
 * @group Upload
 */
class UploadFromUrlTest extends ApiTestCase {

	public function setUp() {
		global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
		parent::setUp();

		$wgEnableUploads = true;
		$wgAllowCopyUploads = true;
		$wgAllowAsyncCopyUploads = true;
		wfSetupSession();

		if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
			$this->deleteFile( 'UploadFromUrlTest.png' );
		}
	}

	protected function doApiRequest( Array $params, Array $unused = null, $appendModule = false, User $user = null ) {
		$sessionId = session_id();
		session_write_close();

		$req = new FauxRequest( $params, true, $_SESSION );
		$module = new ApiMain( $req, true );
		$module->execute();

		wfSetupSession( $sessionId );
		return array( $module->getResultData(), $req );
	}

	/**
	 * Ensure that the job queue is empty before continuing
	 */
	public function testClearQueue() {
		$job = Job::pop();
		while ( $job ) {
			$job = Job::pop();
		}
		$this->assertFalse( $job );
	}

	/**
	 * @todo Document why we test login, since the $wgUser hack used doesn't
	 * require login
	 */
	public function testLogin() {
		$data = $this->doApiRequest( array(
			'action' => 'login',
			'lgname' => $this->user->userName,
			'lgpassword' => $this->user->passWord ) );
		$this->assertArrayHasKey( "login", $data[0] );
		$this->assertArrayHasKey( "result", $data[0]['login'] );
		$this->assertEquals( "NeedToken", $data[0]['login']['result'] );
		$token = $data[0]['login']['token'];

		$data = $this->doApiRequest( array(
			'action' => 'login',
			"lgtoken" => $token,
			'lgname' => $this->user->userName,
			'lgpassword' => $this->user->passWord ) );

		$this->assertArrayHasKey( "login", $data[0] );
		$this->assertArrayHasKey( "result", $data[0]['login'] );
		$this->assertEquals( "Success", $data[0]['login']['result'] );
		$this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );

		return $data;
	}

	/**
	 * @depends testLogin
	 * @depends testClearQueue
	 */
	public function testSetupUrlDownload( $data ) {
		$token = $this->user->getEditToken();
		$exception = false;

		try {
			$this->doApiRequest( array(
				'action' => 'upload',
			) );
		} catch ( UsageException $e ) {
			$exception = true;
			$this->assertEquals( "The token parameter must be set", $e->getMessage() );
		}
		$this->assertTrue( $exception, "Got exception" );

		$exception = false;
		try {
			$this->doApiRequest( array(
				'action' => 'upload',
				'token' => $token,
			), $data );
		} catch ( UsageException $e ) {
			$exception = true;
			$this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
				$e->getMessage() );
		}
		$this->assertTrue( $exception, "Got exception" );

		$exception = false;
		try {
			$this->doApiRequest( array(
				'action' => 'upload',
				'url' => 'http://www.example.com/test.png',
				'token' => $token,
			), $data );
		} catch ( UsageException $e ) {
			$exception = true;
			$this->assertEquals( "The filename parameter must be set", $e->getMessage() );
		}
		$this->assertTrue( $exception, "Got exception" );

		$this->user->removeGroup( 'sysop' );
		$exception = false;
		try {
			$this->doApiRequest( array(
				'action' => 'upload',
				'url' => 'http://www.example.com/test.png',
				'filename' => 'UploadFromUrlTest.png',
				'token' => $token,
			), $data );
		} catch ( UsageException $e ) {
			$exception = true;
			$this->assertEquals( "Permission denied", $e->getMessage() );
		}
		$this->assertTrue( $exception, "Got exception" );

		$this->user->addGroup( 'sysop' );
		$data = $this->doApiRequest( array(
			'action' => 'upload',
			'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
			'asyncdownload' => 1,
			'filename' => 'UploadFromUrlTest.png',
			'token' => $token,
		), $data );

		$this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );

		$job = Job::pop();
		$this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
	}

	/**
	 * @depends testLogin
	 * @depends testClearQueue
	 */
	public function testAsyncUpload( $data ) {
		$token = $this->user->getEditToken();

		$this->user->addGroup( 'users' );

		$data = $this->doAsyncUpload( $token, true );
		$this->assertEquals( $data[0]['upload']['result'], 'Success' );
		$this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
		$this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );

		$this->deleteFile( 'UploadFromUrlTest.png' );

		return $data;
	}

	/**
	 * @depends testLogin
	 * @depends testClearQueue
	 */
	public function testAsyncUploadWarning( $data ) {
		$token = $this->user->getEditToken();

		$this->user->addGroup( 'users' );


		$data = $this->doAsyncUpload( $token );

		$this->assertEquals( $data[0]['upload']['result'], 'Warning' );
		$this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );

		$data = $this->doApiRequest( array(
			'action' => 'upload',
			'sessionkey' => $data[0]['upload']['sessionkey'],
			'filename' => 'UploadFromUrlTest.png',
			'ignorewarnings' => 1,
			'token' => $token,
		) );
		$this->assertEquals( $data[0]['upload']['result'], 'Success' );
		$this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
		$this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );

		$this->deleteFile( 'UploadFromUrlTest.png' );

		return $data;
	}

	/**
	 * @depends testLogin
	 * @depends testClearQueue
	 */
	public function testSyncDownload( $data ) {
		$token = $this->user->getEditToken();

		$job = Job::pop();
		$this->assertFalse( $job, 'Starting with an empty jobqueue' );

		$this->user->addGroup( 'users' );
		$data = $this->doApiRequest( array(
			'action' => 'upload',
			'filename' => 'UploadFromUrlTest.png',
			'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
			'ignorewarnings' => true,
			'token' => $token,
		), $data );

		$job = Job::pop();
		$this->assertFalse( $job );

		$this->assertEquals( 'Success', $data[0]['upload']['result'] );
		$this->deleteFile( 'UploadFromUrlTest.png' );

		return $data;
	}

	public function testLeaveMessage() {
		$token = $this->user->user->getEditToken();

		$talk = $this->user->user->getTalkPage();
		if ( $talk->exists() ) {
			$page = WikiPage::factory( $talk );
			$page->doDeleteArticle( '' );
		}

		$this->assertFalse( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );

		$data = $this->doApiRequest( array(
			'action' => 'upload',
			'filename' => 'UploadFromUrlTest.png',
			'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
			'asyncdownload' => 1,
			'token' => $token,
			'leavemessage' => 1,
			'ignorewarnings' => 1,
		) );

		$job = Job::pop();
		$this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
		$job->run();

		$this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
		$this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );

		$this->deleteFile( 'UploadFromUrlTest.png' );

		$talkRev = Revision::newFromTitle( $talk );
		$talkSize = $talkRev->getSize();

		$exception = false;
		try {
			$data = $this->doApiRequest( array(
				'action' => 'upload',
				'filename' => 'UploadFromUrlTest.png',
				'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
				'asyncdownload' => 1,
				'token' => $token,
				'leavemessage' => 1,
			) );
		} catch ( UsageException $e ) {
			$exception = true;
			$this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
		}
		$this->assertTrue( $exception );

		$job = Job::pop();
		$this->assertFalse( $job );

		return;

		/*
		// Broken until using leavemessage with ignorewarnings is supported
		$job->run();

		$this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );

		$talkRev = Revision::newFromTitle( $talk );
		$this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
		*/
	}

	/**
	 * Helper function to perform an async upload, execute the job and fetch
	 * the status
	 *
	 * @return array The result of action=upload&statuskey=key
	 */
	private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
		$params = array(
			'action' => 'upload',
			'filename' => 'UploadFromUrlTest.png',
			'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
			'asyncdownload' => 1,
			'token' => $token,
		);
		if ( $ignoreWarnings ) {
			$params['ignorewarnings'] = 1;
		}
		if ( $leaveMessage ) {
			$params['leavemessage'] = 1;
		}

		$data = $this->doApiRequest( $params );
		$this->assertEquals( $data[0]['upload']['result'], 'Queued' );
		$this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
		$statusKey = $data[0]['upload']['statuskey'];

		$job = Job::pop();
		$this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );

		$status = $job->run();
		$this->assertTrue( $status );

		$data = $this->doApiRequest( array(
			'action' => 'upload',
			'statuskey' => $statusKey,
			'token' => $token,
		) );

		return $data;
	}


	/**
	 *
	 */
	protected function deleteFile( $name ) {
		$t = Title::newFromText( $name, NS_FILE );
		$this->assertTrue($t->exists(), "File '$name' exists");

		if ( $t->exists() ) {
			$file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
			$empty = "";
			FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
			$page = WikiPage::factory( $t );
			$page->doDeleteArticle( "testing" );
		}
		$t = Title::newFromText( $name, NS_FILE );

		$this->assertFalse($t->exists(), "File '$name' was deleted");
	}
 }