Merge "Use MessageCache::getMsgFromNamespace() to fetch conversion tables."
[lhc/web/wiklou.git] / tests / phpunit / suites / UploadFromUrlTestSuite.php
1 <?php
2
3 require_once( dirname( __DIR__ ) . '/includes/upload/UploadFromUrlTest.php' );
4
5 class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite {
6 public $savedGlobals = array();
7
8 public static function addTables( &$tables ) {
9 $tables[] = 'user_properties';
10 $tables[] = 'filearchive';
11 $tables[] = 'logging';
12 $tables[] = 'updatelog';
13 $tables[] = 'iwlinks';
14
15 return true;
16 }
17
18 protected function setUp() {
19 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgUser,
20 $wgLang, $wgOut, $wgRequest, $wgStyleDirectory,
21 $wgEnableParserCache, $wgNamespaceAliases, $wgNamespaceProtection,
22 $parserMemc;
23
24 $tmpGlobals = array();
25
26 $tmpGlobals['wgScript'] = '/index.php';
27 $tmpGlobals['wgScriptPath'] = '/';
28 $tmpGlobals['wgArticlePath'] = '/wiki/$1';
29 $tmpGlobals['wgStyleSheetPath'] = '/skins';
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 'lockManager' => 'fsLockManager',
41 'containerPaths' => array(
42 'local-public' => wfTempDir() . '/test-repo/public',
43 'local-thumb' => wfTempDir() . '/test-repo/thumb',
44 'local-temp' => wfTempDir() . '/test-repo/temp',
45 'local-deleted' => wfTempDir() . '/test-repo/delete',
46 )
47 ) ),
48 );
49 foreach ( $tmpGlobals as $var => $val ) {
50 if ( array_key_exists( $var, $GLOBALS ) ) {
51 $this->savedGlobals[$var] = $GLOBALS[$var];
52 }
53 $GLOBALS[$var] = $val;
54 }
55
56 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
57 $wgNamespaceAliases['Image'] = NS_FILE;
58 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
59
60
61 $wgEnableParserCache = false;
62 DeferredUpdates::clearPendingUpdates();
63 $wgMemc = wfGetMainCache();
64 $messageMemc = wfGetMessageCacheStorage();
65 $parserMemc = wfGetParserCacheStorage();
66
67 // $wgContLang = new StubContLang;
68 $wgUser = new User;
69 $context = new RequestContext;
70 $wgLang = $context->getLanguage();
71 $wgOut = $context->getOutput();
72 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
73 $wgRequest = $context->getRequest();
74
75 if ( $wgStyleDirectory === false ) {
76 $wgStyleDirectory = "$IP/skins";
77 }
78
79 RepoGroup::destroySingleton();
80 FileBackendGroup::destroySingleton();
81 }
82
83 protected function tearDown() {
84 foreach ( $this->savedGlobals as $var => $val ) {
85 $GLOBALS[$var] = $val;
86 }
87 // Restore backends
88 RepoGroup::destroySingleton();
89 FileBackendGroup::destroySingleton();
90
91 $this->teardownUploadDir( $this->uploadDir );
92
93 parent::tearDown();
94 }
95
96 private $uploadDir;
97 private $keepUploads;
98
99 /**
100 * Remove the dummy uploads directory
101 */
102 private function teardownUploadDir( $dir ) {
103 if ( $this->keepUploads ) {
104 return;
105 }
106
107 // delete the files first, then the dirs.
108 self::deleteFiles(
109 array(
110 "$dir/3/3a/Foobar.jpg",
111 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
112 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
113 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
114 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
115
116 "$dir/0/09/Bad.jpg",
117 )
118 );
119
120 self::deleteDirs(
121 array(
122 "$dir/3/3a",
123 "$dir/3",
124 "$dir/thumb/6/65",
125 "$dir/thumb/6",
126 "$dir/thumb/3/3a/Foobar.jpg",
127 "$dir/thumb/3/3a",
128 "$dir/thumb/3",
129
130 "$dir/0/09/",
131 "$dir/0/",
132
133 "$dir/thumb",
134 "$dir",
135 )
136 );
137 }
138
139 /**
140 * Delete the specified files, if they exist.
141 *
142 * @param $files Array: full paths to files to delete.
143 */
144 private static function deleteFiles( $files ) {
145 foreach ( $files as $file ) {
146 if ( file_exists( $file ) ) {
147 unlink( $file );
148 }
149 }
150 }
151
152 /**
153 * Delete the specified directories, if they exist. Must be empty.
154 *
155 * @param $dirs Array: full paths to directories to delete.
156 */
157 private static function deleteDirs( $dirs ) {
158 foreach ( $dirs as $dir ) {
159 if ( is_dir( $dir ) ) {
160 rmdir( $dir );
161 }
162 }
163 }
164
165 /**
166 * Create a dummy uploads directory which will contain a couple
167 * of files in order to pass existence tests.
168 *
169 * @return String: the directory
170 */
171 private function setupUploadDir() {
172 global $IP;
173
174 if ( $this->keepUploads ) {
175 $dir = wfTempDir() . '/mwParser-images';
176
177 if ( is_dir( $dir ) ) {
178 return $dir;
179 }
180 } else {
181 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
182 }
183
184 wfDebug( "Creating upload directory $dir\n" );
185
186 if ( file_exists( $dir ) ) {
187 wfDebug( "Already exists!\n" );
188 return $dir;
189 }
190
191 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
192 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
193
194 wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
195 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
196
197 return $dir;
198 }
199
200 public static function suite() {
201 // Hack to invoke the autoloader required to get phpunit to recognize
202 // the UploadFromUrlTest class
203 class_exists( 'UploadFromUrlTest' );
204 $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
205 return $suite;
206 }
207 }