b4f7e973513221f5bdaf8200c780f8c3c443013a
3 /* Force User.php include for EDIT_TOKEN_SUFFIX */
4 require_once dirname(__FILE__
) . "/../../includes/User.php";
7 public function handleOutput() { }
8 public function purgeRedundantText() { }
11 class UploadFromUrlTest
extends ApiTestSetup
{
14 global $wgEnableUploads, $wgAllowCopyUploads;
16 $wgEnableUploads = true;
17 $wgAllowCopyUploads = true;
20 ini_set( 'log_errors', 1 );
21 ini_set( 'error_reporting', 1 );
22 ini_set( 'display_errors', 1 );
25 function doApiRequest( $params, $data = null ) {
26 $session = isset( $data[2] ) ?
$data[2] : array();
29 $req = new FauxRequest( $params, true, $session );
30 $module = new ApiMain( $req, true );
33 return array( $module->getResultData(), $req, $_SESSION );
36 function testClearQueue() {
37 while ( $job = Job
::pop() ) { }
38 $this->assertFalse( $job );
41 function testLogin() {
42 $data = $this->doApiRequest( array(
44 'lgname' => self
::$userName,
45 'lgpassword' => self
::$passWord ) );
46 $this->assertArrayHasKey( "login", $data[0] );
47 $this->assertArrayHasKey( "result", $data[0]['login'] );
48 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
49 $token = $data[0]['login']['token'];
51 $data = $this->doApiRequest( array(
54 "lgname" => self
::$userName,
55 "lgpassword" => self
::$passWord ) );
57 $this->assertArrayHasKey( "login", $data[0] );
58 $this->assertArrayHasKey( "result", $data[0]['login'] );
59 $this->assertEquals( "Success", $data[0]['login']['result'] );
60 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
68 function testSetupUrlDownload( $data ) {
70 $wgUser = User
::newFromName( self
::$userName );
72 $data[2]['wsEditToken'] = $data[2]['wsToken'];
73 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX
;
77 $this->doApiRequest( array(
80 } catch ( UsageException
$e ) {
82 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
84 $this->assertTrue( $exception, "Got exception" );
88 $this->doApiRequest( array(
92 } catch ( UsageException
$e ) {
94 $this->assertEquals( "One of the parameters sessionkey, file, url is required",
97 $this->assertTrue( $exception, "Got exception" );
101 $this->doApiRequest( array(
102 'action' => 'upload',
103 'url' => 'http://www.example.com/test.png',
106 } catch ( UsageException
$e ) {
108 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
110 $this->assertTrue( $exception, "Got exception" );
112 $wgUser->removeGroup( 'sysop' );
115 $this->doApiRequest( array(
116 'action' => 'upload',
117 'url' => 'http://www.example.com/test.png',
118 'filename' => 'UploadFromUrlTest.png',
121 } catch ( UsageException
$e ) {
123 $this->assertEquals( "Permission denied", $e->getMessage() );
125 $this->assertTrue( $exception, "Got exception" );
127 $wgUser->addGroup( '*' );
128 $wgUser->addGroup( 'sysop' );
130 $data = $this->doApiRequest( array(
131 'action' => 'upload',
132 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
133 'asyncdownload' => 1,
134 'filename' => 'UploadFromUrlTest.png',
138 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
141 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
147 function testDoDownload( $data ) {
149 $data[2]['wsEditToken'] = $data[2]['wsToken'];
150 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX
;
152 $wgUser->addGroup( 'users' );
153 $data = $this->doApiRequest( array(
154 'action' => 'upload',
155 'filename' => 'UploadFromUrlTest.png',
156 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
157 'asyncdownload' => 1,
162 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
164 $status = $job->run();
166 $this->assertTrue( $status );
174 function testSyncDownload( $data ) {
176 $data[2]['wsEditToken'] = $data[2]['wsToken'];
177 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX
;
180 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
182 //$this->deleteFile( 'UploadFromUrlTest.png' );
184 $wgUser->addGroup( 'users' );
185 $data = $this->doApiRequest( array(
186 'action' => 'upload',
187 'filename' => 'UploadFromUrlTest.png',
188 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
189 'ignorewarnings' => true,
194 $this->assertFalse( $job );
196 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
202 * @depends testDoDownload
204 function testVerifyDownload( $data ) {
205 $t = Title
::newFromText( "UploadFromUrlTest.png", NS_FILE
);
207 $this->assertTrue( $t->exists() );
209 $this->deleteFile( 'UploadFromUrlTest.png' );
215 function deleteFile( $name ) {
216 $t = Title
::newFromText( $name, NS_FILE
);
217 $this->assertTrue($t->exists(), "File '$name' exists");
219 if ( $t->exists() ) {
220 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
222 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
223 $a = new Article ( $t );
224 $a->doDeleteArticle( "testing" );
226 $t = Title
::newFromText( $name, NS_FILE
);
228 $this->assertFalse($t->exists(), "File '$name' was deleted");