Merge "Avoid global state in tests involving CommentParser"

This commit is contained in:
jenkins-bot 2023-12-15 16:21:21 +00:00 committed by Gerrit Code Review
commit 70bd4e8f0e
9 changed files with 149 additions and 95 deletions

View file

@ -61,8 +61,10 @@ class CommentFormatterTest extends IntegrationTestCase {
$config = static::getJson( $config );
$data = static::getJson( $data );
$this->setupEnv( $config, $data );
$this->overrideConfigValues( [
// Consistent defaults for generating canonical URLs
MainConfigNames::Server => 'https://example.org',
MainConfigNames::CanonicalServer => 'https://example.org',
MainConfigNames::ScriptPath => '/w',
MainConfigNames::Script => '/w/index.php',
] );
@ -81,8 +83,7 @@ class CommentFormatterTest extends IntegrationTestCase {
$outputPage->method( 'getSkin' )->willReturn( $skin );
$outputPage->method( 'msg' )->willReturn( 'a label' );
MockCommentFormatter::$parser = static::createParser( $data );
MockCommentFormatter::$parser = $this->createParser( $config, $data );
$commentFormatter = TestingAccessWrapper::newFromClass( MockCommentFormatter::class );
$pout = new ParserOutput();

View file

@ -3,7 +3,6 @@
namespace MediaWiki\Extension\DiscussionTools\Tests;
use MediaWiki\Extension\DiscussionTools\CommentModifier;
use MediaWiki\MediaWikiServices;
use Wikimedia\Parsoid\Utils\DOMCompat;
use Wikimedia\Parsoid\Utils\DOMUtils;
use Wikimedia\Parsoid\Wt2Html\XMLSerializer;
@ -29,13 +28,12 @@ class CommentModifierTest extends IntegrationTestCase {
$config = static::getJson( $config );
$data = static::getJson( $data );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$title = $this->createTitleParser( $config )->parseTitle( $title );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
$comments = $threadItemSet->getCommentItems();
foreach ( $comments as $comment ) {
@ -79,13 +77,12 @@ class CommentModifierTest extends IntegrationTestCase {
$config = static::getJson( $config );
$data = static::getJson( $data );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$title = $this->createTitleParser( $config )->parseTitle( $title );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
$comments = $threadItemSet->getCommentItems();
foreach ( $comments as $comment ) {

View file

@ -9,7 +9,6 @@ use MediaWiki\Extension\DiscussionTools\ImmutableRange;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
use MediaWiki\MediaWikiServices;
use RuntimeException;
use stdClass;
use Wikimedia\Parsoid\DOM\Element;
@ -117,9 +116,11 @@ class CommentParserTest extends IntegrationTestCase {
public function testGetTimestampRegexp(
string $format, string $expected, string $message
): void {
$config = static::getJson( "../data/enwiki-config.json" );
$data = static::getJson( "../data/enwiki-data.json" );
/** @var CommentParser $parser */
$parser = TestingAccessWrapper::newFromObject(
MediaWikiServices::getInstance()->getService( 'DiscussionTools.CommentParser' )
$this->createParser( $config, $data )
);
// HACK: Fix differences between JS & PHP regexes
@ -140,17 +141,19 @@ class CommentParserTest extends IntegrationTestCase {
* @dataProvider provideTimestampParser
*/
public function testGetTimestampParser(
string $format, ?array $digits, array $data, string $expected, string $message
string $format, ?array $digits, array $matchData, string $expected, string $message
): void {
$config = static::getJson( "../data/enwiki-config.json" );
$data = static::getJson( "../data/enwiki-data.json" );
/** @var CommentParser $parser */
$parser = TestingAccessWrapper::newFromObject(
MediaWikiServices::getInstance()->getService( 'DiscussionTools.CommentParser' )
$this->createParser( $config, $data )
);
$expected = new DateTimeImmutable( $expected );
$tsParser = $parser->getTimestampParser( 'en', $format, $digits, 'UTC', [ 'UTC' => 'UTC' ] );
static::assertEquals( $expected, $tsParser( $data )['date'], $message );
static::assertEquals( $expected, $tsParser( $matchData )['date'], $message );
}
public static function provideTimestampParser(): array {
@ -164,9 +167,11 @@ class CommentParserTest extends IntegrationTestCase {
string $sample, string $expected, string $expectedUtc, string $format,
string $timezone, array $timezoneAbbrs, string $message
): void {
$config = static::getJson( "../data/enwiki-config.json" );
$data = static::getJson( "../data/enwiki-data.json" );
/** @var CommentParser $parser */
$parser = TestingAccessWrapper::newFromObject(
MediaWikiServices::getInstance()->getService( 'DiscussionTools.CommentParser' )
$this->createParser( $config, $data )
);
$regexp = $parser->getTimestampRegexp( 'en', $format, '\\d', $timezoneAbbrs );
@ -201,9 +206,8 @@ class CommentParserTest extends IntegrationTestCase {
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$title = $this->createTitleParser( $config )->parseTitle( $title );
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
$threads = $threadItemSet->getThreads();
$processedThreads = [];

View file

@ -4,7 +4,6 @@ namespace MediaWiki\Extension\DiscussionTools\Tests;
use MediaWiki\Config\GlobalVarConfig;
use MediaWiki\Extension\DiscussionTools\CommentUtils;
use MediaWiki\MediaWikiServices;
/**
* @group DiscussionTools
@ -22,9 +21,8 @@ class CommentUtilsTest extends IntegrationTestCase {
$config = static::getJson( "../data/enwiki-config.json" );
$data = static::getJson( "../data/enwiki-data.json" );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$parser = static::createParser( $data );
$title = $this->createTitleParser( $config )->parseTitle( $title );
$parser = $this->createParser( $config, $data );
$threadItemSet = $parser->parse( $container, $title );
$isSigned = CommentUtils::isSingleCommentSignedBy( $threadItemSet, $username, $container );

View file

@ -9,7 +9,6 @@ use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ThreadItem;
use MediaWiki\MediaWikiServices;
/**
* @group DiscussionTools
@ -70,8 +69,7 @@ class ContentThreadItemTest extends IntegrationTestCase {
$config = static::getJson( $config );
$data = static::getJson( $data );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$title = $this->createTitleParser( $config )->parseTitle( $title );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
@ -80,7 +78,7 @@ class ContentThreadItemTest extends IntegrationTestCase {
// comments in the sections to be treated as transcluded from another page.
CommentUtils::unwrapParsoidSections( $container );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
$comments = $threadItemSet->getCommentItems();
$transcludedFrom = [];
@ -119,9 +117,8 @@ class ContentThreadItemTest extends IntegrationTestCase {
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$title = $this->createTitleParser( $config )->parseTitle( $title );
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
$items = $threadItemSet->getThreadItems();
$output = [];
@ -162,9 +159,8 @@ class ContentThreadItemTest extends IntegrationTestCase {
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$title = $this->createTitleParser( $config )->parseTitle( $title );
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
$items = $threadItemSet->getThreadItems();
$output = [];

View file

@ -4,7 +4,6 @@ namespace MediaWiki\Extension\DiscussionTools\Tests;
use DateTimeImmutable;
use MediaWiki\Language\RawMessage;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\User\UserIdentityValue;
@ -36,9 +35,8 @@ class EventDispatcherTest extends IntegrationTestCase {
$doc2 = static::createDocument( $dom2 );
$container2 = static::getThreadContainer( $doc2 );
$this->setupEnv( $config, $data );
$dummyTitle = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( 'Dummy' );
$parser = static::createParser( $data );
$dummyTitle = $this->createTitleParser( $config )->parseTitle( 'Dummy' );
$parser = $this->createParser( $config, $data );
$itemSet1 = $parser->parse( $container1, $dummyTitle );
$itemSet2 = $parser->parse( $container2, $dummyTitle );

View file

@ -2,35 +2,10 @@
namespace MediaWiki\Extension\DiscussionTools\Tests;
use MediaWiki\MainConfigNames;
use MediaWikiIntegrationTestCase;
abstract class IntegrationTestCase extends MediaWikiIntegrationTestCase {
use TestUtils;
/**
* Setup the MW environment
*
* @param array $config
* @param array $data
*/
protected function setupEnv( array $config, array $data ): void {
$this->overrideConfigValues( [
MainConfigNames::NamespaceAliases => $config['wgNamespaceIds'],
MainConfigNames::MetaNamespace => strtr( $config['wgFormattedNamespaces'][NS_PROJECT], ' ', '_' ),
MainConfigNames::MetaNamespaceTalk =>
strtr( $config['wgFormattedNamespaces'][NS_PROJECT_TALK], ' ', '_' ),
// TODO: Move this to $config
MainConfigNames::Localtimezone => $data['localTimezone'],
// Data used for the tests assumes there are no variants for English.
// Language variants are tested using other languages.
MainConfigNames::UsePigLatinVariant => false,
// Consistent defaults for generating canonical URLs
MainConfigNames::Server => 'https://example.org',
MainConfigNames::CanonicalServer => 'https://example.org',
] + $config );
$this->setUserLang( $config['wgContentLanguage'] );
$this->setContentLang( $config['wgContentLanguage'] );
}
}

View file

@ -3,8 +3,18 @@
namespace MediaWiki\Extension\DiscussionTools\Tests;
use FormatJson;
use GenderCache;
use HashConfig;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\DiscussionTools\CommentParser;
use MediaWiki\Interwiki\NullInterwikiLookup;
use MediaWiki\Languages\LanguageConverterFactory;
use MediaWiki\Languages\LanguageFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\StaticUserOptionsLookup;
use MediaWikiTitleCodec;
use MultiConfig;
use NamespaceInfo;
use Wikimedia\Parsoid\DOM\Document;
use Wikimedia\Parsoid\DOM\Element;
use Wikimedia\Parsoid\Utils\DOMCompat;
@ -138,19 +148,118 @@ trait TestUtils {
}
/**
* Create a comment parser
*
* @param array $config
* @param array $data
* @return CommentParser
* @return array
*/
public static function createParser( array $data ): CommentParser {
private static function prepareConfig( array $config, array $data ) {
return [
'LanguageCode' => $config['wgContentLanguage'],
'ArticlePath' => $config['wgArticlePath'],
// TODO: Move this to $config
'Localtimezone' => $data['localTimezone'],
// Defaults for NamespaceInfo
'CanonicalNamespaceNames' => NamespaceInfo::CANONICAL_NAMES,
'CapitalLinkOverrides' => [],
'CapitalLinks' => true,
'ContentNamespaces' => [ NS_MAIN ],
'ExtraNamespaces' => [],
'ExtraSignatureNamespaces' => [],
'NamespaceContentModels' => [],
'NamespacesWithSubpages' => [
NS_TALK => true,
NS_USER => true,
NS_USER_TALK => true,
NS_PROJECT => true,
NS_PROJECT_TALK => true,
NS_FILE_TALK => true,
NS_MEDIAWIKI => true,
NS_MEDIAWIKI_TALK => true,
NS_TEMPLATE => true,
NS_TEMPLATE_TALK => true,
NS_HELP => true,
NS_HELP_TALK => true,
NS_CATEGORY_TALK => true
],
'NonincludableNamespaces' => [],
// Defaults for LanguageFactory
'DummyLanguageCodes' => [],
// Defaults for LanguageConverterFactory
'UsePigLatinVariant' => false,
'DisableLangConversion' => false,
'DisableTitleConversion' => false,
// Defaults for Language
'ExtraGenderNamespaces' => [],
// Overrides
'ExtraNamespaces' => array_diff_key( $config['wgFormattedNamespaces'], NamespaceInfo::CANONICAL_NAMES ),
'MetaNamespace' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT], ' ', '_' ),
'MetaNamespaceTalk' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT_TALK], ' ', '_' ),
'NamespaceAliases' => $config['wgNamespaceIds'],
];
}
public function createParser( array $config, array $data ): CommentParser {
// TODO: Derive everything from $config and $data without using global services
$services = MediaWikiServices::getInstance();
$config = self::prepareConfig( $config, $data );
$langConvFactory = new LanguageConverterFactory(
new ServiceOptions( LanguageConverterFactory::CONSTRUCTOR_OPTIONS, $config ),
$services->getObjectFactory(),
static function () use ( $services ) {
return $services->getLanguageFactory()->getLanguage( $config['LanguageCode'] );
}
);
return new CommentParser(
$services->getMainConfig(),
$services->getContentLanguage(),
$services->getLanguageConverterFactory(),
new HashConfig( $config ),
$services->getLanguageFactory()->getLanguage( $config['LanguageCode'] ),
$langConvFactory,
new MockLanguageData( $data ),
$services->getTitleParser()
$this->createTitleParser( $config )
);
}
public function createTitleParser( array $config ): MediaWikiTitleCodec {
// TODO: Derive everything from $config and $data without using global services
$services = MediaWikiServices::getInstance();
if ( isset( $config['wgArticlePath'] ) ) {
$config = self::prepareConfig( $config, [ 'localTimezone' => '' ] );
}
$nsInfo = new NamespaceInfo(
new ServiceOptions( NamespaceInfo::CONSTRUCTOR_OPTIONS, $config ),
$services->getHookContainer(),
[],
[]
);
$langFactory = new LanguageFactory(
new ServiceOptions( LanguageFactory::CONSTRUCTOR_OPTIONS, $config ),
$nsInfo,
$services->getLocalisationCache(),
$services->getLanguageNameUtils(),
$services->getLanguageFallback(),
$services->getLanguageConverterFactory(),
$services->getHookContainer(),
new MultiConfig( [ new HashConfig( $config ), $services->getMainConfig() ] )
);
$contLang = $langFactory->getLanguage( $config['LanguageCode'] );
return new MediaWikiTitleCodec(
$contLang,
new GenderCache( $nsInfo, null, new StaticUserOptionsLookup( [] ) ),
[],
new NullInterwikiLookup(),
$nsInfo
);
}
}

View file

@ -4,7 +4,6 @@ namespace MediaWiki\Extension\DiscussionTools\Tests;
use ApiTestCase;
use MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsPageInfo;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
@ -17,28 +16,6 @@ class ApiDiscussionToolsPageInfoTest extends ApiTestCase {
use TestUtils;
/**
* Setup the MW environment
*
* @param array $config
* @param array $data
*/
protected function setupEnv( array $config, array $data ): void {
$this->overrideConfigValues( [
MainConfigNames::NamespaceAliases => $config['wgNamespaceIds'],
MainConfigNames::MetaNamespace => strtr( $config['wgFormattedNamespaces'][NS_PROJECT], ' ', '_' ),
MainConfigNames::MetaNamespaceTalk =>
strtr( $config['wgFormattedNamespaces'][NS_PROJECT_TALK], ' ', '_' ),
// TODO: Move this to $config
MainConfigNames::Localtimezone => $data['localTimezone'],
// Data used for the tests assumes there are no variants for English.
// Language variants are tested using other languages.
MainConfigNames::UsePigLatinVariant => false,
] + $config );
$this->setUserLang( $config['wgContentLanguage'] );
$this->setContentLang( $config['wgContentLanguage'] );
}
/**
* @dataProvider provideGetThreadItemsHtml
*/
@ -54,9 +31,8 @@ class ApiDiscussionToolsPageInfoTest extends ApiTestCase {
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
$pageInfo = TestingAccessWrapper::newFromClass( ApiDiscussionToolsPageInfo::class );