From 2d1c6e26342d766fecf263be9ac08ffc0361b70f Mon Sep 17 00:00:00 2001 From: Jackmcbarn Date: Fri, 20 Jun 2014 11:36:08 -0400 Subject: [PATCH] Add support for testing transparent tags 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 | 28 +++++++++++++++ .../phpunit/includes/parser/NewParserTest.php | 7 ++++ tests/testHelpers.inc | 35 +++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/tests/parser/parserTest.inc b/tests/parser/parserTest.inc index 7e7c42ffce..5d521608a7 100644 --- a/tests/parser/parserTest.inc +++ b/tests/parser/parserTest.inc @@ -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 diff --git a/tests/phpunit/includes/parser/NewParserTest.php b/tests/phpunit/includes/parser/NewParserTest.php index d322e47212..5c42faecb0 100644 --- a/tests/phpunit/includes/parser/NewParserTest.php +++ b/tests/phpunit/includes/parser/NewParserTest.php @@ -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 /** diff --git a/tests/testHelpers.inc b/tests/testHelpers.inc index a0340314b9..d2e7f6e31a 100644 --- a/tests/testHelpers.inc +++ b/tests/testHelpers.inc @@ -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; + } + } /** -- 2.20.1