MediaWiki  master
NewParserTest.php
Go to the documentation of this file.
00001 <?php
00002 
00010 class NewParserTest extends MediaWikiTestCase {
00011         static protected $articles = array();   // Array of test articles defined by the tests
00012         /* The data provider is run on a different instance than the test, so it must be static
00013          * When running tests from several files, all tests will see all articles.
00014          */
00015         static protected $backendToUse;
00016 
00017         public $keepUploads = false;
00018         public $runDisabled = false;
00019         public $regex = '';
00020         public $showProgress = true;
00021         public $savedInitialGlobals = array();
00022         public $savedWeirdGlobals = array();
00023         public $savedGlobals = array();
00024         public $hooks = array();
00025         public $functionHooks = array();
00026 
00027         //Fuzz test
00028         public $maxFuzzTestLength = 300;
00029         public $fuzzSeed = 0;
00030         public $memoryLimit = 50;
00031 
00032         protected $file = false;
00033 
00034         protected function setUp() {
00035                 global $wgContLang, $wgNamespaceProtection, $wgNamespaceAliases;
00036                 global $wgHooks, $IP;
00037                 $wgContLang = Language::factory( 'en' );
00038 
00039                 //Setup CLI arguments
00040                 if ( $this->getCliArg( 'regex=' ) ) {
00041                         $this->regex = $this->getCliArg( 'regex=' );
00042                 } else {
00043                         # Matches anything
00044                         $this->regex = '';
00045                 }
00046 
00047                 $this->keepUploads = $this->getCliArg( 'keep-uploads' );
00048 
00049                 $tmpGlobals = array();
00050 
00051                 $tmpGlobals['wgScript'] = '/index.php';
00052                 $tmpGlobals['wgScriptPath'] = '/';
00053                 $tmpGlobals['wgArticlePath'] = '/wiki/$1';
00054                 $tmpGlobals['wgStyleSheetPath'] = '/skins';
00055                 $tmpGlobals['wgStylePath'] = '/skins';
00056                 $tmpGlobals['wgThumbnailScriptPath'] = false;
00057                 $tmpGlobals['wgLocalFileRepo'] = array(
00058                         'class'           => 'LocalRepo',
00059                         'name'            => 'local',
00060                         'url'             => 'http://example.com/images',
00061                         'hashLevels'      => 2,
00062                         'transformVia404' => false,
00063                         'backend'         => 'local-backend'
00064                 );
00065                 $tmpGlobals['wgForeignFileRepos'] = array();
00066                 $tmpGlobals['wgEnableParserCache'] = false;
00067                 $tmpGlobals['wgHooks'] = $wgHooks;
00068                 $tmpGlobals['wgDeferredUpdateList'] = array();
00069                 $tmpGlobals['wgMemc'] = wfGetMainCache();
00070                 $tmpGlobals['messageMemc'] = wfGetMessageCacheStorage();
00071                 $tmpGlobals['parserMemc'] = wfGetParserCacheStorage();
00072 
00073                 // $tmpGlobals['wgContLang'] = new StubContLang;
00074                 $tmpGlobals['wgUser'] = new User;
00075                 $context = new RequestContext();
00076                 $tmpGlobals['wgLang'] = $context->getLanguage();
00077                 $tmpGlobals['wgOut'] = $context->getOutput();
00078                 $tmpGlobals['wgParser'] = new StubObject( 'wgParser', $GLOBALS['wgParserConf']['class'], array( $GLOBALS['wgParserConf'] ) );
00079                 $tmpGlobals['wgRequest'] = $context->getRequest();
00080 
00081                 if ( $GLOBALS['wgStyleDirectory'] === false ) {
00082                         $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
00083                 }
00084 
00085 
00086                 foreach ( $tmpGlobals as $var => $val ) {
00087                         if ( array_key_exists( $var, $GLOBALS ) ) {
00088                                 $this->savedInitialGlobals[$var] = $GLOBALS[$var];
00089                         }
00090 
00091                         $GLOBALS[$var] = $val;
00092                 }
00093 
00094                 $this->savedWeirdGlobals['mw_namespace_protection'] = $wgNamespaceProtection[NS_MEDIAWIKI];
00095                 $this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
00096                 $this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
00097 
00098                 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
00099                 $wgNamespaceAliases['Image'] = NS_FILE;
00100                 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
00101         }
00102 
00103         protected function tearDown() {
00104                 foreach ( $this->savedInitialGlobals as $var => $val ) {
00105                         $GLOBALS[$var] = $val;
00106                 }
00107 
00108                 global $wgNamespaceProtection, $wgNamespaceAliases;
00109 
00110                 $wgNamespaceProtection[NS_MEDIAWIKI] = $this->savedWeirdGlobals['mw_namespace_protection'];
00111                 $wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
00112                 $wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
00113 
00114                 // Restore backends
00115                 RepoGroup::destroySingleton();
00116                 FileBackendGroup::destroySingleton();
00117         }
00118 
00119         function addDBData() {
00120                 $this->tablesUsed[] = 'site_stats';
00121                 $this->tablesUsed[] = 'interwiki';
00122                 # disabled for performance
00123                 #$this->tablesUsed[] = 'image';
00124 
00125                 # Hack: insert a few Wikipedia in-project interwiki prefixes,
00126                 # for testing inter-language links
00127                 $this->db->insert( 'interwiki', array(
00128                         array( 'iw_prefix' => 'wikipedia',
00129                                    'iw_url'    => 'http://en.wikipedia.org/wiki/$1',
00130                                    'iw_api'    => '',
00131                                    'iw_wikiid' => '',
00132                                    'iw_local'  => 0 ),
00133                         array( 'iw_prefix' => 'meatball',
00134                                    'iw_url'    => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
00135                                    'iw_api'    => '',
00136                                    'iw_wikiid' => '',
00137                                    'iw_local'  => 0 ),
00138                         array( 'iw_prefix' => 'zh',
00139                                    'iw_url'    => 'http://zh.wikipedia.org/wiki/$1',
00140                                    'iw_api'    => '',
00141                                    'iw_wikiid' => '',
00142                                    'iw_local'  => 1 ),
00143                         array( 'iw_prefix' => 'es',
00144                                    'iw_url'    => 'http://es.wikipedia.org/wiki/$1',
00145                                    'iw_api'    => '',
00146                                    'iw_wikiid' => '',
00147                                    'iw_local'  => 1 ),
00148                         array( 'iw_prefix' => 'fr',
00149                                    'iw_url'    => 'http://fr.wikipedia.org/wiki/$1',
00150                                    'iw_api'    => '',
00151                                    'iw_wikiid' => '',
00152                                    'iw_local'  => 1 ),
00153                         array( 'iw_prefix' => 'ru',
00154                                    'iw_url'    => 'http://ru.wikipedia.org/wiki/$1',
00155                                    'iw_api'    => '',
00156                                    'iw_wikiid' => '',
00157                                    'iw_local'  => 1 ),
00162                         ), __METHOD__, array( 'IGNORE' )
00163                 );
00164 
00165 
00166                 # Update certain things in site_stats
00167                 $this->db->insert( 'site_stats',
00168                         array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ),
00169                         __METHOD__
00170                 );
00171 
00172                 # Reinitialise the LocalisationCache to match the database state
00173                 Language::getLocalisationCache()->unloadAll();
00174 
00175                 # Clear the message cache
00176                 MessageCache::singleton()->clear();
00177 
00178                 $user = User::newFromId( 0 );
00179                 LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
00180 
00181                 # Upload DB table entries for files.
00182                 # We will upload the actual files later. Note that if anything causes LocalFile::load()
00183                 # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
00184                 # member to false and storing it in cache.
00185                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
00186                 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
00187                         $image->recordUpload2(
00188                                 '', // archive name
00189                                 'Upload of some lame file',
00190                                 'Some lame file',
00191                                 array(
00192                                         'size'        => 12345,
00193                                         'width'       => 1941,
00194                                         'height'      => 220,
00195                                         'bits'        => 24,
00196                                         'media_type'  => MEDIATYPE_BITMAP,
00197                                         'mime'        => 'image/jpeg',
00198                                         'metadata'    => serialize( array() ),
00199                                         'sha1'        => wfBaseConvert( '', 16, 36, 31 ),
00200                                         'fileExists'  => true ),
00201                                 $this->db->timestamp( '20010115123500' ), $user
00202                         );
00203                 }
00204 
00205                 # This image will be blacklisted in [[MediaWiki:Bad image list]]
00206                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
00207                 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
00208                         $image->recordUpload2(
00209                                 '', // archive name
00210                                 'zomgnotcensored',
00211                                 'Borderline image',
00212                                 array(
00213                                         'size'        => 12345,
00214                                         'width'       => 320,
00215                                         'height'      => 240,
00216                                         'bits'        => 24,
00217                                         'media_type'  => MEDIATYPE_BITMAP,
00218                                         'mime'        => 'image/jpeg',
00219                                         'metadata'    => serialize( array() ),
00220                                         'sha1'        => wfBaseConvert( '', 16, 36, 31 ),
00221                                         'fileExists'  => true ),
00222                                 $this->db->timestamp( '20010115123500' ), $user
00223                         );
00224                 }
00225         }
00226 
00227 
00228 
00229 
00230         //ParserTest setup/teardown functions
00231 
00236         protected function setupGlobals( $opts = '', $config = '' ) {
00237                 global $wgFileBackends;
00238                 # Find out values for some special options.
00239                 $lang =
00240                         self::getOptionValue( 'language', $opts, 'en' );
00241                 $variant =
00242                         self::getOptionValue( 'variant', $opts, false );
00243                 $maxtoclevel =
00244                         self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
00245                 $linkHolderBatchSize =
00246                         self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
00247 
00248                 $uploadDir = $this->getUploadDir();
00249                 if ( $this->getCliArg( 'use-filebackend=' ) ) {
00250                         if ( self::$backendToUse ) {
00251                                 $backend = self::$backendToUse;
00252                         } else {
00253                                 $name = $this->getCliArg( 'use-filebackend=' );
00254                                 $useConfig = array();
00255                                 foreach ( $wgFileBackends as $conf ) {
00256                                         if ( $conf['name'] == $name ) {
00257                                                 $useConfig = $conf;
00258                                         }
00259                                 }
00260                                 $useConfig['name'] = 'local-backend'; // swap name
00261                                 $class = $conf['class'];
00262                                 self::$backendToUse = new $class( $useConfig );
00263                                 $backend = self::$backendToUse;
00264                         }
00265                 } else {
00266                         $backend = new FSFileBackend( array(
00267                                 'name'        => 'local-backend',
00268                                 'lockManager' => 'nullLockManager',
00269                                 'containerPaths' => array(
00270                                         'local-public' => "$uploadDir",
00271                                         'local-thumb'  => "$uploadDir/thumb",
00272                                 )
00273                         ) );
00274                 }
00275 
00276                 $settings = array(
00277                         'wgServer' => 'http://Britney-Spears',
00278                         'wgScript' => '/index.php',
00279                         'wgScriptPath' => '/',
00280                         'wgArticlePath' => '/wiki/$1',
00281                         'wgExtensionAssetsPath' => '/extensions',
00282                         'wgActionPaths' => array(),
00283                         'wgLocalFileRepo' => array(
00284                                 'class'           => 'LocalRepo',
00285                                 'name'            => 'local',
00286                                 'url'             => 'http://example.com/images',
00287                                 'hashLevels'      => 2,
00288                                 'transformVia404' => false,
00289                                 'backend'         => $backend
00290                         ),
00291                         'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
00292                         'wgStylePath' => '/skins',
00293                         'wgStyleSheetPath' => '/skins',
00294                         'wgSitename' => 'MediaWiki',
00295                         'wgLanguageCode' => $lang,
00296                         'wgDBprefix' => $this->db->getType() != 'oracle' ? 'unittest_' : 'ut_',
00297                         'wgRawHtml' => isset( $opts['rawhtml'] ),
00298                         'wgLang' => null,
00299                         'wgContLang' => null,
00300                         'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ),
00301                         'wgMaxTocLevel' => $maxtoclevel,
00302                         'wgCapitalLinks' => true,
00303                         'wgNoFollowLinks' => true,
00304                         'wgNoFollowDomainExceptions' => array(),
00305                         'wgThumbnailScriptPath' => false,
00306                         'wgUseImageResize' => true,
00307                         'wgUseTeX' => isset( $opts['math'] ),
00308                         'wgMathDirectory' => $uploadDir . '/math',
00309                         'wgLocaltimezone' => 'UTC',
00310                         'wgAllowExternalImages' => true,
00311                         'wgUseTidy' => false,
00312                         'wgDefaultLanguageVariant' => $variant,
00313                         'wgVariantArticlePath' => false,
00314                         'wgGroupPermissions' => array( '*' => array(
00315                                 'createaccount' => true,
00316                                 'read'          => true,
00317                                 'edit'          => true,
00318                                 'createpage'    => true,
00319                                 'createtalk'    => true,
00320                         ) ),
00321                         'wgNamespaceProtection' => array( NS_MEDIAWIKI => 'editinterface' ),
00322                         'wgDefaultExternalStore' => array(),
00323                         'wgForeignFileRepos' => array(),
00324                         'wgLinkHolderBatchSize' => $linkHolderBatchSize,
00325                         'wgExperimentalHtmlIds' => false,
00326                         'wgExternalLinkTarget' => false,
00327                         'wgAlwaysUseTidy' => false,
00328                         'wgHtml5' => true,
00329                         'wgCleanupPresentationalAttributes' => true,
00330                         'wgWellFormedXml' => true,
00331                         'wgAllowMicrodataAttributes' => true,
00332                         'wgAdaptiveMessageCache' => true,
00333                         'wgUseDatabaseMessages' => true,
00334                 );
00335 
00336                 if ( $config ) {
00337                         $configLines = explode( "\n", $config );
00338 
00339                         foreach ( $configLines as $line ) {
00340                                 list( $var, $value ) = explode( '=', $line, 2 );
00341 
00342                                 $settings[$var] = eval( "return $value;" ); //???
00343                         }
00344                 }
00345 
00346                 $this->savedGlobals = array();
00347 
00349                 wfRunHooks( 'ParserTestGlobals', array( &$settings ) );
00350 
00351                 foreach ( $settings as $var => $val ) {
00352                         if ( array_key_exists( $var, $GLOBALS ) ) {
00353                                 $this->savedGlobals[$var] = $GLOBALS[$var];
00354                         }
00355 
00356                         $GLOBALS[$var] = $val;
00357                 }
00358 
00359                 $langObj = Language::factory( $lang );
00360                 $GLOBALS['wgContLang'] = $langObj;
00361                 $context = new RequestContext();
00362                 $GLOBALS['wgLang'] = $context->getLanguage();
00363 
00364                 $GLOBALS['wgMemc'] = new EmptyBagOStuff;
00365                 $GLOBALS['wgOut'] = $context->getOutput();
00366                 $GLOBALS['wgUser'] = $context->getUser();
00367 
00368                 global $wgHooks;
00369 
00370                 $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
00371                 $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
00372 
00373                 MagicWord::clearCache();
00374                 RepoGroup::destroySingleton();
00375                 FileBackendGroup::destroySingleton();
00376 
00377                 # Create dummy files in storage
00378                 $this->setupUploads();
00379 
00380                 # Publish the articles after we have the final language set
00381                 $this->publishTestArticles();
00382 
00383                 # The entries saved into RepoGroup cache with previous globals will be wrong.
00384                 RepoGroup::destroySingleton();
00385                 FileBackendGroup::destroySingleton();
00386                 MessageCache::destroyInstance();
00387 
00388                 return $context;
00389         }
00390 
00396         protected function getUploadDir() {
00397                 if ( $this->keepUploads ) {
00398                         $dir = wfTempDir() . '/mwParser-images';
00399 
00400                         if ( is_dir( $dir ) ) {
00401                                 return $dir;
00402                         }
00403                 } else {
00404                         $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
00405                 }
00406 
00407                 // wfDebug( "Creating upload directory $dir\n" );
00408                 if ( file_exists( $dir ) ) {
00409                         wfDebug( "Already exists!\n" );
00410                         return $dir;
00411                 }
00412 
00413                 return $dir;
00414         }
00415 
00422         protected function setupUploads() {
00423                 global $IP;
00424 
00425                 $base = $this->getBaseDir();
00426                 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
00427                 $backend->prepare( array( 'dir' => "$base/local-public/3/3a" ) );
00428                 $backend->store( array(
00429                         'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/3/3a/Foobar.jpg"
00430                 ) );
00431                 $backend->prepare( array( 'dir' => "$base/local-public/0/09" ) );
00432                 $backend->store( array(
00433                         'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/0/09/Bad.jpg"
00434                 ) );
00435         }
00436 
00441         protected function teardownGlobals() {
00442                 $this->teardownUploads();
00443 
00444                 foreach ( $this->savedGlobals as $var => $val ) {
00445                         $GLOBALS[$var] = $val;
00446                 }
00447 
00448                 RepoGroup::destroySingleton();
00449                 LinkCache::singleton()->clear();
00450         }
00451 
00455         private function teardownUploads() {
00456                 if ( $this->keepUploads ) {
00457                         return;
00458                 }
00459 
00460                 $base = $this->getBaseDir();
00461                 // delete the files first, then the dirs.
00462                 self::deleteFiles(
00463                         array (
00464                                 "$base/local-public/3/3a/Foobar.jpg",
00465                                 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
00466                                 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
00467                                 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
00468                                 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
00469 
00470                                 "$base/local-public/0/09/Bad.jpg",
00471                                 "$base/local-thumb/0/09/Bad.jpg",
00472 
00473                                 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
00474                         )
00475                 );
00476         }
00477 
00482         private static function deleteFiles( $files ) {
00483                 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
00484                 foreach ( $files as $file ) {
00485                         $backend->delete( array( 'src' => $file ), array( 'force' => 1 ) );
00486                 }
00487                 foreach ( $files as $file ) {
00488                         $tmp = $file;
00489                         while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
00490                                 if ( !$backend->clean( array( 'dir' => $tmp ) )->isOK() ) {
00491                                         break;
00492                                 }
00493                         }
00494                 }
00495         }
00496 
00497         protected function getBaseDir() {
00498                 return 'mwstore://local-backend';
00499         }
00500 
00501         public function parserTestProvider() {
00502                 if ( $this->file === false ) {
00503                         global $wgParserTestFiles;
00504                         $this->file = $wgParserTestFiles[0];
00505                 }
00506                 return new TestFileIterator( $this->file, $this );
00507         }
00508 
00512         public function setParserTestFile( $filename ) {
00513                 $this->file = $filename;
00514         }
00515 
00520         public function testParserTest( $desc, $input, $result, $opts, $config ) {
00521                 if ( $this->regex != '' && !preg_match( '/' . $this->regex . '/', $desc ) ) {
00522                         $this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
00523                         //$this->markTestSkipped( 'Filtered out by the user' );
00524                         return;
00525                 }
00526 
00527                 if ( !$this->isWikitextNS( NS_MAIN ) ) {
00528                         // parser tests frequently assume that the main namespace contains wikitext.
00529                         // @todo: When setting up pages, force the content model. Only skip if
00530                         //        $wgtContentModelUseDB is false.
00531                         $this->markTestSkipped( "Main namespace does not support wikitext,"
00532                                         . "skipping parser test: $desc" );
00533                 }
00534 
00535                 wfDebug( "Running parser test: $desc\n" );
00536 
00537                 $opts = $this->parseOptions( $opts );
00538                 $context = $this->setupGlobals( $opts, $config );
00539 
00540                 $user = $context->getUser();
00541                 $options = ParserOptions::newFromContext( $context );
00542 
00543                 if ( isset( $opts['title'] ) ) {
00544                         $titleText = $opts['title'];
00545                 }
00546                 else {
00547                         $titleText = 'Parser test';
00548                 }
00549 
00550                 $local = isset( $opts['local'] );
00551                 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
00552                 $parser = $this->getParser( $preprocessor );
00553 
00554                 $title = Title::newFromText( $titleText );
00555 
00556                 if ( isset( $opts['pst'] ) ) {
00557                         $out = $parser->preSaveTransform( $input, $title, $user, $options );
00558                 } elseif ( isset( $opts['msg'] ) ) {
00559                         $out = $parser->transformMsg( $input, $options, $title );
00560                 } elseif ( isset( $opts['section'] ) ) {
00561                         $section = $opts['section'];
00562                         $out = $parser->getSection( $input, $section );
00563                 } elseif ( isset( $opts['replace'] ) ) {
00564                         $section = $opts['replace'][0];
00565                         $replace = $opts['replace'][1];
00566                         $out = $parser->replaceSection( $input, $section, $replace );
00567                 } elseif ( isset( $opts['comment'] ) ) {
00568                         $out = Linker::formatComment( $input, $title, $local );
00569                 } elseif ( isset( $opts['preload'] ) ) {
00570                         $out = $parser->getpreloadText( $input, $title, $options );
00571                 } else {
00572                         $output = $parser->parse( $input, $title, $options, true, true, 1337 );
00573                         $out = $output->getText();
00574 
00575                         if ( isset( $opts['showtitle'] ) ) {
00576                                 if ( $output->getTitleText() ) {
00577                                         $title = $output->getTitleText();
00578                                 }
00579 
00580                                 $out = "$title\n$out";
00581                         }
00582 
00583                         if ( isset( $opts['ill'] ) ) {
00584                                 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
00585                         } elseif ( isset( $opts['cat'] ) ) {
00586                                 $outputPage = $context->getOutput();
00587                                 $outputPage->addCategoryLinks( $output->getCategories() );
00588                                 $cats = $outputPage->getCategoryLinks();
00589 
00590                                 if ( isset( $cats['normal'] ) ) {
00591                                         $out = $this->tidy( implode( ' ', $cats['normal'] ) );
00592                                 } else {
00593                                         $out = '';
00594                                 }
00595                         }
00596                         $parser->mPreprocessor = null;
00597 
00598                         $result = $this->tidy( $result );
00599                 }
00600 
00601                 $this->teardownGlobals();
00602 
00603                 $this->assertEquals( $result, $out, $desc );
00604         }
00605 
00614         function testFuzzTests() {
00615                 global $wgParserTestFiles;
00616 
00617                 $files = $wgParserTestFiles;
00618 
00619                 if( $this->getCliArg( 'file=' ) ) {
00620                         $files = array( $this->getCliArg( 'file=' ) );
00621                 }
00622 
00623                 $dict = $this->getFuzzInput( $files );
00624                 $dictSize = strlen( $dict );
00625                 $logMaxLength = log( $this->maxFuzzTestLength );
00626 
00627                 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
00628 
00629                 $user = new User;
00630                 $opts = ParserOptions::newFromUser( $user );
00631                 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
00632 
00633                 $id = 1;
00634 
00635                 while ( true ) {
00636 
00637                         // Generate test input
00638                         mt_srand( ++$this->fuzzSeed );
00639                         $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
00640                         $input = '';
00641 
00642                         while ( strlen( $input ) < $totalLength ) {
00643                                 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
00644                                 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
00645                                 $offset = mt_rand( 0, $dictSize - $hairLength );
00646                                 $input .= substr( $dict, $offset, $hairLength );
00647                         }
00648 
00649                         $this->setupGlobals();
00650                         $parser = $this->getParser();
00651 
00652                         // Run the test
00653                         try {
00654                                 $parser->parse( $input, $title, $opts );
00655                                 $this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
00656                         } catch ( Exception $exception ) {
00657                                 $input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
00658 
00659                                 $this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\nInput: $input_dump\n\nError: {$exception->getMessage()}\n\nBacktrace: {$exception->getTraceAsString()}" );
00660                         }
00661 
00662                         $this->teardownGlobals();
00663                         $parser->__destruct();
00664 
00665                         if ( $id % 100 == 0 ) {
00666                                 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
00667                                 //echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
00668                                 if ( $usage > 90 ) {
00669                                         $ret = "Out of memory:\n";
00670                                         $memStats = $this->getMemoryBreakdown();
00671 
00672                                         foreach ( $memStats as $name => $usage ) {
00673                                                 $ret .= "$name: $usage\n";
00674                                         }
00675 
00676                                         throw new MWException( $ret );
00677                                 }
00678                         }
00679 
00680                         $id++;
00681 
00682                 }
00683         }
00684 
00685         //Various getter functions
00686 
00690         function getFuzzInput( $filenames ) {
00691                 $dict = '';
00692 
00693                 foreach ( $filenames as $filename ) {
00694                         $contents = file_get_contents( $filename );
00695                         preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
00696 
00697                         foreach ( $matches[1] as $match ) {
00698                                 $dict .= $match . "\n";
00699                         }
00700                 }
00701 
00702                 return $dict;
00703         }
00704 
00708         function getMemoryBreakdown() {
00709                 $memStats = array();
00710 
00711                 foreach ( $GLOBALS as $name => $value ) {
00712                         $memStats['$' . $name] = strlen( serialize( $value ) );
00713                 }
00714 
00715                 $classes = get_declared_classes();
00716 
00717                 foreach ( $classes as $class ) {
00718                         $rc = new ReflectionClass( $class );
00719                         $props = $rc->getStaticProperties();
00720                         $memStats[$class] = strlen( serialize( $props ) );
00721                         $methods = $rc->getMethods();
00722 
00723                         foreach ( $methods as $method ) {
00724                                 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
00725                         }
00726                 }
00727 
00728                 $functions = get_defined_functions();
00729 
00730                 foreach ( $functions['user'] as $function ) {
00731                         $rf = new ReflectionFunction( $function );
00732                         $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
00733                 }
00734 
00735                 asort( $memStats );
00736 
00737                 return $memStats;
00738         }
00739 
00743         function getParser( $preprocessor = null ) {
00744                 global $wgParserConf;
00745 
00746                 $class = $wgParserConf['class'];
00747                 $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
00748 
00749                 wfRunHooks( 'ParserTestParser', array( &$parser ) );
00750 
00751                 return $parser;
00752         }
00753 
00754         //Various action functions
00755 
00756         public function addArticle( $name, $text, $line ) {
00757                 self::$articles[$name] = array( $text, $line );
00758         }
00759 
00760         public function publishTestArticles() {
00761                 if ( empty( self::$articles ) ) {
00762                         return;
00763                 }
00764 
00765                 foreach ( self::$articles as $name => $info ) {
00766                         list( $text, $line ) = $info;
00767                         ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
00768                 }
00769         }
00770 
00779         public function requireHook( $name ) {
00780                 global $wgParser;
00781                 $wgParser->firstCallInit( ); // make sure hooks are loaded.
00782                 return isset( $wgParser->mTagHooks[$name] );
00783         }
00784 
00785         public function requireFunctionHook( $name ) {
00786                 global $wgParser;
00787                 $wgParser->firstCallInit( ); // make sure hooks are loaded.
00788                 return isset( $wgParser->mFunctionHooks[$name] );
00789         }
00790         //Various "cleanup" functions
00791 
00799         protected function tidy( $text ) {
00800                 global $wgUseTidy;
00801 
00802                 if ( $wgUseTidy ) {
00803                         $text = MWTidy::tidy( $text );
00804                 }
00805 
00806                 return $text;
00807         }
00808 
00812         public function removeEndingNewline( $s ) {
00813                 if ( substr( $s, -1 ) === "\n" ) {
00814                         return substr( $s, 0, -1 );
00815                 }
00816                 else {
00817                         return $s;
00818                 }
00819         }
00820 
00821         //Test options parser functions
00822 
00823         protected function parseOptions( $instring ) {
00824                 $opts = array();
00825                 // foo
00826                 // foo=bar
00827                 // foo="bar baz"
00828                 // foo=[[bar baz]]
00829                 // foo=bar,"baz quux"
00830                 $regex = '/\b
00831                         ([\w-]+)                                                # Key
00832                         \b
00833                         (?:\s*
00834                                 =                                               # First sub-value
00835                                 \s*
00836                                 (
00837                                         "
00838                                                 [^"]*                   # Quoted val
00839                                         "
00840                                 |
00841                                         \[\[
00842                                                 [^]]*                   # Link target
00843                                         \]\]
00844                                 |
00845                                         [\w-]+                          # Plain word
00846                                 )
00847                                 (?:\s*
00848                                         ,                                       # Sub-vals 1..N
00849                                         \s*
00850                                         (
00851                                                 "[^"]*"                 # Quoted val
00852                                         |
00853                                                 \[\[[^]]*\]\]   # Link target
00854                                         |
00855                                                 [\w-]+                  # Plain word
00856                                         )
00857                                 )*
00858                         )?
00859                         /x';
00860 
00861                 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
00862                         foreach ( $matches as $bits ) {
00863                                 array_shift( $bits );
00864                                 $key = strtolower( array_shift( $bits ) );
00865                                 if ( count( $bits ) == 0 ) {
00866                                         $opts[$key] = true;
00867                                 } elseif ( count( $bits ) == 1 ) {
00868                                         $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
00869                                 } else {
00870                                         // Array!
00871                                         $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
00872                                 }
00873                         }
00874                 }
00875                 return $opts;
00876         }
00877 
00878         protected function cleanupOption( $opt ) {
00879                 if ( substr( $opt, 0, 1 ) == '"' ) {
00880                         return substr( $opt, 1, -1 );
00881                 }
00882 
00883                 if ( substr( $opt, 0, 2 ) == '[[' ) {
00884                         return substr( $opt, 2, -2 );
00885                 }
00886                 return $opt;
00887         }
00888 
00895         protected static function getOptionValue( $key, $opts, $default ) {
00896                 $key = strtolower( $key );
00897 
00898                 if ( isset( $opts[$key] ) ) {
00899                         return $opts[$key];
00900                 } else {
00901                         return $default;
00902                 }
00903         }
00904 }