4 * Abstract class to support upload tests
6 abstract class ApiTestCaseUpload
extends ApiTestCase
{
8 * Fixture -- run before every test
10 protected function setUp() {
13 $this->setMwGlobals( array(
14 'wgEnableUploads' => true,
15 'wgEnableAPI' => true,
20 $this->clearFakeUploads();
24 * Helper function -- remove files and associated articles by Title
26 * @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(
41 $restrictDeletedVersions
44 if ( !$status->isGood() ) {
48 $page = WikiPage
::factory( $title );
49 $page->doDeleteArticle( "removing for test" );
51 // see if it now doesn't exist; reload
52 $title = Title
::newFromText( $title->getText(), NS_FILE
);
55 return !( $title && $title instanceof Title
&& $title->exists() );
59 * Helper function -- remove files and associated articles with a particular filename
61 * @param string $fileName Filename to be removed
65 public function deleteFileByFileName( $fileName ) {
66 return $this->deleteFileByTitle( Title
::newFromText( $fileName, NS_FILE
) );
70 * Helper function -- given a file on the filesystem, find matching
71 * content in the db (and associated articles) and remove them.
73 * @param string $filePath Path to file on the filesystem
77 public function deleteFileByContent( $filePath ) {
78 $hash = FSFile
::getSha1Base36FromPath( $filePath );
79 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
81 foreach ( $dupes as $dupe ) {
82 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
89 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
90 * (This is what PHP would normally do).
92 * @param string $fieldName Name this would have in the upload form
93 * @param string $fileName Name to title this
94 * @param string $type MIME type
95 * @param string $filePath Path where to find file contents
100 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
101 $tmpName = $this->getNewTempFile();
102 if ( !file_exists( $filePath ) ) {
103 throw new Exception( "$filePath doesn't exist!" );
106 if ( !copy( $filePath, $tmpName ) ) {
107 throw new Exception( "couldn't copy $filePath to $tmpName" );
111 $size = filesize( $tmpName );
112 if ( $size === false ) {
113 throw new Exception( "couldn't stat $tmpName" );
116 $_FILES[$fieldName] = array(
119 'tmp_name' => $tmpName,
127 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
128 $tmpName = $this->getNewTempFile();
129 // copy the chunk data to temp location:
130 if ( !file_put_contents( $tmpName, $chunkData ) ) {
131 throw new Exception( "couldn't copy chunk data to $tmpName" );
135 $size = filesize( $tmpName );
136 if ( $size === false ) {
137 throw new Exception( "couldn't stat $tmpName" );
140 $_FILES[$fieldName] = array(
143 'tmp_name' => $tmpName,
150 * Remove traces of previous fake uploads
152 function clearFakeUploads() {