test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / upload / UploadFromUrlTest.php
bloba75fba69f117267cf1193bdbf8660e0c7ae76adc
1 <?php
3 /**
4 * @group Broken
5 * @group Upload
6 * @group Database
7 */
8 class UploadFromUrlTest extends ApiTestCase {
9 protected function setUp() {
10 parent::setUp();
12 $this->setMwGlobals( array(
13 'wgEnableUploads' => true,
14 'wgAllowCopyUploads' => true,
15 'wgAllowAsyncCopyUploads' => true,
16 ) );
17 wfSetupSession();
19 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
20 $this->deleteFile( 'UploadFromUrlTest.png' );
24 protected function doApiRequest( array $params, array $unused = null, $appendModule = false, User $user = null ) {
25 $sessionId = session_id();
26 session_write_close();
28 $req = new FauxRequest( $params, true, $_SESSION );
29 $module = new ApiMain( $req, true );
30 $module->execute();
32 wfSetupSession( $sessionId );
34 return array( $module->getResultData(), $req );
37 /**
38 * Ensure that the job queue is empty before continuing
40 public function testClearQueue() {
41 $job = JobQueueGroup::singleton()->pop();
42 while ( $job ) {
43 $job = JobQueueGroup::singleton()->pop();
45 $this->assertFalse( $job );
48 /**
49 * @todo Document why we test login, since the $wgUser hack used doesn't
50 * require login
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'];
62 $data = $this->doApiRequest( array(
63 'action' => 'login',
64 "lgtoken" => $token,
65 'lgname' => $this->user->userName,
66 'lgpassword' => $this->user->passWord ) );
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'] );
73 return $data;
76 /**
77 * @depends testLogin
78 * @depends testClearQueue
80 public function testSetupUrlDownload( $data ) {
81 $token = $this->user->getEditToken();
82 $exception = false;
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() );
92 $this->assertTrue( $exception, "Got exception" );
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() );
105 $this->assertTrue( $exception, "Got exception" );
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() );
118 $this->assertTrue( $exception, "Got exception" );
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() );
133 $this->assertTrue( $exception, "Got exception" );
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 );
144 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
146 $job = JobQueueGroup::singleton()->pop();
147 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
151 * @depends testLogin
152 * @depends testClearQueue
154 public function testAsyncUpload( $data ) {
155 $token = $this->user->getEditToken();
157 $this->user->addGroup( 'users' );
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() );
164 $this->deleteFile( 'UploadFromUrlTest.png' );
166 return $data;
170 * @depends testLogin
171 * @depends testClearQueue
173 public function testAsyncUploadWarning( $data ) {
174 $token = $this->user->getEditToken();
176 $this->user->addGroup( 'users' );
178 $data = $this->doAsyncUpload( $token );
180 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
181 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
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() );
194 $this->deleteFile( 'UploadFromUrlTest.png' );
196 return $data;
200 * @depends testLogin
201 * @depends testClearQueue
203 public function testSyncDownload( $data ) {
204 $token = $this->user->getEditToken();
206 $job = JobQueueGroup::singleton()->pop();
207 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
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 );
218 $job = JobQueueGroup::singleton()->pop();
219 $this->assertFalse( $job );
221 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
222 $this->deleteFile( 'UploadFromUrlTest.png' );
224 return $data;
227 public function testLeaveMessage() {
228 $token = $this->user->user->getEditToken();
230 $talk = $this->user->user->getTalkPage();
231 if ( $talk->exists() ) {
232 $page = WikiPage::factory( $talk );
233 $page->doDeleteArticle( '' );
236 $this->assertFalse( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );
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 ) );
248 $job = JobQueueGroup::singleton()->pop();
249 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
250 $job->run();
252 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
253 $this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
255 $this->deleteFile( 'UploadFromUrlTest.png' );
257 $talkRev = Revision::newFromTitle( $talk );
258 $talkSize = $talkRev->getSize();
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() );
274 $this->assertTrue( $exception );
276 $job = JobQueueGroup::singleton()->pop();
277 $this->assertFalse( $job );
279 return;
281 // Broken until using leavemessage with ignorewarnings is supported
282 $job->run();
284 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
286 $talkRev = Revision::newFromTitle( $talk );
287 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
292 * Helper function to perform an async upload, execute the job and fetch
293 * the status
295 * @return array The result of action=upload&statuskey=key
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,
305 if ( $ignoreWarnings ) {
306 $params['ignorewarnings'] = 1;
308 if ( $leaveMessage ) {
309 $params['leavemessage'] = 1;
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'];
317 $job = JobQueueGroup::singleton()->pop();
318 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
320 $status = $job->run();
321 $this->assertTrue( $status );
323 $data = $this->doApiRequest( array(
324 'action' => 'upload',
325 'statuskey' => $statusKey,
326 'token' => $token,
327 ) );
329 return $data;
335 protected function deleteFile( $name ) {
336 $t = Title::newFromText( $name, NS_FILE );
337 $this->assertTrue( $t->exists(), "File '$name' exists" );
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" );
346 $t = Title::newFromText( $name, NS_FILE );
348 $this->assertFalse( $t->exists(), "File '$name' was deleted" );