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