MediaWiki  1.23.13
UploadFromUrlTest.php
Go to the documentation of this file.
1 <?php
2 
11  protected function setUp() {
12  parent::setUp();
13 
14  $this->setMwGlobals( array(
15  'wgEnableUploads' => true,
16  'wgAllowCopyUploads' => true,
17  'wgAllowAsyncCopyUploads' => true,
18  ) );
20 
21  if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
22  $this->deleteFile( 'UploadFromUrlTest.png' );
23  }
24  }
25 
26  protected function doApiRequest( array $params, array $unused = null, $appendModule = false, User $user = null ) {
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 
36  return array( $module->getResultData(), $req );
37  }
38 
42  public function testClearQueue() {
43  $job = JobQueueGroup::singleton()->pop();
44  while ( $job ) {
45  $job = JobQueueGroup::singleton()->pop();
46  }
47  $this->assertFalse( $job );
48  }
49 
53  public function testSetupUrlDownload( $data ) {
54  $token = $this->user->getEditToken();
55  $exception = false;
56 
57  try {
58  $this->doApiRequest( array(
59  'action' => 'upload',
60  ) );
61  } catch ( UsageException $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( array(
70  'action' => 'upload',
71  'token' => $token,
72  ), $data );
73  } catch ( UsageException $e ) {
74  $exception = true;
75  $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
76  $e->getMessage() );
77  }
78  $this->assertTrue( $exception, "Got exception" );
79 
80  $exception = false;
81  try {
82  $this->doApiRequest( array(
83  'action' => 'upload',
84  'url' => 'http://www.example.com/test.png',
85  'token' => $token,
86  ), $data );
87  } catch ( UsageException $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( array(
97  'action' => 'upload',
98  'url' => 'http://www.example.com/test.png',
99  'filename' => 'UploadFromUrlTest.png',
100  'token' => $token,
101  ), $data );
102  } catch ( UsageException $e ) {
103  $exception = true;
104  $this->assertEquals( "Permission denied", $e->getMessage() );
105  }
106  $this->assertTrue( $exception, "Got exception" );
107 
108  $this->user->addGroup( 'sysop' );
109  $data = $this->doApiRequest( array(
110  'action' => 'upload',
111  'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
112  'asyncdownload' => 1,
113  'filename' => 'UploadFromUrlTest.png',
114  'token' => $token,
115  ), $data );
116 
117  $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
118 
119  $job = JobQueueGroup::singleton()->pop();
120  $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
121  }
122 
126  public function testAsyncUpload( $data ) {
127  $token = $this->user->getEditToken();
128 
129  $this->user->addGroup( 'users' );
130 
131  $data = $this->doAsyncUpload( $token, true );
132  $this->assertEquals( $data[0]['upload']['result'], 'Success' );
133  $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
134  $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
135 
136  $this->deleteFile( 'UploadFromUrlTest.png' );
137 
138  return $data;
139  }
140 
144  public function testAsyncUploadWarning( $data ) {
145  $token = $this->user->getEditToken();
146 
147  $this->user->addGroup( 'users' );
148 
149  $data = $this->doAsyncUpload( $token );
150 
151  $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
152  $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
153 
154  $data = $this->doApiRequest( array(
155  'action' => 'upload',
156  'sessionkey' => $data[0]['upload']['sessionkey'],
157  'filename' => 'UploadFromUrlTest.png',
158  'ignorewarnings' => 1,
159  'token' => $token,
160  ) );
161  $this->assertEquals( $data[0]['upload']['result'], 'Success' );
162  $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
163  $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
164 
165  $this->deleteFile( 'UploadFromUrlTest.png' );
166 
167  return $data;
168  }
169 
173  public function testSyncDownload( $data ) {
174  $token = $this->user->getEditToken();
175 
176  $job = JobQueueGroup::singleton()->pop();
177  $this->assertFalse( $job, 'Starting with an empty jobqueue' );
178 
179  $this->user->addGroup( 'users' );
180  $data = $this->doApiRequest( array(
181  'action' => 'upload',
182  'filename' => 'UploadFromUrlTest.png',
183  'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
184  'ignorewarnings' => true,
185  'token' => $token,
186  ), $data );
187 
188  $job = JobQueueGroup::singleton()->pop();
189  $this->assertFalse( $job );
190 
191  $this->assertEquals( 'Success', $data[0]['upload']['result'] );
192  $this->deleteFile( 'UploadFromUrlTest.png' );
193 
194  return $data;
195  }
196 
197  public function testLeaveMessage() {
198  $token = $this->user->user->getEditToken();
199 
200  $talk = $this->user->user->getTalkPage();
201  if ( $talk->exists() ) {
202  $page = WikiPage::factory( $talk );
203  $page->doDeleteArticle( '' );
204  }
205 
206  $this->assertFalse( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk does not exist' );
207 
208  $this->doApiRequest( array(
209  'action' => 'upload',
210  'filename' => 'UploadFromUrlTest.png',
211  'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
212  'asyncdownload' => 1,
213  'token' => $token,
214  'leavemessage' => 1,
215  'ignorewarnings' => 1,
216  ) );
217 
218  $job = JobQueueGroup::singleton()->pop();
219  $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
220  $job->run();
221 
222  $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
223  $this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
224 
225  $this->deleteFile( 'UploadFromUrlTest.png' );
226 
227  $exception = false;
228  try {
229  $this->doApiRequest( array(
230  'action' => 'upload',
231  'filename' => 'UploadFromUrlTest.png',
232  'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
233  'asyncdownload' => 1,
234  'token' => $token,
235  'leavemessage' => 1,
236  ) );
237  } catch ( UsageException $e ) {
238  $exception = true;
239  $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
240  }
241  $this->assertTrue( $exception );
242 
243  $job = JobQueueGroup::singleton()->pop();
244  $this->assertFalse( $job );
245 
246  return;
247  /*
248  // Broken until using leavemessage with ignorewarnings is supported
249  $talkRev = Revision::newFromTitle( $talk );
250  $talkSize = $talkRev->getSize();
251 
252  $job->run();
253 
254  $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
255 
256  $talkRev = Revision::newFromTitle( $talk );
257  $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
258  */
259  }
260 
267  private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
268  $params = array(
269  'action' => 'upload',
270  'filename' => 'UploadFromUrlTest.png',
271  'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
272  'asyncdownload' => 1,
273  'token' => $token,
274  );
275  if ( $ignoreWarnings ) {
276  $params['ignorewarnings'] = 1;
277  }
278  if ( $leaveMessage ) {
279  $params['leavemessage'] = 1;
280  }
281 
282  $data = $this->doApiRequest( $params );
283  $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
284  $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
285  $statusKey = $data[0]['upload']['statuskey'];
286 
287  $job = JobQueueGroup::singleton()->pop();
288  $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
289 
290  $status = $job->run();
291  $this->assertTrue( $status );
292 
293  $data = $this->doApiRequest( array(
294  'action' => 'upload',
295  'statuskey' => $statusKey,
296  'token' => $token,
297  ) );
298 
299  return $data;
300  }
301 
302  protected function deleteFile( $name ) {
304  $this->assertTrue( $t->exists(), "File '$name' exists" );
305 
306  if ( $t->exists() ) {
307  $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
308  $empty = "";
309  FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
310  $page = WikiPage::factory( $t );
311  $page->doDeleteArticle( "testing" );
312  }
314 
315  $this->assertFalse( $t->exists(), "File '$name' was deleted" );
316  }
317 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: WebRequest.php:1275
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfSetupSession
wfSetupSession( $sessionId=false)
Initialise php session.
Definition: GlobalFunctions.php:3579
NS_FILE
const NS_FILE
Definition: Defines.php:85
$params
$params
Definition: styleTest.css.php:40
UploadFromUrlTest\testClearQueue
testClearQueue()
Ensure that the job queue is empty before continuing.
Definition: UploadFromUrlTest.php:42
UploadFromUrlTest\doApiRequest
doApiRequest(array $params, array $unused=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: UploadFromUrlTest.php:26
UploadFromUrlTest\testSetupUrlDownload
testSetupUrlDownload( $data)
@depends testClearQueue
Definition: UploadFromUrlTest.php:53
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null)
Really delete the file.
Definition: FileDeleteForm.php:141
UsageException
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1406
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
UploadFromUrlTest\setUp
setUp()
Definition: UploadFromUrlTest.php:11
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
UploadFromUrlTest\doAsyncUpload
doAsyncUpload( $token, $ignoreWarnings=false, $leaveMessage=false)
Helper function to perform an async upload, execute the job and fetch the status.
Definition: UploadFromUrlTest.php:267
user
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
ApiTestCase
Definition: ApiTestCase.php:3
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
Title\GAID_FOR_UPDATE
const GAID_FOR_UPDATE
Used to be GAID_FOR_UPDATE define.
Definition: Title.php:50
UploadFromUrlTest\deleteFile
deleteFile( $name)
Definition: UploadFromUrlTest.php:302
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
UploadFromUrlTest\testSyncDownload
testSyncDownload( $data)
@depends testClearQueue
Definition: UploadFromUrlTest.php:173
UploadFromUrlTest
@group Broken @group Upload @group Database
Definition: UploadFromUrlTest.php:10
UploadFromUrlTest\testAsyncUpload
testAsyncUpload( $data)
@depends testClearQueue
Definition: UploadFromUrlTest.php:126
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:42
UploadFromUrlTest\testLeaveMessage
testLeaveMessage()
Definition: UploadFromUrlTest.php:197
JobQueueGroup\singleton
static singleton( $wiki=false)
Definition: JobQueueGroup.php:61
wfFindFile
wfFindFile( $title, $options=array())
Find a file.
Definition: GlobalFunctions.php:3749
$t
$t
Definition: testCompression.php:65
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3760
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:1632
UploadFromUrlTest\testAsyncUploadWarning
testAsyncUploadWarning( $data)
@depends testClearQueue
Definition: UploadFromUrlTest.php:144