r79438 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79437‎ | r79438 | r79439 >
Date:20:57, 1 January 2011
Author:soxred93
Status:ok
Tags:
Comment:
Reorder functions around, add some form of order to the test suite.
It'd be nice to break some of these functions out to another class, but then again,
I'd also like to see Windows actually become stable, MediaWiki to become fully class based,
and Google to stop taking over the world. We can't all have what we want.
Modified paths:
  • /trunk/phase3/tests/phpunit/includes/parser/NewParserTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/parser/NewParserTest.php
@@ -21,6 +21,10 @@
2222 public $fuzzSeed = 0;
2323 public $memoryLimit = 50;
2424
 25+
 26+
 27+ //PHPUnit + MediaWikiTestCase functions
 28+
2529 function setUp() {
2630 global $wgContLang;
2731 $wgContLang = Language::factory( 'en' );
@@ -88,6 +92,94 @@
8993
9094 }
9195
 96+ public function tearDown() {
 97+ }
 98+
 99+ function addDBData() {
 100+ # Hack: insert a few Wikipedia in-project interwiki prefixes,
 101+ # for testing inter-language links
 102+ $this->db->insert( 'interwiki', array(
 103+ array( 'iw_prefix' => 'wikipedia',
 104+ 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
 105+ 'iw_api' => '',
 106+ 'iw_wikiid' => '',
 107+ 'iw_local' => 0 ),
 108+ array( 'iw_prefix' => 'meatball',
 109+ 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
 110+ 'iw_api' => '',
 111+ 'iw_wikiid' => '',
 112+ 'iw_local' => 0 ),
 113+ array( 'iw_prefix' => 'zh',
 114+ 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
 115+ 'iw_api' => '',
 116+ 'iw_wikiid' => '',
 117+ 'iw_local' => 1 ),
 118+ array( 'iw_prefix' => 'es',
 119+ 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
 120+ 'iw_api' => '',
 121+ 'iw_wikiid' => '',
 122+ 'iw_local' => 1 ),
 123+ array( 'iw_prefix' => 'fr',
 124+ 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
 125+ 'iw_api' => '',
 126+ 'iw_wikiid' => '',
 127+ 'iw_local' => 1 ),
 128+ array( 'iw_prefix' => 'ru',
 129+ 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
 130+ 'iw_api' => '',
 131+ 'iw_wikiid' => '',
 132+ 'iw_local' => 1 ),
 133+ ) );
 134+
 135+
 136+ # Update certain things in site_stats
 137+ $this->db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
 138+
 139+ # Reinitialise the LocalisationCache to match the database state
 140+ Language::getLocalisationCache()->unloadAll();
 141+
 142+ # Make a new message cache
 143+ global $wgMessageCache, $wgMemc;
 144+ $wgMessageCache = new MessageCache( $wgMemc, true, 3600 );
 145+
 146+ $this->uploadDir = $this->setupUploadDir();
 147+
 148+ $user = User::newFromId( 0 );
 149+
 150+ $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
 151+ $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array(
 152+ 'size' => 12345,
 153+ 'width' => 1941,
 154+ 'height' => 220,
 155+ 'bits' => 24,
 156+ 'media_type' => MEDIATYPE_BITMAP,
 157+ 'mime' => 'image/jpeg',
 158+ 'metadata' => serialize( array() ),
 159+ 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
 160+ 'fileExists' => true
 161+ ), $this->db->timestamp( '20010115123500' ), $user );
 162+
 163+ # This image will be blacklisted in [[MediaWiki:Bad image list]]
 164+ $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
 165+ $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', array(
 166+ 'size' => 12345,
 167+ 'width' => 320,
 168+ 'height' => 240,
 169+ 'bits' => 24,
 170+ 'media_type' => MEDIATYPE_BITMAP,
 171+ 'mime' => 'image/jpeg',
 172+ 'metadata' => serialize( array() ),
 173+ 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
 174+ 'fileExists' => true
 175+ ), $this->db->timestamp( '20010115123500' ), $user );
 176+
 177+ }
 178+
 179+
 180+
 181+
 182+ //ParserTest setup/teardown functions
 183+
92184 /**
93185 * Set up the global variables for a consistent environment for each test.
94186 * Ideally this should replace the global configuration entirely.
@@ -211,85 +303,44 @@
212304 }
213305 }
214306
215 - function addDBData() {
216 - # Hack: insert a few Wikipedia in-project interwiki prefixes,
217 - # for testing inter-language links
218 - $this->db->insert( 'interwiki', array(
219 - array( 'iw_prefix' => 'wikipedia',
220 - 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
221 - 'iw_api' => '',
222 - 'iw_wikiid' => '',
223 - 'iw_local' => 0 ),
224 - array( 'iw_prefix' => 'meatball',
225 - 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
226 - 'iw_api' => '',
227 - 'iw_wikiid' => '',
228 - 'iw_local' => 0 ),
229 - array( 'iw_prefix' => 'zh',
230 - 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
231 - 'iw_api' => '',
232 - 'iw_wikiid' => '',
233 - 'iw_local' => 1 ),
234 - array( 'iw_prefix' => 'es',
235 - 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
236 - 'iw_api' => '',
237 - 'iw_wikiid' => '',
238 - 'iw_local' => 1 ),
239 - array( 'iw_prefix' => 'fr',
240 - 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
241 - 'iw_api' => '',
242 - 'iw_wikiid' => '',
243 - 'iw_local' => 1 ),
244 - array( 'iw_prefix' => 'ru',
245 - 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
246 - 'iw_api' => '',
247 - 'iw_wikiid' => '',
248 - 'iw_local' => 1 ),
249 - ) );
 307+ /**
 308+ * Create a dummy uploads directory which will contain a couple
 309+ * of files in order to pass existence tests.
 310+ *
 311+ * @return String: the directory
 312+ */
 313+ protected function setupUploadDir() {
 314+ global $IP;
250315
 316+ if ( $this->keepUploads ) {
 317+ $dir = wfTempDir() . '/mwParser-images';
251318
252 - # Update certain things in site_stats
253 - $this->db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
 319+ if ( is_dir( $dir ) ) {
 320+ return $dir;
 321+ }
 322+ } else {
 323+ $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
 324+ }
254325
255 - # Reinitialise the LocalisationCache to match the database state
256 - Language::getLocalisationCache()->unloadAll();
 326+ // wfDebug( "Creating upload directory $dir\n" );
 327+ if ( file_exists( $dir ) ) {
 328+ wfDebug( "Already exists!\n" );
 329+ return $dir;
 330+ }
257331
258 - # Make a new message cache
259 - global $wgMessageCache, $wgMemc;
260 - $wgMessageCache = new MessageCache( $wgMemc, true, 3600 );
 332+ wfMkdirParents( $dir . '/3/3a' );
 333+ copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
 334+ wfMkdirParents( $dir . '/0/09' );
 335+ copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
261336
262 - $this->uploadDir = $this->setupUploadDir();
263 -
264 - $user = User::newFromId( 0 );
265 -
266 - $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
267 - $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array(
268 - 'size' => 12345,
269 - 'width' => 1941,
270 - 'height' => 220,
271 - 'bits' => 24,
272 - 'media_type' => MEDIATYPE_BITMAP,
273 - 'mime' => 'image/jpeg',
274 - 'metadata' => serialize( array() ),
275 - 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
276 - 'fileExists' => true
277 - ), $this->db->timestamp( '20010115123500' ), $user );
278 -
279 - # This image will be blacklisted in [[MediaWiki:Bad image list]]
280 - $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
281 - $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', array(
282 - 'size' => 12345,
283 - 'width' => 320,
284 - 'height' => 240,
285 - 'bits' => 24,
286 - 'media_type' => MEDIATYPE_BITMAP,
287 - 'mime' => 'image/jpeg',
288 - 'metadata' => serialize( array() ),
289 - 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
290 - 'fileExists' => true
291 - ), $this->db->timestamp( '20010115123500' ), $user );
292 -
 337+ return $dir;
293338 }
 339+
 340+
 341+
 342+
 343+
 344+ //Actual test suites
294345
295346 public function testParserTests() {
296347
@@ -308,8 +359,84 @@
309360 foreach ( $iter as $t ) {
310361
311362 try {
312 - $result = $this->doRunTest( $t['test'], $t['input'], $t['result'], $t['options'], $t['config'] );
313 - } catch( Exception $e ) {
 363+
 364+ $desc = $t['test'];
 365+ $input = $t['input'];
 366+ $result = $t['result'];
 367+ $opts = $t['options'];
 368+ $config = $t['config'];
 369+
 370+
 371+ $opts = $this->parseOptions( $opts );
 372+ $this->setupGlobals( $opts, $config );
 373+
 374+ $user = new User();
 375+ $options = ParserOptions::newFromUser( $user );
 376+
 377+ if ( isset( $opts['title'] ) ) {
 378+ $titleText = $opts['title'];
 379+ }
 380+ else {
 381+ $titleText = 'Parser test';
 382+ }
 383+
 384+ $local = isset( $opts['local'] );
 385+ $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
 386+ $parser = $this->getParser( $preprocessor );
 387+ $title = Title::newFromText( $titleText );
 388+
 389+ if ( isset( $opts['pst'] ) ) {
 390+ $out = $parser->preSaveTransform( $input, $title, $user, $options );
 391+ } elseif ( isset( $opts['msg'] ) ) {
 392+ $out = $parser->transformMsg( $input, $options );
 393+ } elseif ( isset( $opts['section'] ) ) {
 394+ $section = $opts['section'];
 395+ $out = $parser->getSection( $input, $section );
 396+ } elseif ( isset( $opts['replace'] ) ) {
 397+ $section = $opts['replace'][0];
 398+ $replace = $opts['replace'][1];
 399+ $out = $parser->replaceSection( $input, $section, $replace );
 400+ } elseif ( isset( $opts['comment'] ) ) {
 401+ $linker = $user->getSkin();
 402+ $out = $linker->formatComment( $input, $title, $local );
 403+ } elseif ( isset( $opts['preload'] ) ) {
 404+ $out = $parser->getpreloadText( $input, $title, $options );
 405+ } else {
 406+ $output = $parser->parse( $input, $title, $options, true, true, 1337 );
 407+ $out = $output->getText();
 408+
 409+ if ( isset( $opts['showtitle'] ) ) {
 410+ if ( $output->getTitleText() ) {
 411+ $title = $output->getTitleText();
 412+ }
 413+
 414+ $out = "$title\n$out";
 415+ }
 416+
 417+ if ( isset( $opts['ill'] ) ) {
 418+ $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
 419+ } elseif ( isset( $opts['cat'] ) ) {
 420+ global $wgOut;
 421+
 422+ $wgOut->addCategoryLinks( $output->getCategories() );
 423+ $cats = $wgOut->getCategoryLinks();
 424+
 425+ if ( isset( $cats['normal'] ) ) {
 426+ $out = $this->tidy( implode( ' ', $cats['normal'] ) );
 427+ } else {
 428+ $out = '';
 429+ }
 430+ }
 431+
 432+ $result = $this->tidy( $result );
 433+ }
 434+
 435+ $this->teardownGlobals();
 436+
 437+ $this->assertEquals( $result, $out, $desc );
 438+
 439+ }
 440+ catch( Exception $e ) {
314441 $this->assertTrue( false, $t['test'] . ' (failed: ' . $e->getMessage() . ')' );
315442 }
316443
@@ -400,6 +527,14 @@
401528 }
402529 }
403530
 531+
 532+
 533+
 534+
 535+
 536+
 537+ //Various getter functions
 538+
404539 /**
405540 * Get an input dictionary from a set of parser test files
406541 */
@@ -453,91 +588,7 @@
454589 return $memStats;
455590 }
456591
457 -
458592 /**
459 - * Run a given wikitext input through a freshly-constructed wiki parser,
460 - * and compare the output against the expected results.
461 - * Prints status and explanatory messages to stdout.
462 - *
463 - * @param $desc String: test's description
464 - * @param $input String: wikitext to try rendering
465 - * @param $result String: result to output
466 - * @param $opts Array: test's options
467 - * @param $config String: overrides for global variables, one per line
468 - * @return Boolean
469 - */
470 - protected function doRunTest( $desc, $input, $result, $opts, $config ) {
471 -
472 - $opts = $this->parseOptions( $opts );
473 - $this->setupGlobals( $opts, $config );
474 -
475 - $user = new User();
476 - $options = ParserOptions::newFromUser( $user );
477 -
478 - if ( isset( $opts['title'] ) ) {
479 - $titleText = $opts['title'];
480 - }
481 - else {
482 - $titleText = 'Parser test';
483 - }
484 -
485 - $local = isset( $opts['local'] );
486 - $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
487 - $parser = $this->getParser( $preprocessor );
488 - $title = Title::newFromText( $titleText );
489 -
490 - if ( isset( $opts['pst'] ) ) {
491 - $out = $parser->preSaveTransform( $input, $title, $user, $options );
492 - } elseif ( isset( $opts['msg'] ) ) {
493 - $out = $parser->transformMsg( $input, $options );
494 - } elseif ( isset( $opts['section'] ) ) {
495 - $section = $opts['section'];
496 - $out = $parser->getSection( $input, $section );
497 - } elseif ( isset( $opts['replace'] ) ) {
498 - $section = $opts['replace'][0];
499 - $replace = $opts['replace'][1];
500 - $out = $parser->replaceSection( $input, $section, $replace );
501 - } elseif ( isset( $opts['comment'] ) ) {
502 - $linker = $user->getSkin();
503 - $out = $linker->formatComment( $input, $title, $local );
504 - } elseif ( isset( $opts['preload'] ) ) {
505 - $out = $parser->getpreloadText( $input, $title, $options );
506 - } else {
507 - $output = $parser->parse( $input, $title, $options, true, true, 1337 );
508 - $out = $output->getText();
509 -
510 - if ( isset( $opts['showtitle'] ) ) {
511 - if ( $output->getTitleText() ) {
512 - $title = $output->getTitleText();
513 - }
514 -
515 - $out = "$title\n$out";
516 - }
517 -
518 - if ( isset( $opts['ill'] ) ) {
519 - $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
520 - } elseif ( isset( $opts['cat'] ) ) {
521 - global $wgOut;
522 -
523 - $wgOut->addCategoryLinks( $output->getCategories() );
524 - $cats = $wgOut->getCategoryLinks();
525 -
526 - if ( isset( $cats['normal'] ) ) {
527 - $out = $this->tidy( implode( ' ', $cats['normal'] ) );
528 - } else {
529 - $out = '';
530 - }
531 - }
532 -
533 - $result = $this->tidy( $result );
534 - }
535 -
536 - $this->teardownGlobals();
537 -
538 - $this->assertEquals( $result, $out, $desc );
539 - }
540 -
541 - /**
542593 * Get a Parser object
543594 */
544595 function getParser( $preprocessor = null ) {
@@ -559,59 +610,15 @@
560611
561612 return $parser;
562613 }
563 -
564 - /*
565 - * Run the "tidy" command on text if the $wgUseTidy
566 - * global is true
567 - *
568 - * @param $text String: the text to tidy
569 - * @return String
570 - * @static
571 - */
572 - protected function tidy( $text ) {
573 - global $wgUseTidy;
574 -
575 - if ( $wgUseTidy ) {
576 - $text = MWTidy::tidy( $text );
577 - }
578 -
579 - return $text;
580 - }
581614
582615
583 - /**
584 - * Create a dummy uploads directory which will contain a couple
585 - * of files in order to pass existence tests.
586 - *
587 - * @return String: the directory
588 - */
589 - protected function setupUploadDir() {
590 - global $IP;
591 -
592 - if ( $this->keepUploads ) {
593 - $dir = wfTempDir() . '/mwParser-images';
594 -
595 - if ( is_dir( $dir ) ) {
596 - return $dir;
597 - }
598 - } else {
599 - $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
600 - }
601 -
602 - // wfDebug( "Creating upload directory $dir\n" );
603 - if ( file_exists( $dir ) ) {
604 - wfDebug( "Already exists!\n" );
605 - return $dir;
606 - }
607 -
608 - wfMkdirParents( $dir . '/3/3a' );
609 - copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
610 - wfMkdirParents( $dir . '/0/09' );
611 - copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
612 -
613 - return $dir;
614 - }
615616
 617+
 618+
 619+
 620+
 621+ //Various action functions
 622+
616623 /**
617624 * Insert a temporary test article
618625 * @param $name String: the title, including any prefix
@@ -630,14 +637,14 @@
631638 $title = Title::newFromText( $name );
632639
633640 if ( is_null( $title ) ) {
634 - wfDie( "invalid title ('$name' => '$title') at line $line\n" );
 641+ throw new MWException( "invalid title ('$name' => '$title') at line $line\n" );
635642 }
636643
637644 $aid = $title->getArticleID( Title::GAID_FOR_UPDATE );
638645
639646 if ( $aid != 0 ) {
640647 debug_print_backtrace();
641 - wfDie( "duplicate article '$name' at line $line\n" );
 648+ throw new MWException( "duplicate article '$name' at line $line\n" );
642649 }
643650
644651 $art = new Article( $title );
@@ -647,18 +654,6 @@
648655 }
649656
650657 /**
651 - * Remove last character if it is a newline
652 - */
653 - public function removeEndingNewline( $s ) {
654 - if ( substr( $s, -1 ) === "\n" ) {
655 - return substr( $s, 0, -1 );
656 - }
657 - else {
658 - return $s;
659 - }
660 - }
661 -
662 - /**
663658 * Steal a callback function from the primary parser, save it for
664659 * application to our scary parser. If the hook is not installed,
665660 * abort processing of this file.
@@ -681,6 +676,49 @@
682677 return true;
683678 }
684679
 680+
 681+
 682+
 683+
 684+ //Various "cleanup" functions
 685+
 686+ /*
 687+ * Run the "tidy" command on text if the $wgUseTidy
 688+ * global is true
 689+ *
 690+ * @param $text String: the text to tidy
 691+ * @return String
 692+ * @static
 693+ */
 694+ protected function tidy( $text ) {
 695+ global $wgUseTidy;
 696+
 697+ if ( $wgUseTidy ) {
 698+ $text = MWTidy::tidy( $text );
 699+ }
 700+
 701+ return $text;
 702+ }
 703+
 704+ /**
 705+ * Remove last character if it is a newline
 706+ */
 707+ public function removeEndingNewline( $s ) {
 708+ if ( substr( $s, -1 ) === "\n" ) {
 709+ return substr( $s, 0, -1 );
 710+ }
 711+ else {
 712+ return $s;
 713+ }
 714+ }
 715+
 716+
 717+
 718+
 719+
 720+
 721+ //Test options parser functions
 722+
685723 protected function parseOptions( $instring ) {
686724 $opts = array();
687725 // foo

Status & tagging log