r90607 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90606‎ | r90607 | r90608 >
Date:21:34, 22 June 2011
Author:reedy
Status:resolved
Tags:
Comment:
Move upload tests to upload folder
Modified paths:
  • /trunk/phase3/tests/phpunit/includes/UploadFromUrlTest.php (deleted) (history)
  • /trunk/phase3/tests/phpunit/includes/UploadTest.php (deleted) (history)
  • /trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php (added) (history)
  • /trunk/phase3/tests/phpunit/includes/upload/UploadTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/UploadTest.php
@@ -1,132 +0,0 @@
2 -<?php
3 -/**
4 - * @group Upload
5 - */
6 -class UploadTest extends MediaWikiTestCase {
7 - protected $upload;
8 -
9 -
10 - function setUp() {
11 - global $wgHooks;
12 - parent::setUp();
13 -
14 - $this->upload = new UploadTestHandler;
15 - $this->hooks = $wgHooks;
16 - $wgHooks['InterwikiLoadPrefix'][] = 'MediaWikiTestCase::disableInterwikis';
17 - }
18 -
19 - function tearDown() {
20 - global $wgHooks;
21 - $wgHooks = $this->hooks;
22 - }
23 -
24 - /**
25 - * Test various forms of valid and invalid titles that can be supplied.
26 - */
27 - public function testTitleValidation() {
28 -
29 -
30 - /* Test a valid title */
31 - $this->assertUploadTitleAndCode( 'ValidTitle.jpg',
32 - 'ValidTitle.jpg', UploadBase::OK,
33 - 'upload valid title' );
34 -
35 - /* A title with a slash */
36 - $this->assertUploadTitleAndCode( 'A/B.jpg',
37 - 'B.jpg', UploadBase::OK,
38 - 'upload title with slash' );
39 -
40 - /* A title with illegal char */
41 - $this->assertUploadTitleAndCode( 'A:B.jpg',
42 - 'A-B.jpg', UploadBase::OK,
43 - 'upload title with colon' );
44 -
45 - /* A title without extension */
46 - $this->assertUploadTitleAndCode( 'A',
47 - null, UploadBase::FILETYPE_MISSING,
48 - 'upload title without extension' );
49 -
50 - /* A title with no basename */
51 - $this->assertUploadTitleAndCode( '.jpg',
52 - null, UploadBase::MIN_LENGTH_PARTNAME,
53 - 'upload title without basename' );
54 -
55 - }
56 - /**
57 - * Helper function for testTitleValidation. First checks the return code
58 - * of UploadBase::getTitle() and then the actual returned titl
59 - */
60 - private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
61 - /* Check the result code */
62 - $this->assertEquals( $code,
63 - $this->upload->testTitleValidation( $srcFilename ),
64 - "$msg code" );
65 -
66 - /* If we expect a valid title, check the title itself. */
67 - if ( $code == UploadBase::OK ) {
68 - $this->assertEquals( $dstFilename,
69 - $this->upload->getTitle()->getText(),
70 - "$msg text" );
71 - }
72 - }
73 -
74 - /**
75 - * Test the upload verification functions
76 - */
77 - public function testVerifyUpload() {
78 - /* Setup with zero file size */
79 - $this->upload->initializePathInfo( '', '', 0 );
80 - $result = $this->upload->verifyUpload();
81 - $this->assertEquals( UploadBase::EMPTY_FILE,
82 - $result['status'],
83 - 'upload empty file' );
84 - }
85 -
86 - // Helper used to create an empty file of size $size.
87 - private function createFileOfSize( $size ) {
88 - $filename = tempnam( wfTempDir(), "mwuploadtest" );
89 -
90 - $fh = fopen( $filename, 'w' );
91 - ftruncate( $fh, $size );
92 - fclose( $fh );
93 -
94 - return $filename;
95 - }
96 -
97 - /**
98 - * test uploading a 100 bytes file with wgMaxUploadSize = 100
99 - *
100 - * This method should be abstracted so we can test different settings.
101 - */
102 -
103 - public function testMaxUploadSize() {
104 - global $wgMaxUploadSize;
105 - $savedGlobal = $wgMaxUploadSize; // save global
106 - global $wgFileExtensions;
107 - $wgFileExtensions[] = 'txt';
108 -
109 - $wgMaxUploadSize = 100;
110 -
111 - $filename = $this->createFileOfSize( $wgMaxUploadSize );
112 - $this->upload->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
113 - $result = $this->upload->verifyUpload();
114 - unlink( $filename );
115 -
116 - $this->assertEquals(
117 - array( 'status' => UploadBase::OK ), $result );
118 -
119 - $wgMaxUploadSize = $savedGlobal; // restore global
120 - }
121 -}
122 -
123 -class UploadTestHandler extends UploadBase {
124 - public function initializeFromRequest( &$request ) { }
125 - public function testTitleValidation( $name ) {
126 - $this->mTitle = false;
127 - $this->mDesiredDestName = $name;
128 - $this->getTitle();
129 - return $this->mTitleError;
130 - }
131 -
132 -
133 -}
Index: trunk/phase3/tests/phpunit/includes/UploadFromUrlTest.php
@@ -1,350 +0,0 @@
2 -<?php
3 -
4 -require_once dirname( __FILE__ ) . '/api/ApiSetup.php';
5 -
6 -/**
7 - * @group Broken
8 - * @group Upload
9 - */
10 -class UploadFromUrlTest extends ApiTestSetup {
11 -
12 - public function setUp() {
13 - global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
14 - parent::setUp();
15 -
16 - $wgEnableUploads = true;
17 - $wgAllowCopyUploads = true;
18 - $wgAllowAsyncCopyUploads = true;
19 - wfSetupSession();
20 -
21 - if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
22 - $this->deleteFile( 'UploadFromUrlTest.png' );
23 - }
24 - }
25 -
26 - protected function doApiRequest( $params, $unused = null, $appendModule = false ) {
27 - $sessionId = session_id();
28 - session_write_close();
29 -
30 - $req = new FauxRequest( $params, true, $_SESSION );
31 - $module = new ApiMain( $req, true );
32 - $module->execute();
33 -
34 - wfSetupSession( $sessionId );
35 - return array( $module->getResultData(), $req );
36 - }
37 -
38 - /**
39 - * Ensure that the job queue is empty before continuing
40 - */
41 - public function testClearQueue() {
42 - while ( $job = Job::pop() ) { }
43 - $this->assertFalse( $job );
44 - }
45 -
46 - /**
47 - * @todo Document why we test login, since the $wgUser hack used doesn't
48 - * require login
49 - */
50 - public function testLogin() {
51 - $data = $this->doApiRequest( array(
52 - 'action' => 'login',
53 - 'lgname' => $this->user->userName,
54 - 'lgpassword' => $this->user->passWord ) );
55 - $this->assertArrayHasKey( "login", $data[0] );
56 - $this->assertArrayHasKey( "result", $data[0]['login'] );
57 - $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
58 - $token = $data[0]['login']['token'];
59 -
60 - $data = $this->doApiRequest( array(
61 - 'action' => 'login',
62 - "lgtoken" => $token,
63 - 'lgname' => $this->user->userName,
64 - 'lgpassword' => $this->user->passWord ) );
65 -
66 - $this->assertArrayHasKey( "login", $data[0] );
67 - $this->assertArrayHasKey( "result", $data[0]['login'] );
68 - $this->assertEquals( "Success", $data[0]['login']['result'] );
69 - $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
70 -
71 - return $data;
72 - }
73 -
74 - /**
75 - * @depends testLogin
76 - * @depends testClearQueue
77 - */
78 - public function testSetupUrlDownload( $data ) {
79 - $token = $this->user->editToken();
80 - $exception = false;
81 -
82 - try {
83 - $this->doApiRequest( array(
84 - 'action' => 'upload',
85 - ) );
86 - } catch ( UsageException $e ) {
87 - $exception = true;
88 - $this->assertEquals( "The token parameter must be set", $e->getMessage() );
89 - }
90 - $this->assertTrue( $exception, "Got exception" );
91 -
92 - $exception = false;
93 - try {
94 - $this->doApiRequest( array(
95 - 'action' => 'upload',
96 - 'token' => $token,
97 - ), $data );
98 - } catch ( UsageException $e ) {
99 - $exception = true;
100 - $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
101 - $e->getMessage() );
102 - }
103 - $this->assertTrue( $exception, "Got exception" );
104 -
105 - $exception = false;
106 - try {
107 - $this->doApiRequest( array(
108 - 'action' => 'upload',
109 - 'url' => 'http://www.example.com/test.png',
110 - 'token' => $token,
111 - ), $data );
112 - } catch ( UsageException $e ) {
113 - $exception = true;
114 - $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
115 - }
116 - $this->assertTrue( $exception, "Got exception" );
117 -
118 - $this->user->removeGroup( 'sysop' );
119 - $exception = false;
120 - try {
121 - $this->doApiRequest( array(
122 - 'action' => 'upload',
123 - 'url' => 'http://www.example.com/test.png',
124 - 'filename' => 'UploadFromUrlTest.png',
125 - 'token' => $token,
126 - ), $data );
127 - } catch ( UsageException $e ) {
128 - $exception = true;
129 - $this->assertEquals( "Permission denied", $e->getMessage() );
130 - }
131 - $this->assertTrue( $exception, "Got exception" );
132 -
133 - $this->user->addGroup( 'sysop' );
134 - $data = $this->doApiRequest( array(
135 - 'action' => 'upload',
136 - 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
137 - 'asyncdownload' => 1,
138 - 'filename' => 'UploadFromUrlTest.png',
139 - 'token' => $token,
140 - ), $data );
141 -
142 - $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
143 -
144 - $job = Job::pop();
145 - $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
146 - }
147 -
148 - /**
149 - * @depends testLogin
150 - * @depends testClearQueue
151 - */
152 - public function testAsyncUpload( $data ) {
153 - $token = $this->user->editToken();
154 -
155 - $this->user->addGroup( 'users' );
156 -
157 - $data = $this->doAsyncUpload( $token, true );
158 - $this->assertEquals( $data[0]['upload']['result'], 'Success' );
159 - $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
160 - $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
161 -
162 - $this->deleteFile( 'UploadFromUrlTest.png' );
163 -
164 - return $data;
165 - }
166 -
167 - /**
168 - * @depends testLogin
169 - * @depends testClearQueue
170 - */
171 - public function testAsyncUploadWarning( $data ) {
172 - $token = $this->user->editToken();
173 -
174 - $this->user->addGroup( 'users' );
175 -
176 -
177 - $data = $this->doAsyncUpload( $token );
178 -
179 - $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
180 - $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
181 -
182 - $data = $this->doApiRequest( array(
183 - 'action' => 'upload',
184 - 'sessionkey' => $data[0]['upload']['sessionkey'],
185 - 'filename' => 'UploadFromUrlTest.png',
186 - 'ignorewarnings' => 1,
187 - 'token' => $token,
188 - ) );
189 - $this->assertEquals( $data[0]['upload']['result'], 'Success' );
190 - $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
191 - $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
192 -
193 - $this->deleteFile( 'UploadFromUrlTest.png' );
194 -
195 - return $data;
196 - }
197 -
198 - /**
199 - * @depends testLogin
200 - * @depends testClearQueue
201 - */
202 - public function testSyncDownload( $data ) {
203 - $token = $this->user->editToken();
204 -
205 - $job = Job::pop();
206 - $this->assertFalse( $job, 'Starting with an empty jobqueue' );
207 -
208 - $this->user->addGroup( 'users' );
209 - $data = $this->doApiRequest( array(
210 - 'action' => 'upload',
211 - 'filename' => 'UploadFromUrlTest.png',
212 - 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
213 - 'ignorewarnings' => true,
214 - 'token' => $token,
215 - ), $data );
216 -
217 - $job = Job::pop();
218 - $this->assertFalse( $job );
219 -
220 - $this->assertEquals( 'Success', $data[0]['upload']['result'] );
221 - $this->deleteFile( 'UploadFromUrlTest.png' );
222 -
223 - return $data;
224 - }
225 -
226 - public function testLeaveMessage() {
227 - $token = $this->user->user->editToken();
228 -
229 - $talk = $this->user->user->getTalkPage();
230 - if ( $talk->exists() ) {
231 - $a = new Article( $talk );
232 - $a->doDeleteArticle( '' );
233 - }
234 -
235 - $this->assertFalse( (bool)$talk->getArticleId( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );
236 -
237 - $data = $this->doApiRequest( array(
238 - 'action' => 'upload',
239 - 'filename' => 'UploadFromUrlTest.png',
240 - 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
241 - 'asyncdownload' => 1,
242 - 'token' => $token,
243 - 'leavemessage' => 1,
244 - 'ignorewarnings' => 1,
245 - ) );
246 -
247 - $job = Job::pop();
248 - $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
249 - $job->run();
250 -
251 - $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
252 - $this->assertTrue( (bool)$talk->getArticleId( Title::GAID_FOR_UPDATE ), 'User talk exists' );
253 -
254 - $this->deleteFile( 'UploadFromUrlTest.png' );
255 -
256 - $talkRev = Revision::newFromTitle( $talk );
257 - $talkSize = $talkRev->getSize();
258 -
259 - $exception = false;
260 - try {
261 - $data = $this->doApiRequest( array(
262 - 'action' => 'upload',
263 - 'filename' => 'UploadFromUrlTest.png',
264 - 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
265 - 'asyncdownload' => 1,
266 - 'token' => $token,
267 - 'leavemessage' => 1,
268 - ) );
269 - } catch ( UsageException $e ) {
270 - $exception = true;
271 - $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
272 - }
273 - $this->assertTrue( $exception );
274 -
275 - $job = Job::pop();
276 - $this->assertFalse( $job );
277 -
278 - return;
279 -
280 - /**
281 - // Broken until using leavemessage with ignorewarnings is supported
282 - $job->run();
283 -
284 - $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
285 -
286 - $talkRev = Revision::newFromTitle( $talk );
287 - $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
288 - */
289 - }
290 -
291 - /**
292 - * Helper function to perform an async upload, execute the job and fetch
293 - * the status
294 - *
295 - * @return array The result of action=upload&statuskey=key
296 - */
297 - private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
298 - $params = array(
299 - 'action' => 'upload',
300 - 'filename' => 'UploadFromUrlTest.png',
301 - 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
302 - 'asyncdownload' => 1,
303 - 'token' => $token,
304 - );
305 - if ( $ignoreWarnings ) {
306 - $params['ignorewarnings'] = 1;
307 - }
308 - if ( $leaveMessage ) {
309 - $params['leavemessage'] = 1;
310 - }
311 -
312 - $data = $this->doApiRequest( $params );
313 - $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
314 - $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
315 - $statusKey = $data[0]['upload']['statuskey'];
316 -
317 - $job = Job::pop();
318 - $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
319 -
320 - $status = $job->run();
321 - $this->assertTrue( $status );
322 -
323 - $data = $this->doApiRequest( array(
324 - 'action' => 'upload',
325 - 'statuskey' => $statusKey,
326 - 'token' => $token,
327 - ) );
328 -
329 - return $data;
330 - }
331 -
332 -
333 - /**
334 - *
335 - */
336 - protected function deleteFile( $name ) {
337 - $t = Title::newFromText( $name, NS_FILE );
338 - $this->assertTrue($t->exists(), "File '$name' exists");
339 -
340 - if ( $t->exists() ) {
341 - $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
342 - $empty = "";
343 - FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
344 - $a = new Article ( $t );
345 - $a->doDeleteArticle( "testing" );
346 - }
347 - $t = Title::newFromText( $name, NS_FILE );
348 -
349 - $this->assertFalse($t->exists(), "File '$name' was deleted");
350 - }
351 - }
Index: trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php
@@ -0,0 +1,350 @@
 2+<?php
 3+
 4+require_once dirname( __FILE__ ) . '/api/ApiSetup.php';
 5+
 6+/**
 7+ * @group Broken
 8+ * @group Upload
 9+ */
 10+class UploadFromUrlTest extends ApiTestSetup {
 11+
 12+ public function setUp() {
 13+ global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
 14+ parent::setUp();
 15+
 16+ $wgEnableUploads = true;
 17+ $wgAllowCopyUploads = true;
 18+ $wgAllowAsyncCopyUploads = true;
 19+ wfSetupSession();
 20+
 21+ if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
 22+ $this->deleteFile( 'UploadFromUrlTest.png' );
 23+ }
 24+ }
 25+
 26+ protected function doApiRequest( $params, $unused = null, $appendModule = false ) {
 27+ $sessionId = session_id();
 28+ session_write_close();
 29+
 30+ $req = new FauxRequest( $params, true, $_SESSION );
 31+ $module = new ApiMain( $req, true );
 32+ $module->execute();
 33+
 34+ wfSetupSession( $sessionId );
 35+ return array( $module->getResultData(), $req );
 36+ }
 37+
 38+ /**
 39+ * Ensure that the job queue is empty before continuing
 40+ */
 41+ public function testClearQueue() {
 42+ while ( $job = Job::pop() ) { }
 43+ $this->assertFalse( $job );
 44+ }
 45+
 46+ /**
 47+ * @todo Document why we test login, since the $wgUser hack used doesn't
 48+ * require login
 49+ */
 50+ public function testLogin() {
 51+ $data = $this->doApiRequest( array(
 52+ 'action' => 'login',
 53+ 'lgname' => $this->user->userName,
 54+ 'lgpassword' => $this->user->passWord ) );
 55+ $this->assertArrayHasKey( "login", $data[0] );
 56+ $this->assertArrayHasKey( "result", $data[0]['login'] );
 57+ $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
 58+ $token = $data[0]['login']['token'];
 59+
 60+ $data = $this->doApiRequest( array(
 61+ 'action' => 'login',
 62+ "lgtoken" => $token,
 63+ 'lgname' => $this->user->userName,
 64+ 'lgpassword' => $this->user->passWord ) );
 65+
 66+ $this->assertArrayHasKey( "login", $data[0] );
 67+ $this->assertArrayHasKey( "result", $data[0]['login'] );
 68+ $this->assertEquals( "Success", $data[0]['login']['result'] );
 69+ $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
 70+
 71+ return $data;
 72+ }
 73+
 74+ /**
 75+ * @depends testLogin
 76+ * @depends testClearQueue
 77+ */
 78+ public function testSetupUrlDownload( $data ) {
 79+ $token = $this->user->editToken();
 80+ $exception = false;
 81+
 82+ try {
 83+ $this->doApiRequest( array(
 84+ 'action' => 'upload',
 85+ ) );
 86+ } catch ( UsageException $e ) {
 87+ $exception = true;
 88+ $this->assertEquals( "The token parameter must be set", $e->getMessage() );
 89+ }
 90+ $this->assertTrue( $exception, "Got exception" );
 91+
 92+ $exception = false;
 93+ try {
 94+ $this->doApiRequest( array(
 95+ 'action' => 'upload',
 96+ 'token' => $token,
 97+ ), $data );
 98+ } catch ( UsageException $e ) {
 99+ $exception = true;
 100+ $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
 101+ $e->getMessage() );
 102+ }
 103+ $this->assertTrue( $exception, "Got exception" );
 104+
 105+ $exception = false;
 106+ try {
 107+ $this->doApiRequest( array(
 108+ 'action' => 'upload',
 109+ 'url' => 'http://www.example.com/test.png',
 110+ 'token' => $token,
 111+ ), $data );
 112+ } catch ( UsageException $e ) {
 113+ $exception = true;
 114+ $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
 115+ }
 116+ $this->assertTrue( $exception, "Got exception" );
 117+
 118+ $this->user->removeGroup( 'sysop' );
 119+ $exception = false;
 120+ try {
 121+ $this->doApiRequest( array(
 122+ 'action' => 'upload',
 123+ 'url' => 'http://www.example.com/test.png',
 124+ 'filename' => 'UploadFromUrlTest.png',
 125+ 'token' => $token,
 126+ ), $data );
 127+ } catch ( UsageException $e ) {
 128+ $exception = true;
 129+ $this->assertEquals( "Permission denied", $e->getMessage() );
 130+ }
 131+ $this->assertTrue( $exception, "Got exception" );
 132+
 133+ $this->user->addGroup( 'sysop' );
 134+ $data = $this->doApiRequest( array(
 135+ 'action' => 'upload',
 136+ 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
 137+ 'asyncdownload' => 1,
 138+ 'filename' => 'UploadFromUrlTest.png',
 139+ 'token' => $token,
 140+ ), $data );
 141+
 142+ $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
 143+
 144+ $job = Job::pop();
 145+ $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
 146+ }
 147+
 148+ /**
 149+ * @depends testLogin
 150+ * @depends testClearQueue
 151+ */
 152+ public function testAsyncUpload( $data ) {
 153+ $token = $this->user->editToken();
 154+
 155+ $this->user->addGroup( 'users' );
 156+
 157+ $data = $this->doAsyncUpload( $token, true );
 158+ $this->assertEquals( $data[0]['upload']['result'], 'Success' );
 159+ $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
 160+ $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
 161+
 162+ $this->deleteFile( 'UploadFromUrlTest.png' );
 163+
 164+ return $data;
 165+ }
 166+
 167+ /**
 168+ * @depends testLogin
 169+ * @depends testClearQueue
 170+ */
 171+ public function testAsyncUploadWarning( $data ) {
 172+ $token = $this->user->editToken();
 173+
 174+ $this->user->addGroup( 'users' );
 175+
 176+
 177+ $data = $this->doAsyncUpload( $token );
 178+
 179+ $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
 180+ $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
 181+
 182+ $data = $this->doApiRequest( array(
 183+ 'action' => 'upload',
 184+ 'sessionkey' => $data[0]['upload']['sessionkey'],
 185+ 'filename' => 'UploadFromUrlTest.png',
 186+ 'ignorewarnings' => 1,
 187+ 'token' => $token,
 188+ ) );
 189+ $this->assertEquals( $data[0]['upload']['result'], 'Success' );
 190+ $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
 191+ $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
 192+
 193+ $this->deleteFile( 'UploadFromUrlTest.png' );
 194+
 195+ return $data;
 196+ }
 197+
 198+ /**
 199+ * @depends testLogin
 200+ * @depends testClearQueue
 201+ */
 202+ public function testSyncDownload( $data ) {
 203+ $token = $this->user->editToken();
 204+
 205+ $job = Job::pop();
 206+ $this->assertFalse( $job, 'Starting with an empty jobqueue' );
 207+
 208+ $this->user->addGroup( 'users' );
 209+ $data = $this->doApiRequest( array(
 210+ 'action' => 'upload',
 211+ 'filename' => 'UploadFromUrlTest.png',
 212+ 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
 213+ 'ignorewarnings' => true,
 214+ 'token' => $token,
 215+ ), $data );
 216+
 217+ $job = Job::pop();
 218+ $this->assertFalse( $job );
 219+
 220+ $this->assertEquals( 'Success', $data[0]['upload']['result'] );
 221+ $this->deleteFile( 'UploadFromUrlTest.png' );
 222+
 223+ return $data;
 224+ }
 225+
 226+ public function testLeaveMessage() {
 227+ $token = $this->user->user->editToken();
 228+
 229+ $talk = $this->user->user->getTalkPage();
 230+ if ( $talk->exists() ) {
 231+ $a = new Article( $talk );
 232+ $a->doDeleteArticle( '' );
 233+ }
 234+
 235+ $this->assertFalse( (bool)$talk->getArticleId( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );
 236+
 237+ $data = $this->doApiRequest( array(
 238+ 'action' => 'upload',
 239+ 'filename' => 'UploadFromUrlTest.png',
 240+ 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
 241+ 'asyncdownload' => 1,
 242+ 'token' => $token,
 243+ 'leavemessage' => 1,
 244+ 'ignorewarnings' => 1,
 245+ ) );
 246+
 247+ $job = Job::pop();
 248+ $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
 249+ $job->run();
 250+
 251+ $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
 252+ $this->assertTrue( (bool)$talk->getArticleId( Title::GAID_FOR_UPDATE ), 'User talk exists' );
 253+
 254+ $this->deleteFile( 'UploadFromUrlTest.png' );
 255+
 256+ $talkRev = Revision::newFromTitle( $talk );
 257+ $talkSize = $talkRev->getSize();
 258+
 259+ $exception = false;
 260+ try {
 261+ $data = $this->doApiRequest( array(
 262+ 'action' => 'upload',
 263+ 'filename' => 'UploadFromUrlTest.png',
 264+ 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
 265+ 'asyncdownload' => 1,
 266+ 'token' => $token,
 267+ 'leavemessage' => 1,
 268+ ) );
 269+ } catch ( UsageException $e ) {
 270+ $exception = true;
 271+ $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
 272+ }
 273+ $this->assertTrue( $exception );
 274+
 275+ $job = Job::pop();
 276+ $this->assertFalse( $job );
 277+
 278+ return;
 279+
 280+ /**
 281+ // Broken until using leavemessage with ignorewarnings is supported
 282+ $job->run();
 283+
 284+ $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
 285+
 286+ $talkRev = Revision::newFromTitle( $talk );
 287+ $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
 288+ */
 289+ }
 290+
 291+ /**
 292+ * Helper function to perform an async upload, execute the job and fetch
 293+ * the status
 294+ *
 295+ * @return array The result of action=upload&statuskey=key
 296+ */
 297+ private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
 298+ $params = array(
 299+ 'action' => 'upload',
 300+ 'filename' => 'UploadFromUrlTest.png',
 301+ 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
 302+ 'asyncdownload' => 1,
 303+ 'token' => $token,
 304+ );
 305+ if ( $ignoreWarnings ) {
 306+ $params['ignorewarnings'] = 1;
 307+ }
 308+ if ( $leaveMessage ) {
 309+ $params['leavemessage'] = 1;
 310+ }
 311+
 312+ $data = $this->doApiRequest( $params );
 313+ $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
 314+ $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
 315+ $statusKey = $data[0]['upload']['statuskey'];
 316+
 317+ $job = Job::pop();
 318+ $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
 319+
 320+ $status = $job->run();
 321+ $this->assertTrue( $status );
 322+
 323+ $data = $this->doApiRequest( array(
 324+ 'action' => 'upload',
 325+ 'statuskey' => $statusKey,
 326+ 'token' => $token,
 327+ ) );
 328+
 329+ return $data;
 330+ }
 331+
 332+
 333+ /**
 334+ *
 335+ */
 336+ protected function deleteFile( $name ) {
 337+ $t = Title::newFromText( $name, NS_FILE );
 338+ $this->assertTrue($t->exists(), "File '$name' exists");
 339+
 340+ if ( $t->exists() ) {
 341+ $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
 342+ $empty = "";
 343+ FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
 344+ $a = new Article ( $t );
 345+ $a->doDeleteArticle( "testing" );
 346+ }
 347+ $t = Title::newFromText( $name, NS_FILE );
 348+
 349+ $this->assertFalse($t->exists(), "File '$name' was deleted");
 350+ }
 351+ }
Property changes on: trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php
___________________________________________________________________
Added: svn:eol-style
1352 + native
Index: trunk/phase3/tests/phpunit/includes/upload/UploadTest.php
@@ -0,0 +1,132 @@
 2+<?php
 3+/**
 4+ * @group Upload
 5+ */
 6+class UploadTest extends MediaWikiTestCase {
 7+ protected $upload;
 8+
 9+
 10+ function setUp() {
 11+ global $wgHooks;
 12+ parent::setUp();
 13+
 14+ $this->upload = new UploadTestHandler;
 15+ $this->hooks = $wgHooks;
 16+ $wgHooks['InterwikiLoadPrefix'][] = 'MediaWikiTestCase::disableInterwikis';
 17+ }
 18+
 19+ function tearDown() {
 20+ global $wgHooks;
 21+ $wgHooks = $this->hooks;
 22+ }
 23+
 24+ /**
 25+ * Test various forms of valid and invalid titles that can be supplied.
 26+ */
 27+ public function testTitleValidation() {
 28+
 29+
 30+ /* Test a valid title */
 31+ $this->assertUploadTitleAndCode( 'ValidTitle.jpg',
 32+ 'ValidTitle.jpg', UploadBase::OK,
 33+ 'upload valid title' );
 34+
 35+ /* A title with a slash */
 36+ $this->assertUploadTitleAndCode( 'A/B.jpg',
 37+ 'B.jpg', UploadBase::OK,
 38+ 'upload title with slash' );
 39+
 40+ /* A title with illegal char */
 41+ $this->assertUploadTitleAndCode( 'A:B.jpg',
 42+ 'A-B.jpg', UploadBase::OK,
 43+ 'upload title with colon' );
 44+
 45+ /* A title without extension */
 46+ $this->assertUploadTitleAndCode( 'A',
 47+ null, UploadBase::FILETYPE_MISSING,
 48+ 'upload title without extension' );
 49+
 50+ /* A title with no basename */
 51+ $this->assertUploadTitleAndCode( '.jpg',
 52+ null, UploadBase::MIN_LENGTH_PARTNAME,
 53+ 'upload title without basename' );
 54+
 55+ }
 56+ /**
 57+ * Helper function for testTitleValidation. First checks the return code
 58+ * of UploadBase::getTitle() and then the actual returned titl
 59+ */
 60+ private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
 61+ /* Check the result code */
 62+ $this->assertEquals( $code,
 63+ $this->upload->testTitleValidation( $srcFilename ),
 64+ "$msg code" );
 65+
 66+ /* If we expect a valid title, check the title itself. */
 67+ if ( $code == UploadBase::OK ) {
 68+ $this->assertEquals( $dstFilename,
 69+ $this->upload->getTitle()->getText(),
 70+ "$msg text" );
 71+ }
 72+ }
 73+
 74+ /**
 75+ * Test the upload verification functions
 76+ */
 77+ public function testVerifyUpload() {
 78+ /* Setup with zero file size */
 79+ $this->upload->initializePathInfo( '', '', 0 );
 80+ $result = $this->upload->verifyUpload();
 81+ $this->assertEquals( UploadBase::EMPTY_FILE,
 82+ $result['status'],
 83+ 'upload empty file' );
 84+ }
 85+
 86+ // Helper used to create an empty file of size $size.
 87+ private function createFileOfSize( $size ) {
 88+ $filename = tempnam( wfTempDir(), "mwuploadtest" );
 89+
 90+ $fh = fopen( $filename, 'w' );
 91+ ftruncate( $fh, $size );
 92+ fclose( $fh );
 93+
 94+ return $filename;
 95+ }
 96+
 97+ /**
 98+ * test uploading a 100 bytes file with wgMaxUploadSize = 100
 99+ *
 100+ * This method should be abstracted so we can test different settings.
 101+ */
 102+
 103+ public function testMaxUploadSize() {
 104+ global $wgMaxUploadSize;
 105+ $savedGlobal = $wgMaxUploadSize; // save global
 106+ global $wgFileExtensions;
 107+ $wgFileExtensions[] = 'txt';
 108+
 109+ $wgMaxUploadSize = 100;
 110+
 111+ $filename = $this->createFileOfSize( $wgMaxUploadSize );
 112+ $this->upload->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
 113+ $result = $this->upload->verifyUpload();
 114+ unlink( $filename );
 115+
 116+ $this->assertEquals(
 117+ array( 'status' => UploadBase::OK ), $result );
 118+
 119+ $wgMaxUploadSize = $savedGlobal; // restore global
 120+ }
 121+}
 122+
 123+class UploadTestHandler extends UploadBase {
 124+ public function initializeFromRequest( &$request ) { }
 125+ public function testTitleValidation( $name ) {
 126+ $this->mTitle = false;
 127+ $this->mDesiredDestName = $name;
 128+ $this->getTitle();
 129+ return $this->mTitleError;
 130+ }
 131+
 132+
 133+}
Property changes on: trunk/phase3/tests/phpunit/includes/upload/UploadTest.php
___________________________________________________________________
Added: svn:eol-style
1134 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r90616require_once dirname( dirname( __FILE__ ) ) . '/api/ApiSetup.php';...reedy22:11, 22 June 2011
r90617require_once( dirname( dirname( __FILE__ ) ) . '/includes/upload/UploadFromUr...reedy22:13, 22 June 2011

Status & tagging log