1e181543a8029e4ef0e0ccfcb468e863b0f379bb
[lhc/web/wiklou.git] / maintenance / tests / UploadFromUrlTest.php
1 <?php
2
3 global $IP;
4 require_once( "ApiSetup.php" );
5 require_once( dirname( dirname( __FILE__ ) ) . "/deleteArchivedFiles.inc" );
6 require_once( dirname( dirname( __FILE__ ) ) . "/deleteArchivedRevisions.inc" );
7
8 class nullClass {
9 public function handleOutput() { }
10 public function purgeRedundantText() { }
11 }
12
13 class UploadFromUrlTest extends ApiSetup {
14
15 function setUp() {
16 global $wgEnableUploads, $wgLocalFileRepo, $wgAllowCopyUploads;
17
18 $wgEnableUploads = true;
19 $wgAllowCopyUploads = true;
20 parent::setup();
21
22 ini_set( 'log_errors', 1 );
23 ini_set( 'error_reporting', 1 );
24 ini_set( 'display_errors', 1 );
25 }
26
27 function doApiRequest( $params, $data = null ) {
28 $session = isset( $data[2] ) ? $data[2] : array();
29 $_SESSION = $session;
30
31 $req = new FauxRequest( $params, true, $session );
32 $module = new ApiMain( $req, true );
33 $module->execute();
34
35 return array( $module->getResultData(), $req, $_SESSION );
36 }
37
38 function testClearQueue() {
39 while ( $job = Job::pop() ) { }
40 $this->assertFalse( $job );
41 }
42
43 function testLogin() {
44 $data = $this->doApiRequest( array(
45 'action' => 'login',
46 'lgname' => self::$userName,
47 'lgpassword' => self::$passWord ) );
48 $this->assertArrayHasKey( "login", $data[0] );
49 $this->assertArrayHasKey( "result", $data[0]['login'] );
50 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
51 $token = $data[0]['login']['token'];
52
53 $data = $this->doApiRequest( array(
54 'action' => 'login',
55 "lgtoken" => $token,
56 "lgname" => self::$userName,
57 "lgpassword" => self::$passWord ) );
58
59 $this->assertArrayHasKey( "login", $data[0] );
60 $this->assertArrayHasKey( "result", $data[0]['login'] );
61 $this->assertEquals( "Success", $data[0]['login']['result'] );
62 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
63
64 return $data;
65 }
66
67 /**
68 * @depends testLogin
69 */
70 function testSetupUrlDownload( $data ) {
71 global $wgUser;
72 $wgUser = User::newFromName( self::$userName );
73 $wgUser->load();
74 $data[2]['wsEditToken'] = $data[2]['wsToken'];
75 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
76 $exception = false;
77
78 try {
79 $this->doApiRequest( array(
80 'action' => 'upload',
81 ), $data );
82 } catch ( UsageException $e ) {
83 $exception = true;
84 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
85 }
86 $this->assertTrue( $exception, "Got exception" );
87
88 $exception = false;
89 try {
90 $this->doApiRequest( array(
91 'action' => 'upload',
92 'token' => $token,
93 ), $data );
94 } catch ( UsageException $e ) {
95 $exception = true;
96 $this->assertEquals( "One of the parameters sessionkey, file, url is required",
97 $e->getMessage() );
98 }
99 $this->assertTrue( $exception, "Got exception" );
100
101 $exception = false;
102 try {
103 $this->doApiRequest( array(
104 'action' => 'upload',
105 'url' => 'http://www.example.com/test.png',
106 'token' => $token,
107 ), $data );
108 } catch ( UsageException $e ) {
109 $exception = true;
110 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
111 }
112 $this->assertTrue( $exception, "Got exception" );
113
114 $wgUser->removeGroup( 'sysop' );
115 $exception = false;
116 try {
117 $this->doApiRequest( array(
118 'action' => 'upload',
119 'url' => 'http://www.example.com/test.png',
120 'filename' => 'Test.png',
121 'token' => $token,
122 ), $data );
123 } catch ( UsageException $e ) {
124 $exception = true;
125 $this->assertEquals( "Permission denied", $e->getMessage() );
126 }
127 $this->assertTrue( $exception, "Got exception" );
128
129 $wgUser->addGroup( '*' );
130 $wgUser->addGroup( 'sysop' );
131 $exception = false;
132 $data = $this->doApiRequest( array(
133 'action' => 'upload',
134 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
135 'asyncdownload' => 1,
136 'filename' => 'Test.png',
137 'token' => $token,
138 ), $data );
139
140 $this->assertTrue( $data[0]['upload']['queued'], 'Job added' );
141
142 $job = Job::pop();
143 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ),
144 "Got Job Object" );
145
146 $job = Job::pop_type( 'upload' );
147 $this->assertFalse( $job );
148 }
149
150 /**
151 * @depends testLogin
152 */
153 function testDoDownload( $data ) {
154 global $wgUser;
155 $data[2]['wsEditToken'] = $data[2]['wsToken'];
156 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
157
158 $wgUser->addGroup( 'users' );
159 $data = $this->doApiRequest( array(
160 'action' => 'upload',
161 'filename' => 'Test.png',
162 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
163 'asyncdownload' => 1,
164 'token' => $token,
165 ), $data );
166
167 $job = Job::pop();
168 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
169
170 $status = $job->run();
171
172 $this->assertTrue( $status->isOk() );
173
174 return $data;
175 }
176
177 /**
178 * @depends testLogin
179 */
180 function testSyncDownload( $data ) {
181 global $wgUser;
182 $data[2]['wsEditToken'] = $data[2]['wsToken'];
183 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
184
185 $job = Job::pop();
186 $this->assertFalse( $job );
187
188 self::deleteFile( 'Test.png' );
189
190 $wgUser->addGroup( 'users' );
191 $data = $this->doApiRequest( array(
192 'action' => 'upload',
193 'filename' => 'Test.png',
194 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
195 'ignorewarnings' => true,
196 'token' => $token,
197 ), $data );
198
199 $job = Job::pop();
200 $this->assertFalse( $job );
201
202 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
203
204 return $data;
205 }
206
207 /**
208 * @depends testDoDownload
209 */
210 function testVerifyDownload( $data ) {
211 $t = Title::newFromText( "Test.png", NS_FILE );
212
213 $this->assertTrue( $t->exists() );
214
215 self::deleteFile( 'Test.png' );
216 }
217
218 /**
219 *
220 */
221 function deleteFile( $name ) {
222
223 $t = Title::newFromText( $name, NS_FILE );
224 $this->assertTrue($t->exists(), "File '$name' exists");
225
226 if ( $t->exists() ) {
227 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
228 $empty = "";
229 $status = FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
230 $a = new Article ( $t );
231 $a->doDeleteArticle( "testing" );
232 }
233 $t = Title::newFromText( $name, NS_FILE );
234
235 $this->assertFalse($t->exists(), "File '$name' was deleted");
236 }
237 }