MediaWiki  REL1_22
UploadFromUrlTestSuite.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once dirname( __DIR__ ) . '/includes/upload/UploadFromUrlTest.php';
00004 
00005 class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite {
00006     public $savedGlobals = array();
00007 
00008     public static function addTables( &$tables ) {
00009         $tables[] = 'user_properties';
00010         $tables[] = 'filearchive';
00011         $tables[] = 'logging';
00012         $tables[] = 'updatelog';
00013         $tables[] = 'iwlinks';
00014 
00015         return true;
00016     }
00017 
00018     protected function setUp() {
00019         global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgUser,
00020             $wgLang, $wgOut, $wgRequest, $wgStyleDirectory,
00021             $wgEnableParserCache, $wgNamespaceAliases, $wgNamespaceProtection,
00022             $parserMemc;
00023 
00024         $tmpGlobals = array();
00025 
00026         $tmpGlobals['wgScript'] = '/index.php';
00027         $tmpGlobals['wgScriptPath'] = '/';
00028         $tmpGlobals['wgArticlePath'] = '/wiki/$1';
00029         $tmpGlobals['wgStylePath'] = '/skins';
00030         $tmpGlobals['wgThumbnailScriptPath'] = false;
00031         $tmpGlobals['wgLocalFileRepo'] = array(
00032             'class' => 'LocalRepo',
00033             'name' => 'local',
00034             'url' => 'http://example.com/images',
00035             'hashLevels' => 2,
00036             'transformVia404' => false,
00037             'backend' => new FSFileBackend( array(
00038                 'name' => 'local-backend',
00039                 'lockManager' => 'fsLockManager',
00040                 'containerPaths' => array(
00041                     'local-public' => wfTempDir() . '/test-repo/public',
00042                     'local-thumb' => wfTempDir() . '/test-repo/thumb',
00043                     'local-temp' => wfTempDir() . '/test-repo/temp',
00044                     'local-deleted' => wfTempDir() . '/test-repo/delete',
00045                 )
00046             ) ),
00047         );
00048         foreach ( $tmpGlobals as $var => $val ) {
00049             if ( array_key_exists( $var, $GLOBALS ) ) {
00050                 $this->savedGlobals[$var] = $GLOBALS[$var];
00051             }
00052             $GLOBALS[$var] = $val;
00053         }
00054 
00055         $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
00056         $wgNamespaceAliases['Image'] = NS_FILE;
00057         $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
00058 
00059         $wgEnableParserCache = false;
00060         DeferredUpdates::clearPendingUpdates();
00061         $wgMemc = wfGetMainCache();
00062         $messageMemc = wfGetMessageCacheStorage();
00063         $parserMemc = wfGetParserCacheStorage();
00064 
00065         // $wgContLang = new StubContLang;
00066         $wgUser = new User;
00067         $context = new RequestContext;
00068         $wgLang = $context->getLanguage();
00069         $wgOut = $context->getOutput();
00070         $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
00071         $wgRequest = $context->getRequest();
00072 
00073         if ( $wgStyleDirectory === false ) {
00074             $wgStyleDirectory = "$IP/skins";
00075         }
00076 
00077         RepoGroup::destroySingleton();
00078         FileBackendGroup::destroySingleton();
00079     }
00080 
00081     protected function tearDown() {
00082         foreach ( $this->savedGlobals as $var => $val ) {
00083             $GLOBALS[$var] = $val;
00084         }
00085         // Restore backends
00086         RepoGroup::destroySingleton();
00087         FileBackendGroup::destroySingleton();
00088 
00089         $this->teardownUploadDir( $this->uploadDir );
00090 
00091         parent::tearDown();
00092     }
00093 
00094     private $uploadDir;
00095     private $keepUploads;
00096 
00100     private function teardownUploadDir( $dir ) {
00101         if ( $this->keepUploads ) {
00102             return;
00103         }
00104 
00105         // delete the files first, then the dirs.
00106         self::deleteFiles(
00107             array(
00108                 "$dir/3/3a/Foobar.jpg",
00109                 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
00110                 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
00111                 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
00112                 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
00113 
00114                 "$dir/0/09/Bad.jpg",
00115             )
00116         );
00117 
00118         self::deleteDirs(
00119             array(
00120                 "$dir/3/3a",
00121                 "$dir/3",
00122                 "$dir/thumb/6/65",
00123                 "$dir/thumb/6",
00124                 "$dir/thumb/3/3a/Foobar.jpg",
00125                 "$dir/thumb/3/3a",
00126                 "$dir/thumb/3",
00127 
00128                 "$dir/0/09/",
00129                 "$dir/0/",
00130 
00131                 "$dir/thumb",
00132                 "$dir",
00133             )
00134         );
00135     }
00136 
00142     private static function deleteFiles( $files ) {
00143         foreach ( $files as $file ) {
00144             if ( file_exists( $file ) ) {
00145                 unlink( $file );
00146             }
00147         }
00148     }
00149 
00155     private static function deleteDirs( $dirs ) {
00156         foreach ( $dirs as $dir ) {
00157             if ( is_dir( $dir ) ) {
00158                 rmdir( $dir );
00159             }
00160         }
00161     }
00162 
00169     private function setupUploadDir() {
00170         global $IP;
00171 
00172         if ( $this->keepUploads ) {
00173             $dir = wfTempDir() . '/mwParser-images';
00174 
00175             if ( is_dir( $dir ) ) {
00176                 return $dir;
00177             }
00178         } else {
00179             $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
00180         }
00181 
00182         wfDebug( "Creating upload directory $dir\n" );
00183 
00184         if ( file_exists( $dir ) ) {
00185             wfDebug( "Already exists!\n" );
00186 
00187             return $dir;
00188         }
00189 
00190         wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
00191         copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
00192 
00193         wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
00194         copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
00195 
00196         return $dir;
00197     }
00198 
00199     public static function suite() {
00200         // Hack to invoke the autoloader required to get phpunit to recognize
00201         // the UploadFromUrlTest class
00202         class_exists( 'UploadFromUrlTest' );
00203         $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
00204 
00205         return $suite;
00206     }
00207 }