parser: Add unit tests for parser output flags ('showflags' option)
[lhc/web/wiklou.git] / tests / parser / ParserTestRunner.php
index 3ae3561..9dce73f 100644 (file)
@@ -28,6 +28,7 @@
 use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\MediaWikiServices;
 use Wikimedia\ScopedCallback;
+use Wikimedia\TestingAccessWrapper;
 
 /**
  * @ingroup Testing
@@ -265,7 +266,10 @@ class ParserTestRunner {
                $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ];
 
                // Fake constant timestamp
-               Hooks::register( 'ParserGetVariableValueTs', 'ParserTestRunner::getFakeTimestamp' );
+               Hooks::register( 'ParserGetVariableValueTs', function ( &$parser, &$ts ) {
+                       $ts = $this->getFakeTimestamp();
+                       return true;
+               } );
                $teardown[] = function () {
                        Hooks::clear( 'ParserGetVariableValueTs' );
                };
@@ -746,6 +750,7 @@ class ParserTestRunner {
                $context = RequestContext::getMain();
                $user = $context->getUser();
                $options = ParserOptions::newFromContext( $context );
+               $options->setTimestamp( $this->getFakeTimestamp() );
 
                if ( !isset( $opts['wrap'] ) ) {
                        $options->setWrapOutputClass( false );
@@ -773,6 +778,7 @@ class ParserTestRunner {
 
                if ( isset( $opts['pst'] ) ) {
                        $out = $parser->preSaveTransform( $test['input'], $title, $user, $options );
+                       $output = $parser->getOutput();
                } elseif ( isset( $opts['msg'] ) ) {
                        $out = $parser->transformMsg( $test['input'], $options, $title );
                } elseif ( isset( $opts['section'] ) ) {
@@ -823,6 +829,12 @@ class ParserTestRunner {
                        }
                }
 
+               if ( isset( $output ) && isset( $opts['showflags'] ) ) {
+                       $actualFlags = array_keys( TestingAccessWrapper::newFromObject( $output )->mFlags );
+                       sort( $actualFlags );
+                       $out .= "\nflags=" . join( ', ', $actualFlags );
+               }
+
                ScopedCallback::consume( $teardownGuard );
 
                $expected = $test['result'];
@@ -1045,6 +1057,11 @@ class ParserTestRunner {
                $context->setUser( $user );
                $context->setLanguage( $lang );
                $teardown[] = function () use ( $context ) {
+                       // Clear language conversion tables
+                       $wrapper = TestingAccessWrapper::newFromObject(
+                               $context->getLanguage()->getConverter()
+                       );
+                       $wrapper->reloadTables();
                        // Reset context to the restored globals
                        $context->setUser( $GLOBALS['wgUser'] );
                        $context->setLanguage( $GLOBALS['wgContLang'] );
@@ -1592,11 +1609,14 @@ class ParserTestRunner {
        }
 
        /**
-        * The ParserGetVariableValueTs hook, used to make sure time-related parser
+        * Fake constant timestamp to make sure time-related parser
         * functions give a persistent value.
+        *
+        * - Parser::getVariableValue (via ParserGetVariableValueTs hook)
+        * - Parser::preSaveTransform (via ParserOptions)
         */
-       static function getFakeTimestamp( &$parser, &$ts ) {
-               $ts = 123; // parsed as '1970-01-01T00:02:03Z'
-               return true;
+       private function getFakeTimestamp() {
+               // parsed as '1970-01-01T00:02:03Z'
+               return 123;
        }
 }