phpcs: More require/include is not a function
[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 'lockManager' => 'fsLockManager',
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 // $wgContLang = new StubContLang;
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();
72
73 if ( $wgStyleDirectory === false ) {
74 $wgStyleDirectory = "$IP/skins";
75 }
76
77 RepoGroup::destroySingleton();
78 FileBackendGroup::destroySingleton();
79 }
80
81 protected function tearDown() {
82 foreach ( $this->savedGlobals as $var => $val ) {
83 $GLOBALS[$var] = $val;
84 }
85 // Restore backends
86 RepoGroup::destroySingleton();
87 FileBackendGroup::destroySingleton();
88
89 $this->teardownUploadDir( $this->uploadDir );
90
91 parent::tearDown();
92 }
93
94 private $uploadDir;
95 private $keepUploads;
96
97 /**
98 * Remove the dummy uploads directory
99 */
100 private function teardownUploadDir( $dir ) {
101 if ( $this->keepUploads ) {
102 return;
103 }
104
105 // delete the files first, then the dirs.
106 self::deleteFiles(
107 array(
108 "$dir/3/3a/Foobar.jpg",
109 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
110 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
111 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
112 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
113
114 "$dir/0/09/Bad.jpg",
115 )
116 );
117
118 self::deleteDirs(
119 array(
120 "$dir/3/3a",
121 "$dir/3",
122 "$dir/thumb/6/65",
123 "$dir/thumb/6",
124 "$dir/thumb/3/3a/Foobar.jpg",
125 "$dir/thumb/3/3a",
126 "$dir/thumb/3",
127
128 "$dir/0/09/",
129 "$dir/0/",
130
131 "$dir/thumb",
132 "$dir",
133 )
134 );
135 }
136
137 /**
138 * Delete the specified files, if they exist.
139 *
140 * @param $files Array: full paths to files to delete.
141 */
142 private static function deleteFiles( $files ) {
143 foreach ( $files as $file ) {
144 if ( file_exists( $file ) ) {
145 unlink( $file );
146 }
147 }
148 }
149
150 /**
151 * Delete the specified directories, if they exist. Must be empty.
152 *
153 * @param $dirs Array: full paths to directories to delete.
154 */
155 private static function deleteDirs( $dirs ) {
156 foreach ( $dirs as $dir ) {
157 if ( is_dir( $dir ) ) {
158 rmdir( $dir );
159 }
160 }
161 }
162
163 /**
164 * Create a dummy uploads directory which will contain a couple
165 * of files in order to pass existence tests.
166 *
167 * @return String: the directory
168 */
169 private function setupUploadDir() {
170 global $IP;
171
172 if ( $this->keepUploads ) {
173 $dir = wfTempDir() . '/mwParser-images';
174
175 if ( is_dir( $dir ) ) {
176 return $dir;
177 }
178 } else {
179 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
180 }
181
182 wfDebug( "Creating upload directory $dir\n" );
183
184 if ( file_exists( $dir ) ) {
185 wfDebug( "Already exists!\n" );
186
187 return $dir;
188 }
189
190 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
191 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
192
193 wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
194 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
195
196 return $dir;
197 }
198
199 public static function suite() {
200 // Hack to invoke the autoloader required to get phpunit to recognize
201 // the UploadFromUrlTest class
202 class_exists( 'UploadFromUrlTest' );
203 $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
204
205 return $suite;
206 }
207 }