w/s changes
[lhc/web/wiklou.git] / maintenance / parserTests.inc
index eb00f0c..05327c1 100644 (file)
@@ -61,9 +61,7 @@ class ParserTest {
         * Sets terminal colorization and diff/quick modes depending on OS and
         * command-line options (--color and --quick).
         */
-       public function ParserTest() {
-               global $options;
-
+       public function ParserTest( $options = array() ) {
                # Only colorize output if stdout is a terminal.
                $this->color = !wfIsWindows() && posix_isatty( 1 );
 
@@ -103,7 +101,7 @@ class ParserTest {
                        $this->regex = '';
                }
 
-               $this->setupRecorder();
+               $this->setupRecorder( $options );
                $this->keepUploads = isset( $options['keep-uploads'] );
 
                if ( isset( $options['seed'] ) ) {
@@ -116,9 +114,11 @@ class ParserTest {
                $this->functionHooks = array();
        }
 
-       public function setupRecorder() {
+       public function setupRecorder ( $options ) {
                if ( isset( $options['record'] ) ) {
                        $this->recorder = new DbTestRecorder( $this );
+                       $this->recorder->version = isset( $options['setversion'] ) ?
+                                       $options['setversion'] : SpecialVersion::getVersion();
                } elseif ( isset( $options['compare'] ) ) {
                        $this->recorder = new DbTestPreviewer( $this );
                } elseif ( isset( $options['upload'] ) ) {
@@ -145,6 +145,7 @@ class ParserTest {
         * Draw input from a set of test files
         */
        function fuzzTest( $filenames ) {
+               $GLOBALS['wgContLang'] = Language::factory( 'en' );
                $dict = $this->getFuzzInput( $filenames );
                $dictSize = strlen( $dict );
                $logMaxLength = log( $this->maxFuzzTestLength );
@@ -265,6 +266,7 @@ class ParserTest {
         * @return Boolean: true if passed all tests, false if any tests failed.
         */
        public function runTestsFromFiles( $filenames ) {
+               $GLOBALS['wgContLang'] = Language::factory( 'en' );
                $this->recorder->start();
                $this->setupDatabase();
                $ok = true;
@@ -333,7 +335,6 @@ class ParserTest {
                $user = new User();
                $options = ParserOptions::newFromUser( $user );
 
-               $m = array();
                if ( isset( $opts['title'] ) ) {
                        $titleText = $opts['title'];
                }
@@ -347,7 +348,6 @@ class ParserTest {
                $parser = $this->getParser( $preprocessor );
                $title = Title::newFromText( $titleText );
 
-               $matches = array();
                if ( isset( $opts['pst'] ) ) {
                        $out = $parser->preSaveTransform( $input, $title, $user, $options );
                } elseif ( isset( $opts['msg'] ) ) {
@@ -486,9 +486,6 @@ class ParserTest {
         */
        private function setupGlobals( $opts = '', $config = '' ) {
                global $wgDBtype;
-               if ( !isset( $this->uploadDir ) ) {
-                       $this->uploadDir = $this->setupUploadDir();
-               }
 
                # Find out values for some special options.
                $lang =
@@ -597,16 +594,16 @@ class ParserTest {
         */
        private function listTables() {
                global $wgDBtype;
-               $tables = array( 'user', 'page', 'page_restrictions',
+               $tables = array( 'user', 'user_properties', 'page', 'page_restrictions',
                        'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
                        'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
                        'site_stats', 'hitcounter',     'ipblocks', 'image', 'oldimage',
-                       'recentchanges', 'watchlist', 'math', 'interwiki',
+                       'recentchanges', 'watchlist', 'math', 'interwiki', 'logging',
                        'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
                        'archive', 'user_groups', 'page_props', 'category',
                );
 
-               if ( $wgDBtype === 'mysql' )
+               if ( in_array( $wgDBtype, array( 'mysql', 'sqlite' ) ) )
                        array_push( $tables, 'searchindex' );
 
                // Allow extensions to add to the list of tables to duplicate;
@@ -656,7 +653,14 @@ class ParserTest {
                        $this->changePrefix( $wgDBtype != 'oracle' ? 'parsertest_' : 'pt_' );
                        $newTableName = $db->tableName( $tbl );
 
-                       if ( $db->tableExists( $tbl ) && $wgDBtype != 'postgres' && $wgDBtype != 'oracle' ) {
+                       if ( $wgDBtype == 'mysql' ) {
+                               $db->query( "DROP TABLE IF EXISTS $newTableName" );
+                       } elseif ( in_array( $wgDBtype, array( 'postgres', 'oracle' ) ) ) {
+                               /* DROPs wouldn't work due to Foreign Key Constraints (bug 14990, r58669)
+                                * Use "DROP TABLE IF EXISTS $newTableName CASCADE" for postgres? That
+                                * syntax would also work for mysql.
+                                */
+                       } elseif ( $db->tableExists( $tbl ) ) {
                                $db->query( "DROP TABLE $newTableName" );
                        }
                        # Create new table
@@ -672,72 +676,45 @@ class ParserTest {
                $db->insert( 'interwiki', array(
                        array( 'iw_prefix' => 'wikipedia',
                                   'iw_url'    => 'http://en.wikipedia.org/wiki/$1',
+                                  'iw_api'    => '',
+                                  'iw_wikiid' => '',
                                   'iw_local'  => 0 ),
                        array( 'iw_prefix' => 'meatball',
                                   'iw_url'    => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
+                                  'iw_api'    => '',
+                                  'iw_wikiid' => '',
                                   'iw_local'  => 0 ),
                        array( 'iw_prefix' => 'zh',
                                   'iw_url'    => 'http://zh.wikipedia.org/wiki/$1',
+                                  'iw_api'    => '',
+                                  'iw_wikiid' => '',
                                   'iw_local'  => 1 ),
                        array( 'iw_prefix' => 'es',
                                   'iw_url'    => 'http://es.wikipedia.org/wiki/$1',
+                                  'iw_api'    => '',
+                                  'iw_wikiid' => '',
                                   'iw_local'  => 1 ),
                        array( 'iw_prefix' => 'fr',
                                   'iw_url'    => 'http://fr.wikipedia.org/wiki/$1',
+                                  'iw_api'    => '',
+                                  'iw_wikiid' => '',
                                   'iw_local'  => 1 ),
                        array( 'iw_prefix' => 'ru',
                                   'iw_url'    => 'http://ru.wikipedia.org/wiki/$1',
+                                  'iw_api'    => '',
+                                  'iw_wikiid' => '',
                                   'iw_local'  => 1 ),
                        ) );
 
 
                if ( $wgDBtype == 'oracle' ) {
-                       # Insert 0 and 1 user_ids to prevent FK violations
+                       # Insert 0 user to prevent FK violations
 
                        # Anonymous user
                        $db->insert( 'user', array(
                                'user_id'         => 0,
                                'user_name'       => 'Anonymous' ) );
-
-                       # Hack-on-Hack: Insert a test user to be able to insert an image
-                       $db->insert( 'user', array(
-                               'user_id'         => 1,
-                               'user_name'       => 'Tester' ) );
-               }
-
-               # 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_media_type'  => MEDIATYPE_BITMAP,
-                       'img_major_mime'  => "image",
-                       'img_minor_mime'  => "jpeg",
-                       'img_metadata'    => serialize( array() ),
-                       ) );
-
-               # This image will be blacklisted in [[MediaWiki:Bad image list]]
-               $db->insert( 'image', array(
-                       'img_name'        => 'Bad.jpg',
-                       'img_size'        => 12345,
-                       'img_description' => 'zomgnotcensored',
-                       'img_user'        => 1,
-                       'img_user_text'   => 'WikiSysop',
-                       'img_timestamp'   => $db->timestamp( '20010115123500' ),
-                       'img_width'       => 320,
-                       'img_height'      => 240,
-                       'img_bits'        => 24,
-                       'img_media_type'  => MEDIATYPE_BITMAP,
-                       'img_major_mime'  => "image",
-                       'img_minor_mime'  => "jpeg",
-                       'img_metadata'    => serialize( array() ),
-                       ) );
+               }
 
                # Update certain things in site_stats
                $db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
@@ -747,7 +724,36 @@ class ParserTest {
 
                # Make a new message cache
                global $wgMessageCache, $wgMemc;
-               $wgMessageCache = new MessageCache( $wgMemc, true, 3600, '' );
+               $wgMessageCache = new MessageCache( $wgMemc, true, 3600 );
+
+               $this->uploadDir = $this->setupUploadDir();
+               $user = User::createNew( 'WikiSysop' );
+               $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
+               $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array(
+                       'size'        => 12345,
+                       'width'       => 1941,
+                       'height'      => 220,
+                       'bits'        => 24,
+                       'media_type'  => MEDIATYPE_BITMAP,
+                       'mime'        => 'image/jpeg',
+                       'metadata'    => serialize( array() ),
+                       'sha1'        => sha1(''),
+                       'fileExists'  => true
+                       ), $db->timestamp( '20010115123500' ), $user );
+
+               # This image will be blacklisted in [[MediaWiki:Bad image list]]
+               $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
+               $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', array(
+                       'size'        => 12345,
+                       'width'       => 320,
+                       'height'      => 240,
+                       'bits'        => 24,
+                       'media_type'  => MEDIATYPE_BITMAP,
+                       'mime'        => 'image/jpeg',
+                       'metadata'    => serialize( array() ),
+                       'sha1'        => sha1(''),
+                       'fileExists'  => true
+                       ), $db->timestamp( '20010115123500' ), $user );
        }
 
        /**
@@ -772,6 +778,8 @@ class ParserTest {
                if ( !$this->databaseSetupDone ) {
                        return;
                }
+               $this->teardownUploadDir( $this->uploadDir );
+
                $this->changePrefix( $this->oldTablePrefix );
                $this->databaseSetupDone = false;
                if ( $this->useTemporaryTables ) {
@@ -779,7 +787,6 @@ class ParserTest {
                        return;
                }
 
-               /*
                $tables = $this->listTables();
                $db = wfGetDB( DB_MASTER );
                foreach ( $tables as $table ) {
@@ -788,7 +795,6 @@ class ParserTest {
                }
                if ($wgDBtype == 'oracle')
                        $db->query('BEGIN FILL_WIKI_INFO; END;');
-               */
        }
 
        /**
@@ -808,14 +814,13 @@ class ParserTest {
                        $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
                }
 
-               wfDebug( "Creating upload directory $dir\n" );
+               //wfDebug( "Creating upload directory $dir\n" );
                if ( file_exists( $dir ) ) {
                        wfDebug( "Already exists!\n" );
                        return $dir;
                }
                wfMkdirParents( $dir . '/3/3a' );
                copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
-
                wfMkdirParents( $dir . '/0/09' );
                copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
                return $dir;
@@ -831,10 +836,6 @@ class ParserTest {
                foreach ( $this->savedGlobals as $var => $val ) {
                        $GLOBALS[$var] = $val;
                }
-               if ( isset( $this->uploadDir ) ) {
-                       $this->teardownUploadDir( $this->uploadDir );
-                       unset( $this->uploadDir );
-               }
        }
 
        /**
@@ -1005,7 +1006,7 @@ class ParserTest {
                return preg_replace(
                        array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
                        array( $this->term->color( 34 ) . '$1' . $this->term->reset(),
-                              $this->term->color( 31 ) . '$1' . $this->term->reset() ),
+                                  $this->term->color( 31 ) . '$1' . $this->term->reset() ),
                        $text );
        }
 
@@ -1028,7 +1029,10 @@ class ParserTest {
         * @param $line Integer: the input line number, for reporting errors
         */
        public function addArticle( $name, $text, $line ) {
-               $this->setupGlobals();
+               global $wgCapitalLinks;
+               $oldCapitalLinks = $wgCapitalLinks;
+               $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637
+
                $title = Title::newFromText( $name );
                if ( is_null( $title ) ) {
                        wfDie( "invalid title at line $line\n" );
@@ -1040,18 +1044,18 @@ class ParserTest {
                }
 
                $art = new Article( $title );
-               $art->doEdit( $text, '', EDIT_NEW | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY,
-                       false, null, false, false, '', true );
+               $art->insertNewArticle( $text, '', false, false );
 
-               $this->teardownGlobals();
+               $wgCapitalLinks = $oldCapitalLinks;
        }
 
        /**
         * Steal a callback function from the primary parser, save it for
         * application to our scary parser. If the hook is not installed,
-        * die a painful dead to warn the others.
+        * abort processing of this file.
         *
         * @param $name String
+        * @return Bool true if tag hook is present
         */
        public function requireHook( $name ) {
                global $wgParser;
@@ -1059,16 +1063,19 @@ class ParserTest {
                if ( isset( $wgParser->mTagHooks[$name] ) ) {
                        $this->hooks[$name] = $wgParser->mTagHooks[$name];
                } else {
-                       wfDie( "This test suite requires the '$name' hook extension.\n" );
+                       echo "   This test suite requires the '$name' hook extension, skipping.\n";
+                       return false;
                }
+               return true;
        }
 
        /**
         * Steal a callback function from the primary parser, save it for
         * application to our scary parser. If the hook is not installed,
-        * die a painful dead to warn the others.
+        * abort processing of this file.
         *
         * @param $name String
+        * @return Bool true if function hook is present
         */
        public function requireFunctionHook( $name ) {
                global $wgParser;
@@ -1076,8 +1083,10 @@ class ParserTest {
                if ( isset( $wgParser->mFunctionHooks[$name] ) ) {
                        $this->functionHooks[$name] = $wgParser->mFunctionHooks[$name];
                } else {
-                       wfDie( "This test suite requires the '$name' function hook extension.\n" );
+                       echo "   This test suite requires the '$name' function hook extension, skipping.\n";
+                       return false;
                }
+               return true;
        }
 
        /*
@@ -1091,7 +1100,7 @@ class ParserTest {
        private function tidy( $text ) {
                global $wgUseTidy;
                if ( $wgUseTidy ) {
-                       $text = Parser::tidy( $text );
+                       $text = MWTidy::tidy( $text );
                }
                return $text;
        }
@@ -1254,7 +1263,6 @@ class DbTestPreviewer extends TestRecorder  {
         * and all that fun stuff
         */
        function start() {
-               global $wgDBtype;
                parent::start();
 
                if ( ! $this->db->tableExists( 'testrun' )
@@ -1344,25 +1352,25 @@ class DbTestPreviewer extends TestRecorder  {
        }
 
        /**
-        ** Returns a string giving information about when a test last had a status change.
-        ** Could help to track down when regressions were introduced, as distinct from tests
-        ** which have never passed (which are more change requests than regressions).
+        * Returns a string giving information about when a test last had a status change.
+        * Could help to track down when regressions were introduced, as distinct from tests
+        * which have never passed (which are more change requests than regressions).
         */
        private function getTestStatusInfo( $testname, $after ) {
 
                // If we're looking at a test that has just been removed, then say when it first appeared.
                if ( $after == 'n' ) {
                        $changedRun = $this->db->selectField ( 'testitem',
-                                                                                                  'MIN(ti_run)',
-                                                                                                  array( 'ti_name' => $testname ),
-                                                                                                  __METHOD__ );
+                               'MIN(ti_run)',
+                               array( 'ti_name' => $testname ),
+                               __METHOD__ );
                        $appear = $this->db->selectRow ( 'testrun',
-                                                                                        array( 'tr_date', 'tr_mw_version' ),
-                                                                                        array( 'tr_id' => $changedRun ),
-                                                                                        __METHOD__ );
+                               array( 'tr_date', 'tr_mw_version' ),
+                               array( 'tr_id' => $changedRun ),
+                               __METHOD__ );
                        return "First recorded appearance: "
-                              . date( "d-M-Y H:i:s",  strtotime ( $appear->tr_date ) )
-                              .  ", " . $appear->tr_mw_version;
+                                  . date( "d-M-Y H:i:s",  strtotime ( $appear->tr_date ) )
+                                  .  ", " . $appear->tr_mw_version;
                }
 
                // Otherwise, this test has previous recorded results.
@@ -1389,15 +1397,15 @@ class DbTestPreviewer extends TestRecorder  {
                // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
                // In this situation, give as much info as we can as to when it changed status.
                $pre  = $this->db->selectRow ( 'testrun',
-                                                                               array( 'tr_date', 'tr_mw_version' ),
-                                                                               array( 'tr_id' => $changedRun ),
-                                                                               __METHOD__ );
+                       array( 'tr_date', 'tr_mw_version' ),
+                       array( 'tr_id' => $changedRun ),
+                       __METHOD__ );
                $post = $this->db->selectRow ( 'testrun',
-                                                                               array( 'tr_date', 'tr_mw_version' ),
-                                                                               array( "tr_id > " . $this->db->addQuotes ( $changedRun ) ),
-                                                                               __METHOD__,
-                                                                               array( "LIMIT" => 1, "ORDER BY" => 'tr_id' )
-                                                                        );
+                       array( 'tr_date', 'tr_mw_version' ),
+                       array( "tr_id > " . $this->db->addQuotes ( $changedRun ) ),
+                       __METHOD__,
+                       array( "LIMIT" => 1, "ORDER BY" => 'tr_id' )
+               );
 
                if ( $post ) {
                        $postDate = date( "d-M-Y H:i:s",  strtotime ( $post->tr_date  ) ) . ", {$post->tr_mw_version}";
@@ -1422,12 +1430,14 @@ class DbTestPreviewer extends TestRecorder  {
 }
 
 class DbTestRecorder extends DbTestPreviewer  {
+       var $version;
+
        /**
         * Set up result recording; insert a record for the run with the date
         * and all that fun stuff
         */
        function start() {
-               global $wgDBtype, $options;
+               global $wgDBtype;
                $this->db->begin();
 
                if ( ! $this->db->tableExists( 'testrun' )
@@ -1448,8 +1458,7 @@ class DbTestRecorder extends DbTestPreviewer  {
                $this->db->insert( 'testrun',
                        array(
                                'tr_date'        => $this->db->timestamp(),
-                               'tr_mw_version'  => isset( $options['setversion'] ) ?
-                                       $options['setversion'] : SpecialVersion::getVersion(),
+                               'tr_mw_version'  => $this->version,
                                'tr_php_version' => phpversion(),
                                'tr_db_version'  => $this->db->getServerVersion(),
                                'tr_uname'       => php_uname()
@@ -1509,7 +1518,7 @@ class RemoteTestRecorder extends TestRecorder {
 
                $remote = $wgParserTestRemote;
                $revId = SpecialVersion::getSvnRevision( $IP );
-               $jsonResults = json_encode( $results );
+               $jsonResults = FormatJson::encode( $results );
 
                if ( !$remote ) {
                        print "Can't do remote upload without configuring \$wgParserTestRemote!\n";
@@ -1566,11 +1575,11 @@ class RemoteTestRecorder extends TestRecorder {
 }
 
 class TestFileIterator implements Iterator {
-    private $file;
-    private $fh;
-    private $parser;
-    private $index = 0;
-    private $test;
+       private $file;
+       private $fh;
+       private $parser;
+       private $index = 0;
+       private $test;
        private $lineNum;
        private $eof;
 
@@ -1578,8 +1587,8 @@ class TestFileIterator implements Iterator {
                global $IP;
 
                $this->file = $file;
-       $this->fh = fopen( $this->file, "rt" );
-        if ( !$this->fh ) {
+               $this->fh = fopen( $this->file, "rt" );
+               if ( !$this->fh ) {
                        wfDie( "Couldn't open file '$file'\n" );
                }
 
@@ -1603,28 +1612,28 @@ class TestFileIterator implements Iterator {
                $this->next();
 
                return true;
-    }
+       }
 
-    function current() {
+       function current() {
                return $this->test;
-    }
+       }
 
-    function key() {
+       function key() {
                return $this->index;
-    }
+       }
 
-    function next() {
-        if ( $this->readNextTest() ) {
+       function next() {
+               if ( $this->readNextTest() ) {
                        $this->index++;
                        return true;
                } else {
                        $this->eof = true;
                }
-    }
+       }
 
-    function valid() {
+       function valid() {
                return $this->eof != true;
-    }
+       }
 
        function readNextTest() {
                $data = array();
@@ -1657,7 +1666,9 @@ class TestFileIterator implements Iterator {
                                        foreach ( explode( "\n", $data['hooks'] ) as $line ) {
                                                $line = trim( $line );
                                                if ( $line ) {
-                                                       if ( $this->parser ) $this->parser->requireHook( $line );
+                                                       if ( $this->parser && !$this->parser->requireHook( $line ) ) {
+                                                               return false;
+                                                       }
                                                }
                                        }
                                        $data = array();
@@ -1671,7 +1682,9 @@ class TestFileIterator implements Iterator {
                                        foreach ( explode( "\n", $data['functionhooks'] ) as $line ) {
                                                $line = trim( $line );
                                                if ( $line ) {
-                                                       if ( $this->parser ) $this->parser->requireFunctionHook( $line );
+                                                       if ( $this->parser && !$this->parser->requireFunctionHook( $line ) ) {
+                                                               return false;
+                                                       }
                                                }
                                        }
                                        $data = array();
@@ -1702,8 +1715,9 @@ class TestFileIterator implements Iterator {
                                                $section = null;
                                                continue;
                                        }
+                                       global $wgUseTeX;
                                        if ( $this->parser &&
-                                                preg_match( '/\\bmath\\b/i', $data['options'] ) && !$this->parser->savedGlobals['wgUseTeX'] ) {
+                                                preg_match( '/\\bmath\\b/i', $data['options'] ) && !$wgUseTeX ) {
                                                # don't run math tests if $wgUseTeX is set to false in LocalSettings
                                                $data = array();
                                                $section = null;