(Bug 44192) Do not attempt to send a real e-mail in ApiAccountCreationTest
[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
10 protected function setUp() {
11 global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
12 parent::setUp();
13
14 $wgEnableUploads = true;
15 $wgAllowCopyUploads = true;
16 $wgAllowAsyncCopyUploads = true;
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 return array( $module->getResultData(), $req );
34 }
35
36 /**
37 * Ensure that the job queue is empty before continuing
38 */
39 public function testClearQueue() {
40 $job = JobQueueGroup::singleton()->pop();
41 while ( $job ) {
42 $job = JobQueueGroup::singleton()->pop();
43 }
44 $this->assertFalse( $job );
45 }
46
47 /**
48 * @todo Document why we test login, since the $wgUser hack used doesn't
49 * require login
50 */
51 public function testLogin() {
52 $data = $this->doApiRequest( array(
53 'action' => 'login',
54 'lgname' => $this->user->userName,
55 'lgpassword' => $this->user->passWord ) );
56 $this->assertArrayHasKey( "login", $data[0] );
57 $this->assertArrayHasKey( "result", $data[0]['login'] );
58 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
59 $token = $data[0]['login']['token'];
60
61 $data = $this->doApiRequest( array(
62 'action' => 'login',
63 "lgtoken" => $token,
64 'lgname' => $this->user->userName,
65 'lgpassword' => $this->user->passWord ) );
66
67 $this->assertArrayHasKey( "login", $data[0] );
68 $this->assertArrayHasKey( "result", $data[0]['login'] );
69 $this->assertEquals( "Success", $data[0]['login']['result'] );
70 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
71
72 return $data;
73 }
74
75 /**
76 * @depends testLogin
77 * @depends testClearQueue
78 */
79 public function testSetupUrlDownload( $data ) {
80 $token = $this->user->getEditToken();
81 $exception = false;
82
83 try {
84 $this->doApiRequest( array(
85 'action' => 'upload',
86 ) );
87 } catch ( UsageException $e ) {
88 $exception = true;
89 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
90 }
91 $this->assertTrue( $exception, "Got exception" );
92
93 $exception = false;
94 try {
95 $this->doApiRequest( array(
96 'action' => 'upload',
97 'token' => $token,
98 ), $data );
99 } catch ( UsageException $e ) {
100 $exception = true;
101 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
102 $e->getMessage() );
103 }
104 $this->assertTrue( $exception, "Got exception" );
105
106 $exception = false;
107 try {
108 $this->doApiRequest( array(
109 'action' => 'upload',
110 'url' => 'http://www.example.com/test.png',
111 'token' => $token,
112 ), $data );
113 } catch ( UsageException $e ) {
114 $exception = true;
115 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
116 }
117 $this->assertTrue( $exception, "Got exception" );
118
119 $this->user->removeGroup( 'sysop' );
120 $exception = false;
121 try {
122 $this->doApiRequest( array(
123 'action' => 'upload',
124 'url' => 'http://www.example.com/test.png',
125 'filename' => 'UploadFromUrlTest.png',
126 'token' => $token,
127 ), $data );
128 } catch ( UsageException $e ) {
129 $exception = true;
130 $this->assertEquals( "Permission denied", $e->getMessage() );
131 }
132 $this->assertTrue( $exception, "Got exception" );
133
134 $this->user->addGroup( 'sysop' );
135 $data = $this->doApiRequest( array(
136 'action' => 'upload',
137 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
138 'asyncdownload' => 1,
139 'filename' => 'UploadFromUrlTest.png',
140 'token' => $token,
141 ), $data );
142
143 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
144
145 $job = JobQueueGroup::singleton()->pop();
146 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
147 }
148
149 /**
150 * @depends testLogin
151 * @depends testClearQueue
152 */
153 public function testAsyncUpload( $data ) {
154 $token = $this->user->getEditToken();
155
156 $this->user->addGroup( 'users' );
157
158 $data = $this->doAsyncUpload( $token, true );
159 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
160 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
161 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
162
163 $this->deleteFile( 'UploadFromUrlTest.png' );
164
165 return $data;
166 }
167
168 /**
169 * @depends testLogin
170 * @depends testClearQueue
171 */
172 public function testAsyncUploadWarning( $data ) {
173 $token = $this->user->getEditToken();
174
175 $this->user->addGroup( 'users' );
176
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 $data = $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 $data = $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 /*
282 // Broken until using leavemessage with ignorewarnings is supported
283 $job->run();
284
285 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
286
287 $talkRev = Revision::newFromTitle( $talk );
288 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
289 */
290 }
291
292 /**
293 * Helper function to perform an async upload, execute the job and fetch
294 * the status
295 *
296 * @return array The result of action=upload&statuskey=key
297 */
298 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
299 $params = array(
300 'action' => 'upload',
301 'filename' => 'UploadFromUrlTest.png',
302 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
303 'asyncdownload' => 1,
304 'token' => $token,
305 );
306 if ( $ignoreWarnings ) {
307 $params['ignorewarnings'] = 1;
308 }
309 if ( $leaveMessage ) {
310 $params['leavemessage'] = 1;
311 }
312
313 $data = $this->doApiRequest( $params );
314 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
315 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
316 $statusKey = $data[0]['upload']['statuskey'];
317
318 $job = JobQueueGroup::singleton()->pop();
319 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
320
321 $status = $job->run();
322 $this->assertTrue( $status );
323
324 $data = $this->doApiRequest( array(
325 'action' => 'upload',
326 'statuskey' => $statusKey,
327 'token' => $token,
328 ) );
329
330 return $data;
331 }
332
333
334 /**
335 *
336 */
337 protected function deleteFile( $name ) {
338 $t = Title::newFromText( $name, NS_FILE );
339 $this->assertTrue($t->exists(), "File '$name' exists");
340
341 if ( $t->exists() ) {
342 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
343 $empty = "";
344 FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
345 $page = WikiPage::factory( $t );
346 $page->doDeleteArticle( "testing" );
347 }
348 $t = Title::newFromText( $name, NS_FILE );
349
350 $this->assertFalse($t->exists(), "File '$name' was deleted");
351 }
352 }