Merge "Cache the result of User::getDefaultOptions"
[lhc/web/wiklou.git] / tests / testHelpers.inc
index 9287d53..4b2e923 100644 (file)
@@ -1,4 +1,25 @@
 <?php
+/**
+ * Recording for passing/failing tests.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Testing
+ */
 
 class TestRecorder {
        var $parent;
@@ -313,8 +334,6 @@ class TestFileIterator implements Iterator {
        private $eof;
 
        function __construct( $file, $parserTest ) {
-               global $IP;
-
                $this->file = $file;
                $this->fh = fopen( $this->file, "rt" );
 
@@ -323,7 +342,6 @@ class TestFileIterator implements Iterator {
                }
 
                $this->parserTest = $parserTest;
-               $this->parserTest->showRunFile( wfRelativePath( $this->file, $IP ) );
 
                $this->lineNum = $this->index = 0;
        }
@@ -365,6 +383,10 @@ class TestFileIterator implements Iterator {
        function readNextTest() {
                $this->clearSection();
 
+               # 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();
+
                while ( false !== ( $line = fgets( $this->fh ) ) ) {
                        $this->lineNum++;
                        $matches = array();
@@ -390,9 +412,7 @@ class TestFileIterator implements Iterator {
                                                $line = trim( $line );
 
                                                if ( $line ) {
-                                                       if ( !$this->parserTest->requireHook( $line ) ) {
-                                                               return false;
-                                                       }
+                                                       $delayedParserTest->requireHook( $line );
                                                }
                                        }
 
@@ -408,9 +428,7 @@ class TestFileIterator implements Iterator {
                                                $line = trim( $line );
 
                                                if ( $line ) {
-                                                       if ( !$this->parserTest->requireFunctionHook( $line ) ) {
-                                                               return false;
-                                                       }
+                                                       $delayedParserTest->requireFunctionHook( $line );
                                                }
                                        }
 
@@ -428,17 +446,29 @@ class TestFileIterator implements Iterator {
                                                $this->sectionData['options'] = '';
                                        }
 
-                                       if ( !isset( $this->sectionData['config'] ) )
+                                       if ( !isset( $this->sectionData['config'] ) ) {
                                                $this->sectionData['config'] = '';
+                                       }
 
                                        if ( ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled )
                                                         || !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']   ),
@@ -451,7 +481,7 @@ class TestFileIterator implements Iterator {
                                }
 
                                if ( isset ( $this->sectionData[$this->section] ) ) {
-                                       throw new MWException( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" );
+                                       throw new MWException( "duplicate section '$this->section' at line {$this->lineNum} of $this->file\n" );
                                }
 
                                $this->sectionData[$this->section] = '';
@@ -474,7 +504,7 @@ class TestFileIterator implements Iterator {
        private function clearSection() {
                $this->sectionData = array();
                $this->section = null;
-               
+
        }
 
        /**
@@ -483,7 +513,7 @@ 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 $token String: expected token that should have been mentionned before closing this section 
+        * @param $token String: expected token that should have been mentionned before closing this section
         */
        private function checkSection( $token ) {
                if( is_null( $this->section ) ) {
@@ -502,3 +532,72 @@ class TestFileIterator implements Iterator {
                return true;
        }
 }
+
+/**
+ * A class to delay execution of a parser test hooks.
+ */
+class DelayedParserTest {
+
+       /** Initialized on construction */
+       private $hooks;
+       private $fnHooks;
+
+       public function __construct() {
+               $this->reset();
+       }
+
+       /**
+        * Init/reset or forgot about the current delayed test.
+        * Call to this will erase any hooks function that were pending.
+        */
+       public function reset() {
+               $this->hooks   = array();
+               $this->fnHooks = array();
+       }
+
+       /**
+        * Called whenever we actually want to run the hook.
+        * Should be the case if we found the parserTest is not disabled
+        */
+       public function unleash( &$parserTest ) {
+               if( !($parserTest instanceof ParserTest || $parserTest instanceof NewParserTest
+               ) ) {
+                       throw new MWException( __METHOD__ . " must be passed an instance of ParserTest or NewParserTest classes\n" );
+               }
+
+               # Trigger delayed hooks. Any failure will make us abort
+               foreach( $this->hooks as $hook ) {
+                       $ret = $parserTest->requireHook( $hook );
+                       if( !$ret ) {
+                               return false;
+                       }
+               }
+
+               # Trigger delayed function hooks. Any failure will make us abort
+               foreach( $this->fnHooks as $fnHook ) {
+                       $ret = $parserTest->requireFunctionHook( $fnHook );
+                       if( !$ret ) {
+                               return false;
+                       }
+               }
+
+               # Delayed execution was successful.
+               return true;
+       }
+
+       /**
+        * Similar to ParserTest object but does not run anything
+        * Use unleash() to really execute the hook
+        */
+       public function requireHook( $hook ) {
+               $this->hooks[] = $hook;
+       }
+       /**
+        * Similar to ParserTest object but does not run anything
+        * Use unleash() to really execute the hook function
+        */
+       public function requireFunctionHook( $fnHook ) {
+               $this->fnHooks[] = $fnHook;
+       }
+
+}