2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-08 21:38:47 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
2020-01-19 13:15:35 +00:00
|
|
|
* @group large
|
2011-03-06 09:01:19 +00:00
|
|
|
* @group Upload
|
2012-12-09 09:27:56 +00:00
|
|
|
* @group Database
|
2013-10-24 20:30:43 +00:00
|
|
|
*
|
|
|
|
|
* @covers UploadFromUrl
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2011-07-01 16:34:02 +00:00
|
|
|
class UploadFromUrlTest extends ApiTestCase {
|
2020-09-21 20:47:18 +00:00
|
|
|
use MockHttpTrait;
|
|
|
|
|
|
2020-01-19 13:15:35 +00:00
|
|
|
private $user;
|
|
|
|
|
|
2019-10-20 18:11:08 +00:00
|
|
|
protected function setUp() : void {
|
2010-12-14 16:26:35 +00:00
|
|
|
parent::setUp();
|
2020-01-19 13:15:35 +00:00
|
|
|
$this->user = self::$users['sysop']->getUser();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2013-03-21 19:35:44 +00:00
|
|
|
'wgEnableUploads' => true,
|
|
|
|
|
'wgAllowCopyUploads' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2020-01-19 13:15:35 +00:00
|
|
|
$this->setGroupPermissions( 'sysop', 'upload_by_url', true );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2020-03-08 21:38:47 +00:00
|
|
|
if ( MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
|
|
|
|
|
->newFile( 'UploadFromUrlTest.png' )->exists()
|
|
|
|
|
) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->deleteFile( 'UploadFromUrlTest.png' );
|
|
|
|
|
}
|
2020-10-08 13:00:43 +00:00
|
|
|
|
|
|
|
|
$this->installMockHttp();
|
2020-09-09 19:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* Ensure that the job queue is empty before continuing
|
|
|
|
|
*/
|
|
|
|
|
public function testClearQueue() {
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2011-10-16 03:27:12 +00:00
|
|
|
while ( $job ) {
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2011-10-16 03:27:12 +00:00
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( $job );
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-19 13:28:16 +00:00
|
|
|
public function testIsAllowedHostEmpty() {
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgCopyUploadsDomains' => [],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.bar' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIsAllowedHostDirectMatch() {
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgCopyUploadsDomains' => [
|
|
|
|
|
'foo.baz',
|
|
|
|
|
'bar.example.baz',
|
|
|
|
|
],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://example.com' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.baz' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://.foo.baz' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://example.baz' ) );
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://bar.example.baz' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIsAllowedHostLastWildcard() {
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgCopyUploadsDomains' => [
|
|
|
|
|
'*.baz',
|
|
|
|
|
],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://baz' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.example' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.example.baz' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo/bar.baz' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.baz' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://subdomain.foo.baz' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIsAllowedHostWildcardInMiddle() {
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgCopyUploadsDomains' => [
|
|
|
|
|
'foo.*.baz',
|
|
|
|
|
],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.baz' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.bar.bar.baz' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.bar.baz.baz' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.com/.baz' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.example.baz' ) );
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.bar.baz' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* @depends testClearQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testSetupUrlDownload( $data ) {
|
|
|
|
|
$exception = false;
|
|
|
|
|
|
|
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->doApiRequest( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2016-10-19 16:54:25 +00:00
|
|
|
} catch ( ApiUsageException $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = true;
|
2020-01-19 13:15:35 +00:00
|
|
|
$this->assertEquals( 'The "token" parameter must be set.', $e->getMessage() );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2021-06-01 18:43:35 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
2021-06-01 18:43:35 +00:00
|
|
|
], $data, $this->user );
|
2016-10-19 16:54:25 +00:00
|
|
|
} catch ( ApiUsageException $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = true;
|
2020-01-19 13:15:35 +00:00
|
|
|
$this->assertEquals( 'One of the parameters "filekey", "file" and "url" is required.',
|
2010-12-14 16:26:35 +00:00
|
|
|
$e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2021-06-01 18:43:35 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
2021-06-01 18:43:35 +00:00
|
|
|
], $data, $this->user );
|
2016-10-19 16:54:25 +00:00
|
|
|
} catch ( ApiUsageException $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = true;
|
2020-01-19 13:15:35 +00:00
|
|
|
$this->assertEquals( 'The "filename" parameter must be set.', $e->getMessage() );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->removeGroup( 'sysop' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2021-06-01 18:43:35 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
2021-06-01 18:43:35 +00:00
|
|
|
], $data, $this->user );
|
2016-10-19 16:54:25 +00:00
|
|
|
} catch ( ApiUsageException $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = true;
|
2020-10-30 16:14:12 +00:00
|
|
|
// Two error messages are possible depending on the number of groups in the wiki with upload rights:
|
|
|
|
|
// - The action you have requested is limited to users in the group:
|
|
|
|
|
// - The action you have requested is limited to users in one of the groups:
|
|
|
|
|
$this->assertStringStartsWith( "The action you have requested is limited to users in",
|
2020-01-19 13:15:35 +00:00
|
|
|
$e->getMessage() );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 10:58:13 +00:00
|
|
|
private function assertUploadOk( UploadBase $upload ) {
|
|
|
|
|
$verificationResult = $upload->verifyUpload();
|
|
|
|
|
|
|
|
|
|
if ( $verificationResult['status'] !== UploadBase::OK ) {
|
|
|
|
|
$this->fail(
|
|
|
|
|
'Upload verification returned ' . $upload->getVerificationErrorCode(
|
|
|
|
|
$verificationResult['status']
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* @depends testClearQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testSyncDownload( $data ) {
|
2020-09-09 19:22:20 +00:00
|
|
|
$file = __DIR__ . '/../../data/upload/png-plain.png';
|
|
|
|
|
$this->installMockHttp( file_get_contents( $file ) );
|
|
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->addGroup( 'users' );
|
2021-06-01 18:43:35 +00:00
|
|
|
$data = $this->doApiRequestWithToken( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
2014-09-05 18:37:53 +00:00
|
|
|
'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
|
2010-12-14 16:26:35 +00:00
|
|
|
'ignorewarnings' => true,
|
2021-06-01 18:43:35 +00:00
|
|
|
], $data, $this->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'Success', $data[0]['upload']['result'] );
|
|
|
|
|
$this->deleteFile( 'UploadFromUrlTest.png' );
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function deleteFile( $name ) {
|
|
|
|
|
$t = Title::newFromText( $name, NS_FILE );
|
2013-02-15 10:35:55 +00:00
|
|
|
$this->assertTrue( $t->exists(), "File '$name' exists" );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
if ( $t->exists() ) {
|
2020-03-08 21:38:47 +00:00
|
|
|
$file = MediaWikiServices::getInstance()->getRepoGroup()
|
|
|
|
|
->findFile( $name, [ 'ignoreRedirect' => true ] );
|
2010-12-14 16:26:35 +00:00
|
|
|
$empty = "";
|
2020-02-19 22:31:24 +00:00
|
|
|
FileDeleteForm::doDelete( $t, $file, $empty, "none", true, $this->user );
|
2012-02-13 16:38:37 +00:00
|
|
|
$page = WikiPage::factory( $t );
|
2020-03-25 17:05:26 +00:00
|
|
|
$page->doDeleteArticleReal( "testing", $this->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
$t = Title::newFromText( $name, NS_FILE );
|
|
|
|
|
|
2013-02-15 10:35:55 +00:00
|
|
|
$this->assertFalse( $t->exists(), "File '$name' was deleted" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2020-09-10 10:58:13 +00:00
|
|
|
|
|
|
|
|
public function testUploadFromUrl() {
|
|
|
|
|
$file = __DIR__ . '/../../data/upload/png-plain.png';
|
|
|
|
|
$this->installMockHttp( file_get_contents( $file ) );
|
|
|
|
|
|
|
|
|
|
$upload = new UploadFromUrl();
|
|
|
|
|
$upload->initialize( 'Test.png', 'http://www.example.com/test.png' );
|
|
|
|
|
$status = $upload->fetchFile();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertUploadOk( $upload );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUploadFromUrlWithRedirect() {
|
|
|
|
|
$file = __DIR__ . '/../../data/upload/png-plain.png';
|
|
|
|
|
$this->installMockHttp( [
|
|
|
|
|
// First response is a redirect
|
|
|
|
|
$this->makeFakeHttpRequest(
|
|
|
|
|
'Blaba',
|
|
|
|
|
302,
|
|
|
|
|
[ 'Location' => 'http://static.example.com/files/test.png' ]
|
|
|
|
|
),
|
|
|
|
|
// Second response is a file
|
|
|
|
|
$this->makeFakeHttpRequest(
|
|
|
|
|
file_get_contents( $file )
|
|
|
|
|
),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$upload = new UploadFromUrl();
|
|
|
|
|
$upload->initialize( 'Test.png', 'http://www.example.com/test.png' );
|
|
|
|
|
$status = $upload->fetchFile();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertUploadOk( $upload );
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-15 10:35:55 +00:00
|
|
|
}
|