Check validity and availability of usernames during signup via AJAX
[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 * @depends testClearQueue
52 */
53 public function testSetupUrlDownload( $data ) {
54 $token = $this->user->getEditToken();
55 $exception = false;
56
57 try {
58 $this->doApiRequest( array(
59 'action' => 'upload',
60 ) );
61 } catch ( UsageException $e ) {
62 $exception = true;
63 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
64 }
65 $this->assertTrue( $exception, "Got exception" );
66
67 $exception = false;
68 try {
69 $this->doApiRequest( array(
70 'action' => 'upload',
71 'token' => $token,
72 ), $data );
73 } catch ( UsageException $e ) {
74 $exception = true;
75 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
76 $e->getMessage() );
77 }
78 $this->assertTrue( $exception, "Got exception" );
79
80 $exception = false;
81 try {
82 $this->doApiRequest( array(
83 'action' => 'upload',
84 'url' => 'http://www.example.com/test.png',
85 'token' => $token,
86 ), $data );
87 } catch ( UsageException $e ) {
88 $exception = true;
89 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
90 }
91 $this->assertTrue( $exception, "Got exception" );
92
93 $this->user->removeGroup( 'sysop' );
94 $exception = false;
95 try {
96 $this->doApiRequest( array(
97 'action' => 'upload',
98 'url' => 'http://www.example.com/test.png',
99 'filename' => 'UploadFromUrlTest.png',
100 'token' => $token,
101 ), $data );
102 } catch ( UsageException $e ) {
103 $exception = true;
104 $this->assertEquals( "Permission denied", $e->getMessage() );
105 }
106 $this->assertTrue( $exception, "Got exception" );
107
108 $this->user->addGroup( 'sysop' );
109 $data = $this->doApiRequest( array(
110 'action' => 'upload',
111 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
112 'asyncdownload' => 1,
113 'filename' => 'UploadFromUrlTest.png',
114 'token' => $token,
115 ), $data );
116
117 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
118
119 $job = JobQueueGroup::singleton()->pop();
120 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
121 }
122
123 /**
124 * @depends testClearQueue
125 */
126 public function testAsyncUpload( $data ) {
127 $token = $this->user->getEditToken();
128
129 $this->user->addGroup( 'users' );
130
131 $data = $this->doAsyncUpload( $token, true );
132 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
133 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
134 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
135
136 $this->deleteFile( 'UploadFromUrlTest.png' );
137
138 return $data;
139 }
140
141 /**
142 * @depends testClearQueue
143 */
144 public function testAsyncUploadWarning( $data ) {
145 $token = $this->user->getEditToken();
146
147 $this->user->addGroup( 'users' );
148
149 $data = $this->doAsyncUpload( $token );
150
151 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
152 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
153
154 $data = $this->doApiRequest( array(
155 'action' => 'upload',
156 'sessionkey' => $data[0]['upload']['sessionkey'],
157 'filename' => 'UploadFromUrlTest.png',
158 'ignorewarnings' => 1,
159 'token' => $token,
160 ) );
161 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
162 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
163 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
164
165 $this->deleteFile( 'UploadFromUrlTest.png' );
166
167 return $data;
168 }
169
170 /**
171 * @depends testClearQueue
172 */
173 public function testSyncDownload( $data ) {
174 $token = $this->user->getEditToken();
175
176 $job = JobQueueGroup::singleton()->pop();
177 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
178
179 $this->user->addGroup( 'users' );
180 $data = $this->doApiRequest( array(
181 'action' => 'upload',
182 'filename' => 'UploadFromUrlTest.png',
183 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
184 'ignorewarnings' => true,
185 'token' => $token,
186 ), $data );
187
188 $job = JobQueueGroup::singleton()->pop();
189 $this->assertFalse( $job );
190
191 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
192 $this->deleteFile( 'UploadFromUrlTest.png' );
193
194 return $data;
195 }
196
197 public function testLeaveMessage() {
198 $token = $this->user->user->getEditToken();
199
200 $talk = $this->user->user->getTalkPage();
201 if ( $talk->exists() ) {
202 $page = WikiPage::factory( $talk );
203 $page->doDeleteArticle( '' );
204 }
205
206 $this->assertFalse( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );
207
208 $this->doApiRequest( array(
209 'action' => 'upload',
210 'filename' => 'UploadFromUrlTest.png',
211 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
212 'asyncdownload' => 1,
213 'token' => $token,
214 'leavemessage' => 1,
215 'ignorewarnings' => 1,
216 ) );
217
218 $job = JobQueueGroup::singleton()->pop();
219 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
220 $job->run();
221
222 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
223 $this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
224
225 $this->deleteFile( 'UploadFromUrlTest.png' );
226
227 $talkRev = Revision::newFromTitle( $talk );
228 $talkSize = $talkRev->getSize();
229
230 $exception = false;
231 try {
232 $this->doApiRequest( array(
233 'action' => 'upload',
234 'filename' => 'UploadFromUrlTest.png',
235 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
236 'asyncdownload' => 1,
237 'token' => $token,
238 'leavemessage' => 1,
239 ) );
240 } catch ( UsageException $e ) {
241 $exception = true;
242 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
243 }
244 $this->assertTrue( $exception );
245
246 $job = JobQueueGroup::singleton()->pop();
247 $this->assertFalse( $job );
248
249 return;
250 /*
251 // Broken until using leavemessage with ignorewarnings is supported
252 $job->run();
253
254 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
255
256 $talkRev = Revision::newFromTitle( $talk );
257 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
258 */
259 }
260
261 /**
262 * Helper function to perform an async upload, execute the job and fetch
263 * the status
264 *
265 * @return array The result of action=upload&statuskey=key
266 */
267 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
268 $params = array(
269 'action' => 'upload',
270 'filename' => 'UploadFromUrlTest.png',
271 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
272 'asyncdownload' => 1,
273 'token' => $token,
274 );
275 if ( $ignoreWarnings ) {
276 $params['ignorewarnings'] = 1;
277 }
278 if ( $leaveMessage ) {
279 $params['leavemessage'] = 1;
280 }
281
282 $data = $this->doApiRequest( $params );
283 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
284 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
285 $statusKey = $data[0]['upload']['statuskey'];
286
287 $job = JobQueueGroup::singleton()->pop();
288 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
289
290 $status = $job->run();
291 $this->assertTrue( $status );
292
293 $data = $this->doApiRequest( array(
294 'action' => 'upload',
295 'statuskey' => $statusKey,
296 'token' => $token,
297 ) );
298
299 return $data;
300 }
301
302 protected function deleteFile( $name ) {
303 $t = Title::newFromText( $name, NS_FILE );
304 $this->assertTrue( $t->exists(), "File '$name' exists" );
305
306 if ( $t->exists() ) {
307 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
308 $empty = "";
309 FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
310 $page = WikiPage::factory( $t );
311 $page->doDeleteArticle( "testing" );
312 }
313 $t = Title::newFromText( $name, NS_FILE );
314
315 $this->assertFalse( $t->exists(), "File '$name' was deleted" );
316 }
317 }