Wrap UploadFromUrlTest with a special TestSuite that ensures the test database +...
authorMark A. Hershberger <mah@users.mediawiki.org>
Sat, 1 May 2010 02:09:08 +0000 (02:09 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Sat, 1 May 2010 02:09:08 +0000 (02:09 +0000)
maintenance/tests/UploadFromUrlTest.php
maintenance/tests/UploadFromUrlTestSuite.php [new file with mode: 0644]
maintenance/tests/phpunit.xml

index 76bd94f..9957f2e 100644 (file)
@@ -13,9 +13,10 @@ class nullClass {
 class UploadFromUrlTest extends ApiSetup {
 
        function setUp() {
-               global $wgEnableUploads, $wgLocalFileRepo;
+               global $wgEnableUploads, $wgLocalFileRepo, $wgAllowCopyUploads;
 
                $wgEnableUploads = true;
+               $wgAllowCopyUploads = true;
                parent::setup();
                $wgLocalFileRepo = array(
                        'class' => 'LocalRepo',
@@ -172,7 +173,7 @@ class UploadFromUrlTest extends ApiSetup {
                $job = Job::pop();
                $this->assertEquals( 'UploadFromUrlJob', get_class($job) );
 
-               $status = $job->run();
+               $status = $job->run();
                $this->assertTrue( $status->isOk() );
 
                return $data;
diff --git a/maintenance/tests/UploadFromUrlTestSuite.php b/maintenance/tests/UploadFromUrlTestSuite.php
new file mode 100644 (file)
index 0000000..5e37089
--- /dev/null
@@ -0,0 +1,168 @@
+<?php
+
+require_once('UploadFromUrlTest.php');
+
+class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite
+{
+       public static function addTables(&$tables) {
+               $tables[] = 'user_properties';
+               $tables[] = 'filearchive';
+               $tables[] = 'logging';
+               $tables[] = 'updatelog';
+               $tables[] = 'iwlinks';
+               return true;
+       }
+
+       function setUp() {
+               global $wgParser,  $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
+                  $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
+                  $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
+                  $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
+                  $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
+                  $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
+
+               $wgScript = '/index.php';
+               $wgScriptPath = '/';
+               $wgArticlePath = '/wiki/$1';
+               $wgStyleSheetPath = '/skins';
+               $wgStylePath = '/skins';
+               $wgThumbnailScriptPath = false;
+               $wgLocalFileRepo = array(
+                       'class' => 'LocalRepo',
+                       'name' => 'local',
+                       'directory' => 'test-repo',
+                       'url' => 'http://example.com/images',
+                       'hashLevels' => 2,
+                       'transformVia404' => false,
+               );
+               $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
+               $wgNamespaceAliases['Image'] = NS_FILE;
+               $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
+
+
+               $wgEnableParserCache = false;
+               $wgDeferredUpdateList = array();
+               $wgMemc =& wfGetMainCache();
+               $messageMemc =& wfGetMessageCacheStorage();
+               $parserMemc =& wfGetParserCacheStorage();
+
+               $wgContLang = new StubContLang;
+               $wgUser = new StubUser;
+               $wgLang = new StubUserLang;
+               $wgOut = new StubObject( 'wgOut', 'OutputPage' );
+               $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
+               $wgRequest = new WebRequest;
+
+               $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
+                                                                                 array( $messageMemc, $wgUseDatabaseMessages,
+                                                                                                $wgMsgCacheExpiry, wfWikiID() ) );
+               if( $wgStyleDirectory === false) $wgStyleDirectory   = "$IP/skins";
+
+       }
+
+       public function tearDown() {
+               $this->teardownUploadDir($this->uploadDir);
+       }
+
+       private $uploadDir;
+       private $keepUploads;
+       /**
+        * Remove the dummy uploads directory
+        */
+       private function teardownUploadDir( $dir ) {
+               if ( $this->keepUploads ) {
+                       return;
+               }
+
+               // delete the files first, then the dirs.
+               self::deleteFiles(
+                       array (
+                               "$dir/3/3a/Foobar.jpg",
+                               "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
+                               "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
+                               "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
+                               "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
+
+                               "$dir/0/09/Bad.jpg",
+                       )
+               );
+
+               self::deleteDirs(
+                       array (
+                               "$dir/3/3a",
+                               "$dir/3",
+                               "$dir/thumb/6/65",
+                               "$dir/thumb/6",
+                               "$dir/thumb/3/3a/Foobar.jpg",
+                               "$dir/thumb/3/3a",
+                               "$dir/thumb/3",
+
+                               "$dir/0/09/",
+                               "$dir/0/",
+
+                               "$dir/thumb",
+                               "$dir",
+                       )
+               );
+       }
+
+
+       /**
+        * Delete the specified files, if they exist.
+        * @param array $files full paths to files to delete.
+        */
+       private static function deleteFiles( $files ) {
+               foreach( $files as $file ) {
+                       if( file_exists( $file ) ) {
+                               unlink( $file );
+                       }
+               }
+       }
+       /**
+        * Delete the specified directories, if they exist. Must be empty.
+        * @param array $dirs full paths to directories to delete.
+        */
+       private static function deleteDirs( $dirs ) {
+               foreach( $dirs as $dir ) {
+                       if( is_dir( $dir ) ) {
+                               rmdir( $dir );
+                       }
+               }
+       }
+
+       /**
+        * Create a dummy uploads directory which will contain a couple
+        * of files in order to pass existence tests.
+        * @return string The directory
+        */
+       private function setupUploadDir() {
+               global $IP;
+               if ( $this->keepUploads ) {
+                       $dir = wfTempDir() . '/mwParser-images';
+                       if ( is_dir( $dir ) ) {
+                               return $dir;
+                       }
+               } else {
+                       $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
+               }
+
+               wfDebug( "Creating upload directory $dir\n" );
+               if ( file_exists( $dir ) ) {
+                       wfDebug( "Already exists!\n" );
+                       return $dir;
+               }
+               wfMkdirParents( $dir . '/3/3a' );
+               copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
+
+               wfMkdirParents( $dir . '/0/09' );
+               copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
+               return $dir;
+       }
+
+    public static function suite()
+    {
+        return new UploadFromUrlTestSuite('UploadFromUrlTest');
+    }
+
+}
+
index 79be95a..1ba58c8 100644 (file)
@@ -32,7 +32,7 @@
     <file>TitlePermissionTest.php</file>
     <file>TitleTest.php</file>
     <file>UploadTest.php</file>
-    <file>UploadFromUrlTest.php</file>
+    <file>UploadFromUrlTestSuite.php</file>
     <file>XmlTest.php</file>
   </testsuite>
   <groups>