3 require_once( dirname( __FILE__
) . '/NewParserHelpers.php' );
8 class NewParserTest
extends MediaWikiTestCase
{
11 public $keepUploads = false;
12 public $runDisabled = false;
14 public $showProgress = true;
15 public $savedGlobals = array();
16 public $hooks = array();
17 public $functionHooks = array();
21 $wgContLang = Language
::factory( 'en' );
23 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
24 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
25 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
26 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
27 $wgThumbnailScriptPath, $wgScriptPath,
28 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
30 $wgScript = '/index.php';
32 $wgArticlePath = '/wiki/$1';
33 $wgStyleSheetPath = '/skins';
34 $wgStylePath = '/skins';
35 $wgThumbnailScriptPath = false;
36 $wgLocalFileRepo = array(
37 'class' => 'LocalRepo',
39 'directory' => wfTempDir() . '/test-repo',
40 'url' => 'http://example.com/images',
41 'deletedDir' => wfTempDir() . '/test-repo/delete',
43 'transformVia404' => false,
45 $wgNamespaceProtection[NS_MEDIAWIKI
] = 'editinterface';
46 $wgNamespaceAliases['Image'] = NS_FILE
;
47 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK
;
50 $wgEnableParserCache = false;
51 $wgDeferredUpdateList = array();
52 $wgMemc = &wfGetMainCache();
53 $messageMemc = &wfGetMessageCacheStorage();
54 $parserMemc = &wfGetParserCacheStorage();
56 // $wgContLang = new StubContLang;
58 $wgLang = new StubUserLang
;
59 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
60 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
61 $wgRequest = new WebRequest
;
63 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
64 array( $messageMemc, $wgUseDatabaseMessages,
65 $wgMsgCacheExpiry ) );
66 if ( $wgStyleDirectory === false ) {
67 $wgStyleDirectory = "$IP/skins";
72 * Set up the global variables for a consistent environment for each test.
73 * Ideally this should replace the global configuration entirely.
75 protected function setupGlobals( $opts = '', $config = '' ) {
76 # Find out values for some special options.
78 self
::getOptionValue( 'language', $opts, 'en' );
80 self
::getOptionValue( 'variant', $opts, false );
82 self
::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
83 $linkHolderBatchSize =
84 self
::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
87 'wgServer' => 'http://Britney-Spears',
88 'wgScript' => '/index.php',
89 'wgScriptPath' => '/',
90 'wgArticlePath' => '/wiki/$1',
91 'wgActionPaths' => array(),
92 'wgLocalFileRepo' => array(
93 'class' => 'LocalRepo',
95 'directory' => $this->uploadDir
,
96 'url' => 'http://example.com/images',
98 'transformVia404' => false,
100 'wgEnableUploads' => self
::getOptionValue( 'wgEnableUploads', $opts, true ),
101 'wgStylePath' => '/skins',
102 'wgStyleSheetPath' => '/skins',
103 'wgSitename' => 'MediaWiki',
104 'wgLanguageCode' => $lang,
105 'wgDBprefix' => $this->db
->getType() != 'oracle' ?
'unittest_' : 'ut_',
106 'wgRawHtml' => isset( $opts['rawhtml'] ),
108 'wgContLang' => null,
109 'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ),
110 'wgMaxTocLevel' => $maxtoclevel,
111 'wgCapitalLinks' => true,
112 'wgNoFollowLinks' => true,
113 'wgNoFollowDomainExceptions' => array(),
114 'wgThumbnailScriptPath' => false,
115 'wgUseImageResize' => false,
116 'wgUseTeX' => isset( $opts['math'] ),
117 'wgMathDirectory' => $this->uploadDir
. '/math',
118 'wgLocaltimezone' => 'UTC',
119 'wgAllowExternalImages' => true,
120 'wgUseTidy' => false,
121 'wgDefaultLanguageVariant' => $variant,
122 'wgVariantArticlePath' => false,
123 'wgGroupPermissions' => array( '*' => array(
124 'createaccount' => true,
127 'createpage' => true,
128 'createtalk' => true,
130 'wgNamespaceProtection' => array( NS_MEDIAWIKI
=> 'editinterface' ),
131 'wgDefaultExternalStore' => array(),
132 'wgForeignFileRepos' => array(),
133 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
134 'wgExperimentalHtmlIds' => false,
135 'wgExternalLinkTarget' => false,
136 'wgAlwaysUseTidy' => false,
138 'wgWellFormedXml' => true,
139 'wgAllowMicrodataAttributes' => true,
140 'wgAdaptiveMessageCache' => true
144 $configLines = explode( "\n", $config );
146 foreach ( $configLines as $line ) {
147 list( $var, $value ) = explode( '=', $line, 2 );
149 $settings[$var] = eval( "return $value;" ); //???
153 $this->savedGlobals
= array();
155 foreach ( $settings as $var => $val ) {
156 if ( array_key_exists( $var, $GLOBALS ) ) {
157 $this->savedGlobals
[$var] = $GLOBALS[$var];
160 $GLOBALS[$var] = $val;
163 $langObj = Language
::factory( $lang );
164 $GLOBALS['wgLang'] = $langObj;
165 $GLOBALS['wgContLang'] = $langObj;
166 $GLOBALS['wgMemc'] = new FakeMemCachedClient
;
167 $GLOBALS['wgOut'] = new OutputPage
;
171 $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
172 $wgHooks['ParserTestParser'][] = 'ParserTestStaticParserHook::setup';
173 $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
175 MagicWord
::clearCache();
178 $wgUser = new User();
182 * Restore default values and perform any necessary clean-up
183 * after each test runs.
185 protected function teardownGlobals() {
186 RepoGroup
::destroySingleton();
187 LinkCache
::singleton()->clear();
189 foreach ( $this->savedGlobals
as $var => $val ) {
190 $GLOBALS[$var] = $val;
194 function addDBData() {
195 # Hack: insert a few Wikipedia in-project interwiki prefixes,
196 # for testing inter-language links
197 $this->db
->insert( 'interwiki', array(
198 array( 'iw_prefix' => 'wikipedia',
199 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
203 array( 'iw_prefix' => 'meatball',
204 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
208 array( 'iw_prefix' => 'zh',
209 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
213 array( 'iw_prefix' => 'es',
214 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
218 array( 'iw_prefix' => 'fr',
219 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
223 array( 'iw_prefix' => 'ru',
224 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
231 # Update certain things in site_stats
232 $this->db
->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
234 # Reinitialise the LocalisationCache to match the database state
235 Language
::getLocalisationCache()->unloadAll();
237 # Make a new message cache
238 global $wgMessageCache, $wgMemc;
239 $wgMessageCache = new MessageCache( $wgMemc, true, 3600 );
241 $this->uploadDir
= $this->setupUploadDir();
243 $user = User
::newFromId( 0 );
245 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.jpg' ) );
246 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array(
251 'media_type' => MEDIATYPE_BITMAP
,
252 'mime' => 'image/jpeg',
253 'metadata' => serialize( array() ),
254 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
256 ), $this->db
->timestamp( '20010115123500' ), $user );
258 # This image will be blacklisted in [[MediaWiki:Bad image list]]
259 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Bad.jpg' ) );
260 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', array(
265 'media_type' => MEDIATYPE_BITMAP
,
266 'mime' => 'image/jpeg',
267 'metadata' => serialize( array() ),
268 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
270 ), $this->db
->timestamp( '20010115123500' ), $user );
274 public function testParserTests() {
277 //$wgParserTestFiles = array( "$IP/tests/parser/testparserTests.txt" );
279 global $wgParserTestFiles;
281 foreach( $wgParserTestFiles as $file ) {
283 $iter = new ParserTestFileIterator( $file, $this );
285 foreach ( $iter as $t ) {
287 $result = $this->doRunTest( $t['test'], $t['input'], $t['result'], $t['options'], $t['config'] );
289 //$this->recorder->record( $t['test'], $result );
299 * Run a given wikitext input through a freshly-constructed wiki parser,
300 * and compare the output against the expected results.
301 * Prints status and explanatory messages to stdout.
303 * @param $desc String: test's description
304 * @param $input String: wikitext to try rendering
305 * @param $result String: result to output
306 * @param $opts Array: test's options
307 * @param $config String: overrides for global variables, one per line
310 protected function doRunTest( $desc, $input, $result, $opts, $config ) {
312 $opts = $this->parseOptions( $opts );
313 $this->setupGlobals( $opts, $config );
316 $options = ParserOptions
::newFromUser( $user );
318 if ( isset( $opts['title'] ) ) {
319 $titleText = $opts['title'];
322 $titleText = 'Parser test';
325 $local = isset( $opts['local'] );
326 $preprocessor = isset( $opts['preprocessor'] ) ?
$opts['preprocessor'] : null;
327 $parser = $this->getParser( $preprocessor );
328 $title = Title
::newFromText( $titleText );
330 if ( isset( $opts['pst'] ) ) {
331 $out = $parser->preSaveTransform( $input, $title, $user, $options );
332 } elseif ( isset( $opts['msg'] ) ) {
333 $out = $parser->transformMsg( $input, $options );
334 } elseif ( isset( $opts['section'] ) ) {
335 $section = $opts['section'];
336 $out = $parser->getSection( $input, $section );
337 } elseif ( isset( $opts['replace'] ) ) {
338 $section = $opts['replace'][0];
339 $replace = $opts['replace'][1];
340 $out = $parser->replaceSection( $input, $section, $replace );
341 } elseif ( isset( $opts['comment'] ) ) {
342 $linker = $user->getSkin();
343 $out = $linker->formatComment( $input, $title, $local );
344 } elseif ( isset( $opts['preload'] ) ) {
345 $out = $parser->getpreloadText( $input, $title, $options );
347 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
348 $out = $output->getText();
350 if ( isset( $opts['showtitle'] ) ) {
351 if ( $output->getTitleText() ) {
352 $title = $output->getTitleText();
355 $out = "$title\n$out";
358 if ( isset( $opts['ill'] ) ) {
359 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
360 } elseif ( isset( $opts['cat'] ) ) {
363 $wgOut->addCategoryLinks( $output->getCategories() );
364 $cats = $wgOut->getCategoryLinks();
366 if ( isset( $cats['normal'] ) ) {
367 $out = $this->tidy( implode( ' ', $cats['normal'] ) );
373 $result = $this->tidy( $result );
376 $this->teardownGlobals();
378 $this->assertEquals( $result, $out, $desc );
382 * Get a Parser object
384 function getParser( $preprocessor = null ) {
385 global $wgParserConf;
387 $class = $wgParserConf['class'];
388 $parser = new $class( array( 'preprocessorClass' => $preprocessor ) +
$wgParserConf );
390 foreach ( $this->hooks
as $tag => $callback ) {
391 $parser->setHook( $tag, $callback );
394 foreach ( $this->functionHooks
as $tag => $bits ) {
395 list( $callback, $flags ) = $bits;
396 $parser->setFunctionHook( $tag, $callback, $flags );
399 wfRunHooks( 'ParserTestParser', array( &$parser ) );
405 * Run the "tidy" command on text if the $wgUseTidy
408 * @param $text String: the text to tidy
412 protected function tidy( $text ) {
416 $text = MWTidy
::tidy( $text );
424 * Create a dummy uploads directory which will contain a couple
425 * of files in order to pass existence tests.
427 * @return String: the directory
429 protected function setupUploadDir() {
432 if ( $this->keepUploads
) {
433 $dir = wfTempDir() . '/mwParser-images';
435 if ( is_dir( $dir ) ) {
439 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
442 // wfDebug( "Creating upload directory $dir\n" );
443 if ( file_exists( $dir ) ) {
444 wfDebug( "Already exists!\n" );
448 wfMkdirParents( $dir . '/3/3a' );
449 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
450 wfMkdirParents( $dir . '/0/09' );
451 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
457 * Insert a temporary test article
458 * @param $name String: the title, including any prefix
459 * @param $text String: the article text
460 * @param $line Integer: the input line number, for reporting errors
462 public function addArticle( $name, $text, $line = 'unknown' ) {
463 global $wgCapitalLinks;
465 $text = $this->removeEndingNewline($text);
467 $oldCapitalLinks = $wgCapitalLinks;
468 $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637
470 $name = $this->removeEndingNewline( $name );
471 $title = Title
::newFromText( $name );
473 if ( is_null( $title ) ) {
474 wfDie( "invalid title ('$name' => '$title') at line $line\n" );
477 $aid = $title->getArticleID( Title
::GAID_FOR_UPDATE
);
480 debug_print_backtrace();
481 wfDie( "duplicate article '$name' at line $line\n" );
484 $art = new Article( $title );
485 $art->doEdit( $text, '', EDIT_NEW
);
487 $wgCapitalLinks = $oldCapitalLinks;
491 * Remove last character if it is a newline
493 public function removeEndingNewline( $s ) {
494 if ( substr( $s, -1 ) === "\n" ) {
495 return substr( $s, 0, -1 );
503 * Steal a callback function from the primary parser, save it for
504 * application to our scary parser. If the hook is not installed,
505 * abort processing of this file.
507 * @param $name String
508 * @return Bool true if tag hook is present
510 public function requireHook( $name ) {
513 $wgParser->firstCallInit( ); // make sure hooks are loaded.
515 if ( isset( $wgParser->mTagHooks
[$name] ) ) {
516 $this->hooks
[$name] = $wgParser->mTagHooks
[$name];
518 echo " This test suite requires the '$name' hook extension, skipping.\n";
525 protected function parseOptions( $instring ) {
531 // foo=bar,"baz quux"
555 \[\[[^]]*\]\] # Link target
563 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER
) ) {
564 foreach ( $matches as $bits ) {
565 array_shift( $bits );
566 $key = strtolower( array_shift( $bits ) );
567 if ( count( $bits ) == 0 ) {
569 } elseif ( count( $bits ) == 1 ) {
570 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
573 $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
580 protected function cleanupOption( $opt ) {
581 if ( substr( $opt, 0, 1 ) == '"' ) {
582 return substr( $opt, 1, -1 );
585 if ( substr( $opt, 0, 2 ) == '[[' ) {
586 return substr( $opt, 2, -2 );
592 * Use a regex to find out the value of an option
593 * @param $key String: name of option val to retrieve
594 * @param $opts Options array to look in
595 * @param $default Mixed: default value returned if not found
597 protected static function getOptionValue( $key, $opts, $default ) {
598 $key = strtolower( $key );
600 if ( isset( $opts[$key] ) ) {