Make tests more portable. Prepping for initial CI use.
[lhc/web/wiklou.git] / maintenance / tests / UploadFromUrlTestSuite.php
1 <?php
2
3 require_once( 'UploadFromUrlTest.php' );
4
5 class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite
6 {
7 public static function addTables( &$tables ) {
8 $tables[] = 'user_properties';
9 $tables[] = 'filearchive';
10 $tables[] = 'logging';
11 $tables[] = 'updatelog';
12 $tables[] = 'iwlinks';
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 $wgNamespacesWithSubpages, $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 'hashLevels' => 2,
36 'transformVia404' => false,
37 );
38 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
39 $wgNamespaceAliases['Image'] = NS_FILE;
40 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
41
42
43 $wgEnableParserCache = false;
44 $wgDeferredUpdateList = array();
45 $wgMemc =& wfGetMainCache();
46 $messageMemc =& wfGetMessageCacheStorage();
47 $parserMemc =& wfGetParserCacheStorage();
48
49 $wgContLang = new StubContLang;
50 $wgUser = new StubUser;
51 $wgLang = new StubUserLang;
52 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
53 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
54 $wgRequest = new WebRequest;
55
56 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
57 array( $messageMemc, $wgUseDatabaseMessages,
58 $wgMsgCacheExpiry, wfWikiID() ) );
59 if ( $wgStyleDirectory === false ) $wgStyleDirectory = "$IP/skins";
60
61 }
62
63 public function tearDown() {
64 $this->teardownUploadDir( $this->uploadDir );
65 }
66
67 private $uploadDir;
68 private $keepUploads;
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 * @param array $files full paths to files to delete.
113 */
114 private static function deleteFiles( $files ) {
115 foreach ( $files as $file ) {
116 if ( file_exists( $file ) ) {
117 unlink( $file );
118 }
119 }
120 }
121 /**
122 * Delete the specified directories, if they exist. Must be empty.
123 * @param array $dirs full paths to directories to delete.
124 */
125 private static function deleteDirs( $dirs ) {
126 foreach ( $dirs as $dir ) {
127 if ( is_dir( $dir ) ) {
128 rmdir( $dir );
129 }
130 }
131 }
132
133 /**
134 * Create a dummy uploads directory which will contain a couple
135 * of files in order to pass existence tests.
136 * @return string The directory
137 */
138 private function setupUploadDir() {
139 global $IP;
140 if ( $this->keepUploads ) {
141 $dir = wfTempDir() . '/mwParser-images';
142 if ( is_dir( $dir ) ) {
143 return $dir;
144 }
145 } else {
146 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
147 }
148
149 wfDebug( "Creating upload directory $dir\n" );
150 if ( file_exists( $dir ) ) {
151 wfDebug( "Already exists!\n" );
152 return $dir;
153 }
154 wfMkdirParents( $dir . '/3/3a' );
155 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
156
157 wfMkdirParents( $dir . '/0/09' );
158 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
159 return $dir;
160 }
161
162 public static function suite()
163 {
164 return new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
165 }
166
167 }
168