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();
25 * Helper function -- remove files and associated articles by Title
27 * @param Title $title Title to be removed
31 public function deleteFileByTitle( $title ) {
32 if ( $title->exists() ) {
33 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
34 $noOldArchive = ""; // yes this really needs to be set this way
35 $comment = "removing for test";
36 $restrictDeletedVersions = false;
37 $status = FileDeleteForm
::doDelete(
42 $restrictDeletedVersions
45 if ( !$status->isGood() ) {
49 $page = WikiPage
::factory( $title );
50 $page->doDeleteArticle( "removing for test" );
52 // see if it now doesn't exist; reload
53 $title = Title
::newFromText( $title->getText(), NS_FILE
);
56 return !( $title && $title instanceof Title
&& $title->exists() );
60 * Helper function -- remove files and associated articles with a particular filename
62 * @param string $fileName Filename to be removed
66 public function deleteFileByFileName( $fileName ) {
67 return $this->deleteFileByTitle( Title
::newFromText( $fileName, NS_FILE
) );
71 * Helper function -- given a file on the filesystem, find matching
72 * content in the db (and associated articles) and remove them.
74 * @param string $filePath Path to file on the filesystem
78 public function deleteFileByContent( $filePath ) {
79 $hash = FSFile
::getSha1Base36FromPath( $filePath );
80 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
82 foreach ( $dupes as $dupe ) {
83 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
90 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
91 * (This is what PHP would normally do).
93 * @param string $fieldName Name this would have in the upload form
94 * @param string $fileName Name to title this
95 * @param string $type MIME type
96 * @param string $filePath Path where to find file contents
101 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
102 $tmpName = $this->getNewTempFile();
103 if ( !file_exists( $filePath ) ) {
104 throw new Exception( "$filePath doesn't exist!" );
107 if ( !copy( $filePath, $tmpName ) ) {
108 throw new Exception( "couldn't copy $filePath to $tmpName" );
112 $size = filesize( $tmpName );
113 if ( $size === false ) {
114 throw new Exception( "couldn't stat $tmpName" );
117 $_FILES[$fieldName] = array(
120 'tmp_name' => $tmpName,
128 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
129 $tmpName = $this->getNewTempFile();
130 // copy the chunk data to temp location:
131 if ( !file_put_contents( $tmpName, $chunkData ) ) {
132 throw new Exception( "couldn't copy chunk data to $tmpName" );
136 $size = filesize( $tmpName );
137 if ( $size === false ) {
138 throw new Exception( "couldn't stat $tmpName" );
141 $_FILES[$fieldName] = array(
144 'tmp_name' => $tmpName,
151 * Remove traces of previous fake uploads
153 function clearFakeUploads() {