Index: trunk/phase3/tests/phpunit/MediaWikiTestCase.php |
— | — | @@ -107,6 +107,7 @@ |
108 | 108 | $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_'; |
109 | 109 | |
110 | 110 | $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix ); |
| 111 | + $this->dbClone->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed |
111 | 112 | $this->dbClone->cloneTableStructure(); |
112 | 113 | |
113 | 114 | if ( $dbType == 'oracle' ) |
Index: trunk/phase3/tests/phpunit/includes/parser/NewParserHelpers.php |
— | — | @@ -0,0 +1,199 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class ParserTestFileIterator implements Iterator { |
| 5 | + |
| 6 | + protected $file; |
| 7 | + protected $fh; |
| 8 | + protected $parserTest; /* An instance of ParserTest (parserTests.php) or MediaWikiParserTest (phpunit) */ |
| 9 | + protected $index = 0; |
| 10 | + protected $test; |
| 11 | + protected $lineNum; |
| 12 | + protected $eof; |
| 13 | + |
| 14 | + function __construct( $file, $parserTest ) { |
| 15 | + global $IP; |
| 16 | + |
| 17 | + $this->file = $file; |
| 18 | + $this->fh = fopen( $this->file, "rt" ); |
| 19 | + |
| 20 | + if ( !$this->fh ) { |
| 21 | + wfDie( "Couldn't open file '$file'\n" ); |
| 22 | + } |
| 23 | + |
| 24 | + $this->parserTest = $parserTest; |
| 25 | + //$this->parserTest->showRunFile( wfRelativePath( $this->file, $IP ) ); |
| 26 | + |
| 27 | + $this->lineNum = $this->index = 0; |
| 28 | + } |
| 29 | + |
| 30 | + function rewind() { |
| 31 | + if ( fseek( $this->fh, 0 ) ) { |
| 32 | + wfDie( "Couldn't fseek to the start of '$this->file'\n" ); |
| 33 | + } |
| 34 | + |
| 35 | + $this->index = -1; |
| 36 | + $this->lineNum = 0; |
| 37 | + $this->eof = false; |
| 38 | + $this->next(); |
| 39 | + |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + function current() { |
| 44 | + return $this->test; |
| 45 | + } |
| 46 | + |
| 47 | + function key() { |
| 48 | + return $this->index; |
| 49 | + } |
| 50 | + |
| 51 | + function next() { |
| 52 | + if ( $this->readNextTest() ) { |
| 53 | + $this->index++; |
| 54 | + return true; |
| 55 | + } else { |
| 56 | + $this->eof = true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + function valid() { |
| 61 | + return $this->eof != true; |
| 62 | + } |
| 63 | + |
| 64 | + function readNextTest() { |
| 65 | + $data = array(); |
| 66 | + $section = null; |
| 67 | + |
| 68 | + while ( false !== ( $line = fgets( $this->fh ) ) ) { |
| 69 | + $this->lineNum++; |
| 70 | + $matches = array(); |
| 71 | + |
| 72 | + if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) { |
| 73 | + $section = strtolower( $matches[1] ); |
| 74 | + |
| 75 | + if ( $section == 'endarticle' ) { |
| 76 | + if ( !isset( $data['text'] ) ) { |
| 77 | + wfDie( "'endarticle' without 'text' at line {$this->lineNum} of $this->file\n" ); |
| 78 | + } |
| 79 | + |
| 80 | + if ( !isset( $data['article'] ) ) { |
| 81 | + wfDie( "'endarticle' without 'article' at line {$this->lineNum} of $this->file\n" ); |
| 82 | + } |
| 83 | + |
| 84 | + $this->parserTest->addArticle( $this->parserTest->removeEndingNewline( $data['article'] ), $data['text'], $this->lineNum ); |
| 85 | + |
| 86 | + |
| 87 | + $data = array(); |
| 88 | + $section = null; |
| 89 | + |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if ( $section == 'endhooks' ) { |
| 94 | + if ( !isset( $data['hooks'] ) ) { |
| 95 | + wfDie( "'endhooks' without 'hooks' at line {$this->lineNum} of $this->file\n" ); |
| 96 | + } |
| 97 | + |
| 98 | + foreach ( explode( "\n", $data['hooks'] ) as $line ) { |
| 99 | + $line = trim( $line ); |
| 100 | + |
| 101 | + if ( $line ) { |
| 102 | + if ( !$this->parserTest->requireHook( $line ) ) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + $data = array(); |
| 109 | + $section = null; |
| 110 | + |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + if ( $section == 'endfunctionhooks' ) { |
| 115 | + if ( !isset( $data['functionhooks'] ) ) { |
| 116 | + wfDie( "'endfunctionhooks' without 'functionhooks' at line {$this->lineNum} of $this->file\n" ); |
| 117 | + } |
| 118 | + |
| 119 | + foreach ( explode( "\n", $data['functionhooks'] ) as $line ) { |
| 120 | + $line = trim( $line ); |
| 121 | + |
| 122 | + if ( $line ) { |
| 123 | + if ( !$this->parserTest->requireFunctionHook( $line ) ) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + $data = array(); |
| 130 | + $section = null; |
| 131 | + |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if ( $section == 'end' ) { |
| 136 | + if ( !isset( $data['test'] ) ) { |
| 137 | + wfDie( "'end' without 'test' at line {$this->lineNum} of $this->file\n" ); |
| 138 | + } |
| 139 | + |
| 140 | + if ( !isset( $data['input'] ) ) { |
| 141 | + wfDie( "'end' without 'input' at line {$this->lineNum} of $this->file\n" ); |
| 142 | + } |
| 143 | + |
| 144 | + if ( !isset( $data['result'] ) ) { |
| 145 | + wfDie( "'end' without 'result' at line {$this->lineNum} of $this->file\n" ); |
| 146 | + } |
| 147 | + |
| 148 | + if ( !isset( $data['options'] ) ) { |
| 149 | + $data['options'] = ''; |
| 150 | + } |
| 151 | + |
| 152 | + if ( !isset( $data['config'] ) ) |
| 153 | + $data['config'] = ''; |
| 154 | + |
| 155 | + if ( ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->parserTest->runDisabled ) |
| 156 | + || !preg_match( "/" . $this->parserTest->regex . "/i", $data['test'] ) ) { |
| 157 | + # disabled test |
| 158 | + $data = array(); |
| 159 | + $section = null; |
| 160 | + |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + global $wgUseTeX; |
| 165 | + |
| 166 | + if ( preg_match( '/\\bmath\\b/i', $data['options'] ) && !$wgUseTeX ) { |
| 167 | + # don't run math tests if $wgUseTeX is set to false in LocalSettings |
| 168 | + $data = array(); |
| 169 | + $section = null; |
| 170 | + |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + $this->test = array( |
| 175 | + 'test' => $this->parserTest->removeEndingNewline( $data['test'] ), |
| 176 | + 'input' => $this->parserTest->removeEndingNewline( $data['input'] ), |
| 177 | + 'result' => $this->parserTest->removeEndingNewline( $data['result'] ), |
| 178 | + 'options' => $this->parserTest->removeEndingNewline( $data['options'] ), |
| 179 | + 'config' => $this->parserTest->removeEndingNewline( $data['config'] ) ); |
| 180 | + |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + if ( isset ( $data[$section] ) ) { |
| 185 | + wfDie( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" ); |
| 186 | + } |
| 187 | + |
| 188 | + $data[$section] = ''; |
| 189 | + |
| 190 | + continue; |
| 191 | + } |
| 192 | + |
| 193 | + if ( $section ) { |
| 194 | + $data[$section] .= $line; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return false; |
| 199 | + } |
| 200 | +} |
\ No newline at end of file |
Index: trunk/phase3/tests/phpunit/includes/parser/NewParserTest.php |
— | — | @@ -0,0 +1,607 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +require_once( dirname( __FILE__ ) . '/NewParserHelpers.php' ); |
| 5 | + |
| 6 | +/** |
| 7 | + * @group Database |
| 8 | + */ |
| 9 | +class NewParserTest extends MediaWikiTestCase { |
| 10 | + |
| 11 | + public $uploadDir; |
| 12 | + public $keepUploads = false; |
| 13 | + public $runDisabled = false; |
| 14 | + public $regex = ''; |
| 15 | + public $showProgress = true; |
| 16 | + public $savedGlobals = array(); |
| 17 | + public $hooks = array(); |
| 18 | + public $functionHooks = array(); |
| 19 | + |
| 20 | + function setUp() { |
| 21 | + global $wgContLang; |
| 22 | + $wgContLang = Language::factory( 'en' ); |
| 23 | + |
| 24 | + global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList, |
| 25 | + $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache, |
| 26 | + $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc, |
| 27 | + $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo, |
| 28 | + $wgThumbnailScriptPath, $wgScriptPath, |
| 29 | + $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath; |
| 30 | + |
| 31 | + $wgScript = '/index.php'; |
| 32 | + $wgScriptPath = '/'; |
| 33 | + $wgArticlePath = '/wiki/$1'; |
| 34 | + $wgStyleSheetPath = '/skins'; |
| 35 | + $wgStylePath = '/skins'; |
| 36 | + $wgThumbnailScriptPath = false; |
| 37 | + $wgLocalFileRepo = array( |
| 38 | + 'class' => 'LocalRepo', |
| 39 | + 'name' => 'local', |
| 40 | + 'directory' => wfTempDir() . '/test-repo', |
| 41 | + 'url' => 'http://example.com/images', |
| 42 | + 'deletedDir' => wfTempDir() . '/test-repo/delete', |
| 43 | + 'hashLevels' => 2, |
| 44 | + 'transformVia404' => false, |
| 45 | + ); |
| 46 | + $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface'; |
| 47 | + $wgNamespaceAliases['Image'] = NS_FILE; |
| 48 | + $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK; |
| 49 | + |
| 50 | + |
| 51 | + $wgEnableParserCache = false; |
| 52 | + $wgDeferredUpdateList = array(); |
| 53 | + $wgMemc = &wfGetMainCache(); |
| 54 | + $messageMemc = &wfGetMessageCacheStorage(); |
| 55 | + $parserMemc = &wfGetParserCacheStorage(); |
| 56 | + |
| 57 | + // $wgContLang = new StubContLang; |
| 58 | + $wgUser = new User; |
| 59 | + $wgLang = new StubUserLang; |
| 60 | + $wgOut = new StubObject( 'wgOut', 'OutputPage' ); |
| 61 | + $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) ); |
| 62 | + $wgRequest = new WebRequest; |
| 63 | + |
| 64 | + $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache', |
| 65 | + array( $messageMemc, $wgUseDatabaseMessages, |
| 66 | + $wgMsgCacheExpiry ) ); |
| 67 | + if ( $wgStyleDirectory === false ) { |
| 68 | + $wgStyleDirectory = "$IP/skins"; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Set up the global variables for a consistent environment for each test. |
| 74 | + * Ideally this should replace the global configuration entirely. |
| 75 | + */ |
| 76 | + protected function setupGlobals( $opts = '', $config = '' ) { |
| 77 | + # Find out values for some special options. |
| 78 | + $lang = |
| 79 | + self::getOptionValue( 'language', $opts, 'en' ); |
| 80 | + $variant = |
| 81 | + self::getOptionValue( 'variant', $opts, false ); |
| 82 | + $maxtoclevel = |
| 83 | + self::getOptionValue( 'wgMaxTocLevel', $opts, 999 ); |
| 84 | + $linkHolderBatchSize = |
| 85 | + self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 ); |
| 86 | + |
| 87 | + $settings = array( |
| 88 | + 'wgServer' => 'http://Britney-Spears', |
| 89 | + 'wgScript' => '/index.php', |
| 90 | + 'wgScriptPath' => '/', |
| 91 | + 'wgArticlePath' => '/wiki/$1', |
| 92 | + 'wgActionPaths' => array(), |
| 93 | + 'wgLocalFileRepo' => array( |
| 94 | + 'class' => 'LocalRepo', |
| 95 | + 'name' => 'local', |
| 96 | + 'directory' => $this->uploadDir, |
| 97 | + 'url' => 'http://example.com/images', |
| 98 | + 'hashLevels' => 2, |
| 99 | + 'transformVia404' => false, |
| 100 | + ), |
| 101 | + 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ), |
| 102 | + 'wgStylePath' => '/skins', |
| 103 | + 'wgStyleSheetPath' => '/skins', |
| 104 | + 'wgSitename' => 'MediaWiki', |
| 105 | + 'wgLanguageCode' => $lang, |
| 106 | + 'wgDBprefix' => $this->db->getType() != 'oracle' ? 'unittest_' : 'ut_', |
| 107 | + 'wgRawHtml' => isset( $opts['rawhtml'] ), |
| 108 | + 'wgLang' => null, |
| 109 | + 'wgContLang' => null, |
| 110 | + 'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ), |
| 111 | + 'wgMaxTocLevel' => $maxtoclevel, |
| 112 | + 'wgCapitalLinks' => true, |
| 113 | + 'wgNoFollowLinks' => true, |
| 114 | + 'wgNoFollowDomainExceptions' => array(), |
| 115 | + 'wgThumbnailScriptPath' => false, |
| 116 | + 'wgUseImageResize' => false, |
| 117 | + 'wgUseTeX' => isset( $opts['math'] ), |
| 118 | + 'wgMathDirectory' => $this->uploadDir . '/math', |
| 119 | + 'wgLocaltimezone' => 'UTC', |
| 120 | + 'wgAllowExternalImages' => true, |
| 121 | + 'wgUseTidy' => false, |
| 122 | + 'wgDefaultLanguageVariant' => $variant, |
| 123 | + 'wgVariantArticlePath' => false, |
| 124 | + 'wgGroupPermissions' => array( '*' => array( |
| 125 | + 'createaccount' => true, |
| 126 | + 'read' => true, |
| 127 | + 'edit' => true, |
| 128 | + 'createpage' => true, |
| 129 | + 'createtalk' => true, |
| 130 | + ) ), |
| 131 | + 'wgNamespaceProtection' => array( NS_MEDIAWIKI => 'editinterface' ), |
| 132 | + 'wgDefaultExternalStore' => array(), |
| 133 | + 'wgForeignFileRepos' => array(), |
| 134 | + 'wgLinkHolderBatchSize' => $linkHolderBatchSize, |
| 135 | + 'wgExperimentalHtmlIds' => false, |
| 136 | + 'wgExternalLinkTarget' => false, |
| 137 | + 'wgAlwaysUseTidy' => false, |
| 138 | + 'wgHtml5' => true, |
| 139 | + 'wgWellFormedXml' => true, |
| 140 | + 'wgAllowMicrodataAttributes' => true, |
| 141 | + 'wgAdaptiveMessageCache' => true |
| 142 | + ); |
| 143 | + |
| 144 | + if ( $config ) { |
| 145 | + $configLines = explode( "\n", $config ); |
| 146 | + |
| 147 | + foreach ( $configLines as $line ) { |
| 148 | + list( $var, $value ) = explode( '=', $line, 2 ); |
| 149 | + |
| 150 | + $settings[$var] = eval( "return $value;" ); //??? |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + $this->savedGlobals = array(); |
| 155 | + |
| 156 | + foreach ( $settings as $var => $val ) { |
| 157 | + if ( array_key_exists( $var, $GLOBALS ) ) { |
| 158 | + $this->savedGlobals[$var] = $GLOBALS[$var]; |
| 159 | + } |
| 160 | + |
| 161 | + $GLOBALS[$var] = $val; |
| 162 | + } |
| 163 | + |
| 164 | + $langObj = Language::factory( $lang ); |
| 165 | + $GLOBALS['wgLang'] = $langObj; |
| 166 | + $GLOBALS['wgContLang'] = $langObj; |
| 167 | + $GLOBALS['wgMemc'] = new FakeMemCachedClient; |
| 168 | + $GLOBALS['wgOut'] = new OutputPage; |
| 169 | + |
| 170 | + global $wgHooks; |
| 171 | + |
| 172 | + $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup'; |
| 173 | + $wgHooks['ParserTestParser'][] = 'ParserTestStaticParserHook::setup'; |
| 174 | + $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp'; |
| 175 | + |
| 176 | + MagicWord::clearCache(); |
| 177 | + |
| 178 | + global $wgUser; |
| 179 | + $wgUser = new User(); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Restore default values and perform any necessary clean-up |
| 184 | + * after each test runs. |
| 185 | + */ |
| 186 | + protected function teardownGlobals() { |
| 187 | + RepoGroup::destroySingleton(); |
| 188 | + LinkCache::singleton()->clear(); |
| 189 | + |
| 190 | + foreach ( $this->savedGlobals as $var => $val ) { |
| 191 | + $GLOBALS[$var] = $val; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + function addDBData() { |
| 196 | + # Hack: insert a few Wikipedia in-project interwiki prefixes, |
| 197 | + # for testing inter-language links |
| 198 | + $this->db->insert( 'interwiki', array( |
| 199 | + array( 'iw_prefix' => 'wikipedia', |
| 200 | + 'iw_url' => 'http://en.wikipedia.org/wiki/$1', |
| 201 | + 'iw_api' => '', |
| 202 | + 'iw_wikiid' => '', |
| 203 | + 'iw_local' => 0 ), |
| 204 | + array( 'iw_prefix' => 'meatball', |
| 205 | + 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1', |
| 206 | + 'iw_api' => '', |
| 207 | + 'iw_wikiid' => '', |
| 208 | + 'iw_local' => 0 ), |
| 209 | + array( 'iw_prefix' => 'zh', |
| 210 | + 'iw_url' => 'http://zh.wikipedia.org/wiki/$1', |
| 211 | + 'iw_api' => '', |
| 212 | + 'iw_wikiid' => '', |
| 213 | + 'iw_local' => 1 ), |
| 214 | + array( 'iw_prefix' => 'es', |
| 215 | + 'iw_url' => 'http://es.wikipedia.org/wiki/$1', |
| 216 | + 'iw_api' => '', |
| 217 | + 'iw_wikiid' => '', |
| 218 | + 'iw_local' => 1 ), |
| 219 | + array( 'iw_prefix' => 'fr', |
| 220 | + 'iw_url' => 'http://fr.wikipedia.org/wiki/$1', |
| 221 | + 'iw_api' => '', |
| 222 | + 'iw_wikiid' => '', |
| 223 | + 'iw_local' => 1 ), |
| 224 | + array( 'iw_prefix' => 'ru', |
| 225 | + 'iw_url' => 'http://ru.wikipedia.org/wiki/$1', |
| 226 | + 'iw_api' => '', |
| 227 | + 'iw_wikiid' => '', |
| 228 | + 'iw_local' => 1 ), |
| 229 | + ) ); |
| 230 | + |
| 231 | + |
| 232 | + # Update certain things in site_stats |
| 233 | + $this->db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) ); |
| 234 | + |
| 235 | + # Reinitialise the LocalisationCache to match the database state |
| 236 | + Language::getLocalisationCache()->unloadAll(); |
| 237 | + |
| 238 | + # Make a new message cache |
| 239 | + global $wgMessageCache, $wgMemc; |
| 240 | + $wgMessageCache = new MessageCache( $wgMemc, true, 3600 ); |
| 241 | + |
| 242 | + $this->uploadDir = $this->setupUploadDir(); |
| 243 | + |
| 244 | + $user = User::newFromId( 0 ); |
| 245 | + |
| 246 | + $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) ); |
| 247 | + $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array( |
| 248 | + 'size' => 12345, |
| 249 | + 'width' => 1941, |
| 250 | + 'height' => 220, |
| 251 | + 'bits' => 24, |
| 252 | + 'media_type' => MEDIATYPE_BITMAP, |
| 253 | + 'mime' => 'image/jpeg', |
| 254 | + 'metadata' => serialize( array() ), |
| 255 | + 'sha1' => wfBaseConvert( '', 16, 36, 31 ), |
| 256 | + 'fileExists' => true |
| 257 | + ), $this->db->timestamp( '20010115123500' ), $user ); |
| 258 | + |
| 259 | + # This image will be blacklisted in [[MediaWiki:Bad image list]] |
| 260 | + $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) ); |
| 261 | + $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', array( |
| 262 | + 'size' => 12345, |
| 263 | + 'width' => 320, |
| 264 | + 'height' => 240, |
| 265 | + 'bits' => 24, |
| 266 | + 'media_type' => MEDIATYPE_BITMAP, |
| 267 | + 'mime' => 'image/jpeg', |
| 268 | + 'metadata' => serialize( array() ), |
| 269 | + 'sha1' => wfBaseConvert( '', 16, 36, 31 ), |
| 270 | + 'fileExists' => true |
| 271 | + ), $this->db->timestamp( '20010115123500' ), $user ); |
| 272 | + |
| 273 | + } |
| 274 | + |
| 275 | + public function testParserTests() { |
| 276 | + |
| 277 | + //global $IP; |
| 278 | + //$wgParserTestFiles = array( "$IP/tests/parser/testparserTests.txt" ); |
| 279 | + |
| 280 | + global $wgParserTestFiles; |
| 281 | + |
| 282 | + foreach( $wgParserTestFiles as $file ) { |
| 283 | + |
| 284 | + $iter = new ParserTestFileIterator( $file, $this ); |
| 285 | + |
| 286 | + foreach ( $iter as $t ) { |
| 287 | + |
| 288 | + $result = $this->doRunTest( $t['test'], $t['input'], $t['result'], $t['options'], $t['config'] ); |
| 289 | + |
| 290 | + //$this->recorder->record( $t['test'], $result ); |
| 291 | + } |
| 292 | + |
| 293 | + } |
| 294 | + |
| 295 | + |
| 296 | + } |
| 297 | + |
| 298 | + |
| 299 | + /** |
| 300 | + * Run a given wikitext input through a freshly-constructed wiki parser, |
| 301 | + * and compare the output against the expected results. |
| 302 | + * Prints status and explanatory messages to stdout. |
| 303 | + * |
| 304 | + * @param $desc String: test's description |
| 305 | + * @param $input String: wikitext to try rendering |
| 306 | + * @param $result String: result to output |
| 307 | + * @param $opts Array: test's options |
| 308 | + * @param $config String: overrides for global variables, one per line |
| 309 | + * @return Boolean |
| 310 | + */ |
| 311 | + protected function doRunTest( $desc, $input, $result, $opts, $config ) { |
| 312 | + |
| 313 | + $opts = $this->parseOptions( $opts ); |
| 314 | + $this->setupGlobals( $opts, $config ); |
| 315 | + |
| 316 | + $user = new User(); |
| 317 | + $options = ParserOptions::newFromUser( $user ); |
| 318 | + |
| 319 | + if ( isset( $opts['title'] ) ) { |
| 320 | + $titleText = $opts['title']; |
| 321 | + } |
| 322 | + else { |
| 323 | + $titleText = 'Parser test'; |
| 324 | + } |
| 325 | + |
| 326 | + $local = isset( $opts['local'] ); |
| 327 | + $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null; |
| 328 | + $parser = $this->getParser( $preprocessor ); |
| 329 | + $title = Title::newFromText( $titleText ); |
| 330 | + |
| 331 | + if ( isset( $opts['pst'] ) ) { |
| 332 | + $out = $parser->preSaveTransform( $input, $title, $user, $options ); |
| 333 | + } elseif ( isset( $opts['msg'] ) ) { |
| 334 | + $out = $parser->transformMsg( $input, $options ); |
| 335 | + } elseif ( isset( $opts['section'] ) ) { |
| 336 | + $section = $opts['section']; |
| 337 | + $out = $parser->getSection( $input, $section ); |
| 338 | + } elseif ( isset( $opts['replace'] ) ) { |
| 339 | + $section = $opts['replace'][0]; |
| 340 | + $replace = $opts['replace'][1]; |
| 341 | + $out = $parser->replaceSection( $input, $section, $replace ); |
| 342 | + } elseif ( isset( $opts['comment'] ) ) { |
| 343 | + $linker = $user->getSkin(); |
| 344 | + $out = $linker->formatComment( $input, $title, $local ); |
| 345 | + } elseif ( isset( $opts['preload'] ) ) { |
| 346 | + $out = $parser->getpreloadText( $input, $title, $options ); |
| 347 | + } else { |
| 348 | + $output = $parser->parse( $input, $title, $options, true, true, 1337 ); |
| 349 | + $out = $output->getText(); |
| 350 | + |
| 351 | + if ( isset( $opts['showtitle'] ) ) { |
| 352 | + if ( $output->getTitleText() ) { |
| 353 | + $title = $output->getTitleText(); |
| 354 | + } |
| 355 | + |
| 356 | + $out = "$title\n$out"; |
| 357 | + } |
| 358 | + |
| 359 | + if ( isset( $opts['ill'] ) ) { |
| 360 | + $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) ); |
| 361 | + } elseif ( isset( $opts['cat'] ) ) { |
| 362 | + global $wgOut; |
| 363 | + |
| 364 | + $wgOut->addCategoryLinks( $output->getCategories() ); |
| 365 | + $cats = $wgOut->getCategoryLinks(); |
| 366 | + |
| 367 | + if ( isset( $cats['normal'] ) ) { |
| 368 | + $out = $this->tidy( implode( ' ', $cats['normal'] ) ); |
| 369 | + } else { |
| 370 | + $out = ''; |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + $result = $this->tidy( $result ); |
| 375 | + } |
| 376 | + |
| 377 | + $this->teardownGlobals(); |
| 378 | + |
| 379 | + $this->assertEquals( $result, $out, $desc ); |
| 380 | + } |
| 381 | + |
| 382 | + /** |
| 383 | + * Get a Parser object |
| 384 | + */ |
| 385 | + function getParser( $preprocessor = null ) { |
| 386 | + global $wgParserConf; |
| 387 | + |
| 388 | + $class = $wgParserConf['class']; |
| 389 | + $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf ); |
| 390 | + |
| 391 | + foreach ( $this->hooks as $tag => $callback ) { |
| 392 | + $parser->setHook( $tag, $callback ); |
| 393 | + } |
| 394 | + |
| 395 | + foreach ( $this->functionHooks as $tag => $bits ) { |
| 396 | + list( $callback, $flags ) = $bits; |
| 397 | + $parser->setFunctionHook( $tag, $callback, $flags ); |
| 398 | + } |
| 399 | + |
| 400 | + wfRunHooks( 'ParserTestParser', array( &$parser ) ); |
| 401 | + |
| 402 | + return $parser; |
| 403 | + } |
| 404 | + |
| 405 | + /* |
| 406 | + * Run the "tidy" command on text if the $wgUseTidy |
| 407 | + * global is true |
| 408 | + * |
| 409 | + * @param $text String: the text to tidy |
| 410 | + * @return String |
| 411 | + * @static |
| 412 | + */ |
| 413 | + protected function tidy( $text ) { |
| 414 | + global $wgUseTidy; |
| 415 | + |
| 416 | + if ( $wgUseTidy ) { |
| 417 | + $text = MWTidy::tidy( $text ); |
| 418 | + } |
| 419 | + |
| 420 | + return $text; |
| 421 | + } |
| 422 | + |
| 423 | + |
| 424 | + /** |
| 425 | + * Create a dummy uploads directory which will contain a couple |
| 426 | + * of files in order to pass existence tests. |
| 427 | + * |
| 428 | + * @return String: the directory |
| 429 | + */ |
| 430 | + protected function setupUploadDir() { |
| 431 | + global $IP; |
| 432 | + |
| 433 | + if ( $this->keepUploads ) { |
| 434 | + $dir = wfTempDir() . '/mwParser-images'; |
| 435 | + |
| 436 | + if ( is_dir( $dir ) ) { |
| 437 | + return $dir; |
| 438 | + } |
| 439 | + } else { |
| 440 | + $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images"; |
| 441 | + } |
| 442 | + |
| 443 | + // wfDebug( "Creating upload directory $dir\n" ); |
| 444 | + if ( file_exists( $dir ) ) { |
| 445 | + wfDebug( "Already exists!\n" ); |
| 446 | + return $dir; |
| 447 | + } |
| 448 | + |
| 449 | + wfMkdirParents( $dir . '/3/3a' ); |
| 450 | + copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" ); |
| 451 | + wfMkdirParents( $dir . '/0/09' ); |
| 452 | + copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" ); |
| 453 | + |
| 454 | + return $dir; |
| 455 | + } |
| 456 | + |
| 457 | + /** |
| 458 | + * Insert a temporary test article |
| 459 | + * @param $name String: the title, including any prefix |
| 460 | + * @param $text String: the article text |
| 461 | + * @param $line Integer: the input line number, for reporting errors |
| 462 | + */ |
| 463 | + public function addArticle( $name, $text, $line = 'unknown' ) { |
| 464 | + global $wgCapitalLinks; |
| 465 | + |
| 466 | + $text = $this->removeEndingNewline($text); |
| 467 | + |
| 468 | + $oldCapitalLinks = $wgCapitalLinks; |
| 469 | + $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637 |
| 470 | + |
| 471 | + $name = $this->removeEndingNewline( $name ); |
| 472 | + $title = Title::newFromText( $name ); |
| 473 | + |
| 474 | + if ( is_null( $title ) ) { |
| 475 | + wfDie( "invalid title ('$name' => '$title') at line $line\n" ); |
| 476 | + } |
| 477 | + |
| 478 | + $aid = $title->getArticleID( Title::GAID_FOR_UPDATE ); |
| 479 | + |
| 480 | + if ( $aid != 0 ) { |
| 481 | + debug_print_backtrace(); |
| 482 | + wfDie( "duplicate article '$name' at line $line\n" ); |
| 483 | + } |
| 484 | + |
| 485 | + $art = new Article( $title ); |
| 486 | + $art->doEdit( $text, '', EDIT_NEW ); |
| 487 | + |
| 488 | + $wgCapitalLinks = $oldCapitalLinks; |
| 489 | + } |
| 490 | + |
| 491 | + /** |
| 492 | + * Remove last character if it is a newline |
| 493 | + */ |
| 494 | + public function removeEndingNewline( $s ) { |
| 495 | + if ( substr( $s, -1 ) === "\n" ) { |
| 496 | + return substr( $s, 0, -1 ); |
| 497 | + } |
| 498 | + else { |
| 499 | + return $s; |
| 500 | + } |
| 501 | + } |
| 502 | + |
| 503 | + /** |
| 504 | + * Steal a callback function from the primary parser, save it for |
| 505 | + * application to our scary parser. If the hook is not installed, |
| 506 | + * abort processing of this file. |
| 507 | + * |
| 508 | + * @param $name String |
| 509 | + * @return Bool true if tag hook is present |
| 510 | + */ |
| 511 | + public function requireHook( $name ) { |
| 512 | + global $wgParser; |
| 513 | + |
| 514 | + $wgParser->firstCallInit( ); // make sure hooks are loaded. |
| 515 | + |
| 516 | + if ( isset( $wgParser->mTagHooks[$name] ) ) { |
| 517 | + $this->hooks[$name] = $wgParser->mTagHooks[$name]; |
| 518 | + } else { |
| 519 | + echo " This test suite requires the '$name' hook extension, skipping.\n"; |
| 520 | + return false; |
| 521 | + } |
| 522 | + |
| 523 | + return true; |
| 524 | + } |
| 525 | + |
| 526 | + protected function parseOptions( $instring ) { |
| 527 | + $opts = array(); |
| 528 | + // foo |
| 529 | + // foo=bar |
| 530 | + // foo="bar baz" |
| 531 | + // foo=[[bar baz]] |
| 532 | + // foo=bar,"baz quux" |
| 533 | + $regex = '/\b |
| 534 | + ([\w-]+) # Key |
| 535 | + \b |
| 536 | + (?:\s* |
| 537 | + = # First sub-value |
| 538 | + \s* |
| 539 | + ( |
| 540 | + " |
| 541 | + [^"]* # Quoted val |
| 542 | + " |
| 543 | + | |
| 544 | + \[\[ |
| 545 | + [^]]* # Link target |
| 546 | + \]\] |
| 547 | + | |
| 548 | + [\w-]+ # Plain word |
| 549 | + ) |
| 550 | + (?:\s* |
| 551 | + , # Sub-vals 1..N |
| 552 | + \s* |
| 553 | + ( |
| 554 | + "[^"]*" # Quoted val |
| 555 | + | |
| 556 | + \[\[[^]]*\]\] # Link target |
| 557 | + | |
| 558 | + [\w-]+ # Plain word |
| 559 | + ) |
| 560 | + )* |
| 561 | + )? |
| 562 | + /x'; |
| 563 | + |
| 564 | + if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) { |
| 565 | + foreach ( $matches as $bits ) { |
| 566 | + array_shift( $bits ); |
| 567 | + $key = strtolower( array_shift( $bits ) ); |
| 568 | + if ( count( $bits ) == 0 ) { |
| 569 | + $opts[$key] = true; |
| 570 | + } elseif ( count( $bits ) == 1 ) { |
| 571 | + $opts[$key] = $this->cleanupOption( array_shift( $bits ) ); |
| 572 | + } else { |
| 573 | + // Array! |
| 574 | + $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits ); |
| 575 | + } |
| 576 | + } |
| 577 | + } |
| 578 | + return $opts; |
| 579 | + } |
| 580 | + |
| 581 | + protected function cleanupOption( $opt ) { |
| 582 | + if ( substr( $opt, 0, 1 ) == '"' ) { |
| 583 | + return substr( $opt, 1, -1 ); |
| 584 | + } |
| 585 | + |
| 586 | + if ( substr( $opt, 0, 2 ) == '[[' ) { |
| 587 | + return substr( $opt, 2, -2 ); |
| 588 | + } |
| 589 | + return $opt; |
| 590 | + } |
| 591 | + |
| 592 | + /** |
| 593 | + * Use a regex to find out the value of an option |
| 594 | + * @param $key String: name of option val to retrieve |
| 595 | + * @param $opts Options array to look in |
| 596 | + * @param $default Mixed: default value returned if not found |
| 597 | + */ |
| 598 | + protected static function getOptionValue( $key, $opts, $default ) { |
| 599 | + $key = strtolower( $key ); |
| 600 | + |
| 601 | + if ( isset( $opts[$key] ) ) { |
| 602 | + return $opts[$key]; |
| 603 | + } else { |
| 604 | + return $default; |
| 605 | + } |
| 606 | + } |
| 607 | +} |
| 608 | + |
Index: trunk/phase3/tests/testHelpers.inc |
— | — | @@ -527,7 +527,8 @@ |
528 | 528 | |
529 | 529 | if ( $this->parserTest ) { |
530 | 530 | $this->parserTest->addArticle( ParserTest::chomp( $data['article'] ), $data['text'], $this->lineNum ); |
531 | | - } else {wfDie("JAJA"); |
| 531 | + } else { |
| 532 | + wfDie("Cannot add an article without a parserTest instance"); |
532 | 533 | ParserTest::addArticle( $data['article'], $data['text'], $this->lineNum ); |
533 | 534 | } |
534 | 535 | $data = array(); |