4 * * Abstract class to support upload tests
7 abstract class ApiTestCaseUpload
extends ApiTestCase
{
9 * Fixture -- run before every test
11 public function setUp() {
12 global $wgEnableUploads, $wgEnableAPI;
15 $wgEnableUploads = true;
19 $this->clearFakeUploads();
22 public function tearDown() {
23 $this->clearTempUpload();
27 * Helper function -- remove files and associated articles by Title
28 * @param $title Title: title to be removed
30 public function deleteFileByTitle( $title ) {
31 if ( $title->exists() ) {
32 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
33 $noOldArchive = ""; // yes this really needs to be set this way
34 $comment = "removing for test";
35 $restrictDeletedVersions = false;
36 $status = FileDeleteForm
::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
37 if ( !$status->isGood() ) {
40 $page = WikiPage
::factory( $title );
41 $page->doDeleteArticle( "removing for test" );
43 // see if it now doesn't exist; reload
44 $title = Title
::newFromText( $title->getText(), NS_FILE
);
46 return ! ( $title && $title instanceof Title
&& $title->exists() );
50 * Helper function -- remove files and associated articles with a particular filename
51 * @param $fileName String: filename to be removed
53 public function deleteFileByFileName( $fileName ) {
54 return $this->deleteFileByTitle( Title
::newFromText( $fileName, NS_FILE
) );
59 * Helper function -- given a file on the filesystem, find matching content in the db (and associated articles) and remove them.
60 * @param $filePath String: path to file on the filesystem
62 public function deleteFileByContent( $filePath ) {
63 $hash = FSFile
::getSha1Base36FromPath( $filePath );
64 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
66 foreach ( $dupes as $dupe ) {
67 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
73 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
74 * (This is what PHP would normally do).
75 * @param $fieldName String: name this would have in the upload form
76 * @param $fileName String: name to title this
77 * @param $type String: mime type
78 * @param $filePath String: path where to find file contents
80 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
81 $tmpName = tempnam( wfTempDir(), "" );
82 if ( !file_exists( $filePath ) ) {
83 throw new Exception( "$filePath doesn't exist!" );
86 if ( !copy( $filePath, $tmpName ) ) {
87 throw new Exception( "couldn't copy $filePath to $tmpName" );
91 $size = filesize( $tmpName );
92 if ( $size === false ) {
93 throw new Exception( "couldn't stat $tmpName" );
96 $_FILES[ $fieldName ] = array(
99 'tmp_name' => $tmpName,
107 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ){
108 $tmpName = tempnam( wfTempDir(), "" );
109 // copy the chunk data to temp location:
110 if ( !file_put_contents( $tmpName, $chunkData ) ) {
111 throw new Exception( "couldn't copy chunk data to $tmpName" );
115 $size = filesize( $tmpName );
116 if ( $size === false ) {
117 throw new Exception( "couldn't stat $tmpName" );
120 $_FILES[ $fieldName ] = array(
123 'tmp_name' => $tmpName,
129 function clearTempUpload() {
130 if( isset( $_FILES['file']['tmp_name'] ) ) {
131 $tmp = $_FILES['file']['tmp_name'];
132 if( file_exists( $tmp ) ) {
139 * Remove traces of previous fake uploads
141 function clearFakeUploads() {