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