Moved tests to maintenance - one directory less to care about when configuring access.
[lhc/web/wiklou.git] / maintenance / tests / MediaWikiParserTest.php
1 <?php
2
3 global $IP;
4 define( "NO_COMMAND_LINE", 1 );
5 require_once( "$IP/maintenance/parserTests.inc" );
6 require_once( "ImageFunctions.php" );
7 require_once( "ProxyTools.php" );
8 require_once( "ObjectCache.php" );
9
10 class PTShell extends ParserTest {
11
12 private $cb;
13
14 function setCallback( $cb ) {
15 $this->cb = $cb;
16 }
17
18 function showTesting( $desc ) {
19 }
20
21 function showRunFile( $path ) {
22 }
23
24 function showSuccess( $desc ) {
25 $this->cb->assertTrue( true, $desc );
26 echo "PASSED: $desc\n";
27 return true;
28 }
29
30 function showFailure( $desc, $expected, $got ) {
31 /* $this->cb->assertEquals( $expected, $got, $desc ); */
32 echo "FAILED: $desc\n";
33 echo "got: $got\n";
34 echo "expected: $expected\n";
35 }
36 }
37
38 class MediaWikiParserTest extends PHPUnit_Framework_TestCase {
39 private $parserTester;
40 private $db;
41 private $uploadDir;
42 private $keepUploads;
43
44 /**
45 * Remove the dummy uploads directory
46 */
47 private function teardownUploadDir( $dir ) {
48 if ( $this->keepUploads ) {
49 return;
50 }
51
52 // delete the files first, then the dirs.
53 self::deleteFiles(
54 array (
55 "$dir/3/3a/Foobar.jpg",
56 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
57 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
58 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
59 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
60
61 "$dir/0/09/Bad.jpg",
62 )
63 );
64
65 self::deleteDirs(
66 array (
67 "$dir/3/3a",
68 "$dir/3",
69 "$dir/thumb/6/65",
70 "$dir/thumb/6",
71 "$dir/thumb/3/3a/Foobar.jpg",
72 "$dir/thumb/3/3a",
73 "$dir/thumb/3",
74
75 "$dir/0/09/",
76 "$dir/0/",
77
78 "$dir/thumb",
79 "$dir",
80 )
81 );
82 }
83
84 /**
85 * Delete the specified files, if they exist.
86 * @param array $files full paths to files to delete.
87 */
88 private static function deleteFiles( $files ) {
89 foreach( $files as $file ) {
90 if( file_exists( $file ) ) {
91 unlink( $file );
92 }
93 }
94 }
95 /**
96 * Delete the specified directories, if they exist. Must be empty.
97 * @param array $dirs full paths to directories to delete.
98 */
99 private static function deleteDirs( $dirs ) {
100 foreach( $dirs as $dir ) {
101 if( is_dir( $dir ) ) {
102 rmdir( $dir );
103 }
104 }
105 }
106
107 /**
108 * Create a dummy uploads directory which will contain a couple
109 * of files in order to pass existence tests.
110 * @return string The directory
111 */
112 private function setupUploadDir() {
113 global $IP;
114 if ( $this->keepUploads ) {
115 $dir = wfTempDir() . '/mwParser-images';
116 if ( is_dir( $dir ) ) {
117 return $dir;
118 }
119 } else {
120 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
121 }
122
123 wfDebug( "Creating upload directory $dir\n" );
124 if ( file_exists( $dir ) ) {
125 wfDebug( "Already exists!\n" );
126 return $dir;
127 }
128 wfMkdirParents( $dir . '/3/3a' );
129 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
130
131 wfMkdirParents( $dir . '/0/09' );
132 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
133 return $dir;
134 }
135
136 function setUp() {
137 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
138 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache, $wgMessageCache,
139 $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc, $wgNamespaceAliases, $wgNamespaceProtection,
140 $wgLocalFileRepo, $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
141 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
142
143 $wgScript = '/index.php';
144 $wgScriptPath = '/';
145 $wgArticlePath = '/wiki/$1';
146 $wgStyleSheetPath = '/skins';
147 $wgStylePath = '/skins';
148 $wgThumbnailScriptPath = false;
149 $this->uploadDir = $this->setupUploadDir();
150 $wgLocalFileRepo = array(
151 'class' => 'LocalRepo',
152 'name' => 'local',
153 'directory' => $this->uploadDir,
154 'url' => 'http://example.com/images',
155 'hashLevels' => 2,
156 'transformVia404' => false,
157 );
158 //$wgNamespacesWithSubpages = array( 0 => isset( $opts['subpage'] ) );
159 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
160 $wgNamespaceAliases['Image'] = NS_FILE;
161 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
162
163
164 $wgEnableParserCache = false;
165 $wgDeferredUpdateList = array();
166 $wgMemc =& wfGetMainCache();
167 $messageMemc =& wfGetMessageCacheStorage();
168 $parserMemc =& wfGetParserCacheStorage();
169
170 $wgContLang = new StubContLang;
171 $wgUser = new StubUser;
172 $wgLang = new StubUserLang;
173 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
174 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
175 $wgRequest = new WebRequest;
176
177 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
178 array( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, wfWikiID() ) );
179 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
180
181 $this->parserTester = new PTShell();
182 $this->parserTester->setCallback( $this );
183
184 /* global $wgDBtype, $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBport, $wgDBmwschema, $wgDBts2chema; */
185 /* $this->db['type'] = $wgDBtype; */
186 /* $this->db['server'] = $wgDBserver; */
187 /* $this->db['name'] = $wgDBname; */
188 /* $this->db['user'] = $wgDBuser; */
189 /* $this->db['password'] = $wgDBpassword; */
190 /* $this->db['port'] = $wgDBport; */
191 /* $this->db['mwschema'] = $wgDBmwschema; */
192 /* $this->db['ts2schema'] = $wgDBts2chema; */
193 }
194
195 function tearDown() {
196 $this->teardownUploadDir($this->uploadDir);
197 /* $db = wfGetDB( DB_MASTER ); */
198 /* $db->close(); */
199 /* global $wgDBtype, $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBport, $wgDBmwschema, $wgDBts2chema; */
200
201 /* $wgDBtype = $this->db['type']; */
202 /* $wgDBserver = $this->db['server']; */
203 /* $wgDBname = $this->db['name']; */
204 /* $wgDBuser = $this->db['user']; */
205 /* $wgDBpassword = $this->db['password']; */
206 /* $wgDBport = $this->db['port']; */
207 /* $wgDBmwschema = $this->db['mwschema']; */
208 /* $wgDBts2chema = $this->db['ts2schema']; */
209
210 }
211
212
213 function testParser() {
214 global $IP;
215
216 $this->parserTester->runTestsFromFiles( array( "$IP/maintenance/parserTests.txt" ) );
217 }
218 }
219