4 * Abstract class to support upload tests
6 abstract class ApiUploadTestCase
extends ApiTestCase
{
8 * Fixture -- run before every test
10 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgEnableUploads' => true,
17 $this->clearFakeUploads();
21 * Helper function -- remove files and associated articles by Title
23 * @param Title $title Title to be removed
27 public function deleteFileByTitle( $title ) {
28 if ( $title->exists() ) {
29 $file = wfFindFile( $title, [ 'ignoreRedirect' => true ] );
30 $noOldArchive = ""; // yes this really needs to be set this way
31 $comment = "removing for test";
32 $restrictDeletedVersions = false;
33 $status = FileDeleteForm
::doDelete(
38 $restrictDeletedVersions
41 if ( !$status->isGood() ) {
45 $page = WikiPage
::factory( $title );
46 $page->doDeleteArticle( "removing for test" );
48 // see if it now doesn't exist; reload
49 $title = Title
::newFromText( $title->getText(), NS_FILE
);
52 return !( $title && $title instanceof Title
&& $title->exists() );
56 * Helper function -- remove files and associated articles with a particular filename
58 * @param string $fileName Filename to be removed
62 public function deleteFileByFileName( $fileName ) {
63 return $this->deleteFileByTitle( Title
::newFromText( $fileName, NS_FILE
) );
67 * Helper function -- given a file on the filesystem, find matching
68 * content in the db (and associated articles) and remove them.
70 * @param string $filePath Path to file on the filesystem
74 public function deleteFileByContent( $filePath ) {
75 $hash = FSFile
::getSha1Base36FromPath( $filePath );
76 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
78 foreach ( $dupes as $dupe ) {
79 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
86 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
87 * (This is what PHP would normally do).
89 * @param string $fieldName Name this would have in the upload form
90 * @param string $fileName Name to title this
91 * @param string $type MIME type
92 * @param string $filePath Path where to find file contents
97 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
98 $tmpName = $this->getNewTempFile();
99 if ( !file_exists( $filePath ) ) {
100 throw new Exception( "$filePath doesn't exist!" );
103 if ( !copy( $filePath, $tmpName ) ) {
104 throw new Exception( "couldn't copy $filePath to $tmpName" );
108 $size = filesize( $tmpName );
109 if ( $size === false ) {
110 throw new Exception( "couldn't stat $tmpName" );
113 $_FILES[$fieldName] = [
116 'tmp_name' => $tmpName,
124 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
125 $tmpName = $this->getNewTempFile();
126 // copy the chunk data to temp location:
127 if ( !file_put_contents( $tmpName, $chunkData ) ) {
128 throw new Exception( "couldn't copy chunk data to $tmpName" );
132 $size = filesize( $tmpName );
133 if ( $size === false ) {
134 throw new Exception( "couldn't stat $tmpName" );
137 $_FILES[$fieldName] = [
140 'tmp_name' => $tmpName,
147 * Remove traces of previous fake uploads
149 function clearFakeUploads() {