00001 <?php
00002
00003 if ( !defined( 'MEDIAWIKI' ) ) {
00004 exit;
00005 }
00006
00007 global $IP;
00008 define( "NO_COMMAND_LINE", 1 );
00009 define( "PARSER_TESTS", "$IP/maintenance/parserTests.txt" );
00010
00011 require_once( "$IP/maintenance/parserTests.inc" );
00012
00013 class PHPUnitTestRecorder extends TestRecorder {
00014
00015 function record( $test, $result ) {
00016 $this->total++;
00017 $this->success += $result;
00018
00019 }
00020
00021 function reportPercentage( $success, $total ) {}
00022 }
00023
00024 class MediaWikiParserTestSuite extends PHPUnit_Framework_TestSuite {
00025 #implements PHPUnit_Framework_SelfDescribing {
00026 static private $count;
00027 static public $parser;
00028 static public $iter;
00029
00030 public static function suite() {
00031 $suite = new PHPUnit_Framework_TestSuite();
00032
00033 self::$iter = new TestFileIterator( PARSER_TESTS );
00034
00035 foreach(self::$iter as $i => $test) {
00036 $suite->addTest(new ParserUnitTest($i, $test['test']));
00037 self::$count++;
00038 }
00039 unset($tests);
00040
00041 self::$parser = new PTShell;
00042 self::$iter->setParser(self::$parser);
00043 self::$parser->recorder->start();
00044 self::$parser->setupDatabase();
00045 self::$iter->rewind();
00046
00047
00048 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
00049 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
00050 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
00051 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
00052 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
00053 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
00054
00055 $wgScript = '/index.php';
00056 $wgScriptPath = '/';
00057 $wgArticlePath = '/wiki/$1';
00058 $wgStyleSheetPath = '/skins';
00059 $wgStylePath = '/skins';
00060 $wgThumbnailScriptPath = false;
00061 $wgLocalFileRepo = array(
00062 'class' => 'LocalRepo',
00063 'name' => 'local',
00064 'directory' => '',
00065 'url' => 'http://example.com/images',
00066 'hashLevels' => 2,
00067 'transformVia404' => false,
00068 );
00069 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
00070 $wgNamespaceAliases['Image'] = NS_FILE;
00071 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
00072
00073
00074 $wgEnableParserCache = false;
00075 $wgDeferredUpdateList = array();
00076 $wgMemc =& wfGetMainCache();
00077 $messageMemc =& wfGetMessageCacheStorage();
00078 $parserMemc =& wfGetParserCacheStorage();
00079
00080 $wgContLang = new StubContLang;
00081 $wgUser = new StubUser;
00082 $wgLang = new StubUserLang;
00083 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
00084 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
00085 $wgRequest = new WebRequest;
00086
00087 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
00088 array( $messageMemc, $wgUseDatabaseMessages,
00089 $wgMsgCacheExpiry, wfWikiID() ) );
00090 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
00091
00092 return $suite;
00093 }
00094
00095 public function tearDown() {
00096 $this->teardownDatabase();
00097 $this->recorder->report();
00098 $this->recorder->end();
00099 $this->teardownUploadDir($this->uploadDir);
00100 }
00101
00102 public function count() {return self::$count;}
00103
00104 public function toString() {
00105 return "MediaWiki Parser Tests";
00106 }
00107
00108
00109 private $db;
00110 private $uploadDir;
00111 private $keepUploads;
00115 private function teardownUploadDir( $dir ) {
00116 if ( $this->keepUploads ) {
00117 return;
00118 }
00119
00120
00121 self::deleteFiles(
00122 array (
00123 "$dir/3/3a/Foobar.jpg",
00124 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
00125 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
00126 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
00127 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
00128
00129 "$dir/0/09/Bad.jpg",
00130 )
00131 );
00132
00133 self::deleteDirs(
00134 array (
00135 "$dir/3/3a",
00136 "$dir/3",
00137 "$dir/thumb/6/65",
00138 "$dir/thumb/6",
00139 "$dir/thumb/3/3a/Foobar.jpg",
00140 "$dir/thumb/3/3a",
00141 "$dir/thumb/3",
00142
00143 "$dir/0/09/",
00144 "$dir/0/",
00145
00146 "$dir/thumb",
00147 "$dir",
00148 )
00149 );
00150 }
00151
00156 private static function deleteFiles( $files ) {
00157 foreach( $files as $file ) {
00158 if( file_exists( $file ) ) {
00159 unlink( $file );
00160 }
00161 }
00162 }
00167 private static function deleteDirs( $dirs ) {
00168 foreach( $dirs as $dir ) {
00169 if( is_dir( $dir ) ) {
00170 rmdir( $dir );
00171 }
00172 }
00173 }
00174
00180 private function setupUploadDir() {
00181 global $IP;
00182 if ( $this->keepUploads ) {
00183 $dir = wfTempDir() . '/mwParser-images';
00184 if ( is_dir( $dir ) ) {
00185 return $dir;
00186 }
00187 } else {
00188 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
00189 }
00190
00191 wfDebug( "Creating upload directory $dir\n" );
00192 if ( file_exists( $dir ) ) {
00193 wfDebug( "Already exists!\n" );
00194 return $dir;
00195 }
00196 wfMkdirParents( $dir . '/3/3a' );
00197 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
00198
00199 wfMkdirParents( $dir . '/0/09' );
00200 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
00201 return $dir;
00202 }
00203 }
00204
00205 class ParserUnitTest extends PHPUnit_Framework_TestCase {
00206 private $number = 0;
00207 private $test = "";
00208
00209 public function __construct($number, $test) {
00210 $this->number = $number;
00211 $this->test = $test;
00212 }
00213
00214 function count() {return 1;}
00215
00216 public function run(PHPUnit_Framework_TestResult $result = NULL) {
00217 PHPUnit_Framework_Assert::resetCount();
00218 if ($result === NULL) {
00219 $result = new PHPUnit_Framework_TestResult;
00220 }
00221
00222 $t = MediaWikiParserTestSuite::$iter->current();
00223 $k = MediaWikiParserTestSuite::$iter->key();
00224
00225 if(!MediaWikiParserTestSuite::$iter->valid()) {
00226 return;
00227 }
00228
00229
00230
00231 if($k != $this->number) {
00232 $i = $this->number;
00233 wfDie("I got confused!\n");
00234 }
00235
00236 $result->startTest($this);
00237 PHPUnit_Util_Timer::start();
00238
00239 $r = false;
00240 try {
00241 $r = MediaWikiParserTestSuite::$parser->runTest(
00242 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
00243 );
00244 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
00245 }
00246 catch (PHPUnit_Framework_AssertionFailedError $e) {
00247 $result->addFailure($this, $e, PHPUnit_Util_Timer::stop());
00248 }
00249 catch (Exception $e) {
00250 $result->addError($this, $e, PHPUnit_Util_Timer::stop());
00251 }
00252 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
00253
00254 $result->endTest($this, PHPUnit_Util_Timer::stop());
00255
00256 MediaWikiParserTestSuite::$parser->recorder->record($t['test'], $r);
00257 MediaWikiParserTestSuite::$iter->next();
00258 $this->addToAssertionCount(PHPUnit_Framework_Assert::getCount());
00259
00260 return $result;
00261 }
00262
00263 }
00264
00265 class PTShell extends ParserTest {
00266 function showTesting( $desc ) {
00267 }
00268
00269 function showRunFile( $path ) {
00270 }
00271
00272 function showSuccess( $desc ) {
00273 PHPUnit_Framework_Assert::assertTrue(true, $desc);
00274 return true;
00275 }
00276
00277 function showFailure( $desc, $expected, $got ) {
00278 PHPUnit_Framework_Assert::assertEquals($expected, $got, $desc);
00279 }
00280
00281 }
00282
00283