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 * Fixture -- run after every test
24 * Clean up temporary files etc.
31 * Helper function -- remove files and associated articles by Title
32 * @param $title Title: title to be removed
34 public function deleteFileByTitle( $title ) {
35 if ( $title->exists() ) {
36 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
37 $noOldArchive = ""; // yes this really needs to be set this way
38 $comment = "removing for test";
39 $restrictDeletedVersions = false;
40 $status = FileDeleteForm
::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
41 if ( !$status->isGood() ) {
44 $article = new Article( $title );
45 $article->doDeleteArticle( "removing for test" );
47 // see if it now doesn't exist; reload
48 $title = Title
::newFromText( $title->getText(), NS_FILE
);
50 return ! ( $title && $title instanceof Title
&& $title->exists() );
54 * Helper function -- remove files and associated articles with a particular filename
55 * @param $fileName String: filename to be removed
57 public function deleteFileByFileName( $fileName ) {
58 return $this->deleteFileByTitle( Title
::newFromText( $fileName, NS_FILE
) );
63 * Helper function -- given a file on the filesystem, find matching content in the db (and associated articles) and remove them.
64 * @param $filePath String: path to file on the filesystem
66 public function deleteFileByContent( $filePath ) {
67 $hash = File
::sha1Base36( $filePath );
68 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
70 foreach ( $dupes as $dupe ) {
71 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
77 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
78 * (This is what PHP would normally do).
79 * @param $fieldName String: name this would have in the upload form
80 * @param $fileName String: name to title this
81 * @param $type String: mime type
82 * @param $filePath String: path where to find file contents
84 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
85 $tmpName = tempnam( wfTempDir(), "" );
86 if ( !file_exists( $filePath ) ) {
87 throw new Exception( "$filePath doesn't exist!" );
90 if ( !copy( $filePath, $tmpName ) ) {
91 throw new Exception( "couldn't copy $filePath to $tmpName" );
95 $size = filesize( $tmpName );
96 if ( $size === false ) {
97 throw new Exception( "couldn't stat $tmpName" );
100 $_FILES[ $fieldName ] = array(
103 'tmp_name' => $tmpName,
113 * Remove traces of previous fake uploads
115 function clearFakeUploads() {