Add support for testing transparent tags
authorJackmcbarn <jackmcbarn@gmail.com>
Fri, 20 Jun 2014 15:36:08 +0000 (11:36 -0400)
committerJackmcbarn <jackmcbarn@gmail.com>
Fri, 20 Jun 2014 15:36:08 +0000 (11:36 -0400)
Allow transparent tag hooks to be loaded during parser tests the way that
regular and function tag hooks can be.

Change-Id: I28ac9cc239628c248f72898d247fa1f6e2c308bd

tests/parser/parserTest.inc
tests/phpunit/includes/parser/NewParserTest.php
tests/testHelpers.inc

index 7e7c42f..5d52160 100644 (file)
@@ -145,6 +145,7 @@ class ParserTest {
 
                $this->hooks = array();
                $this->functionHooks = array();
+               $this->transparentHooks = array();
                self::setUp();
        }
 
@@ -528,6 +529,10 @@ class ParserTest {
                        $parser->setFunctionHook( $tag, $callback, $flags );
                }
 
+               foreach ( $this->transparentHooks as $tag => $callback ) {
+                       $parser->setTransparentTagHook( $tag, $callback );
+               }
+
                wfRunHooks( 'ParserTestParser', array( &$parser ) );
 
                return $parser;
@@ -1523,6 +1528,29 @@ class ParserTest {
                return true;
        }
 
+       /**
+        * Steal a callback function from the primary parser, save it for
+        * application to our scary parser. If the hook is not installed,
+        * abort processing of this file.
+        *
+        * @param string $name
+        * @return bool True if function hook is present
+        */
+       public function requireTransparentHook( $name ) {
+               global $wgParser;
+
+               $wgParser->firstCallInit(); // make sure hooks are loaded.
+
+               if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) {
+                       $this->transparentHooks[$name] = $wgParser->mTransparentTagHooks[$name];
+               } else {
+                       echo "   This test suite requires the '$name' transparent hook extension, skipping.\n";
+                       return false;
+               }
+
+               return true;
+       }
+
        /**
         * Run the "tidy" command on text if the $wgUseTidy
         * global is true
index d322e47..5c42fae 100644 (file)
@@ -25,6 +25,7 @@ class NewParserTest extends MediaWikiTestCase {
        public $savedGlobals = array();
        public $hooks = array();
        public $functionHooks = array();
+       public $transparentHooks = array();
 
        //Fuzz test
        public $maxFuzzTestLength = 300;
@@ -945,6 +946,12 @@ class NewParserTest extends MediaWikiTestCase {
                return isset( $wgParser->mFunctionHooks[$name] );
        }
 
+       public function requireTransparentHook( $name ) {
+               global $wgParser;
+               $wgParser->firstCallInit(); // make sure hooks are loaded.
+               return isset( $wgParser->mTransparentTagHooks[$name] );
+       }
+
        //Various "cleanup" functions
 
        /**
index a034031..d2e7f6e 100644 (file)
@@ -465,6 +465,22 @@ class TestFileIterator implements Iterator {
                                        continue;
                                }
 
+                               if ( $this->section == 'endtransparenthooks' ) {
+                                       $this->checkSection( 'transparenthooks' );
+
+                                       foreach ( explode( "\n", $this->sectionData['transparenthooks'] ) as $line ) {
+                                               $line = trim( $line );
+
+                                               if ( $line ) {
+                                                       $delayedParserTest->requireTransparentHook( $line );
+                                               }
+                                       }
+
+                                       $this->clearSection();
+
+                                       continue;
+                               }
+
                                if ( $this->section == 'end' ) {
                                        $this->checkSection( 'test' );
                                        // "input" and "result" are old section names allowed
@@ -601,6 +617,7 @@ class DelayedParserTest {
        /** Initialized on construction */
        private $hooks;
        private $fnHooks;
+       private $transparentHooks;
 
        public function __construct() {
                $this->reset();
@@ -613,6 +630,7 @@ class DelayedParserTest {
        public function reset() {
                $this->hooks = array();
                $this->fnHooks = array();
+               $this->transparentHooks = array();
        }
 
        /**
@@ -641,6 +659,14 @@ class DelayedParserTest {
                        }
                }
 
+               # Trigger delayed transparent hooks. Any failure will make us abort
+               foreach ( $this->transparentHooks as $hook ) {
+                       $ret = $parserTest->requireTransparentHook( $hook );
+                       if ( !$ret ) {
+                               return false;
+                       }
+               }
+
                # Delayed execution was successful.
                return true;
        }
@@ -663,6 +689,15 @@ class DelayedParserTest {
                $this->fnHooks[] = $fnHook;
        }
 
+       /**
+        * Similar to ParserTest object but does not run anything
+        * Use unleash() to really execute the hook function
+        * @param string $fnHook
+        */
+       public function requireTransparentHook( $hook ) {
+               $this->transparentHooks[] = $hook;
+       }
+
 }
 
 /**