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