06bd99779e502d6d028a4781cc9542d06db955be
[lhc/web/wiklou.git] / maintenance / tests / UploadFromUrlTestSuite.php
1 <?php
2
3 class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite
4 {
5 public static function addTables( &$tables ) {
6 $tables[] = 'user_properties';
7 $tables[] = 'filearchive';
8 $tables[] = 'logging';
9 $tables[] = 'updatelog';
10 $tables[] = 'iwlinks';
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 StubUser;
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 ) $wgStyleDirectory = "$IP/skins";
59
60 }
61
62 public function tearDown() {
63 $this->teardownUploadDir( $this->uploadDir );
64 }
65
66 private $uploadDir;
67 private $keepUploads;
68
69 /**
70 * Remove the dummy uploads directory
71 */
72 private function teardownUploadDir( $dir ) {
73 if ( $this->keepUploads ) {
74 return;
75 }
76
77 // delete the files first, then the dirs.
78 self::deleteFiles(
79 array (
80 "$dir/3/3a/Foobar.jpg",
81 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
82 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
83 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
84 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
85
86 "$dir/0/09/Bad.jpg",
87 )
88 );
89
90 self::deleteDirs(
91 array (
92 "$dir/3/3a",
93 "$dir/3",
94 "$dir/thumb/6/65",
95 "$dir/thumb/6",
96 "$dir/thumb/3/3a/Foobar.jpg",
97 "$dir/thumb/3/3a",
98 "$dir/thumb/3",
99
100 "$dir/0/09/",
101 "$dir/0/",
102
103 "$dir/thumb",
104 "$dir",
105 )
106 );
107 }
108
109
110 /**
111 * Delete the specified files, if they exist.
112 *
113 * @param $files Array: full paths to files to delete.
114 */
115 private static function deleteFiles( $files ) {
116 foreach ( $files as $file ) {
117 if ( file_exists( $file ) ) {
118 unlink( $file );
119 }
120 }
121 }
122
123 /**
124 * Delete the specified directories, if they exist. Must be empty.
125 *
126 * @param $dirs Array: full paths to directories to delete.
127 */
128 private static function deleteDirs( $dirs ) {
129 foreach ( $dirs as $dir ) {
130 if ( is_dir( $dir ) ) {
131 rmdir( $dir );
132 }
133 }
134 }
135
136 /**
137 * Create a dummy uploads directory which will contain a couple
138 * of files in order to pass existence tests.
139 *
140 * @return String: the directory
141 */
142 private function setupUploadDir() {
143 global $IP;
144 if ( $this->keepUploads ) {
145 $dir = wfTempDir() . '/mwParser-images';
146 if ( is_dir( $dir ) ) {
147 return $dir;
148 }
149 } else {
150 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
151 }
152
153 wfDebug( "Creating upload directory $dir\n" );
154 if ( file_exists( $dir ) ) {
155 wfDebug( "Already exists!\n" );
156 return $dir;
157 }
158 wfMkdirParents( $dir . '/3/3a' );
159 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
160
161 wfMkdirParents( $dir . '/0/09' );
162 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
163 return $dir;
164 }
165
166 public static function suite()
167 {
168 return new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
169 }
170
171 }
172