2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-08-14 13:47:11 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-03-09 20:01:39 +00:00
|
|
|
use MediaWiki\Page\File\FileDeleteForm;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-01-30 19:15:13 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2021-07-22 03:11:47 +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
|
|
|
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::EnableUploads => true,
|
|
|
|
|
MainConfigNames::AllowCopyUploads => 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
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
if ( $this->getServiceContainer()->getRepoGroup()->getLocalRepo()
|
2020-03-08 21:38:47 +00:00
|
|
|
->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() {
|
2022-01-27 20:19:18 +00:00
|
|
|
$jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
|
|
|
|
|
$job = $jobQueueGroup->pop();
|
2011-10-16 03:27:12 +00:00
|
|
|
while ( $job ) {
|
2022-01-27 20:19:18 +00:00
|
|
|
$job = $jobQueueGroup->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() {
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::CopyUploadsDomains => [],
|
|
|
|
|
MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
|
2020-01-19 13:28:16 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.bar' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIsAllowedHostDirectMatch() {
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::CopyUploadsDomains => [
|
2020-01-19 13:28:16 +00:00
|
|
|
'foo.baz',
|
|
|
|
|
'bar.example.baz',
|
|
|
|
|
],
|
2022-08-14 13:47:11 +00:00
|
|
|
MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
|
2020-01-19 13:28:16 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$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() {
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::CopyUploadsDomains => [
|
2020-01-19 13:28:16 +00:00
|
|
|
'*.baz',
|
|
|
|
|
],
|
2022-08-14 13:47:11 +00:00
|
|
|
MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
|
2020-01-19 13:28:16 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$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() {
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::CopyUploadsDomains => [
|
2020-01-19 13:28:16 +00:00
|
|
|
'foo.*.baz',
|
|
|
|
|
],
|
2022-08-14 13:47:11 +00:00
|
|
|
MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
|
2020-01-19 13:28:16 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$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' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-30 19:15:13 +00:00
|
|
|
public function testOnWikiDomainConfigEnabled() {
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::CopyUploadsDomains => [ 'example.com' ],
|
|
|
|
|
MainConfigNames::CopyUploadAllowOnWikiDomainConfig => true,
|
2022-01-30 19:15:13 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$messageContent = "example.org # this is a comment\n# this too is commented foo.example.com\nexample.net";
|
|
|
|
|
$mock = $this->createMock( MessageCache::class );
|
|
|
|
|
$mock->method( 'get' )->willReturn( $messageContent );
|
|
|
|
|
$this->setService( 'MessageCache', $mock );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
[ 'example.com', 'example.org', 'example.net' ],
|
|
|
|
|
TestingAccessWrapper::newFromClass( UploadFromUrl::class )->getAllowedHosts()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://example.com' ) );
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://example.org' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.example.com' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testOnWikiDomainConfigDisabled() {
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::CopyUploadsDomains => [ 'example.com' ],
|
|
|
|
|
MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
|
2022-01-30 19:15:13 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$mock = $this->createMock( MessageCache::class );
|
|
|
|
|
$mock->expects( $this->never() )->method( 'get' );
|
|
|
|
|
$this->setService( 'MessageCache', $mock );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
[ 'example.com' ],
|
|
|
|
|
TestingAccessWrapper::newFromClass( UploadFromUrl::class )->getAllowedHosts()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( UploadFromUrl::isAllowedHost( 'https://example.com' ) );
|
|
|
|
|
$this->assertFalse( UploadFromUrl::isAllowedHost( 'https://example.org' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* @depends testClearQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testSetupUrlDownload( $data ) {
|
2021-08-05 06:54:11 +00:00
|
|
|
$token = $this->user->getEditToken();
|
2010-12-14 16:26:35 +00:00
|
|
|
$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-08-05 06:54:11 +00:00
|
|
|
$this->doApiRequest( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
2021-08-05 06:54:11 +00:00
|
|
|
'token' => $token,
|
|
|
|
|
], $data );
|
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-08-05 06:54:11 +00:00
|
|
|
$this->doApiRequest( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
2021-08-05 06:54:11 +00:00
|
|
|
'token' => $token,
|
|
|
|
|
], $data );
|
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" );
|
|
|
|
|
|
2021-07-01 10:32:24 +00:00
|
|
|
$this->getServiceContainer()->getUserGroupManager()->removeUserFromGroup( $this->user, 'sysop' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2021-08-05 06:54:11 +00:00
|
|
|
$this->doApiRequest( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
2021-08-05 06:54:11 +00:00
|
|
|
'token' => $token,
|
|
|
|
|
], $data );
|
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 ) );
|
|
|
|
|
|
2021-07-01 10:32:24 +00:00
|
|
|
$this->getServiceContainer()->getUserGroupManager()->addUserToGroup( $this->user, '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-08-05 06:54:11 +00:00
|
|
|
], $data );
|
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() ) {
|
2022-01-12 20:13:39 +00:00
|
|
|
$file = $this->getServiceContainer()->getRepoGroup()
|
2020-03-08 21:38:47 +00:00
|
|
|
->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 );
|
2022-01-12 20:13:39 +00:00
|
|
|
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $t );
|
2021-10-30 15:45:49 +00:00
|
|
|
$this->deletePage( $page );
|
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();
|
|
|
|
|
|
2022-03-04 22:00:02 +00:00
|
|
|
$this->assertStatusOK( $status );
|
2020-09-10 10:58:13 +00:00
|
|
|
$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();
|
|
|
|
|
|
2022-03-04 22:00:02 +00:00
|
|
|
$this->assertStatusOK( $status );
|
2020-09-10 10:58:13 +00:00
|
|
|
$this->assertUploadOk( $upload );
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-15 10:35:55 +00:00
|
|
|
}
|