MediaWiki  REL1_19
UploadFromUrlTestSuite.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once( dirname( dirname( __FILE__ ) ) . '/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         function setUp() {
00019                 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc,
00020                           $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
00021                           $wgNamespaceAliases, $wgNamespaceProtection, $parserMemc;
00022 
00023                 $tmpGlobals = array();
00024 
00025                 $tmpGlobals['wgScript'] = '/index.php';
00026                 $tmpGlobals['wgScriptPath'] = '/';
00027                 $tmpGlobals['wgArticlePath'] = '/wiki/$1';
00028                 $tmpGlobals['wgStyleSheetPath'] = '/skins';
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 
00060                 $wgEnableParserCache = false;
00061                 DeferredUpdates::clearPendingUpdates();
00062                 $wgMemc = wfGetMainCache();
00063                 $messageMemc = wfGetMessageCacheStorage();
00064                 $parserMemc = wfGetParserCacheStorage();
00065 
00066                 // $wgContLang = new StubContLang;
00067                 $wgUser = new User;
00068                 $context = new RequestContext;
00069                 $wgLang = $context->getLanguage();
00070                 $wgOut = $context->getOutput();
00071                 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
00072                 $wgRequest = $context->getRequest();
00073 
00074                 if ( $wgStyleDirectory === false ) {
00075                         $wgStyleDirectory   = "$IP/skins";
00076                 }
00077 
00078                 RepoGroup::destroySingleton();
00079                 FileBackendGroup::destroySingleton();
00080         }
00081 
00082         public function tearDown() {
00083                 foreach ( $this->savedGlobals as $var => $val ) {
00084                         $GLOBALS[$var] = $val;
00085                 }
00086                 // Restore backends
00087                 RepoGroup::destroySingleton();
00088                 FileBackendGroup::destroySingleton();
00089 
00090                 $this->teardownUploadDir( $this->uploadDir );
00091         }
00092 
00093         private $uploadDir;
00094         private $keepUploads;
00095 
00099         private function teardownUploadDir( $dir ) {
00100                 if ( $this->keepUploads ) {
00101                         return;
00102                 }
00103 
00104                 // delete the files first, then the dirs.
00105                 self::deleteFiles(
00106                         array (
00107                                 "$dir/3/3a/Foobar.jpg",
00108                                 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
00109                                 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
00110                                 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
00111                                 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
00112 
00113                                 "$dir/0/09/Bad.jpg",
00114                         )
00115                 );
00116 
00117                 self::deleteDirs(
00118                         array (
00119                                 "$dir/3/3a",
00120                                 "$dir/3",
00121                                 "$dir/thumb/6/65",
00122                                 "$dir/thumb/6",
00123                                 "$dir/thumb/3/3a/Foobar.jpg",
00124                                 "$dir/thumb/3/3a",
00125                                 "$dir/thumb/3",
00126 
00127                                 "$dir/0/09/",
00128                                 "$dir/0/",
00129 
00130                                 "$dir/thumb",
00131                                 "$dir",
00132                         )
00133                 );
00134         }
00135 
00141         private static function deleteFiles( $files ) {
00142                 foreach ( $files as $file ) {
00143                         if ( file_exists( $file ) ) {
00144                                 unlink( $file );
00145                         }
00146                 }
00147         }
00148 
00154         private static function deleteDirs( $dirs ) {
00155                 foreach ( $dirs as $dir ) {
00156                         if ( is_dir( $dir ) ) {
00157                                 rmdir( $dir );
00158                         }
00159                 }
00160         }
00161 
00168         private function setupUploadDir() {
00169                 global $IP;
00170 
00171                 if ( $this->keepUploads ) {
00172                         $dir = wfTempDir() . '/mwParser-images';
00173 
00174                         if ( is_dir( $dir ) ) {
00175                                 return $dir;
00176                         }
00177                 } else {
00178                         $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
00179                 }
00180 
00181                 wfDebug( "Creating upload directory $dir\n" );
00182 
00183                 if ( file_exists( $dir ) ) {
00184                         wfDebug( "Already exists!\n" );
00185                         return $dir;
00186                 }
00187 
00188                 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
00189                 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
00190 
00191                 wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
00192                 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
00193 
00194                 return $dir;
00195         }
00196 
00197         public static function suite() {
00198                 // Hack to invoke the autoloader required to get phpunit to recognize
00199                 // the UploadFromUrlTest class
00200                 class_exists( 'UploadFromUrlTest' );
00201                 $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
00202                 return $suite;
00203         }
00204 }