MediaWiki  REL1_19
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 dataProvider 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         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         public 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' => false,
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 
00348                 foreach ( $settings as $var => $val ) {
00349                         if ( array_key_exists( $var, $GLOBALS ) ) {
00350                                 $this->savedGlobals[$var] = $GLOBALS[$var];
00351                         }
00352 
00353                         $GLOBALS[$var] = $val;
00354                 }
00355 
00356                 $langObj = Language::factory( $lang );
00357                 $GLOBALS['wgContLang'] = $langObj;
00358                 $context = new RequestContext();
00359                 $GLOBALS['wgLang'] = $context->getLanguage();
00360 
00361                 $GLOBALS['wgMemc'] = new EmptyBagOStuff;
00362                 $GLOBALS['wgOut'] = $context->getOutput();
00363                 $GLOBALS['wgUser'] = $context->getUser();
00364 
00365                 global $wgHooks;
00366 
00367                 $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
00368                 $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
00369 
00370                 MagicWord::clearCache();
00371                 RepoGroup::destroySingleton();
00372                 FileBackendGroup::destroySingleton();
00373 
00374                 # Create dummy files in storage
00375                 $this->setupUploads();
00376 
00377                 # Publish the articles after we have the final language set
00378                 $this->publishTestArticles();
00379 
00380                 # The entries saved into RepoGroup cache with previous globals will be wrong.
00381                 RepoGroup::destroySingleton();
00382                 FileBackendGroup::destroySingleton();
00383                 MessageCache::singleton()->destroyInstance();
00384 
00385                 return $context;
00386         }
00387 
00393         protected function getUploadDir() {
00394                 if ( $this->keepUploads ) {
00395                         $dir = wfTempDir() . '/mwParser-images';
00396 
00397                         if ( is_dir( $dir ) ) {
00398                                 return $dir;
00399                         }
00400                 } else {
00401                         $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
00402                 }
00403 
00404                 // wfDebug( "Creating upload directory $dir\n" );
00405                 if ( file_exists( $dir ) ) {
00406                         wfDebug( "Already exists!\n" );
00407                         return $dir;
00408                 }
00409 
00410                 return $dir;
00411         }
00412 
00419         protected function setupUploads() {
00420                 global $IP;
00421 
00422                 $base = $this->getBaseDir();
00423                 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
00424                 $backend->prepare( array( 'dir' => "$base/local-public/3/3a" ) );
00425                 $backend->store( array(
00426                         'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/3/3a/Foobar.jpg"
00427                 ) );
00428                 $backend->prepare( array( 'dir' => "$base/local-public/0/09" ) );
00429                 $backend->store( array(
00430                         'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/0/09/Bad.jpg"
00431                 ) );
00432         }
00433 
00438         protected function teardownGlobals() {
00439                 $this->teardownUploads();
00440 
00441                 foreach ( $this->savedGlobals as $var => $val ) {
00442                         $GLOBALS[$var] = $val;
00443                 }
00444 
00445                 RepoGroup::destroySingleton();
00446                 LinkCache::singleton()->clear();
00447         }
00448 
00452         private function teardownUploads() {
00453                 if ( $this->keepUploads ) {
00454                         return;
00455                 }
00456 
00457                 $base = $this->getBaseDir();
00458                 // delete the files first, then the dirs.
00459                 self::deleteFiles(
00460                         array (
00461                                 "$base/local-public/3/3a/Foobar.jpg",
00462                                 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
00463                                 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
00464                                 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
00465                                 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
00466 
00467                                 "$base/local-public/0/09/Bad.jpg",
00468                                 "$base/local-thumb/0/09/Bad.jpg",
00469 
00470                                 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
00471                         )
00472                 );
00473         }
00474 
00479         private static function deleteFiles( $files ) {
00480                 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
00481                 foreach ( $files as $file ) {
00482                         $backend->delete( array( 'src' => $file ), array( 'force' => 1 ) );
00483                 }
00484                 foreach ( $files as $file ) {
00485                         $tmp = $file;
00486                         while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
00487                                 if ( !$backend->clean( array( 'dir' => $tmp ) )->isOK() ) {
00488                                         break;
00489                                 }
00490                         }
00491                 }
00492         }
00493 
00494         protected function getBaseDir() {
00495                 return 'mwstore://local-backend';
00496         }
00497 
00498         public function parserTestProvider() {
00499                 if ( $this->file === false ) {
00500                         global $wgParserTestFiles;
00501                         $this->file = $wgParserTestFiles[0];
00502                 }
00503                 return new TestFileIterator( $this->file, $this );
00504         }
00505 
00509         public function setParserTestFile( $filename ) {
00510                 $this->file = $filename;
00511         }
00512 
00517         public function testParserTest( $desc, $input, $result, $opts, $config ) {
00518                 if ( $this->regex != '' && !preg_match( '/' . $this->regex . '/', $desc ) ) {
00519                         $this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
00520                         //$this->markTestSkipped( 'Filtered out by the user' );
00521                         return;
00522                 }
00523 
00524                 wfDebug( "Running parser test: $desc\n" );
00525 
00526                 $opts = $this->parseOptions( $opts );
00527                 $context = $this->setupGlobals( $opts, $config );
00528 
00529                 $user = $context->getUser();
00530                 $options = ParserOptions::newFromContext( $context );
00531 
00532                 if ( isset( $opts['title'] ) ) {
00533                         $titleText = $opts['title'];
00534                 }
00535                 else {
00536                         $titleText = 'Parser test';
00537                 }
00538 
00539                 $local = isset( $opts['local'] );
00540                 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
00541                 $parser = $this->getParser( $preprocessor );
00542 
00543                 $title = Title::newFromText( $titleText );
00544 
00545                 if ( isset( $opts['pst'] ) ) {
00546                         $out = $parser->preSaveTransform( $input, $title, $user, $options );
00547                 } elseif ( isset( $opts['msg'] ) ) {
00548                         $out = $parser->transformMsg( $input, $options, $title );
00549                 } elseif ( isset( $opts['section'] ) ) {
00550                         $section = $opts['section'];
00551                         $out = $parser->getSection( $input, $section );
00552                 } elseif ( isset( $opts['replace'] ) ) {
00553                         $section = $opts['replace'][0];
00554                         $replace = $opts['replace'][1];
00555                         $out = $parser->replaceSection( $input, $section, $replace );
00556                 } elseif ( isset( $opts['comment'] ) ) {
00557                         $out = Linker::formatComment( $input, $title, $local );
00558                 } elseif ( isset( $opts['preload'] ) ) {
00559                         $out = $parser->getpreloadText( $input, $title, $options );
00560                 } else {
00561                         $output = $parser->parse( $input, $title, $options, true, true, 1337 );
00562                         $out = $output->getText();
00563 
00564                         if ( isset( $opts['showtitle'] ) ) {
00565                                 if ( $output->getTitleText() ) {
00566                                         $title = $output->getTitleText();
00567                                 }
00568 
00569                                 $out = "$title\n$out";
00570                         }
00571 
00572                         if ( isset( $opts['ill'] ) ) {
00573                                 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
00574                         } elseif ( isset( $opts['cat'] ) ) {
00575                                 $outputPage = $context->getOutput();
00576                                 $outputPage->addCategoryLinks( $output->getCategories() );
00577                                 $cats = $outputPage->getCategoryLinks();
00578 
00579                                 if ( isset( $cats['normal'] ) ) {
00580                                         $out = $this->tidy( implode( ' ', $cats['normal'] ) );
00581                                 } else {
00582                                         $out = '';
00583                                 }
00584                         }
00585                         $parser->mPreprocessor = null;
00586 
00587                         $result = $this->tidy( $result );
00588                 }
00589 
00590                 $this->teardownGlobals();
00591 
00592                 $this->assertEquals( $result, $out, $desc );
00593         }
00594 
00603         function testFuzzTests() {
00604                 global $wgParserTestFiles;
00605 
00606                 $files = $wgParserTestFiles;
00607 
00608                 if( $this->getCliArg( 'file=' ) ) {
00609                         $files = array( $this->getCliArg( 'file=' ) );
00610                 }
00611 
00612                 $dict = $this->getFuzzInput( $files );
00613                 $dictSize = strlen( $dict );
00614                 $logMaxLength = log( $this->maxFuzzTestLength );
00615 
00616                 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
00617 
00618                 $user = new User;
00619                 $opts = ParserOptions::newFromUser( $user );
00620                 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
00621 
00622                 $id = 1;
00623 
00624                 while ( true ) {
00625 
00626                         // Generate test input
00627                         mt_srand( ++$this->fuzzSeed );
00628                         $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
00629                         $input = '';
00630 
00631                         while ( strlen( $input ) < $totalLength ) {
00632                                 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
00633                                 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
00634                                 $offset = mt_rand( 0, $dictSize - $hairLength );
00635                                 $input .= substr( $dict, $offset, $hairLength );
00636                         }
00637 
00638                         $this->setupGlobals();
00639                         $parser = $this->getParser();
00640 
00641                         // Run the test
00642                         try {
00643                                 $parser->parse( $input, $title, $opts );
00644                                 $this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
00645                         } catch ( Exception $exception ) {
00646                                 $input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
00647 
00648                                 $this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\nInput: $input_dump\n\nError: {$exception->getMessage()}\n\nBacktrace: {$exception->getTraceAsString()}" );
00649                         }
00650 
00651                         $this->teardownGlobals();
00652                         $parser->__destruct();
00653 
00654                         if ( $id % 100 == 0 ) {
00655                                 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
00656                                 //echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
00657                                 if ( $usage > 90 ) {
00658                                         $ret = "Out of memory:\n";
00659                                         $memStats = $this->getMemoryBreakdown();
00660 
00661                                         foreach ( $memStats as $name => $usage ) {
00662                                                 $ret .= "$name: $usage\n";
00663                                         }
00664 
00665                                         throw new MWException( $ret );
00666                                 }
00667                         }
00668 
00669                         $id++;
00670 
00671                 }
00672         }
00673 
00674         //Various getter functions
00675 
00679         function getFuzzInput( $filenames ) {
00680                 $dict = '';
00681 
00682                 foreach ( $filenames as $filename ) {
00683                         $contents = file_get_contents( $filename );
00684                         preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
00685 
00686                         foreach ( $matches[1] as $match ) {
00687                                 $dict .= $match . "\n";
00688                         }
00689                 }
00690 
00691                 return $dict;
00692         }
00693 
00697         function getMemoryBreakdown() {
00698                 $memStats = array();
00699 
00700                 foreach ( $GLOBALS as $name => $value ) {
00701                         $memStats['$' . $name] = strlen( serialize( $value ) );
00702                 }
00703 
00704                 $classes = get_declared_classes();
00705 
00706                 foreach ( $classes as $class ) {
00707                         $rc = new ReflectionClass( $class );
00708                         $props = $rc->getStaticProperties();
00709                         $memStats[$class] = strlen( serialize( $props ) );
00710                         $methods = $rc->getMethods();
00711 
00712                         foreach ( $methods as $method ) {
00713                                 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
00714                         }
00715                 }
00716 
00717                 $functions = get_defined_functions();
00718 
00719                 foreach ( $functions['user'] as $function ) {
00720                         $rf = new ReflectionFunction( $function );
00721                         $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
00722                 }
00723 
00724                 asort( $memStats );
00725 
00726                 return $memStats;
00727         }
00728 
00732         function getParser( $preprocessor = null ) {
00733                 global $wgParserConf;
00734 
00735                 $class = $wgParserConf['class'];
00736                 $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
00737 
00738                 wfRunHooks( 'ParserTestParser', array( &$parser ) );
00739 
00740                 return $parser;
00741         }
00742 
00743         //Various action functions
00744 
00745         public function addArticle( $name, $text, $line ) {
00746                 self::$articles[$name] = array( $text, $line );
00747         }
00748 
00749         public function publishTestArticles() {
00750                 if ( empty( self::$articles ) ) {
00751                         return;
00752                 }
00753 
00754                 foreach ( self::$articles as $name => $info ) {
00755                         list( $text, $line ) = $info;
00756                         ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
00757                 }
00758         }
00759 
00768         public function requireHook( $name ) {
00769                 global $wgParser;
00770                 $wgParser->firstCallInit( ); // make sure hooks are loaded.
00771                 return isset( $wgParser->mTagHooks[$name] );
00772         }
00773 
00774         public function requireFunctionHook( $name ) {
00775                 global $wgParser;
00776                 $wgParser->firstCallInit( ); // make sure hooks are loaded.
00777                 return isset( $wgParser->mFunctionHooks[$name] );
00778         }
00779         //Various "cleanup" functions
00780 
00788         protected function tidy( $text ) {
00789                 global $wgUseTidy;
00790 
00791                 if ( $wgUseTidy ) {
00792                         $text = MWTidy::tidy( $text );
00793                 }
00794 
00795                 return $text;
00796         }
00797 
00801         public function removeEndingNewline( $s ) {
00802                 if ( substr( $s, -1 ) === "\n" ) {
00803                         return substr( $s, 0, -1 );
00804                 }
00805                 else {
00806                         return $s;
00807                 }
00808         }
00809 
00810         //Test options parser functions
00811 
00812         protected function parseOptions( $instring ) {
00813                 $opts = array();
00814                 // foo
00815                 // foo=bar
00816                 // foo="bar baz"
00817                 // foo=[[bar baz]]
00818                 // foo=bar,"baz quux"
00819                 $regex = '/\b
00820                         ([\w-]+)                                                # Key
00821                         \b
00822                         (?:\s*
00823                                 =                                               # First sub-value
00824                                 \s*
00825                                 (
00826                                         "
00827                                                 [^"]*                   # Quoted val
00828                                         "
00829                                 |
00830                                         \[\[
00831                                                 [^]]*                   # Link target
00832                                         \]\]
00833                                 |
00834                                         [\w-]+                          # Plain word
00835                                 )
00836                                 (?:\s*
00837                                         ,                                       # Sub-vals 1..N
00838                                         \s*
00839                                         (
00840                                                 "[^"]*"                 # Quoted val
00841                                         |
00842                                                 \[\[[^]]*\]\]   # Link target
00843                                         |
00844                                                 [\w-]+                  # Plain word
00845                                         )
00846                                 )*
00847                         )?
00848                         /x';
00849 
00850                 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
00851                         foreach ( $matches as $bits ) {
00852                                 array_shift( $bits );
00853                                 $key = strtolower( array_shift( $bits ) );
00854                                 if ( count( $bits ) == 0 ) {
00855                                         $opts[$key] = true;
00856                                 } elseif ( count( $bits ) == 1 ) {
00857                                         $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
00858                                 } else {
00859                                         // Array!
00860                                         $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
00861                                 }
00862                         }
00863                 }
00864                 return $opts;
00865         }
00866 
00867         protected function cleanupOption( $opt ) {
00868                 if ( substr( $opt, 0, 1 ) == '"' ) {
00869                         return substr( $opt, 1, -1 );
00870                 }
00871 
00872                 if ( substr( $opt, 0, 2 ) == '[[' ) {
00873                         return substr( $opt, 2, -2 );
00874                 }
00875                 return $opt;
00876         }
00877 
00884         protected static function getOptionValue( $key, $opts, $default ) {
00885                 $key = strtolower( $key );
00886 
00887                 if ( isset( $opts[$key] ) ) {
00888                         return $opts[$key];
00889                 } else {
00890                         return $default;
00891                 }
00892         }
00893 }