* upgrade patches for oracle 1.17->1.19
[mediawiki.git] / tests / phpunit / includes / UploadFromUrlTest.php
blobc86e9e2a866130b91aa64845b6377b21ca4d5fac
1 <?php
3 require_once dirname( __FILE__ ) . '/api/ApiSetup.php';
5 /**
6 * @group Broken
7 * @group Upload
8 */
9 class UploadFromUrlTest extends ApiTestSetup {
11 public function setUp() {
12 global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
13 parent::setUp();
15 $wgEnableUploads = true;
16 $wgAllowCopyUploads = true;
17 $wgAllowAsyncCopyUploads = true;
18 wfSetupSession();
20 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
21 $this->deleteFile( 'UploadFromUrlTest.png' );
25 protected function doApiRequest( $params, $unused = null, $appendModule = false ) {
26 $sessionId = session_id();
27 session_write_close();
29 $req = new FauxRequest( $params, true, $_SESSION );
30 $module = new ApiMain( $req, true );
31 $module->execute();
33 wfSetupSession( $sessionId );
34 return array( $module->getResultData(), $req );
37 /**
38 * Ensure that the job queue is empty before continuing
40 public function testClearQueue() {
41 while ( $job = Job::pop() ) { }
42 $this->assertFalse( $job );
45 /**
46 * @todo Document why we test login, since the $wgUser hack used doesn't
47 * require login
49 public function testLogin() {
50 $data = $this->doApiRequest( array(
51 'action' => 'login',
52 'lgname' => $this->user->userName,
53 'lgpassword' => $this->user->passWord ) );
54 $this->assertArrayHasKey( "login", $data[0] );
55 $this->assertArrayHasKey( "result", $data[0]['login'] );
56 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
57 $token = $data[0]['login']['token'];
59 $data = $this->doApiRequest( array(
60 'action' => 'login',
61 "lgtoken" => $token,
62 'lgname' => $this->user->userName,
63 'lgpassword' => $this->user->passWord ) );
65 $this->assertArrayHasKey( "login", $data[0] );
66 $this->assertArrayHasKey( "result", $data[0]['login'] );
67 $this->assertEquals( "Success", $data[0]['login']['result'] );
68 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
70 return $data;
73 /**
74 * @depends testLogin
75 * @depends testClearQueue
77 public function testSetupUrlDownload( $data ) {
78 $token = $this->user->editToken();
79 $exception = false;
81 try {
82 $this->doApiRequest( array(
83 'action' => 'upload',
84 ) );
85 } catch ( UsageException $e ) {
86 $exception = true;
87 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
89 $this->assertTrue( $exception, "Got exception" );
91 $exception = false;
92 try {
93 $this->doApiRequest( array(
94 'action' => 'upload',
95 'token' => $token,
96 ), $data );
97 } catch ( UsageException $e ) {
98 $exception = true;
99 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
100 $e->getMessage() );
102 $this->assertTrue( $exception, "Got exception" );
104 $exception = false;
105 try {
106 $this->doApiRequest( array(
107 'action' => 'upload',
108 'url' => 'http://www.example.com/test.png',
109 'token' => $token,
110 ), $data );
111 } catch ( UsageException $e ) {
112 $exception = true;
113 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
115 $this->assertTrue( $exception, "Got exception" );
117 $this->user->removeGroup( 'sysop' );
118 $exception = false;
119 try {
120 $this->doApiRequest( array(
121 'action' => 'upload',
122 'url' => 'http://www.example.com/test.png',
123 'filename' => 'UploadFromUrlTest.png',
124 'token' => $token,
125 ), $data );
126 } catch ( UsageException $e ) {
127 $exception = true;
128 $this->assertEquals( "Permission denied", $e->getMessage() );
130 $this->assertTrue( $exception, "Got exception" );
132 $this->user->addGroup( 'sysop' );
133 $data = $this->doApiRequest( array(
134 'action' => 'upload',
135 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
136 'asyncdownload' => 1,
137 'filename' => 'UploadFromUrlTest.png',
138 'token' => $token,
139 ), $data );
141 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
143 $job = Job::pop();
144 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
148 * @depends testLogin
149 * @depends testClearQueue
151 public function testAsyncUpload( $data ) {
152 $token = $this->user->editToken();
154 $this->user->addGroup( 'users' );
156 $data = $this->doAsyncUpload( $token, true );
157 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
158 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
159 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
161 $this->deleteFile( 'UploadFromUrlTest.png' );
163 return $data;
167 * @depends testLogin
168 * @depends testClearQueue
170 public function testAsyncUploadWarning( $data ) {
171 $token = $this->user->editToken();
173 $this->user->addGroup( 'users' );
176 $data = $this->doAsyncUpload( $token );
178 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
179 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
181 $data = $this->doApiRequest( array(
182 'action' => 'upload',
183 'sessionkey' => $data[0]['upload']['sessionkey'],
184 'filename' => 'UploadFromUrlTest.png',
185 'ignorewarnings' => 1,
186 'token' => $token,
187 ) );
188 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
189 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
190 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
192 $this->deleteFile( 'UploadFromUrlTest.png' );
194 return $data;
198 * @depends testLogin
199 * @depends testClearQueue
201 public function testSyncDownload( $data ) {
202 $token = $this->user->editToken();
204 $job = Job::pop();
205 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
207 $this->user->addGroup( 'users' );
208 $data = $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 'ignorewarnings' => true,
213 'token' => $token,
214 ), $data );
216 $job = Job::pop();
217 $this->assertFalse( $job );
219 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
220 $this->deleteFile( 'UploadFromUrlTest.png' );
222 return $data;
225 public function testLeaveMessage() {
226 $token = $this->user->user->editToken();
228 $talk = $this->user->user->getTalkPage();
229 if ( $talk->exists() ) {
230 $a = new Article( $talk );
231 $a->doDeleteArticle( '' );
234 $this->assertFalse( (bool)$talk->getArticleId( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );
236 $data = $this->doApiRequest( array(
237 'action' => 'upload',
238 'filename' => 'UploadFromUrlTest.png',
239 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
240 'asyncdownload' => 1,
241 'token' => $token,
242 'leavemessage' => 1,
243 'ignorewarnings' => 1,
244 ) );
246 $job = Job::pop();
247 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
248 $job->run();
250 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
251 $this->assertTrue( (bool)$talk->getArticleId( Title::GAID_FOR_UPDATE ), 'User talk exists' );
253 $this->deleteFile( 'UploadFromUrlTest.png' );
255 $talkRev = Revision::newFromTitle( $talk );
256 $talkSize = $talkRev->getSize();
258 $exception = false;
259 try {
260 $data = $this->doApiRequest( array(
261 'action' => 'upload',
262 'filename' => 'UploadFromUrlTest.png',
263 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
264 'asyncdownload' => 1,
265 'token' => $token,
266 'leavemessage' => 1,
267 ) );
268 } catch ( UsageException $e ) {
269 $exception = true;
270 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
272 $this->assertTrue( $exception );
274 $job = Job::pop();
275 $this->assertFalse( $job );
277 return;
280 // Broken until using leavemessage with ignorewarnings is supported
281 $job->run();
283 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
285 $talkRev = Revision::newFromTitle( $talk );
286 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
291 * Helper function to perform an async upload, execute the job and fetch
292 * the status
294 * @return array The result of action=upload&statuskey=key
296 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
297 $params = array(
298 'action' => 'upload',
299 'filename' => 'UploadFromUrlTest.png',
300 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
301 'asyncdownload' => 1,
302 'token' => $token,
304 if ( $ignoreWarnings ) {
305 $params['ignorewarnings'] = 1;
307 if ( $leaveMessage ) {
308 $params['leavemessage'] = 1;
311 $data = $this->doApiRequest( $params );
312 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
313 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
314 $statusKey = $data[0]['upload']['statuskey'];
316 $job = Job::pop();
317 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
319 $status = $job->run();
320 $this->assertTrue( $status );
322 $data = $this->doApiRequest( array(
323 'action' => 'upload',
324 'statuskey' => $statusKey,
325 'token' => $token,
326 ) );
328 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 $a = new Article ( $t );
344 $a->doDeleteArticle( "testing" );
346 $t = Title::newFromText( $name, NS_FILE );
348 $this->assertFalse($t->exists(), "File '$name' was deleted");