4 public function handleOutput() { }
5 public function purgeRedundantText() { }
8 class UploadFromUrlTest
extends ApiTestSetup
{
11 global $wgEnableUploads, $wgAllowCopyUploads;
13 $wgEnableUploads = true;
14 $wgAllowCopyUploads = true;
17 ini_set( 'log_errors', 1 );
18 ini_set( 'error_reporting', 1 );
19 ini_set( 'display_errors', 1 );
22 function doApiRequest( $params, $data = null ) {
23 $session = isset( $data[2] ) ?
$data[2] : array();
26 $req = new FauxRequest( $params, true, $session );
27 $module = new ApiMain( $req, true );
30 return array( $module->getResultData(), $req, $_SESSION );
33 function testClearQueue() {
34 while ( $job = Job
::pop() ) { }
35 $this->assertFalse( $job );
38 function testLogin() {
39 $data = $this->doApiRequest( array(
41 'lgname' => self
::$userName,
42 'lgpassword' => self
::$passWord ) );
43 $this->assertArrayHasKey( "login", $data[0] );
44 $this->assertArrayHasKey( "result", $data[0]['login'] );
45 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
46 $token = $data[0]['login']['token'];
48 $data = $this->doApiRequest( array(
51 "lgname" => self
::$userName,
52 "lgpassword" => self
::$passWord ) );
54 $this->assertArrayHasKey( "login", $data[0] );
55 $this->assertArrayHasKey( "result", $data[0]['login'] );
56 $this->assertEquals( "Success", $data[0]['login']['result'] );
57 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
65 function testSetupUrlDownload( $data ) {
67 $wgUser = User
::newFromName( self
::$userName );
69 $data[2]['wsEditToken'] = $data[2]['wsToken'];
70 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX
;
74 $this->doApiRequest( array(
77 } catch ( UsageException
$e ) {
79 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
81 $this->assertTrue( $exception, "Got exception" );
85 $this->doApiRequest( array(
89 } catch ( UsageException
$e ) {
91 $this->assertEquals( "One of the parameters sessionkey, file, url is required",
94 $this->assertTrue( $exception, "Got exception" );
98 $this->doApiRequest( array(
100 'url' => 'http://www.example.com/test.png',
103 } catch ( UsageException
$e ) {
105 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
107 $this->assertTrue( $exception, "Got exception" );
109 $wgUser->removeGroup( 'sysop' );
112 $this->doApiRequest( array(
113 'action' => 'upload',
114 'url' => 'http://www.example.com/test.png',
115 'filename' => 'UploadFromUrlTest.png',
118 } catch ( UsageException
$e ) {
120 $this->assertEquals( "Permission denied", $e->getMessage() );
122 $this->assertTrue( $exception, "Got exception" );
124 $wgUser->addGroup( '*' );
125 $wgUser->addGroup( 'sysop' );
127 $data = $this->doApiRequest( array(
128 'action' => 'upload',
129 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
130 'asyncdownload' => 1,
131 'filename' => 'UploadFromUrlTest.png',
135 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
138 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
144 function testDoDownload( $data ) {
146 $data[2]['wsEditToken'] = $data[2]['wsToken'];
147 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX
;
149 $wgUser->addGroup( 'users' );
150 $data = $this->doApiRequest( array(
151 'action' => 'upload',
152 'filename' => 'UploadFromUrlTest.png',
153 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
154 'asyncdownload' => 1,
159 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
161 $status = $job->run();
163 $this->assertTrue( $status );
171 function testSyncDownload( $data ) {
173 $data[2]['wsEditToken'] = $data[2]['wsToken'];
174 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX
;
177 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
179 //$this->deleteFile( 'UploadFromUrlTest.png' );
181 $wgUser->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,
191 $this->assertFalse( $job );
193 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
199 * @depends testDoDownload
201 function testVerifyDownload( $data ) {
202 $t = Title
::newFromText( "UploadFromUrlTest.png", NS_FILE
);
204 $this->assertTrue( $t->exists() );
206 $this->deleteFile( 'UploadFromUrlTest.png' );
212 function deleteFile( $name ) {
213 $t = Title
::newFromText( $name, NS_FILE
);
214 $this->assertTrue($t->exists(), "File '$name' exists");
216 if ( $t->exists() ) {
217 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
219 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
220 $a = new Article ( $t );
221 $a->doDeleteArticle( "testing" );
223 $t = Title
::newFromText( $name, NS_FILE
);
225 $this->assertFalse($t->exists(), "File '$name' was deleted");