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