4 * * Abstract class to support upload tests
7 abstract class ApiTestCaseUpload
extends ApiTestCase
{
9 * Fixture -- run before every test
11 protected function setUp() {
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgEnableAPI' => true,
21 $this->clearFakeUploads();
24 protected function tearDown() {
25 $this->clearTempUpload();
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 $page = WikiPage
::factory( $title );
45 $page->doDeleteArticle( "removing for test" );
47 // see if it now doesn't exist; reload
48 $title = Title
::newFromText( $title->getText(), NS_FILE
);
51 return !( $title && $title instanceof Title
&& $title->exists() );
55 * Helper function -- remove files and associated articles with a particular filename
56 * @param $fileName String: filename to be removed
58 public function deleteFileByFileName( $fileName ) {
59 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 = FSFile
::getSha1Base36FromPath( $filePath );
68 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
70 foreach ( $dupes as $dupe ) {
71 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
78 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
79 * (This is what PHP would normally do).
80 * @param $fieldName String: name this would have in the upload form
81 * @param $fileName String: name to title this
82 * @param $type String: mime type
83 * @param $filePath String: path where to find file contents
85 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
86 $tmpName = tempnam( wfTempDir(), "" );
87 if ( !file_exists( $filePath ) ) {
88 throw new Exception( "$filePath doesn't exist!" );
91 if ( !copy( $filePath, $tmpName ) ) {
92 throw new Exception( "couldn't copy $filePath to $tmpName" );
96 $size = filesize( $tmpName );
97 if ( $size === false ) {
98 throw new Exception( "couldn't stat $tmpName" );
101 $_FILES[$fieldName] = array(
104 'tmp_name' => $tmpName,
112 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
113 $tmpName = tempnam( wfTempDir(), "" );
114 // copy the chunk data to temp location:
115 if ( !file_put_contents( $tmpName, $chunkData ) ) {
116 throw new Exception( "couldn't copy chunk data to $tmpName" );
120 $size = filesize( $tmpName );
121 if ( $size === false ) {
122 throw new Exception( "couldn't stat $tmpName" );
125 $_FILES[$fieldName] = array(
128 'tmp_name' => $tmpName,
134 function clearTempUpload() {
135 if ( isset( $_FILES['file']['tmp_name'] ) ) {
136 $tmp = $_FILES['file']['tmp_name'];
137 if ( file_exists( $tmp ) ) {
144 * Remove traces of previous fake uploads
146 function clearFakeUploads() {