2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-09-09 19:22:20 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
|
|
|
use MediaWiki\Http\HttpRequestFactory;
|
2020-03-08 21:38:47 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-09-09 19:22:20 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2020-03-08 21:38:47 +00:00
|
|
|
|
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-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-09-09 19:22:20 +00:00
|
|
|
|
|
|
|
|
$this->installMockHttp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-10 10:58:13 +00:00
|
|
|
* @param null|string|array|callable|MWHttpRequest $request
|
2020-09-09 19:22:20 +00:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function installMockHttp( $request = null ) {
|
|
|
|
|
$options = new ServiceOptions( HttpRequestFactory::CONSTRUCTOR_OPTIONS, [
|
|
|
|
|
'HTTPTimeout' => 1,
|
|
|
|
|
'HTTPConnectTimeout' => 1,
|
|
|
|
|
'HTTPMaxTimeout' => 1,
|
|
|
|
|
'HTTPMaxConnectTimeout' => 1,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$mockHttpRequestFactory = $this->getMockBuilder( HttpRequestFactory::class )
|
|
|
|
|
->setConstructorArgs( [ $options, new NullLogger() ] )
|
|
|
|
|
->onlyMethods( [ 'create' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
if ( $request === null ) {
|
2020-09-10 10:58:13 +00:00
|
|
|
$mockHttpRequestFactory->expects( $this->never() )->method( 'create' );
|
2020-09-09 19:22:20 +00:00
|
|
|
} elseif ( $request instanceof MWHttpRequest ) {
|
|
|
|
|
$mockHttpRequestFactory->method( 'create' )
|
|
|
|
|
->willReturn( $request );
|
|
|
|
|
} elseif ( is_callable( $request ) ) {
|
|
|
|
|
$mockHttpRequestFactory->method( 'create' )
|
|
|
|
|
->willReturnCallback( $request );
|
|
|
|
|
} elseif ( is_array( $request ) ) {
|
|
|
|
|
$mockHttpRequestFactory->method( 'create' )
|
2020-09-10 10:58:13 +00:00
|
|
|
->willReturnOnConsecutiveCalls( ...$request );
|
2020-09-09 19:22:20 +00:00
|
|
|
} elseif ( is_string( $request ) ) {
|
|
|
|
|
$mockHttpRequestFactory->method( 'create' )
|
|
|
|
|
->willReturn( $this->makeFakeHttpRequest( $request ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->setService( 'HttpRequestFactory', function () use ( $mockHttpRequestFactory ) {
|
|
|
|
|
return $mockHttpRequestFactory;
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $body
|
|
|
|
|
* @param int $status
|
|
|
|
|
* @param array $headers
|
|
|
|
|
*
|
|
|
|
|
* @return MWHttpRequest
|
|
|
|
|
*/
|
|
|
|
|
private function makeFakeHttpRequest( $body = 'Lorem Ipsum', $statusCode = 200, $headers = [] ) {
|
2020-09-10 10:58:13 +00:00
|
|
|
$mockHttpRequest = $this->createNoOpMock(
|
|
|
|
|
MWHttpRequest::class,
|
|
|
|
|
[ 'execute', 'setCallback', 'isRedirect', 'getFinalUrl' ]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$mockHttpRequest->method( 'isRedirect' )->willReturn(
|
|
|
|
|
$statusCode >= 300 && $statusCode < 400
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$mockHttpRequest->method( 'getFinalUrl' )->willReturn( $headers[ 'Location' ] ?? '' );
|
2020-09-09 19:22:20 +00:00
|
|
|
|
|
|
|
|
$dataCallback = null;
|
|
|
|
|
$mockHttpRequest->method( 'setCallback' )
|
|
|
|
|
->willReturnCallback(
|
|
|
|
|
function ( $callback ) use ( &$dataCallback ) {
|
|
|
|
|
$dataCallback = $callback;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = Status::newGood( $statusCode );
|
|
|
|
|
$mockHttpRequest->method( 'execute' )
|
|
|
|
|
->willReturnCallback(
|
|
|
|
|
function () use ( &$dataCallback, $body, $status ) {
|
|
|
|
|
if ( $dataCallback ) {
|
|
|
|
|
$dataCallback( $this, $body );
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $mockHttpRequest;
|
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 ) {
|
2012-01-07 23:26:35 +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 {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->doApiRequest( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'token' => $token,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $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 {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->doApiRequest( [
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
|
|
|
|
'token' => $token,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $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" );
|
|
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->removeGroup( 'sysop' );
|
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',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'token' => $token,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $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->assertStringStartsWith( "The action you have requested is limited to users in the group:",
|
|
|
|
|
$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 ) );
|
|
|
|
|
|
2012-01-07 23:26:35 +00:00
|
|
|
$token = $this->user->getEditToken();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->addGroup( 'users' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = $this->doApiRequest( [
|
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,
|
|
|
|
|
'token' => $token,
|
2016-02-17 09:09:32 +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() ) {
|
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
|
|
|
}
|