Merge "Consolidate parts of WebStart.php and doMaintenance.php into Initialize.php"
[lhc/web/wiklou.git] / tests / testHelpers.inc
index 717c5f3..62dccbf 100644 (file)
  */
 interface ITestRecorder {
 
-       /** Called at beginning of the parser test run */
+       /**
+        * Called at beginning of the parser test run
+        */
        public function start();
 
-       /** Called after each test */
+       /**
+        * Called after each test
+        * @param string $test
+        * @param bool $result
+        */
        public function record( $test, $result );
 
-       /** Called before finishing the test run */
+       /**
+        * Called before finishing the test run
+        */
        public function report();
 
-       /** Called at the end of the parser test run */
+       /**
+        * Called at the end of the parser test run
+        */
        public function end();
 
 }
 
 class TestRecorder implements ITestRecorder {
-       var $parent;
-       var $term;
+       public $parent;
+       public $term;
 
        function __construct( $parent ) {
                $this->parent = $parent;
@@ -107,6 +117,7 @@ class DbTestPreviewer extends TestRecorder {
 
        /**
         * This should be called before the table prefix is changed
+        * @param TestRecorder $parent
         */
        function __construct( $parent ) {
                parent::__construct( $parent );
@@ -220,6 +231,9 @@ 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).
+        * @param string $testname
+        * @param string $after
+        * @return string
         */
        private function getTestStatusInfo( $testname, $after ) {
                // If we're looking at a test that has just been removed, then say when it first appeared.
@@ -295,7 +309,7 @@ class DbTestPreviewer extends TestRecorder {
 }
 
 class DbTestRecorder extends DbTestPreviewer {
-       var $version;
+       public $version;
 
        /**
         * Set up result recording; insert a record for the run with the date
@@ -364,6 +378,10 @@ class TestFileIterator implements Iterator {
        private $sectionData = array();
        private $lineNum;
        private $eof;
+       # Create a fake parser tests which never run anything unless
+       # asked to do so. This will avoid running hooks for a disabled test
+       private $delayedParserTest;
+       private $nextSubTest = 0;
 
        function __construct( $file, $parserTest ) {
                $this->file = $file;
@@ -374,6 +392,7 @@ class TestFileIterator implements Iterator {
                }
 
                $this->parserTest = $parserTest;
+               $this->delayedParserTest = new DelayedParserTest();
 
                $this->lineNum = $this->index = 0;
        }
@@ -412,12 +431,73 @@ class TestFileIterator implements Iterator {
                return $this->eof != true;
        }
 
+       function setupCurrentTest() {
+               // "input" and "result" are old section names allowed
+               // for backwards-compatibility.
+               $input = $this->checkSection( array( 'wikitext', 'input' ), false );
+               $result = $this->checkSection( array( 'html/php', 'html/*', 'html', 'result' ), false );
+               // some tests have "with tidy" and "without tidy" variants
+               $tidy = $this->checkSection( array( 'html/php+tidy', 'html+tidy' ), false );
+               if ( $tidy != false ) {
+                       if ( $this->nextSubTest == 0 ) {
+                               if ( $result != false ) {
+                                       $this->nextSubTest = 1; // rerun non-tidy variant later
+                               }
+                               $result = $tidy;
+                       } else {
+                               $this->nextSubTest = 0; // go on to next test after this
+                               $tidy = false;
+                       }
+               }
+
+               if ( !isset( $this->sectionData['options'] ) ) {
+                       $this->sectionData['options'] = '';
+               }
+
+               if ( !isset( $this->sectionData['config'] ) ) {
+                       $this->sectionData['config'] = '';
+               }
+
+               $isDisabled = preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled;
+               $isParsoidOnly = preg_match( '/\\bparsoid\\b/i', $this->sectionData['options'] ) && $result == 'html' && !$this->parserTest->runParsoid;
+               $isFiltered = !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] );
+               if ( $input == false || $result == false || $isDisabled || $isParsoidOnly || $isFiltered ) {
+                       # disabled test
+                       return false;
+               }
+
+               # We are really going to run the test, run pending hooks and hooks function
+               wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" );
+               $hooksResult = $this->delayedParserTest->unleash( $this->parserTest );
+               if ( !$hooksResult ) {
+                       # Some hook reported an issue. Abort.
+                       throw new MWException( "Problem running hook" );
+               }
+
+               $this->test = array(
+                       'test' => ParserTest::chomp( $this->sectionData['test'] ),
+                       'input' => ParserTest::chomp( $this->sectionData[$input] ),
+                       'result' => ParserTest::chomp( $this->sectionData[$result] ),
+                       'options' => ParserTest::chomp( $this->sectionData['options'] ),
+                       'config' => ParserTest::chomp( $this->sectionData['config'] ),
+               );
+               if ( $tidy != false ) {
+                       $this->test['options'] .= " tidy";
+               }
+               return true;
+       }
+
        function readNextTest() {
-               $this->clearSection();
+               # Run additional subtests of previous test
+               while ( $this->nextSubTest > 0 ) {
+                       if ( $this->setupCurrentTest() ) {
+                               return true;
+                       }
+               }
 
-               # Create a fake parser tests which never run anything unless
-               # asked to do so. This will avoid running hooks for a disabled test
-               $delayedParserTest = new DelayedParserTest();
+               $this->clearSection();
+               # Reset hooks for the delayed test object
+               $this->delayedParserTest->reset();
 
                while ( false !== ( $line = fgets( $this->fh ) ) ) {
                        $this->lineNum++;
@@ -446,7 +526,7 @@ class TestFileIterator implements Iterator {
                                                $line = trim( $line );
 
                                                if ( $line ) {
-                                                       $delayedParserTest->requireHook( $line );
+                                                       $this->delayedParserTest->requireHook( $line );
                                                }
                                        }
 
@@ -462,7 +542,7 @@ class TestFileIterator implements Iterator {
                                                $line = trim( $line );
 
                                                if ( $line ) {
-                                                       $delayedParserTest->requireFunctionHook( $line );
+                                                       $this->delayedParserTest->requireFunctionHook( $line );
                                                }
                                        }
 
@@ -489,52 +569,15 @@ class TestFileIterator implements Iterator {
 
                                if ( $this->section == 'end' ) {
                                        $this->checkSection( 'test' );
-                                       // "input" and "result" are old section names allowed
-                                       // for backwards-compatibility.
-                                       $input = $this->checkSection( array( 'wikitext', 'input' ), false );
-                                       $result = $this->checkSection( array( 'html/php', 'html/*', 'html', 'result' ), false );
-
-                                       if ( !isset( $this->sectionData['options'] ) ) {
-                                               $this->sectionData['options'] = '';
-                                       }
-
-                                       if ( !isset( $this->sectionData['config'] ) ) {
-                                               $this->sectionData['config'] = '';
-                                       }
-
-                                       if ( $input == false || $result == false ||
-                                               ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] )
-                                                       && !$this->parserTest->runDisabled )
-                                               || ( preg_match( '/\\bparsoid\\b/i', $this->sectionData['options'] )
-                                                       && $result != 'html/php' && !$this->parserTest->runParsoid )
-                                               || !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] ) )
-                                       ) {
-                                               # disabled test
-                                               $this->clearSection();
-
-                                               # Forget any pending hooks call since test is disabled
-                                               $delayedParserTest->reset();
-
-                                               continue;
-                                       }
-
-                                       # We are really going to run the test, run pending hooks and hooks function
-                                       wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" );
-                                       $hooksResult = $delayedParserTest->unleash( $this->parserTest );
-                                       if ( !$hooksResult ) {
-                                               # Some hook reported an issue. Abort.
-                                               return false;
-                                       }
-
-                                       $this->test = array(
-                                               'test' => ParserTest::chomp( $this->sectionData['test'] ),
-                                               'input' => ParserTest::chomp( $this->sectionData[$input] ),
-                                               'result' => ParserTest::chomp( $this->sectionData[$result] ),
-                                               'options' => ParserTest::chomp( $this->sectionData['options'] ),
-                                               'config' => ParserTest::chomp( $this->sectionData['config'] ),
-                                       );
-
-                                       return true;
+                                       do {
+                                               if ( $this->setupCurrentTest() ) {
+                                                       return true;
+                                               }
+                                       } while ( $this->nextSubTest > 0 );
+                                       # go on to next test (since this was disabled)
+                                       $this->clearSection();
+                                       $this->delayedParserTest->reset();
+                                       continue;
                                }
 
                                if ( isset( $this->sectionData[$this->section] ) ) {
@@ -570,10 +613,11 @@ class TestFileIterator implements Iterator {
         * Throw an exception if it is not set, referencing current section
         * and adding the current file name and line number
         *
-        * @param string|array $token Expected token(s) that should have been
+        * @param string|array $tokens Expected token(s) that should have been
         * mentioned before closing this section
         * @param bool $fatal True iff an exception should be thrown if
         * the section is not found.
+        * @return bool|string
         */
        private function checkSection( $tokens, $fatal = true ) {
                if ( is_null( $this->section ) ) {
@@ -646,6 +690,7 @@ class DelayedParserTest {
         * Called whenever we actually want to run the hook.
         * Should be the case if we found the parserTest is not disabled
         * @param ParserTest|NewParserTest $parserTest
+        * @return bool
         */
        public function unleash( &$parserTest ) {
                if ( !( $parserTest instanceof ParserTest || $parserTest instanceof NewParserTest )     ) {
@@ -702,7 +747,7 @@ class DelayedParserTest {
        /**
         * Similar to ParserTest object but does not run anything
         * Use unleash() to really execute the hook function
-        * @param string $fnHook
+        * @param string $hook
         */
        public function requireTransparentHook( $hook ) {
                $this->transparentHooks[] = $hook;
@@ -732,7 +777,7 @@ class DjVuSupport {
        }
 
        /**
-        * Returns if the DjVu tools are usable
+        * Returns true if the DjVu tools are usable
         *
         * @return bool
         */
@@ -745,3 +790,43 @@ class DjVuSupport {
                        && is_executable( $wgDjvuTxt );
        }
 }
+
+/**
+ * Initialize and detect the tidy support
+ */
+class TidySupport {
+       private $internalTidy;
+       private $externalTidy;
+
+       /**
+        * Determine if there is a usable tidy.
+        */
+       public function __construct() {
+               global $wgTidyBin;
+
+               $this->internalTidy = extension_loaded( 'tidy' ) &&
+                       class_exists( 'tidy' );
+
+               $this->externalTidy = is_executable( $wgTidyBin ) ||
+                       Installer::locateExecutableInDefaultPaths( array( $wgTidyBin ) )
+                       !== false;
+       }
+
+       /**
+        * Returns true if we should use internal tidy.
+        *
+        * @return bool
+        */
+       public function isInternal() {
+               return $this->internalTidy;
+       }
+
+       /**
+        * Returns true if tidy is usable
+        *
+        * @return bool
+        */
+       public function isEnabled() {
+               return $this->internalTidy || $this->externalTidy;
+       }
+}