From 4b49e61188b07243ed07795b899e958fe2312fa9 Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Sat, 1 May 2010 02:09:08 +0000 Subject: [PATCH] Wrap UploadFromUrlTest with a special TestSuite that ensures the test database + global vars are set correctly. Eventually need to use this wrapper for all tests. --- maintenance/tests/UploadFromUrlTest.php | 5 +- maintenance/tests/UploadFromUrlTestSuite.php | 168 +++++++++++++++++++ maintenance/tests/phpunit.xml | 2 +- 3 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 maintenance/tests/UploadFromUrlTestSuite.php diff --git a/maintenance/tests/UploadFromUrlTest.php b/maintenance/tests/UploadFromUrlTest.php index 76bd94fcf5..9957f2e7f6 100644 --- a/maintenance/tests/UploadFromUrlTest.php +++ b/maintenance/tests/UploadFromUrlTest.php @@ -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 index 0000000000..5e3708918c --- /dev/null +++ b/maintenance/tests/UploadFromUrlTestSuite.php @@ -0,0 +1,168 @@ + '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'); + } + +} + diff --git a/maintenance/tests/phpunit.xml b/maintenance/tests/phpunit.xml index 79be95adc1..1ba58c8198 100644 --- a/maintenance/tests/phpunit.xml +++ b/maintenance/tests/phpunit.xml @@ -32,7 +32,7 @@ TitlePermissionTest.php TitleTest.php UploadTest.php - UploadFromUrlTest.php + UploadFromUrlTestSuite.php XmlTest.php -- 2.20.1