wiki.techinc.nl/tests/phpunit/includes/upload/UploadFromUrlTest.php
Brad Jorsch 4e6810e4a2 API: i18n for warnings and errors
API warnings and error messages are currently hard-coded English
strings. This patch changes that.

With a few exceptions, this patch should be compatible with non-updated
extensions:
* The change to ApiBase::$messageMap will blow up anything trying to
  mess with it.
* The changes to the 'ApiCheckCanExecute' hook will cause a wrong
  (probably unparsed) error message to be emitted for extensions not
  already using an ApiMessage. Unless they're currently broken like
  Wikibase.

Bug: T37074
Bug: T47843
Depends-On: Ia2b66b57cd4eaddc30b3ffdd7b97d6ca3e02d898
Depends-On: I2e1bb975bb0045476c03ebe6cdec00259bae22ec
Depends-On: I53987bf87c48f6c00deec17a8e957d24fcc3eaa6
Depends-On: Ibf93a459eb62d30f7c70d20e91ec9faeb80d10ed
Depends-On: I3cf889811f44a15935e454dd42f081164d4a098c
Depends-On: Ieae527de86735ddcba34724730e8730fb277b99b
Depends-On: I535344c29d51521147c2a26c341dae38cec3e931
Change-Id: Iae0e2ce3bd42dd4776a9779664086119ac188412
2016-12-06 10:20:48 -05:00

151 lines
3.7 KiB
PHP

<?php
/**
* @group Broken
* @group Upload
* @group Database
*
* @covers UploadFromUrl
*/
class UploadFromUrlTest extends ApiTestCase {
protected function setUp() {
parent::setUp();
$this->setMwGlobals( [
'wgEnableUploads' => true,
'wgAllowCopyUploads' => true,
] );
if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
$this->deleteFile( 'UploadFromUrlTest.png' );
}
}
protected function doApiRequest( array $params, array $unused = null,
$appendModule = false, User $user = null
) {
global $wgRequest;
$req = new FauxRequest( $params, true, $wgRequest->getSession() );
$module = new ApiMain( $req, true );
$module->execute();
return [
$module->getResult()->getResultData( null, [ 'Strip' => 'all' ] ),
$req
];
}
/**
* Ensure that the job queue is empty before continuing
*/
public function testClearQueue() {
$job = JobQueueGroup::singleton()->pop();
while ( $job ) {
$job = JobQueueGroup::singleton()->pop();
}
$this->assertFalse( $job );
}
/**
* @depends testClearQueue
*/
public function testSetupUrlDownload( $data ) {
$token = $this->user->getEditToken();
$exception = false;
try {
$this->doApiRequest( [
'action' => 'upload',
] );
} catch ( ApiUsageException $e ) {
$exception = true;
$this->assertEquals( "The token parameter must be set", $e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
$exception = false;
try {
$this->doApiRequest( [
'action' => 'upload',
'token' => $token,
], $data );
} catch ( ApiUsageException $e ) {
$exception = true;
$this->assertEquals( "One of the parameters sessionkey, file, url is required",
$e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
$exception = false;
try {
$this->doApiRequest( [
'action' => 'upload',
'url' => 'http://www.example.com/test.png',
'token' => $token,
], $data );
} catch ( ApiUsageException $e ) {
$exception = true;
$this->assertEquals( "The filename parameter must be set", $e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
$this->user->removeGroup( 'sysop' );
$exception = false;
try {
$this->doApiRequest( [
'action' => 'upload',
'url' => 'http://www.example.com/test.png',
'filename' => 'UploadFromUrlTest.png',
'token' => $token,
], $data );
} catch ( ApiUsageException $e ) {
$exception = true;
$this->assertEquals( "Permission denied", $e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
}
/**
* @depends testClearQueue
*/
public function testSyncDownload( $data ) {
$token = $this->user->getEditToken();
$job = JobQueueGroup::singleton()->pop();
$this->assertFalse( $job, 'Starting with an empty jobqueue' );
$this->user->addGroup( 'users' );
$data = $this->doApiRequest( [
'action' => 'upload',
'filename' => 'UploadFromUrlTest.png',
'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
'ignorewarnings' => true,
'token' => $token,
], $data );
$job = JobQueueGroup::singleton()->pop();
$this->assertFalse( $job );
$this->assertEquals( 'Success', $data[0]['upload']['result'] );
$this->deleteFile( 'UploadFromUrlTest.png' );
return $data;
}
protected function deleteFile( $name ) {
$t = Title::newFromText( $name, NS_FILE );
$this->assertTrue( $t->exists(), "File '$name' exists" );
if ( $t->exists() ) {
$file = wfFindFile( $name, [ 'ignoreRedirect' => true ] );
$empty = "";
FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
$page = WikiPage::factory( $t );
$page->doDeleteArticle( "testing" );
}
$t = Title::newFromText( $name, NS_FILE );
$this->assertFalse( $t->exists(), "File '$name' was deleted" );
}
}