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();
23 * Helper function -- remove files and associated articles by Title
24 * @param $title Title: title to be removed
26 public function deleteFileByTitle( $title ) {
27 if ( $title->exists() ) {
28 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
29 $noOldArchive = ""; // yes this really needs to be set this way
30 $comment = "removing for test";
31 $restrictDeletedVersions = false;
32 $status = FileDeleteForm
::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
33 if ( !$status->isGood() ) {
36 $article = new Article( $title );
37 $article->doDeleteArticle( "removing for test" );
39 // see if it now doesn't exist; reload
40 $title = Title
::newFromText( $title->getText(), NS_FILE
);
42 return ! ( $title && $title instanceof Title
&& $title->exists() );
46 * Helper function -- remove files and associated articles with a particular filename
47 * @param $fileName String: filename to be removed
49 public function deleteFileByFileName( $fileName ) {
50 return $this->deleteFileByTitle( Title
::newFromText( $fileName, NS_FILE
) );
55 * Helper function -- given a file on the filesystem, find matching content in the db (and associated articles) and remove them.
56 * @param $filePath String: path to file on the filesystem
58 public function deleteFileByContent( $filePath ) {
59 $hash = File
::sha1Base36( $filePath );
60 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
62 foreach ( $dupes as $dupe ) {
63 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
69 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
70 * (This is what PHP would normally do).
71 * @param $fieldName String: name this would have in the upload form
72 * @param $fileName String: name to title this
73 * @param $type String: mime type
74 * @param $filePath String: path where to find file contents
76 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
77 $tmpName = tempnam( wfTempDir(), "" );
78 if ( !file_exists( $filePath ) ) {
79 throw new Exception( "$filePath doesn't exist!" );
82 if ( !copy( $filePath, $tmpName ) ) {
83 throw new Exception( "couldn't copy $filePath to $tmpName" );
87 $size = filesize( $tmpName );
88 if ( $size === false ) {
89 throw new Exception( "couldn't stat $tmpName" );
92 $_FILES[ $fieldName ] = array(
95 'tmp_name' => $tmpName,
105 * Remove traces of previous fake uploads
107 function clearFakeUploads() {