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