* Deglobalised link placeholder handling by moving the relevant functionality from...
[lhc/web/wiklou.git] / maintenance / parserTests.php
index b2dd968..9199252 100644 (file)
@@ -29,9 +29,13 @@ $options = array( 'quick', 'color', 'quiet', 'help' );
 $optionsWithArgs = array( 'regex' );
 
 require_once( 'commandLine.inc' );
+require_once( "$IP/includes/ObjectCache.php" );
 require_once( "$IP/languages/LanguageUtf8.php" );
 
-/** */
+/**
+ * @package MediaWiki
+ * @subpackage Maintenance
+ */
 class ParserTest {
        /**
         * boolean $color whereas output should be colorized
@@ -183,7 +187,7 @@ class ParserTest {
                        }
                }
                if( $total > 0 ) {
-                       $ratio = IntVal( 100.0 * $success / $total );
+                       $ratio = sprintf( "%.2f", 100 * $success / $total );
                        print $this->termColor( 1 ) . "\nPassed $success of $total tests ($ratio%) ";
                        if( $success == $total ) {
                                print $this->termColor( 32 ) . "PASSED!";
@@ -252,7 +256,7 @@ class ParserTest {
                
                $this->teardownGlobals();
                
-               if( $result === $out ) {
+               if( $result === $out && $this->wellFormed( $out ) ) {
                        return $this->showSuccess( $desc );
                } else {
                        return $this->showFailure( $desc, $result, $out );
@@ -287,14 +291,18 @@ class ParserTest {
                        'wgSitename' => 'MediaWiki',
                        'wgLanguageCode' => 'en',
                        'wgContLanguageCode' => 'en',
-                       'wgUseLatin1' => false,
                        'wgDBprefix' => 'parsertest',
+                       'wgDefaultUserOptions' => array(),
                        
                        'wgLoadBalancer' => LoadBalancer::newFromParams( $GLOBALS['wgDBservers'] ),
                        'wgLang' => new LanguageUtf8(),
                        'wgContLang' => new LanguageUtf8(),
                        'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)),
                        'wgMaxTocLevel' => 999,
+                       'wgCapitalLinks' => true,
+                       'wgDefaultUserOptions' => array(),
+                       'wgNoFollowLinks' => true,
+                       'wgThumbnailScriptPath' => false,
                        );
                $this->savedGlobals = array();
                foreach( $settings as $var => $val ) {
@@ -304,19 +312,22 @@ class ParserTest {
                $GLOBALS['wgLoadBalancer']->loadMasterPos();
                $GLOBALS['wgMessageCache']->initialise( new BagOStuff(), false, 0, $GLOBALS['wgDBname'] );
                $this->setupDatabase();
+               
+               global $wgUser;
+               $wgUser = new User();
        }
        
        # List of temporary tables to create, without prefix
        # Some of these probably aren't necessary
        function listTables() {
-               return array('user', 'cur', 'old', 'links',
+               return array('user', 'page', 'revision', 'text', 'links',
                        'brokenlinks', 'imagelinks', 'categorylinks',
                        'linkscc', 'site_stats', 'hitcounter',
                        'ipblocks', 'image', 'oldimage',
                        'recentchanges',
                        'watchlist', 'math', 'searchindex',
                        'interwiki', 'querycache',
-                       'objectcache'
+                       'objectcache', 'group'
                );
        }
        
@@ -393,7 +404,27 @@ class ParserTest {
                                       'iw_local'  => 1 ),
                                ) );
 
-
+                       # Hack: initialize a group
+                       $db->insert( 'group', array(
+                               'group_id' => 1,
+                               'group_name' => 'Anonymous',
+                               'group_description' => 'Anonymous users',
+                               'group_rights' => 'read' ) );
+                       
+                       # Hack: Insert an image to work with
+                       $db->insert( 'image', array(
+                               'img_name'        => 'Foobar.jpg',
+                               'img_size'        => 12345,
+                               'img_description' => 'Some lame file',
+                               'img_user'        => 1,
+                               'img_user_text'   => 'WikiSysop',
+                               'img_timestamp'   => $db->timestamp( '20010115123500' ),
+                               'img_width'       => 1941,
+                               'img_height'      => 220,
+                               'img_bits'        => 24,
+                               'img_type'        => 2, // 2 == JPEG
+                               ) );
+                       
                        $setupDB = true;
                }
        }
@@ -405,12 +436,20 @@ class ParserTest {
         * @access private
         */
        function setupUploadDir() {
+               global $IP;
+               
                $dir = "/tmp/mwParser-" . mt_rand() . "-images";
                mkdir( $dir );
                mkdir( $dir . '/3' );
                mkdir( $dir . '/3/3a' );
+               
+               $img = "$IP/skins/monobook/headbg.jpg";
+               $h = fopen($img, 'r');
+               $c = fread($h, filesize($img));
+               fclose($h);
+               
                $f = fopen( $dir . '/3/3a/Foobar.jpg', 'wb' );
-               fwrite( $f, 'Dummy file' );
+               fwrite( $f, $c );
                fclose( $f );
                return $dir;
        }
@@ -439,7 +478,11 @@ class ParserTest {
                unlink( "$dir/3/3a/Foobar.jpg" );
                rmdir( "$dir/3/3a" );
                rmdir( "$dir/3" );
-               @rmdir( "$dir/thumb/3/39" );
+
+               @unlink( "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg" );
+               @rmdir( "$dir/thumb/3/3a/Foobar.jpg" );
+               @rmdir( "$dir/thumb/3/3a" );
+               @rmdir( "$dir/thumb/3/39" ); # wtf?
                @rmdir( "$dir/thumb/3" );
                @rmdir( "$dir/thumb" );
                rmdir( "$dir" );
@@ -487,6 +530,9 @@ class ParserTest {
                if( $this->showDiffs ) {
                        print $this->quickDiff( $result, $html );
                }
+               if( !$this->wellFormed( $html ) ) {
+                       print "XML error: $this->mXmlError\n";
+               }
                return false;
        }
        
@@ -612,6 +658,56 @@ class ParserTest {
                }
                return $text;
        }
+       
+       function wellFormed( $text ) {
+               $html = 
+                       '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' .
+                       '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' .
+                       '<html>' .
+                       $text .
+                       '</html>';
+               
+               $parser = xml_parser_create( "UTF-8" );
+               
+               # case folding violates XML standard, turn it off
+               xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
+               
+               if( !xml_parse( $parser, $html, true ) ) {
+                       $err = xml_error_string( xml_get_error_code( $parser ) );
+                       $position = xml_get_current_byte_index( $parser );
+                       $fragment = $this->extractFragment( $html, $position );
+                       $this->mXmlError = "$err at byte $position:\n$fragment";
+                       xml_parser_free( $parser );
+                       return false;
+               }
+               xml_parser_free( $parser );
+               return true;
+       }
+       
+       function extractFragment( $text, $position ) {
+               $start = max( 0, $position - 10 );
+               $before = $position - $start;
+               $fragment = '...' .
+                       $this->termColor( 34 ) .
+                       substr( $text, $start, $before ) .
+                       $this->termColor( 0 ) .
+                       $this->termColor( 31 ) .
+                       $this->termColor( 1 ) .
+                       substr( $text, $position, 1 ) .
+                       $this->termColor( 0 ) .
+                       $this->termColor( 34 ) .
+                       substr( $text, $position + 1, 9 ) .
+                       $this->termColor( 0 ) .
+                       '...';
+               $display = str_replace( "\n", ' ', $fragment );
+               $caret = '   ' .
+                       str_repeat( ' ', $before ) .
+                       $this->termColor( 31 ) .
+                       '^' .
+                       $this->termColor( 0 );
+               return "$display\n$caret";
+       }
+       
 }
 
 if( isset( $options['help'] ) ) {