MediaWiki  REL1_31
UploadFromUrlTest.php
Go to the documentation of this file.
1 <?php
2 
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, $tokenType = null
26  ) {
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 
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( [
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 
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 ) {
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  }
148 
149  $this->assertFalse( $t->exists(), "File '$name' was deleted" );
150  }
151 }
$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:247
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:43
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
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:273
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:103
array
the array() calling protocol came about after MediaWiki 1.4rc1.
NS_FILE
const NS_FILE
Definition: Defines.php:80
$params
$params
Definition: styleTest.css.php:40
UploadFromUrlTest\testClearQueue
testClearQueue()
Ensure that the job queue is empty before continuing.
Definition: UploadFromUrlTest.php:42
UploadFromUrlTest\testSetupUrlDownload
testSetupUrlDownload( $data)
@depends testClearQueue
Definition: UploadFromUrlTest.php:53
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
UploadFromUrlTest\doApiRequest
doApiRequest(array $params, array $unused=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: UploadFromUrlTest.php:24
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
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
Wikitext formatted, in the key only.
Definition: distributors.txt:25
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
UploadFromUrlTest\setUp
setUp()
Definition: UploadFromUrlTest.php:11
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
ApiTestCase
Definition: ApiTestCase.php:5
UploadFromUrlTest\deleteFile
deleteFile( $name)
Definition: UploadFromUrlTest.php:136
wfFindFile
wfFindFile( $title, $options=[])
Find a file.
Definition: GlobalFunctions.php:2853
$req
this hook is for auditing only $req
Definition: hooks.txt:990
UploadFromUrlTest\testSyncDownload
testSyncDownload( $data)
@depends testClearQueue
Definition: UploadFromUrlTest.php:112
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
UploadFromUrlTest
Broken Upload Database.
Definition: UploadFromUrlTest.php:10
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:72
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:47
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
Definition: FileDeleteForm.php:156
$t
$t
Definition: testCompression.php:69
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:737
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:2864
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2171