Merge "[FileBackend] Reduced stat calls when using the multiwrite backend."
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTestCaseUpload.php
index a7fc9bd..39c7954 100644 (file)
@@ -3,8 +3,6 @@
 /**
  *  * Abstract class to support upload tests
  */
-require_once( 'ApiTestUser.php' );
-require_once( 'ApiTestCase.php' );
 
 abstract class ApiTestCaseUpload extends ApiTestCase {
        /**
@@ -18,21 +16,13 @@ abstract class ApiTestCaseUpload extends ApiTestCase {
                $wgEnableAPI = true;
                wfSetupSession();
 
-               ini_set( 'log_errors', 1 );
-               ini_set( 'error_reporting', 1 );
-               ini_set( 'display_errors', 1 );
-
                $this->clearFakeUploads();
        }
 
-       /**
-        * Fixture -- run after every test
-        * Clean up temporary files etc.
-        */
-       function tearDown() {
+       public function tearDown() {
+               $this->clearTempUpload();
        }
 
-
        /**
         * Helper function -- remove files and associated articles by Title
         * @param $title Title: title to be removed
@@ -47,11 +37,11 @@ abstract class ApiTestCaseUpload extends ApiTestCase {
                        if ( !$status->isGood() ) {
                                return false;
                        }
-                       $article = new Article( $title );
-                       $article->doDeleteArticle( "removing for test" );
+                       $page = WikiPage::factory( $title );
+                       $page->doDeleteArticle( "removing for test" );
 
                        // see if it now doesn't exist; reload
-                       $title = Title::newFromText( $fileName, NS_FILE );
+                       $title = Title::newFromText( $title->getText(), NS_FILE );
                }
                return ! ( $title && $title instanceof Title && $title->exists() );
        }
@@ -70,7 +60,7 @@ abstract class ApiTestCaseUpload extends ApiTestCase {
         * @param $filePath String: path to file on the filesystem
         */
        public function deleteFileByContent( $filePath ) {
-               $hash = File::sha1Base36( $filePath );
+               $hash = FSFile::getSha1Base36FromPath( $filePath );
                $dupes = RepoGroup::singleton()->findBySha1( $hash );
                $success = true;
                foreach ( $dupes as $dupe ) {
@@ -114,6 +104,36 @@ abstract class ApiTestCaseUpload extends ApiTestCase {
                return true;
 
        }
+       function fakeUploadChunk(  $fieldName, $fileName, $type, & $chunkData ){
+               $tmpName = tempnam( wfTempDir(), "" );
+               // copy the chunk data to temp location: 
+               if ( !file_put_contents( $tmpName, $chunkData ) ) {
+                       throw new Exception( "couldn't copy chunk data to $tmpName" );
+               }
+               
+               clearstatcache();
+               $size = filesize( $tmpName );
+               if ( $size === false ) {
+                       throw new Exception( "couldn't stat $tmpName" );
+               }
+               
+               $_FILES[ $fieldName ] = array(
+                       'name'          => $fileName,
+                       'type'          => $type,
+                       'tmp_name'      => $tmpName,
+                       'size'          => $size,
+                       'error'         => null
+               );
+       }
+
+       function clearTempUpload() {
+               if( isset( $_FILES['file']['tmp_name'] ) ) {
+                       $tmp = $_FILES['file']['tmp_name'];
+                       if( file_exists( $tmp ) ) {
+                               unlink( $tmp );
+                       }
+               }
+       }
 
        /**
         * Remove traces of previous fake uploads