c9729fbbd3a93a25e13e539b9f90fb2d5afd1c2e
[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 $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 $wgLocalFileRepo = array(
30 'class' => 'LocalRepo',
31 'name' => 'local',
32 'directory' => wfTempDir() . '/test-repo',
33 'url' => 'http://example.com/images',
34 'deletedDir' => wfTempDir() . '/test-repo/delete',
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 User;
51 $context = new RequestContext;
52 $wgLang = $context->getLang();
53 $wgOut = $context->getOutput();
54 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
55 $wgRequest = new WebRequest;
56
57 if ( $wgStyleDirectory === false ) {
58 $wgStyleDirectory = "$IP/skins";
59 }
60
61 }
62
63 public function tearDown() {
64 $this->teardownUploadDir( $this->uploadDir );
65 }
66
67 private $uploadDir;
68 private $keepUploads;
69
70 /**
71 * Remove the dummy uploads directory
72 */
73 private function teardownUploadDir( $dir ) {
74 if ( $this->keepUploads ) {
75 return;
76 }
77
78 // delete the files first, then the dirs.
79 self::deleteFiles(
80 array (
81 "$dir/3/3a/Foobar.jpg",
82 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
83 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
84 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
85 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
86
87 "$dir/0/09/Bad.jpg",
88 )
89 );
90
91 self::deleteDirs(
92 array (
93 "$dir/3/3a",
94 "$dir/3",
95 "$dir/thumb/6/65",
96 "$dir/thumb/6",
97 "$dir/thumb/3/3a/Foobar.jpg",
98 "$dir/thumb/3/3a",
99 "$dir/thumb/3",
100
101 "$dir/0/09/",
102 "$dir/0/",
103
104 "$dir/thumb",
105 "$dir",
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
145 if ( $this->keepUploads ) {
146 $dir = wfTempDir() . '/mwParser-images';
147
148 if ( is_dir( $dir ) ) {
149 return $dir;
150 }
151 } else {
152 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
153 }
154
155 wfDebug( "Creating upload directory $dir\n" );
156
157 if ( file_exists( $dir ) ) {
158 wfDebug( "Already exists!\n" );
159 return $dir;
160 }
161
162 wfMkdirParents( $dir . '/3/3a' );
163 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
164
165 wfMkdirParents( $dir . '/0/09' );
166 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
167
168 return $dir;
169 }
170
171 public static function suite() {
172 // Hack to invoke the autoloader required to get phpunit to recognize
173 // the UploadFromUrlTest class
174 class_exists( 'UploadFromUrlTest' );
175 $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );
176 return $suite;
177 }
178 }