]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/upload/UploadFromUrlTest.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / upload / UploadFromUrlTest.php
1 <?php
2
3 /**
4  * @group Broken
5  * @group Upload
6  * @group Database
7  *
8  * @covers UploadFromUrl
9  */
10 class UploadFromUrlTest extends ApiTestCase {
11         protected function setUp() {
12                 parent::setUp();
13
14                 $this->setMwGlobals( [
15                         'wgEnableUploads' => true,
16                         'wgAllowCopyUploads' => true,
17                 ] );
18
19                 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
20                         $this->deleteFile( 'UploadFromUrlTest.png' );
21                 }
22         }
23
24         protected function doApiRequest( array $params, array $unused = null,
25                 $appendModule = false, User $user = null
26         ) {
27                 global $wgRequest;
28
29                 $req = new FauxRequest( $params, true, $wgRequest->getSession() );
30                 $module = new ApiMain( $req, true );
31                 $module->execute();
32
33                 return [
34                         $module->getResult()->getResultData( null, [ 'Strip' => 'all' ] ),
35                         $req
36                 ];
37         }
38
39         /**
40          * Ensure that the job queue is empty before continuing
41          */
42         public function testClearQueue() {
43                 $job = JobQueueGroup::singleton()->pop();
44                 while ( $job ) {
45                         $job = JobQueueGroup::singleton()->pop();
46                 }
47                 $this->assertFalse( $job );
48         }
49
50         /**
51          * @depends testClearQueue
52          */
53         public function testSetupUrlDownload( $data ) {
54                 $token = $this->user->getEditToken();
55                 $exception = false;
56
57                 try {
58                         $this->doApiRequest( [
59                                 'action' => 'upload',
60                         ] );
61                 } catch ( ApiUsageException $e ) {
62                         $exception = true;
63                         $this->assertEquals( "The token parameter must be set", $e->getMessage() );
64                 }
65                 $this->assertTrue( $exception, "Got exception" );
66
67                 $exception = false;
68                 try {
69                         $this->doApiRequest( [
70                                 'action' => 'upload',
71                                 'token' => $token,
72                         ], $data );
73                 } catch ( ApiUsageException $e ) {
74                         $exception = true;
75                         $this->assertEquals( "One of the parameters sessionkey, file, url is required",
76                                 $e->getMessage() );
77                 }
78                 $this->assertTrue( $exception, "Got exception" );
79
80                 $exception = false;
81                 try {
82                         $this->doApiRequest( [
83                                 'action' => 'upload',
84                                 'url' => 'http://www.example.com/test.png',
85                                 'token' => $token,
86                         ], $data );
87                 } catch ( ApiUsageException $e ) {
88                         $exception = true;
89                         $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
90                 }
91                 $this->assertTrue( $exception, "Got exception" );
92
93                 $this->user->removeGroup( 'sysop' );
94                 $exception = false;
95                 try {
96                         $this->doApiRequest( [
97                                 'action' => 'upload',
98                                 'url' => 'http://www.example.com/test.png',
99                                 'filename' => 'UploadFromUrlTest.png',
100                                 'token' => $token,
101                         ], $data );
102                 } catch ( ApiUsageException $e ) {
103                         $exception = true;
104                         $this->assertEquals( "Permission denied", $e->getMessage() );
105                 }
106                 $this->assertTrue( $exception, "Got exception" );
107         }
108
109         /**
110          * @depends testClearQueue
111          */
112         public function testSyncDownload( $data ) {
113                 $token = $this->user->getEditToken();
114
115                 $job = JobQueueGroup::singleton()->pop();
116                 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
117
118                 $this->user->addGroup( 'users' );
119                 $data = $this->doApiRequest( [
120                         'action' => 'upload',
121                         'filename' => 'UploadFromUrlTest.png',
122                         'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
123                         'ignorewarnings' => true,
124                         'token' => $token,
125                 ], $data );
126
127                 $job = JobQueueGroup::singleton()->pop();
128                 $this->assertFalse( $job );
129
130                 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
131                 $this->deleteFile( 'UploadFromUrlTest.png' );
132
133                 return $data;
134         }
135
136         protected function deleteFile( $name ) {
137                 $t = Title::newFromText( $name, NS_FILE );
138                 $this->assertTrue( $t->exists(), "File '$name' exists" );
139
140                 if ( $t->exists() ) {
141                         $file = wfFindFile( $name, [ 'ignoreRedirect' => true ] );
142                         $empty = "";
143                         FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
144                         $page = WikiPage::factory( $t );
145                         $page->doDeleteArticle( "testing" );
146                 }
147                 $t = Title::newFromText( $name, NS_FILE );
148
149                 $this->assertFalse( $t->exists(), "File '$name' was deleted" );
150         }
151 }