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