Remove deprecated class StubContLang
[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['wgStylePath'] = '/skins';
30 $tmpGlobals['wgThumbnailScriptPath'] = false;
31 $tmpGlobals['wgLocalFileRepo'] = array(
32 'class' => 'LocalRepo',
33 'name' => 'local',
34 'url' => 'http://example.com/images',
35 'hashLevels' => 2,
36 'transformVia404' => false,
37 'backend' => new FSFileBackend( array(
38 'name' => 'local-backend',
39 'wikiId' => wfWikiId(),
40 'containerPaths' => array(
41 'local-public' => wfTempDir() . '/test-repo/public',
42 'local-thumb' => wfTempDir() . '/test-repo/thumb',
43 'local-temp' => wfTempDir() . '/test-repo/temp',
44 'local-deleted' => wfTempDir() . '/test-repo/delete',
45 )
46 ) ),
47 );
48 foreach ( $tmpGlobals as $var => $val ) {
49 if ( array_key_exists( $var, $GLOBALS ) ) {
50 $this->savedGlobals[$var] = $GLOBALS[$var];
51 }
52 $GLOBALS[$var] = $val;
53 }
54
55 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
56 $wgNamespaceAliases['Image'] = NS_FILE;
57 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
58
59 $wgEnableParserCache = false;
60 DeferredUpdates::clearPendingUpdates();
61 $wgMemc = wfGetMainCache();
62 $messageMemc = wfGetMessageCacheStorage();
63 $parserMemc = wfGetParserCacheStorage();
64
65 $wgUser = new User;
66 $context = new RequestContext;
67 $wgLang = $context->getLanguage();
68 $wgOut = $context->getOutput();
69 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
70 $wgRequest = $context->getRequest();
71
72 if ( $wgStyleDirectory === false ) {
73 $wgStyleDirectory = "$IP/skins";
74 }
75
76 RepoGroup::destroySingleton();
77 FileBackendGroup::destroySingleton();
78 }
79
80 protected function tearDown() {
81 foreach ( $this->savedGlobals as $var => $val ) {
82 $GLOBALS[$var] = $val;
83 }
84 // Restore backends
85 RepoGroup::destroySingleton();
86 FileBackendGroup::destroySingleton();
87
88 $this->teardownUploadDir( $this->uploadDir );
89
90 parent::tearDown();
91 }
92
93 private $uploadDir;
94 private $keepUploads;
95
96 /**
97 * Remove the dummy uploads directory
98 */
99 private function teardownUploadDir( $dir ) {
100 if ( $this->keepUploads ) {
101 return;
102 }
103
104 // delete the files first, then the dirs.
105 self::deleteFiles(
106 array(
107 "$dir/3/3a/Foobar.jpg",
108 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
109 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
110 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
111 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
112
113 "$dir/0/09/Bad.jpg",
114 )
115 );
116
117 self::deleteDirs(
118 array(
119 "$dir/3/3a",
120 "$dir/3",
121 "$dir/thumb/6/65",
122 "$dir/thumb/6",
123 "$dir/thumb/3/3a/Foobar.jpg",
124 "$dir/thumb/3/3a",
125 "$dir/thumb/3",
126
127 "$dir/0/09/",
128 "$dir/0/",
129
130 "$dir/thumb",
131 "$dir",
132 )
133 );
134 }
135
136 /**
137 * Delete the specified files, if they exist.
138 *
139 * @param array $files Full paths to files to delete.
140 */
141 private static function deleteFiles( $files ) {
142 foreach ( $files as $file ) {
143 if ( file_exists( $file ) ) {
144 unlink( $file );
145 }
146 }
147 }
148
149 /**
150 * Delete the specified directories, if they exist. Must be empty.
151 *
152 * @param array $dirs Full paths to directories to delete.
153 */
154 private static function deleteDirs( $dirs ) {
155 foreach ( $dirs as $dir ) {
156 if ( is_dir( $dir ) ) {
157 rmdir( $dir );
158 }
159 }
160 }
161
162 /**
163 * Create a dummy uploads directory which will contain a couple
164 * of files in order to pass existence tests.
165 *
166 * @return string The directory
167 */
168 private function setupUploadDir() {
169 global $IP;
170
171 if ( $this->keepUploads ) {
172 $dir = wfTempDir() . '/mwParser-images';
173
174 if ( is_dir( $dir ) ) {
175 return $dir;
176 }
177 } else {
178 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
179 }
180
181 wfDebug( "Creating upload directory $dir\n" );
182
183 if ( file_exists( $dir ) ) {
184 wfDebug( "Already exists!\n" );
185
186 return $dir;
187 }
188
189 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
190 copy( "$IP/tests/phpunit/data/upload/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
191
192 wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
193 copy( "$IP/tests/phpunit/data/upload/headbg.jpg", "$dir/0/09/Bad.jpg" );
194
195 return $dir;
196 }
197
198 public static function suite() {
199 // Hack to invoke the autoloader required to get phpunit to recognize
200 // the UploadFromUrlTest class
201 class_exists( 'UploadFromUrlTest' );
202 $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
203
204 return $suite;
205 }
206 }