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