(bug 24593) Native name for Sorani now uses only Arabic script
[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' => 'UploadFromUrlTest.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' => 'UploadFromUrlTest.png',
132 'token' => $token,
133 ), $data );
134
135 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
136
137 $job = Job::pop();
138 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
139 }
140
141 /**
142 * @depends testLogin
143 */
144 function testDoDownload( $data ) {
145 global $wgUser;
146 $data[2]['wsEditToken'] = $data[2]['wsToken'];
147 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
148
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,
155 'token' => $token,
156 ), $data );
157
158 $job = Job::pop();
159 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
160
161 $status = $job->run();
162
163 $this->assertTrue( $status );
164
165 return $data;
166 }
167
168 /**
169 * @depends testLogin
170 */
171 function testSyncDownload( $data ) {
172 global $wgUser;
173 $data[2]['wsEditToken'] = $data[2]['wsToken'];
174 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
175
176 $job = Job::pop();
177 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
178
179 //$this->deleteFile( 'UploadFromUrlTest.png' );
180
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,
187 'token' => $token,
188 ), $data );
189
190 $job = Job::pop();
191 $this->assertFalse( $job );
192
193 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
194
195 return $data;
196 }
197
198 /**
199 * @depends testDoDownload
200 */
201 function testVerifyDownload( $data ) {
202 $t = Title::newFromText( "UploadFromUrlTest.png", NS_FILE );
203
204 $this->assertTrue( $t->exists() );
205
206 $this->deleteFile( 'UploadFromUrlTest.png' );
207 }
208
209 /**
210 *
211 */
212 function deleteFile( $name ) {
213 $t = Title::newFromText( $name, NS_FILE );
214 $this->assertTrue($t->exists(), "File '$name' exists");
215
216 if ( $t->exists() ) {
217 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
218 $empty = "";
219 $status = FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
220 $a = new Article ( $t );
221 $a->doDeleteArticle( "testing" );
222 }
223 $t = Title::newFromText( $name, NS_FILE );
224
225 $this->assertFalse($t->exists(), "File '$name' was deleted");
226 }
227 }