247d840181f8797b9663980d593966e57eb33e1c
[lhc/web/wiklou.git] / maintenance / tests / MediaWikiParserTest.php
1 <?php
2
3 global $IP;
4 define( "NO_COMMAND_LINE", 1 );
5 define( "PARSER_TESTS", "$IP/maintenance/parserTests.txt" );
6
7 require_once( "$IP/maintenance/parserTests.inc" );
8
9 class PHPUnitTestRecorder extends TestRecorder {
10
11 function record( $test, $result ) {
12 $this->total++;
13 $this->success += $result;
14
15 }
16
17 function reportPercentage( $success, $total ) {}
18 }
19
20 class MediaWikiParserTestSuite extends PHPUnit_Framework_TestSuite {
21 static private $count;
22 static public $parser;
23 static public $iter;
24
25 public static function addTables(&$tables) {
26 $tables[] = 'user_properties';
27 $tables[] = 'filearchive';
28 $tables[] = 'logging';
29 $tables[] = 'updatelog';
30 return true;
31 }
32
33 public static function suite() {
34 $suite = new PHPUnit_Framework_TestSuite();
35
36 global $wgHooks;
37 $wgHooks['ParserTestTables'][] = "MediaWikiParserTestSuite::addTables";
38
39 self::$iter = new TestFileIterator( PARSER_TESTS );
40
41 foreach(self::$iter as $i => $test) {
42 $suite->addTest(new ParserUnitTest($i, $test['test']));
43 self::$count++;
44 }
45 unset($tests);
46
47 self::$parser = new PTShell;
48 self::$iter->setParser(self::$parser);
49 self::$parser->recorder->start();
50 self::$parser->setupDatabase();
51 self::$iter->rewind();
52 /* } */
53 /* function setUp() { */
54 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
55 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
56 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
57 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
58 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
59 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
60
61 $wgScript = '/index.php';
62 $wgScriptPath = '/';
63 $wgArticlePath = '/wiki/$1';
64 $wgStyleSheetPath = '/skins';
65 $wgStylePath = '/skins';
66 $wgThumbnailScriptPath = false;
67 $wgLocalFileRepo = array(
68 'class' => 'LocalRepo',
69 'name' => 'local',
70 'directory' => 'test-repo',
71 'url' => 'http://example.com/images',
72 'hashLevels' => 2,
73 'transformVia404' => false,
74 );
75 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
76 $wgNamespaceAliases['Image'] = NS_FILE;
77 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
78
79
80 $wgEnableParserCache = false;
81 $wgDeferredUpdateList = array();
82 $wgMemc =& wfGetMainCache();
83 $messageMemc =& wfGetMessageCacheStorage();
84 $parserMemc =& wfGetParserCacheStorage();
85
86 $wgContLang = new StubContLang;
87 $wgUser = new StubUser;
88 $wgLang = new StubUserLang;
89 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
90 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
91 $wgRequest = new WebRequest;
92
93 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
94 array( $messageMemc, $wgUseDatabaseMessages,
95 $wgMsgCacheExpiry, wfWikiID() ) );
96 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
97
98 return $suite;
99 }
100
101 public function tearDown() {
102 /* $this->teardownDatabase(); */
103 $this->recorder->report();
104 $this->recorder->end();
105 $this->teardownUploadDir($this->uploadDir);
106 }
107
108 public function count() {return self::$count;}
109
110 public function toString() {
111 return "MediaWiki Parser Tests";
112 }
113
114
115 private $uploadDir;
116 private $keepUploads;
117 /**
118 * Remove the dummy uploads directory
119 */
120 private function teardownUploadDir( $dir ) {
121 if ( $this->keepUploads ) {
122 return;
123 }
124
125 // delete the files first, then the dirs.
126 self::deleteFiles(
127 array (
128 "$dir/3/3a/Foobar.jpg",
129 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
130 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
131 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
132 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
133
134 "$dir/0/09/Bad.jpg",
135 )
136 );
137
138 self::deleteDirs(
139 array (
140 "$dir/3/3a",
141 "$dir/3",
142 "$dir/thumb/6/65",
143 "$dir/thumb/6",
144 "$dir/thumb/3/3a/Foobar.jpg",
145 "$dir/thumb/3/3a",
146 "$dir/thumb/3",
147
148 "$dir/0/09/",
149 "$dir/0/",
150
151 "$dir/thumb",
152 "$dir",
153 )
154 );
155 }
156
157 /**
158 * Delete the specified files, if they exist.
159 * @param array $files full paths to files to delete.
160 */
161 private static function deleteFiles( $files ) {
162 foreach( $files as $file ) {
163 if( file_exists( $file ) ) {
164 unlink( $file );
165 }
166 }
167 }
168 /**
169 * Delete the specified directories, if they exist. Must be empty.
170 * @param array $dirs full paths to directories to delete.
171 */
172 private static function deleteDirs( $dirs ) {
173 foreach( $dirs as $dir ) {
174 if( is_dir( $dir ) ) {
175 rmdir( $dir );
176 }
177 }
178 }
179
180 /**
181 * Create a dummy uploads directory which will contain a couple
182 * of files in order to pass existence tests.
183 * @return string The directory
184 */
185 private function setupUploadDir() {
186 global $IP;
187 if ( $this->keepUploads ) {
188 $dir = wfTempDir() . '/mwParser-images';
189 if ( is_dir( $dir ) ) {
190 return $dir;
191 }
192 } else {
193 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
194 }
195
196 wfDebug( "Creating upload directory $dir\n" );
197 if ( file_exists( $dir ) ) {
198 wfDebug( "Already exists!\n" );
199 return $dir;
200 }
201 wfMkdirParents( $dir . '/3/3a' );
202 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
203
204 wfMkdirParents( $dir . '/0/09' );
205 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
206 return $dir;
207 }
208 }
209
210 /**
211 * @group Stub
212 */
213 class ParserUnitTest extends PHPUnit_Framework_TestCase {
214 private $number = 0;
215 private $test = "";
216
217 public function testBogus() {
218 $this->markTestSkipped("This is a stub");
219 }
220
221 public function __construct($number = null, $test = null) {
222 $this->number = $number;
223 $this->test = $test;
224 }
225
226 function count() {return 1;}
227
228 public function run(PHPUnit_Framework_TestResult $result = NULL) {
229 PHPUnit_Framework_Assert::resetCount();
230 if ($result === NULL) {
231 $result = new PHPUnit_Framework_TestResult;
232 }
233
234 $t = MediaWikiParserTestSuite::$iter->current();
235 $k = MediaWikiParserTestSuite::$iter->key();
236
237 if(!MediaWikiParserTestSuite::$iter->valid()) {
238 return;
239 }
240
241 // The only way this should happen is if the parserTest.txt
242 // file were modified while the script is running.
243 if($k != $this->number) {
244 $i = $this->number;
245 wfDie("I got confused!\n");
246 }
247
248 $result->startTest($this);
249 PHPUnit_Util_Timer::start();
250
251 $r = false;
252 try {
253 $r = MediaWikiParserTestSuite::$parser->runTest(
254 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
255 );
256 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
257 }
258 catch (PHPUnit_Framework_AssertionFailedError $e) {
259 $result->addFailure($this, $e, PHPUnit_Util_Timer::stop());
260 }
261 catch (Exception $e) {
262 $result->addError($this, $e, PHPUnit_Util_Timer::stop());
263 }
264 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
265
266 $result->endTest($this, PHPUnit_Util_Timer::stop());
267
268 MediaWikiParserTestSuite::$parser->recorder->record($t['test'], $r);
269 MediaWikiParserTestSuite::$iter->next();
270 $this->addToAssertionCount(PHPUnit_Framework_Assert::getCount());
271
272 return $result;
273 }
274
275 }
276
277 class PTShell extends ParserTest {
278 function showTesting( $desc ) {
279 }
280
281 function showRunFile( $path ) {
282 }
283
284 function showSuccess( $desc ) {
285 PHPUnit_Framework_Assert::assertTrue(true, $desc);
286 return true;
287 }
288
289 function showFailure( $desc, $expected, $got ) {
290 PHPUnit_Framework_Assert::assertEquals($expected, $got, $desc);
291 }
292
293 }
294
295