Merge "Don't pass Config to Parser(Factory)"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 22 May 2019 10:13:48 +0000 (10:13 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 22 May 2019 10:13:48 +0000 (10:13 +0000)
RELEASE-NOTES-1.34
includes/ServiceWiring.php
includes/parser/Parser.php
includes/parser/ParserFactory.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/parser/ParserFactoryTest.php
tests/phpunit/includes/parser/ParserTest.php [new file with mode: 0644]

index f613516..f1bdfd3 100644 (file)
@@ -213,6 +213,8 @@ because of Phabricator reports.
 * The Block class is separated into Block (for blocks stored in the database),
   and SystemBlock (for temporary blocks created by the system). SystemBlock
   should be used when creating any temporary blocks.
+* Parser::$mConf is deprecated. It will be removed entirely in a later version.
+* Constructing Parser directly is deprecated. Obtain one from ParserFactory.
 
 === Other changes in 1.34 ===
 * …
index 9b064ce..c9db5a8 100644 (file)
@@ -416,13 +416,22 @@ return [
        },
 
        'ParserFactory' => function ( MediaWikiServices $services ) : ParserFactory {
-               return new ParserFactory(
+               $options = new ServiceOptions( Parser::$constructorOptions,
+                       // 'class' and 'preprocessorClass'
                        $services->getMainConfig()->get( 'ParserConf' ),
+                       // Make sure to have defaults in case someone overrode ParserConf with something silly
+                       [ 'class' => Parser::class,
+                               'preprocessorClass' => Parser::getDefaultPreprocessorClass() ],
+                       // Plus a buch of actual config options
+                       $services->getMainConfig()
+               );
+
+               return new ParserFactory(
+                       $options,
                        $services->getMagicWordFactory(),
                        $services->getContentLanguage(),
                        wfUrlProtocols(),
                        $services->getSpecialPageFactory(),
-                       $services->getMainConfig(),
                        $services->getLinkRendererFactory(),
                        $services->getNamespaceInfo()
                );
index b4caff2..27c34a6 100644 (file)
@@ -20,6 +20,7 @@
  * @file
  * @ingroup Parser
  */
+use MediaWiki\Config\ServiceOptions;
 use MediaWiki\Linker\LinkRenderer;
 use MediaWiki\Linker\LinkRendererFactory;
 use MediaWiki\Linker\LinkTarget;
@@ -169,8 +170,15 @@ class Parser {
         * @var MagicWordArray
         */
        public $mSubstWords;
+
+       /**
+        * @deprecated since 1.34, there should be no need to use this
+        * @var array
+        */
+       public $mConf;
+
        # Initialised in constructor
-       public $mConf, $mExtLinkBracketedRegex, $mUrlProtocols;
+       public $mExtLinkBracketedRegex, $mUrlProtocols;
 
        # Initialized in getPreprocessor()
        /** @var Preprocessor */
@@ -269,8 +277,14 @@ class Parser {
        /** @var SpecialPageFactory */
        private $specialPageFactory;
 
-       /** @var Config */
-       private $siteConfig;
+       /**
+        * This is called $svcOptions instead of $options like elsewhere to avoid confusion with
+        * $mOptions, which is public and widely used, and also with the local variable $options used
+        * for ParserOptions throughout this file.
+        *
+        * @var ServiceOptions
+        */
+       private $svcOptions;
 
        /** @var LinkRendererFactory */
        private $linkRendererFactory;
@@ -279,45 +293,84 @@ class Parser {
        private $nsInfo;
 
        /**
-        * @param array $parserConf See $wgParserConf documentation
+        * TODO Make this a const when HHVM support is dropped (T192166)
+        *
+        * @var array
+        * @since 1.33
+        */
+       public static $constructorOptions = [
+               // See $wgParserConf documentation
+               'class',
+               'preprocessorClass',
+               // See documentation for the corresponding config options
+               'ArticlePath',
+               'EnableScaryTranscluding',
+               'ExtraInterlanguageLinkPrefixes',
+               'FragmentMode',
+               'LanguageCode',
+               'MaxSigChars',
+               'MaxTocLevel',
+               'MiserMode',
+               'ScriptPath',
+               'Server',
+               'ServerName',
+               'ShowHostnames',
+               'Sitename',
+               'StylePath',
+               'TranscludeCacheExpiry',
+       ];
+
+       /**
+        * Constructing parsers directly is deprecated! Use a ParserFactory.
+        *
+        * @param ServiceOptions|null $svcOptions
         * @param MagicWordFactory|null $magicWordFactory
         * @param Language|null $contLang Content language
         * @param ParserFactory|null $factory
         * @param string|null $urlProtocols As returned from wfUrlProtocols()
         * @param SpecialPageFactory|null $spFactory
-        * @param Config|null $siteConfig
         * @param LinkRendererFactory|null $linkRendererFactory
         * @param NamespaceInfo|null $nsInfo
         */
        public function __construct(
-               array $parserConf = [], MagicWordFactory $magicWordFactory = null,
+               $svcOptions = null, MagicWordFactory $magicWordFactory = null,
                Language $contLang = null, ParserFactory $factory = null, $urlProtocols = null,
-               SpecialPageFactory $spFactory = null, Config $siteConfig = null,
-               LinkRendererFactory $linkRendererFactory = null,
-               NamespaceInfo $nsInfo = null
+               SpecialPageFactory $spFactory = null, $linkRendererFactory = null, $nsInfo = null
        ) {
-               $this->mConf = $parserConf;
+               $services = MediaWikiServices::getInstance();
+               if ( !$svcOptions || is_array( $svcOptions ) ) {
+                       // Pre-1.34 calling convention is the first parameter is just ParserConf, the seventh is
+                       // Config, and the eighth is LinkRendererFactory.
+                       $this->mConf = (array)$svcOptions;
+                       if ( empty( $this->mConf['class'] ) ) {
+                               $this->mConf['class'] = self::class;
+                       }
+                       if ( empty( $this->mConf['preprocessorClass'] ) ) {
+                               $this->mConf['preprocessorClass'] = self::getDefaultPreprocessorClass();
+                       }
+                       $this->svcOptions = new ServiceOptions( self::$constructorOptions,
+                               $this->mConf,
+                               func_num_args() > 6 ? func_get_arg( 6 ) : $services->getMainConfig()
+                       );
+                       $linkRendererFactory = func_num_args() > 7 ? func_get_arg( 7 ) : null;
+                       $nsInfo = func_num_args() > 8 ? func_get_arg( 8 ) : null;
+               } else {
+                       // New calling convention
+                       $svcOptions->assertRequiredOptions( self::$constructorOptions );
+                       // $this->mConf is public, so we'll keep those two options there as well for
+                       // compatibility until it's removed
+                       $this->mConf = [
+                               'class' => $svcOptions->get( 'class' ),
+                               'preprocessorClass' => $svcOptions->get( 'preprocessorClass' ),
+                       ];
+                       $this->svcOptions = $svcOptions;
+               }
+
                $this->mUrlProtocols = $urlProtocols ?? wfUrlProtocols();
                $this->mExtLinkBracketedRegex = '/\[(((?i)' . $this->mUrlProtocols . ')' .
                        self::EXT_LINK_ADDR .
                        self::EXT_LINK_URL_CLASS . '*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*?)\]/Su';
-               if ( isset( $parserConf['preprocessorClass'] ) ) {
-                       $this->mPreprocessorClass = $parserConf['preprocessorClass'];
-               } elseif ( wfIsHHVM() ) {
-                       # Under HHVM Preprocessor_Hash is much faster than Preprocessor_DOM
-                       $this->mPreprocessorClass = Preprocessor_Hash::class;
-               } elseif ( extension_loaded( 'domxml' ) ) {
-                       # PECL extension that conflicts with the core DOM extension (T15770)
-                       wfDebug( "Warning: you have the obsolete domxml extension for PHP. Please remove it!\n" );
-                       $this->mPreprocessorClass = Preprocessor_Hash::class;
-               } elseif ( extension_loaded( 'dom' ) ) {
-                       $this->mPreprocessorClass = Preprocessor_DOM::class;
-               } else {
-                       $this->mPreprocessorClass = Preprocessor_Hash::class;
-               }
-               wfDebug( __CLASS__ . ": using preprocessor: {$this->mPreprocessorClass}\n" );
 
-               $services = MediaWikiServices::getInstance();
                $this->magicWordFactory = $magicWordFactory ??
                        $services->getMagicWordFactory();
 
@@ -325,9 +378,7 @@ class Parser {
 
                $this->factory = $factory ?? $services->getParserFactory();
                $this->specialPageFactory = $spFactory ?? $services->getSpecialPageFactory();
-               $this->siteConfig = $siteConfig ?? $services->getMainConfig();
-               $this->linkRendererFactory =
-                       $linkRendererFactory ?? $services->getLinkRendererFactory();
+               $this->linkRendererFactory = $linkRendererFactory ?? $services->getLinkRendererFactory();
                $this->nsInfo = $nsInfo ?? $services->getNamespaceInfo();
        }
 
@@ -366,6 +417,28 @@ class Parser {
                Hooks::run( 'ParserCloned', [ $this ] );
        }
 
+       /**
+        * Which class should we use for the preprocessor if not otherwise specified?
+        *
+        * @since 1.34
+        * @return string
+        */
+       public static function getDefaultPreprocessorClass() {
+               if ( wfIsHHVM() ) {
+                       # Under HHVM Preprocessor_Hash is much faster than Preprocessor_DOM
+                       return Preprocessor_Hash::class;
+               }
+               if ( extension_loaded( 'domxml' ) ) {
+                       # PECL extension that conflicts with the core DOM extension (T15770)
+                       wfDebug( "Warning: you have the obsolete domxml extension for PHP. Please remove it!\n" );
+                       return Preprocessor_Hash::class;
+               }
+               if ( extension_loaded( 'dom' ) ) {
+                       return Preprocessor_DOM::class;
+               }
+               return Preprocessor_Hash::class;
+       }
+
        /**
         * Do various kinds of initialisation on the first call of the parser
         */
@@ -597,7 +670,7 @@ class Parser {
                Hooks::run( 'ParserLimitReportPrepare', [ $this, $this->mOutput ] );
 
                $limitReport = "NewPP limit report\n";
-               if ( $this->siteConfig->get( 'ShowHostnames' ) ) {
+               if ( $this->svcOptions->get( 'ShowHostnames' ) ) {
                        $limitReport .= 'Parsed by ' . wfHostname() . "\n";
                }
                $limitReport .= 'Cached time: ' . $this->mOutput->getCacheTime() . "\n";
@@ -648,7 +721,7 @@ class Parser {
                $this->mOutput->setLimitReportData( 'limitreport-timingprofile', $profileReport );
 
                // Add other cache related metadata
-               if ( $this->siteConfig->get( 'ShowHostnames' ) ) {
+               if ( $this->svcOptions->get( 'ShowHostnames' ) ) {
                        $this->mOutput->setLimitReportData( 'cachereport-origin', wfHostname() );
                }
                $this->mOutput->setLimitReportData( 'cachereport-timestamp',
@@ -969,7 +1042,7 @@ class Parser {
         */
        public function getPreprocessor() {
                if ( !isset( $this->mPreprocessor ) ) {
-                       $class = $this->mPreprocessorClass;
+                       $class = $this->svcOptions->get( 'preprocessorClass' );
                        $this->mPreprocessor = new $class( $this );
                }
                return $this->mPreprocessor;
@@ -2386,7 +2459,7 @@ class Parser {
                                if (
                                        $iw && $this->mOptions->getInterwikiMagic() && $nottalk && (
                                                Language::fetchLanguageName( $iw, null, 'mw' ) ||
-                                               in_array( $iw, $this->siteConfig->get( 'ExtraInterlanguageLinkPrefixes' ) )
+                                               in_array( $iw, $this->svcOptions->get( 'ExtraInterlanguageLinkPrefixes' ) )
                                        )
                                ) {
                                        # T26502: filter duplicates
@@ -2718,7 +2791,7 @@ class Parser {
                                break;
                        case 'revisionid':
                                if (
-                                       $this->siteConfig->get( 'MiserMode' ) &&
+                                       $this->svcOptions->get( 'MiserMode' ) &&
                                        !$this->mOptions->getInterfaceMessage() &&
                                        // @TODO: disallow this word on all namespaces
                                        $this->nsInfo->isContent( $this->mTitle->getNamespace() )
@@ -2882,21 +2955,21 @@ class Parser {
                                $value = SpecialVersion::getVersion();
                                break;
                        case 'articlepath':
-                               return $this->siteConfig->get( 'ArticlePath' );
+                               return $this->svcOptions->get( 'ArticlePath' );
                        case 'sitename':
-                               return $this->siteConfig->get( 'Sitename' );
+                               return $this->svcOptions->get( 'Sitename' );
                        case 'server':
-                               return $this->siteConfig->get( 'Server' );
+                               return $this->svcOptions->get( 'Server' );
                        case 'servername':
-                               return $this->siteConfig->get( 'ServerName' );
+                               return $this->svcOptions->get( 'ServerName' );
                        case 'scriptpath':
-                               return $this->siteConfig->get( 'ScriptPath' );
+                               return $this->svcOptions->get( 'ScriptPath' );
                        case 'stylepath':
-                               return $this->siteConfig->get( 'StylePath' );
+                               return $this->svcOptions->get( 'StylePath' );
                        case 'directionmark':
                                return $pageLang->getDirMark();
                        case 'contentlanguage':
-                               return $this->siteConfig->get( 'LanguageCode' );
+                               return $this->svcOptions->get( 'LanguageCode' );
                        case 'pagelanguage':
                                $value = $pageLang->getCode();
                                break;
@@ -3838,7 +3911,7 @@ class Parser {
         * @return string
         */
        public function interwikiTransclude( $title, $action ) {
-               if ( !$this->siteConfig->get( 'EnableScaryTranscluding' ) ) {
+               if ( !$this->svcOptions->get( 'EnableScaryTranscluding' ) ) {
                        return wfMessage( 'scarytranscludedisabled' )->inContentLanguage()->text();
                }
 
@@ -3858,7 +3931,7 @@ class Parser {
                                ( $wikiId !== false ) ? $wikiId : 'external',
                                sha1( $url )
                        ),
-                       $this->siteConfig->get( 'TranscludeCacheExpiry' ),
+                       $this->svcOptions->get( 'TranscludeCacheExpiry' ),
                        function ( $oldValue, &$ttl ) use ( $url, $fname, $cache ) {
                                $req = MWHttpRequest::factory( $url, [], $fname );
 
@@ -4230,7 +4303,7 @@ class Parser {
 
                $headlines = $numMatches !== false ? $matches[3] : [];
 
-               $maxTocLevel = $this->siteConfig->get( 'MaxTocLevel' );
+               $maxTocLevel = $this->svcOptions->get( 'MaxTocLevel' );
                foreach ( $headlines as $headline ) {
                        $isTemplate = false;
                        $titleText = false;
@@ -4684,7 +4757,7 @@ class Parser {
 
                $nickname = $nickname == null ? $username : $nickname;
 
-               if ( mb_strlen( $nickname ) > $this->siteConfig->get( 'MaxSigChars' ) ) {
+               if ( mb_strlen( $nickname ) > $this->svcOptions->get( 'MaxSigChars' ) ) {
                        $nickname = $username;
                        wfDebug( __METHOD__ . ": $username has overlong signature.\n" );
                } elseif ( $fancySig !== false ) {
@@ -6005,7 +6078,7 @@ class Parser {
        }
 
        private function makeLegacyAnchor( $sectionName ) {
-               $fragmentMode = $this->siteConfig->get( 'FragmentMode' );
+               $fragmentMode = $this->svcOptions->get( 'FragmentMode' );
                if ( isset( $fragmentMode[1] ) && $fragmentMode[1] === 'legacy' ) {
                        // ForAttribute() and ForLink() are the same for legacy encoding
                        $id = Sanitizer::escapeIdForAttribute( $sectionName, Sanitizer::ID_FALLBACK );
index cddacf4..0446d9c 100644 (file)
@@ -18,6 +18,8 @@
  * @file
  * @ingroup Parser
  */
+
+use MediaWiki\Config\ServiceOptions;
 use MediaWiki\Linker\LinkRendererFactory;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Special\SpecialPageFactory;
@@ -26,8 +28,8 @@ use MediaWiki\Special\SpecialPageFactory;
  * @since 1.32
  */
 class ParserFactory {
-       /** @var array */
-       private $parserConf;
+       /** @var ServiceOptions */
+       private $svcOptions;
 
        /** @var MagicWordFactory */
        private $magicWordFactory;
@@ -41,9 +43,6 @@ class ParserFactory {
        /** @var SpecialPageFactory */
        private $specialPageFactory;
 
-       /** @var Config */
-       private $siteConfig;
-
        /** @var LinkRendererFactory */
        private $linkRendererFactory;
 
@@ -51,31 +50,61 @@ class ParserFactory {
        private $nsInfo;
 
        /**
-        * @param array $parserConf See $wgParserConf documentation
+        * Old parameter list, which we support for backwards compatibility, were:
+        *   array $parserConf See $wgParserConf documentation
+        *   MagicWordFactory $magicWordFactory
+        *   Language $contLang Content language
+        *   string $urlProtocols As returned from wfUrlProtocols()
+        *   SpecialPageFactory $spFactory
+        *   Config $siteConfig
+        *   LinkRendererFactory $linkRendererFactory
+        *   NamespaceInfo|null $nsInfo
+        *
+        * Some type declarations were intentionally omitted so that the backwards compatibility code
+        * would work. When backwards compatibility is no longer required, we should remove it, and
+        * and add the omitted type declarations.
+        *
+        * @param ServiceOptions|array $svcOptions
         * @param MagicWordFactory $magicWordFactory
         * @param Language $contLang Content language
         * @param string $urlProtocols As returned from wfUrlProtocols()
         * @param SpecialPageFactory $spFactory
-        * @param Config $siteConfig
         * @param LinkRendererFactory $linkRendererFactory
-        * @param NamespaceInfo|null $nsInfo
+        * @param NamespaceInfo|LinkRendererFactory|null $nsInfo
         * @since 1.32
         */
        public function __construct(
-               array $parserConf, MagicWordFactory $magicWordFactory, Language $contLang, $urlProtocols,
-               SpecialPageFactory $spFactory, Config $siteConfig,
-               LinkRendererFactory $linkRendererFactory, NamespaceInfo $nsInfo = null
+               $svcOptions, MagicWordFactory $magicWordFactory, Language $contLang,
+               $urlProtocols, SpecialPageFactory $spFactory, $linkRendererFactory,
+               $nsInfo = null
        ) {
+               // @todo Do we need to retain compat for constructing this class directly?
                if ( !$nsInfo ) {
                        wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
                        $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
                }
-               $this->parserConf = $parserConf;
+               if ( $linkRendererFactory instanceof Config ) {
+                       // Old calling convention had an array in the format of $wgParserConf as the first
+                       // parameter, and a Config as the sixth, with LinkRendererFactory as the seventh.
+                       wfDeprecated( __METHOD__ . ' with Config parameter', '1.34' );
+                       $svcOptions = new ServiceOptions( Parser::$constructorOptions,
+                               $svcOptions,
+                               [ 'class' => Parser::class,
+                                       'preprocessorClass' => Parser::getDefaultPreprocessorClass() ],
+                               func_get_arg( 5 )
+                       );
+                       $linkRendererFactory = func_get_arg( 6 );
+                       $nsInfo = func_num_args() > 7 ? func_get_arg( 7 ) : null;
+               }
+               $svcOptions->assertRequiredOptions( Parser::$constructorOptions );
+
+               wfDebug( __CLASS__ . ": using preprocessor: {$svcOptions->get( 'preprocessorClass' )}\n" );
+
+               $this->svcOptions = $svcOptions;
                $this->magicWordFactory = $magicWordFactory;
                $this->contLang = $contLang;
                $this->urlProtocols = $urlProtocols;
                $this->specialPageFactory = $spFactory;
-               $this->siteConfig = $siteConfig;
                $this->linkRendererFactory = $linkRendererFactory;
                $this->nsInfo = $nsInfo;
        }
@@ -85,8 +114,8 @@ class ParserFactory {
         * @since 1.32
         */
        public function create() : Parser {
-               return new Parser( $this->parserConf, $this->magicWordFactory, $this->contLang, $this,
-                       $this->urlProtocols, $this->specialPageFactory, $this->siteConfig,
-                       $this->linkRendererFactory, $this->nsInfo );
+               return new Parser( $this->svcOptions, $this->magicWordFactory, $this->contLang, $this,
+                       $this->urlProtocols, $this->specialPageFactory, $this->linkRendererFactory,
+                       $this->nsInfo );
        }
 }
index 999813f..486b16d 100644 (file)
@@ -954,6 +954,8 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                        $newInstance->redefineService( $name, $callback );
                }
 
+               self::resetGlobalParser();
+
                return $newInstance;
        }
 
@@ -1018,6 +1020,9 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                );
 
                MediaWikiServices::forceGlobalInstance( $newServices );
+
+               self::resetGlobalParser();
+
                return $newServices;
        }
 
@@ -1046,9 +1051,26 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                MediaWikiServices::forceGlobalInstance( self::$originalServices );
                $currentServices->destroy();
 
+               self::resetGlobalParser();
+
                return true;
        }
 
+       /**
+        * If $wgParser has been unstubbed, replace it with a fresh one so it picks up any config
+        * changes. $wgParser is deprecated, but we still support it for now.
+        */
+       private static function resetGlobalParser() {
+               // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgParser
+               global $wgParser;
+               if ( $wgParser instanceof StubObject ) {
+                       return;
+               }
+               $wgParser = new StubObject( 'wgParser', function () {
+                       return MediaWikiServices::getInstance()->getParser();
+               } );
+       }
+
        /**
         * @since 1.27
         * @param string|Language $lang
index f37bdfc..048256d 100644 (file)
@@ -22,9 +22,11 @@ class ParserFactoryTest extends MediaWikiTestCase {
        public function testAllArgumentsWerePassed() {
                $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
                $mocks = [];
-               foreach ( $factoryConstructor->getParameters() as $param ) {
+               foreach ( $factoryConstructor->getParameters() as $index => $param ) {
                        $type = (string)$param->getType();
-                       if ( $type === 'array' ) {
+                       if ( $index === 0 ) {
+                               $val = $this->createMock( 'MediaWiki\Config\ServiceOptions' );
+                       } elseif ( $type === 'array' ) {
                                $val = [ 'porcupines will tell me your secrets' . count( $mocks ) ];
                        } elseif ( class_exists( $type ) || interface_exists( $type ) ) {
                                $val = $this->createMock( $type );
@@ -52,4 +54,54 @@ class ParserFactoryTest extends MediaWikiTestCase {
                $this->assertCount( 0, $mocks, 'Not all arguments to the ParserFactory constructor were ' .
                        'found in Parser member variables' );
        }
+
+       public function provideConstructorArguments() {
+               // Create a mock Config object that will satisfy ServiceOptions::__construct
+               $mockConfig = $this->createMock( 'Config' );
+               $mockConfig->method( 'has' )->willReturn( true );
+               $mockConfig->method( 'get' )->willReturn( 'I like otters.' );
+
+               $mocks = [
+                       [ 'the plural of platypus...' ],
+                       $this->createMock( 'MagicWordFactory' ),
+                       $this->createMock( 'Language' ),
+                       '...is platypodes',
+                       $this->createMock( 'MediaWiki\Special\SpecialPageFactory' ),
+                       $mockConfig,
+                       $this->createMock( 'MediaWiki\Linker\LinkRendererFactory' ),
+               ];
+
+               yield 'args_without_namespace_info' => [
+                       $mocks,
+               ];
+               yield 'args_with_namespace_info' => [
+                       array_merge( $mocks, [ $this->createMock( 'NamespaceInfo' ) ] ),
+               ];
+       }
+
+       /**
+        * @dataProvider provideConstructorArguments
+        * @covers ParserFactory::__construct
+        */
+       public function testBackwardsCompatibleConstructorArguments( $args ) {
+               $this->hideDeprecated( 'ParserFactory::__construct with Config parameter' );
+               $factory = new ParserFactory( ...$args );
+               $parser = $factory->create();
+
+               // It is expected that these are not present on the parser.
+               unset( $args[5] );
+               unset( $args[0] );
+
+               foreach ( ( new ReflectionObject( $parser ) )->getProperties() as $prop ) {
+                       $prop->setAccessible( true );
+                       foreach ( $args as $idx => $mockTest ) {
+                               if ( $prop->getValue( $parser ) === $mockTest ) {
+                                       unset( $args[$idx] );
+                               }
+                       }
+               }
+
+               $this->assertCount( 0, $args, 'Not all arguments to the ParserFactory constructor were ' .
+                       'found in Parser member variables' );
+       }
 }
diff --git a/tests/phpunit/includes/parser/ParserTest.php b/tests/phpunit/includes/parser/ParserTest.php
new file mode 100644 (file)
index 0000000..19341f5
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+
+/**
+ * @covers Parser::__construct
+ */
+class ParserTest extends MediaWikiTestCase {
+       public function provideConstructorArguments() {
+               // Create a mock Config object that will satisfy ServiceOptions::__construct
+               $mockConfig = $this->createMock( 'Config' );
+               $mockConfig->method( 'has' )->willReturn( true );
+               $mockConfig->method( 'get' )->willReturn( 'I like otters.' );
+
+               $newArgs = [
+                       $this->createMock( 'MediaWiki\Config\ServiceOptions' ),
+                       $this->createMock( 'MagicWordFactory' ),
+                       $this->createMock( 'Language' ),
+                       $this->createMock( 'ParserFactory' ),
+                       'a snail can sleep for three years',
+                       $this->createMock( 'MediaWiki\Special\SpecialPageFactory' ),
+                       $this->createMock( 'MediaWiki\Linker\LinkRendererFactory' ),
+                       $this->createMock( 'NamespaceInfo' )
+               ];
+
+               $oldArgs = [
+                       [],
+                       $this->createMock( 'MagicWordFactory' ),
+                       $this->createMock( 'Language' ),
+                       $this->createMock( 'ParserFactory' ),
+                       'a snail can sleep for three years',
+                       $this->createMock( 'MediaWiki\Special\SpecialPageFactory' )
+               ];
+
+               yield 'current_args_without_namespace_info' => [
+                       $newArgs,
+               ];
+
+               yield 'backward_compatible_args_minimal' => [
+                       array_merge( $oldArgs ),
+               ];
+
+               yield 'backward_compatible_args_with_config' => [
+                       array_merge( $oldArgs, [ $mockConfig ] ),
+               ];
+
+               yield 'backward_compatible_args_with_link_renderer' => [
+                       array_merge( $oldArgs, [
+                               $mockConfig,
+                               $this->createMock( 'MediaWiki\Linker\LinkRendererFactory' )
+                       ] ),
+               ];
+
+               yield 'backward_compatible_args_with_ns_info' => [
+                       array_merge( $oldArgs, [
+                               $mockConfig,
+                               $this->createMock( 'MediaWiki\Linker\LinkRendererFactory' ),
+                               $this->createMock( 'NamespaceInfo' )
+                       ] ),
+               ];
+       }
+
+       /**
+        * @dataProvider provideConstructorArguments
+        * @covers Parser::__construct
+        */
+       public function testBackwardsCompatibleConstructorArguments( $args ) {
+               $parser = new Parser( ...$args );
+
+               $refObject = new ReflectionObject( $parser );
+
+               // If testing backwards compatibility, test service options separately
+               if ( is_array( $args[0] ) ) {
+                       $svcOptionsProp = $refObject->getProperty( 'svcOptions' );
+                       $svcOptionsProp->setAccessible( true );
+                       $this->assertType( 'MediaWiki\Config\ServiceOptions',
+                               $svcOptionsProp->getValue( $parser )
+                       );
+                       unset( $args[0] );
+
+                       // If a Config is passed, the fact that we were able to create a ServiceOptions
+                       // instance without error from it proves that this argument works.
+                       if ( isset( $args[6] ) ) {
+                               unset( $args[6] );
+                       }
+               }
+
+               foreach ( $refObject->getProperties() as $prop ) {
+                       $prop->setAccessible( true );
+                       foreach ( $args as $idx => $mockTest ) {
+                               if ( $prop->getValue( $parser ) === $mockTest ) {
+                                       unset( $args[$idx] );
+                               }
+                       }
+               }
+
+               $this->assertCount( 0, $args, 'Not all arguments to the Parser constructor were ' .
+                       'found on the Parser object' );
+       }
+}