+ public static function setUpBeforeClass() {
+ parent::setUpBeforeClass();
+
+ // NOTE: Usually, PHPUnitMaintClass::finalSetup already called this,
+ // but let's make doubly sure.
+ self::prepareServices( new GlobalVarConfig() );
+ }
+
+ /**
+ * Convenience method for getting an immutable test user
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be in.
+ * @return TestUser
+ */
+ public static function getTestUser( $groups = [] ) {
+ return TestUserRegistry::getImmutableTestUser( $groups );
+ }
+
+ /**
+ * Convenience method for getting a mutable test user
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be added in.
+ * @return TestUser
+ */
+ public static function getMutableTestUser( $groups = [] ) {
+ return TestUserRegistry::getMutableTestUser( __CLASS__, $groups );
+ }
+
+ /**
+ * Convenience method for getting an immutable admin test user
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function getTestSysop() {
+ return self::getTestUser( [ 'sysop', 'bureaucrat' ] );
+ }
+
+ /**
+ * Prepare service configuration for unit testing.
+ *
+ * This calls MediaWikiServices::resetGlobalInstance() to allow some critical services
+ * to be overridden for testing.
+ *
+ * prepareServices() only needs to be called once, but should be called as early as possible,
+ * before any class has a chance to grab a reference to any of the global services
+ * instances that get discarded by prepareServices(). Only the first call has any effect,
+ * later calls are ignored.
+ *
+ * @note This is called by PHPUnitMaintClass::finalSetup.
+ *
+ * @see MediaWikiServices::resetGlobalInstance()
+ *
+ * @param Config $bootstrapConfig The bootstrap config to use with the new
+ * MediaWikiServices. Only used for the first call to this method.
+ */
+ public static function prepareServices( Config $bootstrapConfig ) {
+ static $servicesPrepared = false;
+
+ if ( $servicesPrepared ) {
+ return;
+ } else {
+ $servicesPrepared = true;
+ }
+
+ self::resetGlobalServices( $bootstrapConfig );
+ }
+
+ /**
+ * Reset global services, and install testing environment.
+ * This is the testing equivalent of MediaWikiServices::resetGlobalInstance().
+ * This should only be used to set up the testing environment, not when
+ * running unit tests. Use overrideMwServices() for that.
+ *
+ * @see MediaWikiServices::resetGlobalInstance()
+ * @see prepareServices()
+ * @see overrideMwServices()
+ *
+ * @param Config|null $bootstrapConfig The bootstrap config to use with the new
+ * MediaWikiServices.
+ */
+ protected static function resetGlobalServices( Config $bootstrapConfig = null ) {
+ $oldServices = MediaWikiServices::getInstance();
+ $oldConfigFactory = $oldServices->getConfigFactory();
+
+ $testConfig = self::makeTestConfig( $bootstrapConfig );
+
+ MediaWikiServices::resetGlobalInstance( $testConfig );
+
+ self::$serviceLocator = MediaWikiServices::getInstance();
+ self::installTestServices(
+ $oldConfigFactory,
+ self::$serviceLocator
+ );
+ }
+
+ /**
+ * Create a config suitable for testing, based on a base config, default overrides,
+ * and custom overrides.
+ *
+ * @param Config|null $baseConfig
+ * @param Config|null $customOverrides
+ *
+ * @return Config
+ */
+ private static function makeTestConfig(
+ Config $baseConfig = null,
+ Config $customOverrides = null
+ ) {
+ $defaultOverrides = new HashConfig();
+
+ if ( !$baseConfig ) {
+ $baseConfig = MediaWikiServices::getInstance()->getBootstrapConfig();
+ }
+