2018-03-16 17:40:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-08 21:32:26 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2018-03-16 17:40:38 +00:00
|
|
|
/**
|
|
|
|
|
* Abstract class to support upload tests
|
|
|
|
|
*/
|
|
|
|
|
abstract class ApiUploadTestCase extends ApiTestCase {
|
2021-01-04 23:53:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @var array Used to fake $_FILES in tests and given to FauxRequest
|
|
|
|
|
*/
|
|
|
|
|
protected $requestDataFiles = [];
|
|
|
|
|
|
2018-03-16 17:40:38 +00:00
|
|
|
/**
|
|
|
|
|
* Fixture -- run before every test
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2018-03-16 17:40:38 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgEnableUploads' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->clearFakeUploads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function -- remove files and associated articles by Title
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title Title to be removed
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function deleteFileByTitle( $title ) {
|
|
|
|
|
if ( $title->exists() ) {
|
2020-03-08 21:32:26 +00:00
|
|
|
$file = MediaWikiServices::getInstance()->getRepoGroup()
|
|
|
|
|
->findFile( $title, [ 'ignoreRedirect' => true ] );
|
2018-03-16 17:40:38 +00:00
|
|
|
$noOldArchive = ""; // yes this really needs to be set this way
|
|
|
|
|
$comment = "removing for test";
|
|
|
|
|
$restrictDeletedVersions = false;
|
2020-03-25 17:05:26 +00:00
|
|
|
$user = $this->getTestSysop()->getUser();
|
2018-03-16 17:40:38 +00:00
|
|
|
$status = FileDeleteForm::doDelete(
|
|
|
|
|
$title,
|
|
|
|
|
$file,
|
|
|
|
|
$noOldArchive,
|
|
|
|
|
$comment,
|
2020-02-19 22:31:24 +00:00
|
|
|
$restrictDeletedVersions,
|
2020-03-25 17:05:26 +00:00
|
|
|
$user
|
2018-03-16 17:40:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$page = WikiPage::factory( $title );
|
2020-03-25 17:05:26 +00:00
|
|
|
$page->doDeleteArticleReal( "removing for test", $user );
|
2018-03-16 17:40:38 +00:00
|
|
|
|
|
|
|
|
// see if it now doesn't exist; reload
|
|
|
|
|
$title = Title::newFromText( $title->getText(), NS_FILE );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !( $title && $title instanceof Title && $title->exists() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function -- remove files and associated articles with a particular filename
|
|
|
|
|
*
|
|
|
|
|
* @param string $fileName Filename to be removed
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function deleteFileByFileName( $fileName ) {
|
|
|
|
|
return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function -- given a file on the filesystem, find matching
|
|
|
|
|
* content in the db (and associated articles) and remove them.
|
|
|
|
|
*
|
|
|
|
|
* @param string $filePath Path to file on the filesystem
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function deleteFileByContent( $filePath ) {
|
|
|
|
|
$hash = FSFile::getSha1Base36FromPath( $filePath );
|
2020-03-08 21:32:26 +00:00
|
|
|
$dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
|
2018-03-16 17:40:38 +00:00
|
|
|
$success = true;
|
|
|
|
|
foreach ( $dupes as $dupe ) {
|
|
|
|
|
$success &= $this->deleteFileByTitle( $dupe->getTitle() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fake an upload by dumping the file into temp space, and adding info to $_FILES.
|
|
|
|
|
* (This is what PHP would normally do).
|
|
|
|
|
*
|
|
|
|
|
* @param string $fieldName Name this would have in the upload form
|
|
|
|
|
* @param string $fileName Name to title this
|
|
|
|
|
* @param string $type MIME type
|
|
|
|
|
* @param string $filePath Path where to find file contents
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
protected function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
|
2018-03-16 17:40:38 +00:00
|
|
|
$tmpName = $this->getNewTempFile();
|
|
|
|
|
if ( !file_exists( $filePath ) ) {
|
|
|
|
|
throw new Exception( "$filePath doesn't exist!" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !copy( $filePath, $tmpName ) ) {
|
|
|
|
|
throw new Exception( "couldn't copy $filePath to $tmpName" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearstatcache();
|
|
|
|
|
$size = filesize( $tmpName );
|
|
|
|
|
if ( $size === false ) {
|
|
|
|
|
throw new Exception( "couldn't stat $tmpName" );
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 23:53:01 +00:00
|
|
|
$this->requestDataFiles[$fieldName] = [
|
2018-03-16 17:40:38 +00:00
|
|
|
'name' => $fileName,
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'tmp_name' => $tmpName,
|
|
|
|
|
'size' => $size,
|
2019-08-21 19:53:53 +00:00
|
|
|
'error' => UPLOAD_ERR_OK,
|
2018-03-16 17:40:38 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 16:30:40 +00:00
|
|
|
public function fakeUploadChunk( $fieldName, $fileName, $type, &$chunkData ) {
|
2018-03-16 17:40:38 +00:00
|
|
|
$tmpName = $this->getNewTempFile();
|
|
|
|
|
// copy the chunk data to temp location:
|
|
|
|
|
if ( !file_put_contents( $tmpName, $chunkData ) ) {
|
|
|
|
|
throw new Exception( "couldn't copy chunk data to $tmpName" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearstatcache();
|
|
|
|
|
$size = filesize( $tmpName );
|
|
|
|
|
if ( $size === false ) {
|
|
|
|
|
throw new Exception( "couldn't stat $tmpName" );
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 23:53:01 +00:00
|
|
|
$this->requestDataFiles[$fieldName] = [
|
2018-03-16 17:40:38 +00:00
|
|
|
'name' => $fileName,
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'tmp_name' => $tmpName,
|
|
|
|
|
'size' => $size,
|
|
|
|
|
'error' => null
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 23:53:01 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function buildFauxRequest( $params, $session ) {
|
|
|
|
|
$request = parent::buildFauxRequest( $params, $session );
|
|
|
|
|
$request->setUploadData( $this->requestDataFiles );
|
|
|
|
|
return $request;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 17:40:38 +00:00
|
|
|
/**
|
|
|
|
|
* Remove traces of previous fake uploads
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
public function clearFakeUploads() {
|
2021-01-04 23:53:01 +00:00
|
|
|
$this->requestDataFiles = [];
|
2018-03-16 17:40:38 +00:00
|
|
|
}
|
|
|
|
|
}
|