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
33 * @param Title $title title to be removed
37 public function deleteFileByTitle( $title ) {
38 if ( $title->exists() ) {
39 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
40 $noOldArchive = ""; // yes this really needs to be set this way
41 $comment = "removing for test";
42 $restrictDeletedVersions = false;
43 $status = FileDeleteForm
::doDelete(
48 $restrictDeletedVersions
51 if ( !$status->isGood() ) {
55 $page = WikiPage
::factory( $title );
56 $page->doDeleteArticle( "removing for test" );
58 // see if it now doesn't exist; reload
59 $title = Title
::newFromText( $title->getText(), NS_FILE
);
62 return !( $title && $title instanceof Title
&& $title->exists() );
66 * Helper function -- remove files and associated articles with a particular filename
68 * @param string $fileName filename to be removed
72 public function deleteFileByFileName( $fileName ) {
73 return $this->deleteFileByTitle( Title
::newFromText( $fileName, NS_FILE
) );
77 * Helper function -- given a file on the filesystem, find matching
78 * content in the db (and associated articles) and remove them.
80 * @param string $filePath path to file on the filesystem
84 public function deleteFileByContent( $filePath ) {
85 $hash = FSFile
::getSha1Base36FromPath( $filePath );
86 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
88 foreach ( $dupes as $dupe ) {
89 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
96 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
97 * (This is what PHP would normally do).
99 * @param string $fieldName name this would have in the upload form
100 * @param string $fileName name to title this
101 * @param string $type mime type
102 * @param string $filePath path where to find file contents
107 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
108 $tmpName = tempnam( wfTempDir(), "" );
109 if ( !file_exists( $filePath ) ) {
110 throw new Exception( "$filePath doesn't exist!" );
113 if ( !copy( $filePath, $tmpName ) ) {
114 throw new Exception( "couldn't copy $filePath to $tmpName" );
118 $size = filesize( $tmpName );
119 if ( $size === false ) {
120 throw new Exception( "couldn't stat $tmpName" );
123 $_FILES[$fieldName] = array(
126 'tmp_name' => $tmpName,
134 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
135 $tmpName = tempnam( wfTempDir(), "" );
136 // copy the chunk data to temp location:
137 if ( !file_put_contents( $tmpName, $chunkData ) ) {
138 throw new Exception( "couldn't copy chunk data to $tmpName" );
142 $size = filesize( $tmpName );
143 if ( $size === false ) {
144 throw new Exception( "couldn't stat $tmpName" );
147 $_FILES[$fieldName] = array(
150 'tmp_name' => $tmpName,
156 function clearTempUpload() {
157 if ( isset( $_FILES['file']['tmp_name'] ) ) {
158 $tmp = $_FILES['file']['tmp_name'];
159 if ( file_exists( $tmp ) ) {
166 * Remove traces of previous fake uploads
168 function clearFakeUploads() {