Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / suites / UploadFromUrlTestSuite.php
blob80893dab4107eb20d6cabbbe1c815a36f2f3b1e7
1 <?php
3 require_once dirname( __DIR__ ) . '/includes/upload/UploadFromUrlTest.php';
5 class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite {
6 public $savedGlobals = array();
8 public static function addTables( &$tables ) {
9 $tables[] = 'user_properties';
10 $tables[] = 'filearchive';
11 $tables[] = 'logging';
12 $tables[] = 'updatelog';
13 $tables[] = 'iwlinks';
15 return true;
18 protected function setUp() {
19 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgUser,
20 $wgLang, $wgOut, $wgRequest, $wgStyleDirectory,
21 $wgParserCacheType, $wgNamespaceAliases, $wgNamespaceProtection,
22 $parserMemc;
24 $tmpDir = $this->getNewTempDirectory();
25 $tmpGlobals = array();
27 $tmpGlobals['wgScript'] = '/index.php';
28 $tmpGlobals['wgScriptPath'] = '/';
29 $tmpGlobals['wgArticlePath'] = '/wiki/$1';
30 $tmpGlobals['wgStylePath'] = '/skins';
31 $tmpGlobals['wgThumbnailScriptPath'] = false;
32 $tmpGlobals['wgLocalFileRepo'] = array(
33 'class' => 'LocalRepo',
34 'name' => 'local',
35 'url' => 'http://example.com/images',
36 'hashLevels' => 2,
37 'transformVia404' => false,
38 'backend' => new FSFileBackend( array(
39 'name' => 'local-backend',
40 'wikiId' => wfWikiId(),
41 'containerPaths' => array(
42 'local-public' => "{$tmpDir}/test-repo/public",
43 'local-thumb' => "{$tmpDir}/test-repo/thumb",
44 'local-temp' => "{$tmpDir}/test-repo/temp",
45 'local-deleted' => "{$tmpDir}/test-repo/delete",
47 ) ),
49 foreach ( $tmpGlobals as $var => $val ) {
50 if ( array_key_exists( $var, $GLOBALS ) ) {
51 $this->savedGlobals[$var] = $GLOBALS[$var];
53 $GLOBALS[$var] = $val;
56 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
57 $wgNamespaceAliases['Image'] = NS_FILE;
58 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
60 $wgParserCacheType = CACHE_NONE;
61 DeferredUpdates::clearPendingUpdates();
62 $wgMemc = wfGetMainCache();
63 $messageMemc = wfGetMessageCacheStorage();
64 $parserMemc = wfGetParserCacheStorage();
66 $wgUser = new User;
67 $context = new RequestContext;
68 $wgLang = $context->getLanguage();
69 $wgOut = $context->getOutput();
70 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
71 $wgRequest = $context->getRequest();
73 if ( $wgStyleDirectory === false ) {
74 $wgStyleDirectory = "$IP/skins";
77 RepoGroup::destroySingleton();
78 FileBackendGroup::destroySingleton();
81 protected function tearDown() {
82 foreach ( $this->savedGlobals as $var => $val ) {
83 $GLOBALS[$var] = $val;
85 // Restore backends
86 RepoGroup::destroySingleton();
87 FileBackendGroup::destroySingleton();
89 $this->teardownUploadDir( $this->uploadDir );
91 parent::tearDown();
94 private $uploadDir;
95 private $keepUploads;
97 /**
98 * Remove the dummy uploads directory
99 * @param string $dir
101 private function teardownUploadDir( $dir ) {
102 if ( $this->keepUploads ) {
103 return;
106 // delete the files first, then the dirs.
107 self::deleteFiles(
108 array(
109 "$dir/3/3a/Foobar.jpg",
110 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
111 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
112 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
113 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
115 "$dir/0/09/Bad.jpg",
119 self::deleteDirs(
120 array(
121 "$dir/3/3a",
122 "$dir/3",
123 "$dir/thumb/6/65",
124 "$dir/thumb/6",
125 "$dir/thumb/3/3a/Foobar.jpg",
126 "$dir/thumb/3/3a",
127 "$dir/thumb/3",
129 "$dir/0/09/",
130 "$dir/0/",
132 "$dir/thumb",
133 "$dir",
139 * Delete the specified files, if they exist.
141 * @param array $files Full paths to files to delete.
143 private static function deleteFiles( $files ) {
144 foreach ( $files as $file ) {
145 if ( file_exists( $file ) ) {
146 unlink( $file );
152 * Delete the specified directories, if they exist. Must be empty.
154 * @param array $dirs Full paths to directories to delete.
156 private static function deleteDirs( $dirs ) {
157 foreach ( $dirs as $dir ) {
158 if ( is_dir( $dir ) ) {
159 rmdir( $dir );
165 * Create a dummy uploads directory which will contain a couple
166 * of files in order to pass existence tests.
168 * @return string The directory
170 private function setupUploadDir() {
171 global $IP;
173 if ( $this->keepUploads ) {
174 $dir = wfTempDir() . '/mwParser-images';
176 if ( is_dir( $dir ) ) {
177 return $dir;
179 } else {
180 $dir = $this->getNewTempDirectory();
183 wfDebug( "Creating upload directory $dir\n" );
185 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
186 copy( "$IP/tests/phpunit/data/upload/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
188 wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
189 copy( "$IP/tests/phpunit/data/upload/headbg.jpg", "$dir/0/09/Bad.jpg" );
191 return $dir;
194 public static function suite() {
195 // Hack to invoke the autoloader required to get phpunit to recognize
196 // the UploadFromUrlTest class
197 class_exists( 'UploadFromUrlTest' );
198 $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
200 return $suite;