Merge "Slight improvements to FormSpecialPage behavior."
[lhc/web/wiklou.git] / tests / phpunit / includes / upload / UploadFromUrlTest.php
1 <?php
2
3 /**
4 * @group Broken
5 * @group Upload
6 * @group Database
7 */
8 class UploadFromUrlTest extends ApiTestCase {
9 protected function setUp() {
10 parent::setUp();
11
12 $this->setMwGlobals( array(
13 'wgEnableUploads' => true,
14 'wgAllowCopyUploads' => true,
15 'wgAllowAsyncCopyUploads' => true,
16 ) );
17 wfSetupSession();
18
19 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
20 $this->deleteFile( 'UploadFromUrlTest.png' );
21 }
22 }
23
24 protected function doApiRequest( array $params, array $unused = null, $appendModule = false, User $user = null ) {
25 $sessionId = session_id();
26 session_write_close();
27
28 $req = new FauxRequest( $params, true, $_SESSION );
29 $module = new ApiMain( $req, true );
30 $module->execute();
31
32 wfSetupSession( $sessionId );
33
34 return array( $module->getResultData(), $req );
35 }
36
37 /**
38 * Ensure that the job queue is empty before continuing
39 */
40 public function testClearQueue() {
41 $job = JobQueueGroup::singleton()->pop();
42 while ( $job ) {
43 $job = JobQueueGroup::singleton()->pop();
44 }
45 $this->assertFalse( $job );
46 }
47
48 /**
49 * @todo Document why we test login, since the $wgUser hack used doesn't
50 * require login
51 */
52 public function testLogin() {
53 $data = $this->doApiRequest( array(
54 'action' => 'login',
55 'lgname' => $this->user->userName,
56 'lgpassword' => $this->user->passWord ) );
57 $this->assertArrayHasKey( "login", $data[0] );
58 $this->assertArrayHasKey( "result", $data[0]['login'] );
59 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
60 $token = $data[0]['login']['token'];
61
62 $data = $this->doApiRequest( array(
63 'action' => 'login',
64 "lgtoken" => $token,
65 'lgname' => $this->user->userName,
66 'lgpassword' => $this->user->passWord ) );
67
68 $this->assertArrayHasKey( "login", $data[0] );
69 $this->assertArrayHasKey( "result", $data[0]['login'] );
70 $this->assertEquals( "Success", $data[0]['login']['result'] );
71 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
72
73 return $data;
74 }
75
76 /**
77 * @depends testLogin
78 * @depends testClearQueue
79 */
80 public function testSetupUrlDownload( $data ) {
81 $token = $this->user->getEditToken();
82 $exception = false;
83
84 try {
85 $this->doApiRequest( array(
86 'action' => 'upload',
87 ) );
88 } catch ( UsageException $e ) {
89 $exception = true;
90 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
91 }
92 $this->assertTrue( $exception, "Got exception" );
93
94 $exception = false;
95 try {
96 $this->doApiRequest( array(
97 'action' => 'upload',
98 'token' => $token,
99 ), $data );
100 } catch ( UsageException $e ) {
101 $exception = true;
102 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
103 $e->getMessage() );
104 }
105 $this->assertTrue( $exception, "Got exception" );
106
107 $exception = false;
108 try {
109 $this->doApiRequest( array(
110 'action' => 'upload',
111 'url' => 'http://www.example.com/test.png',
112 'token' => $token,
113 ), $data );
114 } catch ( UsageException $e ) {
115 $exception = true;
116 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
117 }
118 $this->assertTrue( $exception, "Got exception" );
119
120 $this->user->removeGroup( 'sysop' );
121 $exception = false;
122 try {
123 $this->doApiRequest( array(
124 'action' => 'upload',
125 'url' => 'http://www.example.com/test.png',
126 'filename' => 'UploadFromUrlTest.png',
127 'token' => $token,
128 ), $data );
129 } catch ( UsageException $e ) {
130 $exception = true;
131 $this->assertEquals( "Permission denied", $e->getMessage() );
132 }
133 $this->assertTrue( $exception, "Got exception" );
134
135 $this->user->addGroup( 'sysop' );
136 $data = $this->doApiRequest( array(
137 'action' => 'upload',
138 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
139 'asyncdownload' => 1,
140 'filename' => 'UploadFromUrlTest.png',
141 'token' => $token,
142 ), $data );
143
144 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
145
146 $job = JobQueueGroup::singleton()->pop();
147 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
148 }
149
150 /**
151 * @depends testLogin
152 * @depends testClearQueue
153 */
154 public function testAsyncUpload( $data ) {
155 $token = $this->user->getEditToken();
156
157 $this->user->addGroup( 'users' );
158
159 $data = $this->doAsyncUpload( $token, true );
160 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
161 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
162 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
163
164 $this->deleteFile( 'UploadFromUrlTest.png' );
165
166 return $data;
167 }
168
169 /**
170 * @depends testLogin
171 * @depends testClearQueue
172 */
173 public function testAsyncUploadWarning( $data ) {
174 $token = $this->user->getEditToken();
175
176 $this->user->addGroup( 'users' );
177
178 $data = $this->doAsyncUpload( $token );
179
180 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
181 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
182
183 $data = $this->doApiRequest( array(
184 'action' => 'upload',
185 'sessionkey' => $data[0]['upload']['sessionkey'],
186 'filename' => 'UploadFromUrlTest.png',
187 'ignorewarnings' => 1,
188 'token' => $token,
189 ) );
190 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
191 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
192 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
193
194 $this->deleteFile( 'UploadFromUrlTest.png' );
195
196 return $data;
197 }
198
199 /**
200 * @depends testLogin
201 * @depends testClearQueue
202 */
203 public function testSyncDownload( $data ) {
204 $token = $this->user->getEditToken();
205
206 $job = JobQueueGroup::singleton()->pop();
207 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
208
209 $this->user->addGroup( 'users' );
210 $data = $this->doApiRequest( array(
211 'action' => 'upload',
212 'filename' => 'UploadFromUrlTest.png',
213 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
214 'ignorewarnings' => true,
215 'token' => $token,
216 ), $data );
217
218 $job = JobQueueGroup::singleton()->pop();
219 $this->assertFalse( $job );
220
221 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
222 $this->deleteFile( 'UploadFromUrlTest.png' );
223
224 return $data;
225 }
226
227 public function testLeaveMessage() {
228 $token = $this->user->user->getEditToken();
229
230 $talk = $this->user->user->getTalkPage();
231 if ( $talk->exists() ) {
232 $page = WikiPage::factory( $talk );
233 $page->doDeleteArticle( '' );
234 }
235
236 $this->assertFalse( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );
237
238 $this->doApiRequest( array(
239 'action' => 'upload',
240 'filename' => 'UploadFromUrlTest.png',
241 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
242 'asyncdownload' => 1,
243 'token' => $token,
244 'leavemessage' => 1,
245 'ignorewarnings' => 1,
246 ) );
247
248 $job = JobQueueGroup::singleton()->pop();
249 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
250 $job->run();
251
252 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
253 $this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
254
255 $this->deleteFile( 'UploadFromUrlTest.png' );
256
257 $talkRev = Revision::newFromTitle( $talk );
258 $talkSize = $talkRev->getSize();
259
260 $exception = false;
261 try {
262 $this->doApiRequest( array(
263 'action' => 'upload',
264 'filename' => 'UploadFromUrlTest.png',
265 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
266 'asyncdownload' => 1,
267 'token' => $token,
268 'leavemessage' => 1,
269 ) );
270 } catch ( UsageException $e ) {
271 $exception = true;
272 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
273 }
274 $this->assertTrue( $exception );
275
276 $job = JobQueueGroup::singleton()->pop();
277 $this->assertFalse( $job );
278
279 return;
280 /*
281 // Broken until using leavemessage with ignorewarnings is supported
282 $job->run();
283
284 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
285
286 $talkRev = Revision::newFromTitle( $talk );
287 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
288 */
289 }
290
291 /**
292 * Helper function to perform an async upload, execute the job and fetch
293 * the status
294 *
295 * @return array The result of action=upload&statuskey=key
296 */
297 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
298 $params = array(
299 'action' => 'upload',
300 'filename' => 'UploadFromUrlTest.png',
301 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
302 'asyncdownload' => 1,
303 'token' => $token,
304 );
305 if ( $ignoreWarnings ) {
306 $params['ignorewarnings'] = 1;
307 }
308 if ( $leaveMessage ) {
309 $params['leavemessage'] = 1;
310 }
311
312 $data = $this->doApiRequest( $params );
313 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
314 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
315 $statusKey = $data[0]['upload']['statuskey'];
316
317 $job = JobQueueGroup::singleton()->pop();
318 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
319
320 $status = $job->run();
321 $this->assertTrue( $status );
322
323 $data = $this->doApiRequest( array(
324 'action' => 'upload',
325 'statuskey' => $statusKey,
326 'token' => $token,
327 ) );
328
329 return $data;
330 }
331
332 /**
333 *
334 */
335 protected function deleteFile( $name ) {
336 $t = Title::newFromText( $name, NS_FILE );
337 $this->assertTrue( $t->exists(), "File '$name' exists" );
338
339 if ( $t->exists() ) {
340 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
341 $empty = "";
342 FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
343 $page = WikiPage::factory( $t );
344 $page->doDeleteArticle( "testing" );
345 }
346 $t = Title::newFromText( $name, NS_FILE );
347
348 $this->assertFalse( $t->exists(), "File '$name' was deleted" );
349 }
350 }