2022-03-25 20:34:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Unit\Parser\Parsoid\Config;
|
|
|
|
|
|
|
|
|
|
use ILanguageConverter;
|
|
|
|
|
use Language;
|
2023-09-20 07:54:42 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
2022-03-25 20:34:04 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-05-22 14:08:45 +00:00
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
use MediaWiki\Interwiki\InterwikiLookup;
|
|
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
|
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
|
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2022-08-17 20:33:06 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-12-09 12:28:41 +00:00
|
|
|
use MediaWiki\Parser\MagicWord;
|
|
|
|
|
use MediaWiki\Parser\MagicWordArray;
|
|
|
|
|
use MediaWiki\Parser\MagicWordFactory;
|
2023-12-13 22:50:44 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2022-03-25 20:34:04 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\Config\SiteConfig;
|
|
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2023-09-18 14:17:28 +00:00
|
|
|
use MediaWiki\Title\NamespaceInfo;
|
2023-11-29 10:21:43 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2022-08-26 19:57:04 +00:00
|
|
|
use MediaWiki\Utils\UrlUtils;
|
2022-03-25 20:34:04 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
|
use MessageCache;
|
|
|
|
|
use NullStatsdDataFactory;
|
2023-07-29 10:48:43 +00:00
|
|
|
use ParserFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
use UnexpectedValueException;
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
use Wikimedia\Bcp47Code\Bcp47CodeValue;
|
2024-07-08 13:43:18 +00:00
|
|
|
use Wikimedia\Stats\StatsFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
use ZhConverter;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig
|
|
|
|
|
*/
|
|
|
|
|
class SiteConfigTest extends MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
|
|
private const DEFAULT_CONFIG = [
|
2022-08-17 20:33:06 +00:00
|
|
|
MainConfigNames::GalleryOptions => [],
|
|
|
|
|
MainConfigNames::AllowExternalImages => false,
|
|
|
|
|
MainConfigNames::AllowExternalImagesFrom => '',
|
|
|
|
|
MainConfigNames::Server => 'localhost',
|
|
|
|
|
MainConfigNames::ArticlePath => false,
|
|
|
|
|
MainConfigNames::InterwikiMagic => true,
|
|
|
|
|
MainConfigNames::ExtraInterlanguageLinkPrefixes => [],
|
2023-02-02 19:42:58 +00:00
|
|
|
MainConfigNames::InterlanguageLinkCodeMap => [],
|
2022-08-17 20:33:06 +00:00
|
|
|
MainConfigNames::LocalInterwikis => [],
|
|
|
|
|
MainConfigNames::LanguageCode => 'qqq',
|
|
|
|
|
MainConfigNames::DisableLangConversion => false,
|
|
|
|
|
MainConfigNames::NamespaceAliases => [],
|
|
|
|
|
MainConfigNames::UrlProtocols => [ 'http://' ],
|
|
|
|
|
MainConfigNames::Script => false,
|
|
|
|
|
MainConfigNames::ScriptPath => '/wiki',
|
|
|
|
|
MainConfigNames::LoadScript => false,
|
|
|
|
|
MainConfigNames::LocalTZoffset => null,
|
|
|
|
|
MainConfigNames::ThumbLimits => [ 4242 ],
|
|
|
|
|
MainConfigNames::MaxTemplateDepth => 42,
|
|
|
|
|
MainConfigNames::LegalTitleChars => 'abc',
|
|
|
|
|
MainConfigNames::NoFollowLinks => true,
|
|
|
|
|
MainConfigNames::NoFollowNsExceptions => [ 5 ],
|
|
|
|
|
MainConfigNames::NoFollowDomainExceptions => [ 'www.mediawiki.org' ],
|
|
|
|
|
MainConfigNames::ExternalLinkTarget => false,
|
2024-02-16 17:03:44 +00:00
|
|
|
MainConfigNames::EnableMagicLinks => [
|
|
|
|
|
'ISBN' => true,
|
|
|
|
|
'PMID' => true,
|
|
|
|
|
'RFC' => true,
|
|
|
|
|
],
|
2022-03-25 20:34:04 +00:00
|
|
|
];
|
|
|
|
|
|
2024-07-08 13:43:18 +00:00
|
|
|
private StatsFactory $statsFactory;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$this->statsFactory = StatsFactory::newNull();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
private function createMockOrOverride( string $class, array $overrides ) {
|
2024-08-08 13:48:11 +00:00
|
|
|
return $overrides[$class] ?? $this->createNoOpMock( $class );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO it might save code to have this helper always return a
|
|
|
|
|
* TestingAccessWrapper?
|
|
|
|
|
*
|
|
|
|
|
* @param array $configOverrides Configuration options overriding default ServiceOptions config defined in
|
|
|
|
|
* DEFAULT_CONFIG above.
|
|
|
|
|
* @param array $parsoidSettings
|
|
|
|
|
* @param array $serviceOverrides
|
|
|
|
|
*
|
|
|
|
|
* @return SiteConfig
|
|
|
|
|
*/
|
|
|
|
|
private function createSiteConfig(
|
|
|
|
|
array $configOverrides = [],
|
|
|
|
|
array $parsoidSettings = [],
|
|
|
|
|
array $serviceOverrides = []
|
|
|
|
|
): SiteConfig {
|
|
|
|
|
return new SiteConfig(
|
|
|
|
|
new ServiceOptions(
|
|
|
|
|
SiteConfig::CONSTRUCTOR_OPTIONS,
|
|
|
|
|
array_replace( self::DEFAULT_CONFIG, $configOverrides )
|
|
|
|
|
),
|
|
|
|
|
$parsoidSettings,
|
|
|
|
|
$this->createSimpleObjectFactory(),
|
|
|
|
|
$this->createMockOrOverride( Language::class, $serviceOverrides ),
|
|
|
|
|
new NullStatsdDataFactory(),
|
2024-07-08 13:43:18 +00:00
|
|
|
$this->statsFactory,
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->createMockOrOverride( MagicWordFactory::class, $serviceOverrides ),
|
|
|
|
|
$this->createMockOrOverride( NamespaceInfo::class, $serviceOverrides ),
|
|
|
|
|
$this->createMockOrOverride( SpecialPageFactory::class, $serviceOverrides ),
|
|
|
|
|
$this->createMockOrOverride( InterwikiLookup::class, $serviceOverrides ),
|
|
|
|
|
$this->createMockOrOverride( UserOptionsLookup::class, $serviceOverrides ),
|
|
|
|
|
$this->createMockOrOverride( LanguageFactory::class, $serviceOverrides ),
|
|
|
|
|
$this->createMockOrOverride( LanguageConverterFactory::class, $serviceOverrides ),
|
|
|
|
|
$this->createMockOrOverride( LanguageNameUtils::class, $serviceOverrides ),
|
2022-08-26 19:57:04 +00:00
|
|
|
$this->createMockOrOverride( UrlUtils::class, $serviceOverrides ),
|
2024-05-22 14:08:45 +00:00
|
|
|
$this->createMockOrOverride( IContentHandlerFactory::class, $serviceOverrides ),
|
2023-07-15 14:51:26 +00:00
|
|
|
[],
|
2023-07-29 10:48:43 +00:00
|
|
|
$this->createMockOrOverride( ParserFactory::class, $serviceOverrides ),
|
2023-07-15 14:51:26 +00:00
|
|
|
new HashConfig( $configOverrides ),
|
|
|
|
|
false
|
2022-03-25 20:34:04 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideConfigParameterPassed(): iterable {
|
2022-03-25 20:34:04 +00:00
|
|
|
yield 'galleryOptions' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::GalleryOptions => [ 'blabla' ] ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'galleryOptions',
|
|
|
|
|
[ 'blabla' ]
|
|
|
|
|
];
|
|
|
|
|
yield 'allowedExternalImagePrefixes, false' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::AllowExternalImages => true ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'allowedExternalImagePrefixes',
|
|
|
|
|
[ '' ]
|
|
|
|
|
];
|
|
|
|
|
yield 'allowedExternalImagePrefixes, true' => [
|
|
|
|
|
[
|
2022-08-17 20:33:06 +00:00
|
|
|
MainConfigNames::AllowExternalImages => false,
|
|
|
|
|
MainConfigNames::AllowExternalImagesFrom => [ 'blabla' ]
|
2022-03-25 20:34:04 +00:00
|
|
|
],
|
|
|
|
|
'allowedExternalImagePrefixes',
|
|
|
|
|
[ 'blabla' ]
|
|
|
|
|
];
|
|
|
|
|
yield 'interwikiMagic' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::InterwikiMagic => true ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'interwikiMagic',
|
|
|
|
|
true
|
|
|
|
|
];
|
|
|
|
|
yield 'script' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::Script => 'blabla' ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'script',
|
|
|
|
|
'blabla'
|
|
|
|
|
];
|
|
|
|
|
yield 'scriptpath' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::ScriptPath => 'blabla' ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'scriptpath',
|
|
|
|
|
'blabla'
|
|
|
|
|
];
|
|
|
|
|
yield 'server' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::Server => 'blabla' ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'server',
|
|
|
|
|
'blabla'
|
|
|
|
|
];
|
|
|
|
|
yield 'timezoneOffset' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::LocalTZoffset => 42 ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'timezoneOffset',
|
|
|
|
|
42
|
|
|
|
|
];
|
|
|
|
|
yield 'getMaxTemplateDepth' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::MaxTemplateDepth => 42 ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'getMaxTemplateDepth',
|
|
|
|
|
42
|
|
|
|
|
];
|
|
|
|
|
/* $wgLegalTitleChars can't be tested with this mechanism.
|
|
|
|
|
yield 'legalTitleChars' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::LegalTitleChars => 'blabla' ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'legalTitleChars',
|
|
|
|
|
'blabla'
|
|
|
|
|
];
|
|
|
|
|
*/
|
|
|
|
|
yield 'getProtocols' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::UrlProtocols => [ 'blabla' ] ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'getProtocols',
|
|
|
|
|
[ 'blabla' ]
|
|
|
|
|
];
|
2022-06-08 14:41:35 +00:00
|
|
|
yield 'getNoFollowConfig' => [
|
|
|
|
|
[],
|
|
|
|
|
'getNoFollowConfig',
|
|
|
|
|
[ 'nofollow' => true, 'nsexceptions' => [ 5 ], 'domainexceptions' => [ 'www.mediawiki.org' ] ]
|
|
|
|
|
];
|
|
|
|
|
yield 'getExternalLinkTargetEmpty' => [
|
|
|
|
|
[],
|
|
|
|
|
'getExternalLinkTarget',
|
|
|
|
|
false
|
|
|
|
|
];
|
|
|
|
|
yield 'getExternalLinkTargetString' => [
|
2022-08-17 20:33:06 +00:00
|
|
|
[ MainConfigNames::ExternalLinkTarget => "_blank" ],
|
2022-06-08 14:41:35 +00:00
|
|
|
'getExternalLinkTarget',
|
|
|
|
|
"_blank"
|
|
|
|
|
];
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::galleryOptions
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::allowedExternalImagePrefixes
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::interwikiMagic
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::script
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::scriptpath
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::server
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::timezoneOffset
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getMaxTemplateDepth
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::legalTitleChars
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getProtocols
|
2022-06-08 14:41:35 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getNoFollowConfig
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getExternalLinkTarget
|
2022-03-25 20:34:04 +00:00
|
|
|
* @dataProvider provideConfigParameterPassed
|
|
|
|
|
* @param array $settings
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param mixed $expectedValue
|
|
|
|
|
*/
|
|
|
|
|
public function testConfigParametersPassed(
|
|
|
|
|
array $settings,
|
|
|
|
|
string $method,
|
|
|
|
|
$expectedValue
|
|
|
|
|
) {
|
|
|
|
|
$config = $this->createSiteConfig( $settings );
|
|
|
|
|
$config = TestingAccessWrapper::newFromObject( $config );
|
|
|
|
|
$this->assertSame( $expectedValue, $config->$method() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideParsoidSettingPassed() {
|
2024-04-19 23:48:46 +00:00
|
|
|
yield 'linterEnabled' => [
|
2023-03-29 16:26:43 +00:00
|
|
|
[ 'linting' => true ],
|
2024-04-19 23:48:46 +00:00
|
|
|
'linterEnabled',
|
2022-03-25 20:34:04 +00:00
|
|
|
true
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-04-19 23:48:46 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::linterEnabled()
|
2022-03-25 20:34:04 +00:00
|
|
|
* @dataProvider provideParsoidSettingPassed
|
|
|
|
|
* @param array $settings
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param mixed $expectedValue
|
|
|
|
|
*/
|
|
|
|
|
public function testParsoidSettingPassed(
|
|
|
|
|
array $settings,
|
|
|
|
|
string $method,
|
|
|
|
|
$expectedValue
|
|
|
|
|
) {
|
|
|
|
|
$config = $this->createSiteConfig( [], $settings );
|
|
|
|
|
$config = TestingAccessWrapper::newFromObject( $config );
|
|
|
|
|
$this->assertSame( $expectedValue, $config->$method() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideServiceMethodProxied() {
|
2022-03-25 20:34:04 +00:00
|
|
|
yield 'canonicalNamespaceId' => [
|
|
|
|
|
NamespaceInfo::class, 'getCanonicalIndex', [ 'blabla_arg' ], 42, 'canonicalNamespaceId', 42
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceId' => [
|
|
|
|
|
Language::class, 'getNsIndex', [ 'blabla_arg' ], 42, 'namespaceId', 42
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceName, NS_MAIN' => [
|
|
|
|
|
Language::class, 'getFormattedNsText', [ NS_MAIN ], '', 'namespaceName', ''
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceName, NS_USER, null' => [
|
|
|
|
|
Language::class, 'getFormattedNsText', [ NS_USER ], '', 'namespaceName', null
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceName, NS_USER' => [
|
|
|
|
|
Language::class, 'getFormattedNsText', [ NS_USER ], 'User', 'namespaceName', 'User'
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceHasSubpages' => [
|
|
|
|
|
NamespaceInfo::class, 'hasSubpages', [ 42 ], true, 'namespaceHasSubpages', true
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceCase, first letter' => [
|
|
|
|
|
NamespaceInfo::class, 'isCapitalized', [ 42 ], true, 'namespaceCase', 'first-letter'
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceCase, case sensitive' => [
|
|
|
|
|
NamespaceInfo::class, 'isCapitalized', [ 42 ], false, 'namespaceCase', 'case-sensitive'
|
|
|
|
|
];
|
|
|
|
|
yield 'namespaceIsTalk' => [
|
|
|
|
|
NamespaceInfo::class, 'isTalk', [ 42 ], true, 'namespaceIsTalk', true
|
|
|
|
|
];
|
|
|
|
|
yield 'ucfirst' => [
|
|
|
|
|
Language::class, 'ucfirst', [ 'bla' ], 'Bla', 'ucfirst', 'Bla'
|
|
|
|
|
];
|
|
|
|
|
yield 'linkTrail' => [
|
|
|
|
|
Language::class, 'linkTrail', [], 'blabla', 'linkTrail', 'blabla'
|
|
|
|
|
];
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
yield 'langBcp47' => [
|
|
|
|
|
Language::class, null, [], null, 'langBcp47', '<service-mock>'
|
|
|
|
|
];
|
2022-03-25 20:34:04 +00:00
|
|
|
yield 'rtl' => [
|
|
|
|
|
Language::class, 'isRTL', [], true, 'rtl', true
|
|
|
|
|
];
|
|
|
|
|
yield 'getVariableIDs' => [
|
|
|
|
|
MagicWordFactory::class, 'getVariableIDs', [], [ 'blabla' ], 'getVariableIDs', [ 'blabla' ]
|
|
|
|
|
];
|
|
|
|
|
yield 'getFunctionSynonyms' => [
|
2023-07-29 10:48:43 +00:00
|
|
|
[ ParserFactory::class, 'getMainInstance' ], [ Parser::class, 'getFunctionSynonyms' ], [], [ 0 => [ 'blabla' ], 1 => [ 'blabla' ] ],
|
2022-03-25 20:34:04 +00:00
|
|
|
'getFunctionSynonyms', [ 0 => [ 'blabla' ], 1 => [ 'blabla' ] ]
|
|
|
|
|
];
|
|
|
|
|
yield 'getMagicWords' => [
|
|
|
|
|
Language::class, 'getMagicWords', [], [ 'blabla' ], 'getMagicWords', [ 'blabla' ]
|
|
|
|
|
];
|
|
|
|
|
yield 'getNonNativeExtensionTags' => [
|
2023-07-29 10:48:43 +00:00
|
|
|
[ ParserFactory::class, 'getMainInstance' ], [ Parser::class, 'getTags' ], [], [ 'blabla' ], 'getNonNativeExtensionTags', [ 'blabla' => true ]
|
2022-03-25 20:34:04 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideServiceMethodProxied
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::canonicalNamespaceId
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::namespaceId
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::namespaceName
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::namespaceHasSubpages
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::namespaceCase
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::namespaceIsTalk
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::ucfirst
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::linkTrail
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::langBcp47
|
2022-03-25 20:34:04 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::rtl
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::widthOption
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getVariableIDs
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getFunctionSynonyms
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getMagicWords
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getNonNativeExtensionTags
|
2023-07-29 10:48:43 +00:00
|
|
|
* @param string|array $serviceClassSpec
|
|
|
|
|
* @param ?string|array $serviceMethodSpec
|
2022-03-25 20:34:04 +00:00
|
|
|
* @param array $arguments
|
|
|
|
|
* @param mixed $returnValue
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param mixed $expectedValue
|
|
|
|
|
*/
|
|
|
|
|
public function testServiceMethodProxied(
|
2023-07-29 10:48:43 +00:00
|
|
|
$serviceClassSpec,
|
|
|
|
|
$serviceMethodSpec,
|
2022-03-25 20:34:04 +00:00
|
|
|
array $arguments,
|
|
|
|
|
$returnValue,
|
|
|
|
|
string $method,
|
|
|
|
|
$expectedValue
|
|
|
|
|
) {
|
2023-07-29 10:48:43 +00:00
|
|
|
$serviceClass = is_array( $serviceClassSpec ) ? $serviceClassSpec[0] : $serviceClassSpec;
|
2022-03-25 20:34:04 +00:00
|
|
|
$serviceMock = $this->createMock( $serviceClass );
|
2023-07-29 10:48:43 +00:00
|
|
|
if ( $serviceMethodSpec ) {
|
|
|
|
|
if ( is_array( $serviceMethodSpec ) ) {
|
|
|
|
|
// Service mock is a factory, create a object and let the factory return that object
|
|
|
|
|
$mock = $this->createMock( $serviceMethodSpec[0] );
|
|
|
|
|
$serviceMock->method( $serviceClassSpec[1] )->willReturn( $mock );
|
|
|
|
|
$serviceMethod = $serviceMethodSpec[1];
|
|
|
|
|
} else {
|
|
|
|
|
// No factory, use the service mock directly
|
|
|
|
|
$mock = $serviceMock;
|
|
|
|
|
$serviceMethod = $serviceMethodSpec;
|
|
|
|
|
}
|
|
|
|
|
// Let the mock return the expected arguments
|
|
|
|
|
$mock
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
->expects( $this->once() )
|
|
|
|
|
->method( $serviceMethod )
|
|
|
|
|
->with( ...$arguments )
|
|
|
|
|
->willReturn( $returnValue );
|
|
|
|
|
}
|
2022-03-25 20:34:04 +00:00
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
$serviceClass => $serviceMock
|
|
|
|
|
] );
|
|
|
|
|
$config = TestingAccessWrapper::newFromObject( $config );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
if ( $expectedValue === '<service-mock>' ) {
|
|
|
|
|
$expectedValue = $serviceMock;
|
|
|
|
|
}
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->assertSame( $expectedValue, $config->$method( ...$arguments ) );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideArticlePath_exception() {
|
2022-03-25 20:34:04 +00:00
|
|
|
yield 'No $1' => [ '/test/test' ];
|
|
|
|
|
yield 'Wrong path' => [ 'test\\test/$1' ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideArticlePath_exception
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::determineArticlePath
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::baseURI
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::relativeLinkPrefix
|
|
|
|
|
* @param string $articlePath
|
|
|
|
|
*/
|
|
|
|
|
public function testArticlePath_exception( string $articlePath ) {
|
|
|
|
|
$this->expectException( UnexpectedValueException::class );
|
2023-08-05 15:41:35 +00:00
|
|
|
$config = $this->createSiteConfig(
|
|
|
|
|
[
|
|
|
|
|
MainConfigNames::ArticlePath => $articlePath
|
|
|
|
|
],
|
|
|
|
|
[],
|
|
|
|
|
[
|
|
|
|
|
UrlUtils::class => $this->createMock( UrlUtils::class )
|
|
|
|
|
]
|
|
|
|
|
);
|
2022-03-25 20:34:04 +00:00
|
|
|
$config->baseURI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::determineArticlePath
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::baseURI
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::relativeLinkPrefix
|
|
|
|
|
*/
|
|
|
|
|
public function testArticlePath_nopath() {
|
2023-08-05 15:41:35 +00:00
|
|
|
$config = $this->createSiteConfig(
|
|
|
|
|
[
|
|
|
|
|
MainConfigNames::ArticlePath => '$1',
|
|
|
|
|
MainConfigNames::Server => 'https://localhost'
|
|
|
|
|
],
|
|
|
|
|
[],
|
|
|
|
|
[
|
|
|
|
|
UrlUtils::class => new UrlUtils()
|
|
|
|
|
]
|
|
|
|
|
);
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->assertSame( 'https://localhost/', $config->baseURI() );
|
|
|
|
|
$this->assertSame( './', $config->relativeLinkPrefix() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::determineArticlePath
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::baseURI
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::relativeLinkPrefix
|
|
|
|
|
*/
|
|
|
|
|
public function testArticlePath() {
|
2023-08-05 15:41:35 +00:00
|
|
|
$config = $this->createSiteConfig(
|
|
|
|
|
[
|
|
|
|
|
MainConfigNames::ArticlePath => '/wiki/$1',
|
|
|
|
|
MainConfigNames::Server => 'https://localhost'
|
|
|
|
|
],
|
|
|
|
|
[],
|
|
|
|
|
[
|
|
|
|
|
UrlUtils::class => new UrlUtils()
|
|
|
|
|
]
|
|
|
|
|
);
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->assertSame( './', $config->relativeLinkPrefix() );
|
|
|
|
|
$this->assertSame( 'https://localhost/wiki/', $config->baseURI() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::mwaToRegex
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::redirectRegexp
|
|
|
|
|
*/
|
|
|
|
|
public function testRedirectRegexp() {
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$magicWordFactoryMock = $this->createMock( MagicWordFactory::class );
|
|
|
|
|
$magicWordFactoryMock
|
|
|
|
|
->method( 'newArray' )
|
|
|
|
|
->willReturn(
|
|
|
|
|
new MagicWordArray( [ 'blabla_case_sen', 'blabla_case_insen' ], $magicWordFactoryMock )
|
|
|
|
|
);
|
|
|
|
|
$magicWordFactoryMock
|
|
|
|
|
->method( 'get' )
|
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
|
new MagicWord( 'blabla_id', [ 'blabla_synonym1' ], true, $langMock ),
|
|
|
|
|
new MagicWord( 'blabla_id', [ 'blabla_synonym2' ], false, $langMock )
|
|
|
|
|
);
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
MagicWordFactory::class => $magicWordFactoryMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame( '@(?i:blabla_synonym2)|blabla_synonym1@Su', $config->redirectRegexp() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::categoryRegexp
|
|
|
|
|
*/
|
|
|
|
|
public function testCategoryRegexp() {
|
|
|
|
|
$nsInfoMock = $this->createMock( NamespaceInfo::class );
|
|
|
|
|
$nsInfoMock
|
|
|
|
|
->method( 'getCanonicalName' )
|
|
|
|
|
->willReturn( 'Bla bla' );
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$langMock
|
|
|
|
|
->method( 'getNamespaceAliases' )
|
|
|
|
|
->willReturn( [ 'Bla_alias' => NS_CATEGORY, 'Ignored' => NS_MAIN ] );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
NamespaceInfo::class => $nsInfoMock,
|
|
|
|
|
Language::class => $langMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame( '@(?i:Bla[ _]bla|Bla[ _]alias)@', $config->categoryRegexp() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::bswRegexp
|
|
|
|
|
*/
|
|
|
|
|
public function testBswRegexp() {
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$magicWordFactoryMock = $this->createMock( MagicWordFactory::class );
|
|
|
|
|
$magicWordFactoryMock
|
|
|
|
|
->method( 'getDoubleUnderscoreArray' )
|
|
|
|
|
->willReturn(
|
|
|
|
|
new MagicWordArray( [ 'blabla' ], $magicWordFactoryMock )
|
|
|
|
|
);
|
|
|
|
|
$magicWordFactoryMock
|
|
|
|
|
->method( 'get' )
|
|
|
|
|
->willReturn(
|
|
|
|
|
new MagicWord( 'blabla_id', [ 'blabla_synonym' ], true, $langMock )
|
|
|
|
|
);
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
MagicWordFactory::class => $magicWordFactoryMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame( '@(?i:(?!))|blabla_synonym@Su', $config->bswRegexp() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::specialPageLocalName
|
|
|
|
|
*/
|
|
|
|
|
public function testSpecialPageLocalName() {
|
|
|
|
|
$specialPageFactoryMock = $this->createMock( SpecialPageFactory::class );
|
|
|
|
|
$specialPageFactoryMock
|
|
|
|
|
->method( 'resolveAlias' )
|
|
|
|
|
->with( 'blabla_alias' )
|
|
|
|
|
->willReturn( [ 'resolved_page', 'resolved_subpage' ] );
|
|
|
|
|
$specialPageFactoryMock
|
|
|
|
|
->method( 'getLocalNameFor' )
|
|
|
|
|
->with( 'resolved_page', 'resolved_subpage' )
|
|
|
|
|
->willReturn( 'blabla' );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
SpecialPageFactory::class => $specialPageFactoryMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame( 'blabla', $config->specialPageLocalName( 'blabla_alias' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::interwikiMap
|
|
|
|
|
*/
|
|
|
|
|
public function testInterwikiMap() {
|
|
|
|
|
$interwikiMock = $this->createMock( InterwikiLookup::class );
|
|
|
|
|
$interwikiMock
|
|
|
|
|
->method( 'getAllPrefixes' )
|
|
|
|
|
->willReturn( [
|
|
|
|
|
[ 'iw_prefix' => 'ru', 'iw_url' => '//test/', 'iw_local' => 1 ]
|
|
|
|
|
] );
|
|
|
|
|
$langNameUtilsMock = $this->createMock( LanguageNameUtils::class );
|
|
|
|
|
$langNameUtilsMock
|
|
|
|
|
->method( 'getLanguageNames' )
|
|
|
|
|
->willReturn( [ 'ru' => 'Russian' ] );
|
|
|
|
|
$messageCacheMock = $this->createMock( MessageCache::class );
|
|
|
|
|
$messageCacheMock
|
|
|
|
|
->method( 'get' )
|
|
|
|
|
->willReturn( false );
|
2022-08-26 19:57:04 +00:00
|
|
|
$urlUtilsMock = $this->createMock( UrlUtils::class );
|
|
|
|
|
$urlUtilsMock
|
|
|
|
|
->method( 'expand' )
|
|
|
|
|
->with( '//test/', 1 )
|
|
|
|
|
->willReturn( 'http://test/' );
|
2022-03-25 20:34:04 +00:00
|
|
|
|
|
|
|
|
$config = $this->createSiteConfig( [
|
2022-08-17 20:33:06 +00:00
|
|
|
MainConfigNames::ExtraInterlanguageLinkPrefixes => [ 'ru' ],
|
|
|
|
|
MainConfigNames::LocalInterwikis => [ 'ru' ],
|
2022-03-25 20:34:04 +00:00
|
|
|
], [], [
|
|
|
|
|
InterwikiLookup::class => $interwikiMock,
|
|
|
|
|
LanguageNameUtils::class => $langNameUtilsMock,
|
|
|
|
|
MessageCache::class => $messageCacheMock,
|
2022-08-26 19:57:04 +00:00
|
|
|
UrlUtils::class => $urlUtilsMock
|
2022-03-25 20:34:04 +00:00
|
|
|
] );
|
|
|
|
|
$this->assertSame( [
|
|
|
|
|
'ru' => [
|
|
|
|
|
'prefix' => 'ru',
|
|
|
|
|
'url' => 'http://test/$1',
|
|
|
|
|
'protorel' => true,
|
|
|
|
|
'local' => true,
|
|
|
|
|
'language' => true,
|
2023-02-02 19:42:58 +00:00
|
|
|
'bcp47' => 'ru',
|
2022-03-25 20:34:04 +00:00
|
|
|
'localinterwiki' => true,
|
|
|
|
|
'extralanglink' => true,
|
2023-02-02 19:42:58 +00:00
|
|
|
'code' => 'ru',
|
2022-03-25 20:34:04 +00:00
|
|
|
]
|
|
|
|
|
], $config->interwikiMap() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::iwp
|
|
|
|
|
*/
|
|
|
|
|
public function testIwp() {
|
|
|
|
|
$config = $this->createSiteConfig();
|
|
|
|
|
$this->assertNotNull( $config->iwp() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::linkPrefixRegex
|
|
|
|
|
*/
|
|
|
|
|
public function testLinkPrefixRegex_disabled() {
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$langMock
|
|
|
|
|
->method( 'linkPrefixExtension' )
|
|
|
|
|
->willReturn( false );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
Language::class => $langMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertNull( $config->linkPrefixRegex() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::linkPrefixRegex
|
|
|
|
|
*/
|
|
|
|
|
public function testLinkPrefixRegex() {
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$langMock
|
|
|
|
|
->method( 'linkPrefixExtension' )
|
|
|
|
|
->willReturn( true );
|
|
|
|
|
$langMock
|
|
|
|
|
->method( 'linkPrefixCharset' )
|
|
|
|
|
->willReturn( 'blabla' );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
Language::class => $langMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertStringContainsString( 'blabla', $config->linkPrefixRegex() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::mainpage
|
|
|
|
|
*/
|
|
|
|
|
public function testMainpage() {
|
|
|
|
|
$this->markTestSkipped( 'Requires MessageCache; not a unit test' );
|
|
|
|
|
$this->assertSame( 'Main Page', $this->createSiteConfig()->mainpage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::langConverterEnabledBcp47
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
public function testLangConverterEnabledBcp47_disabled() {
|
2022-03-25 20:34:04 +00:00
|
|
|
$langConverterFactoryMock = $this->createMock( LanguageConverterFactory::class );
|
|
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'isConversionDisabled' )
|
|
|
|
|
->willReturn( true );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
LanguageConverterFactory::class => $langConverterFactoryMock,
|
|
|
|
|
] );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$zh = new Bcp47CodeValue( 'zh' );
|
|
|
|
|
$this->assertFalse( $config->langConverterEnabledBcp47( $zh ) );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::langConverterEnabledBcp47
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
public function testLangConverterEnabledBcp47_invalidCode() {
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$langMock
|
|
|
|
|
->method( 'getCode' )
|
|
|
|
|
->willReturn( 'bogus' );
|
2022-03-25 20:34:04 +00:00
|
|
|
$langConverterFactoryMock = $this->createMock( LanguageConverterFactory::class );
|
|
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'isConversionDisabled' )
|
|
|
|
|
->willReturn( false );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$langFactoryMock = $this->createMock( LanguageFactory::class );
|
|
|
|
|
$langFactoryMock
|
|
|
|
|
->method( 'getLanguage' )
|
|
|
|
|
->with( $langMock )
|
|
|
|
|
->willReturn( $langMock );
|
2022-03-25 20:34:04 +00:00
|
|
|
$config = $this->createSiteConfig( [], [], [
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
LanguageFactory::class => $langFactoryMock,
|
2022-03-25 20:34:04 +00:00
|
|
|
LanguageConverterFactory::class => $langConverterFactoryMock,
|
|
|
|
|
] );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$this->assertFalse( $config->langConverterEnabledBcp47( $langMock ) );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::langConverterEnabledBcp47
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
|
|
|
|
public function testLangConverterEnabled_valid() {
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$langMock
|
|
|
|
|
->method( 'getCode' )
|
|
|
|
|
->willReturn( 'zh' );
|
2022-07-14 12:42:07 +00:00
|
|
|
$langConverterMock = $this->createMock( ZhConverter::class );
|
2022-03-25 20:34:04 +00:00
|
|
|
$langConverterMock
|
|
|
|
|
->method( 'hasVariants' )
|
|
|
|
|
->willReturn( true );
|
|
|
|
|
$langConverterFactoryMock = $this->createMock( LanguageConverterFactory::class );
|
|
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'getLanguageConverter' )
|
|
|
|
|
->with( $langMock )
|
|
|
|
|
->willReturn( $langConverterMock );
|
|
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'isConversionDisabled' )
|
|
|
|
|
->willReturn( false );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$langFactoryMock = $this->createMock( LanguageFactory::class );
|
|
|
|
|
$langFactoryMock
|
|
|
|
|
->method( 'getLanguage' )
|
|
|
|
|
->with( $langMock )
|
|
|
|
|
->willReturn( $langMock );
|
2022-03-25 20:34:04 +00:00
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
LanguageFactory::class => $langFactoryMock,
|
|
|
|
|
LanguageConverterFactory::class => $langConverterFactoryMock
|
|
|
|
|
] );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$this->assertTrue( $config->langConverterEnabledBcp47( $langMock ) );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-31 16:20:42 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::variantsFor
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
2023-07-31 16:20:42 +00:00
|
|
|
public function testVariantsFor_disabled() {
|
|
|
|
|
$langFactoryMock = $this->createMock( LanguageFactory::class );
|
|
|
|
|
$langFactoryMock
|
|
|
|
|
->method( 'getLanguage' )
|
|
|
|
|
->willReturnCallback( function ( $code ) {
|
|
|
|
|
if ( !is_string( $code ) ) {
|
|
|
|
|
$code = strtolower( $code->toBcp47Code() );
|
|
|
|
|
}
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$langMock->method( 'getCode' )
|
|
|
|
|
->willReturn( $code );
|
|
|
|
|
return $langMock;
|
|
|
|
|
} );
|
|
|
|
|
$converterMock = $this->createMock( ILanguageConverter::class );
|
|
|
|
|
$converterMock
|
|
|
|
|
->method( 'hasVariants' )
|
|
|
|
|
->willReturn( true );
|
|
|
|
|
$converterMock
|
|
|
|
|
->method( 'getVariants' )
|
|
|
|
|
->willReturn( [ 'zh-hans' ] );
|
|
|
|
|
$converterMock
|
|
|
|
|
->method( 'getVariantFallbacks' )
|
|
|
|
|
->willReturn( 'zh-fallback' );
|
2022-03-25 20:34:04 +00:00
|
|
|
$langConverterFactoryMock = $this->createMock( LanguageConverterFactory::class );
|
|
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'isConversionDisabled' )
|
|
|
|
|
->willReturn( true );
|
2023-07-31 16:20:42 +00:00
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'getLanguageConverter' )
|
|
|
|
|
->willReturnCallback( function ( $l ) use ( $converterMock ) {
|
|
|
|
|
if ( $l->getCode() === 'zh' ) {
|
|
|
|
|
return $converterMock;
|
|
|
|
|
}
|
|
|
|
|
return $this->createMock( ILanguageConverter::class );
|
|
|
|
|
} );
|
2022-03-25 20:34:04 +00:00
|
|
|
$config = $this->createSiteConfig( [], [], [
|
2023-07-31 16:20:42 +00:00
|
|
|
LanguageFactory::class => $langFactoryMock,
|
2022-03-25 20:34:04 +00:00
|
|
|
LanguageConverterFactory::class => $langConverterFactoryMock,
|
|
|
|
|
] );
|
2023-07-31 16:20:42 +00:00
|
|
|
$this->assertNull(
|
|
|
|
|
$config->variantsFor( new Bcp47CodeValue( 'zh-Hans' ) )
|
|
|
|
|
);
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-31 16:20:42 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::variantsFor
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
2023-07-31 16:20:42 +00:00
|
|
|
public function testVariantsFor() {
|
2022-03-25 20:34:04 +00:00
|
|
|
$langFactoryMock = $this->createMock( LanguageFactory::class );
|
|
|
|
|
$langFactoryMock
|
|
|
|
|
->method( 'getLanguage' )
|
|
|
|
|
->willReturnCallback( function ( $code ) {
|
2023-07-31 16:20:42 +00:00
|
|
|
if ( !is_string( $code ) ) {
|
|
|
|
|
$code = strtolower( $code->toBcp47Code() );
|
|
|
|
|
}
|
2022-03-25 20:34:04 +00:00
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$langMock->method( 'getCode' )
|
|
|
|
|
->willReturn( $code );
|
|
|
|
|
return $langMock;
|
|
|
|
|
} );
|
|
|
|
|
$converterMock = $this->createMock( ILanguageConverter::class );
|
|
|
|
|
$converterMock
|
|
|
|
|
->method( 'hasVariants' )
|
|
|
|
|
->willReturn( true );
|
|
|
|
|
$converterMock
|
|
|
|
|
->method( 'getVariants' )
|
|
|
|
|
->willReturn( [ 'zh-hans' ] );
|
|
|
|
|
$converterMock
|
|
|
|
|
->method( 'getVariantFallbacks' )
|
|
|
|
|
->willReturn( 'zh-fallback' );
|
|
|
|
|
$langConverterFactoryMock = $this->createMock( LanguageConverterFactory::class );
|
|
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'isConversionDisabled' )
|
|
|
|
|
->willReturn( false );
|
|
|
|
|
$langConverterFactoryMock
|
|
|
|
|
->method( 'getLanguageConverter' )
|
|
|
|
|
->willReturnCallback( function ( $l ) use ( $converterMock ) {
|
|
|
|
|
if ( $l->getCode() === 'zh' ) {
|
|
|
|
|
return $converterMock;
|
|
|
|
|
}
|
|
|
|
|
return $this->createMock( ILanguageConverter::class );
|
|
|
|
|
} );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
LanguageFactory::class => $langFactoryMock,
|
|
|
|
|
LanguageConverterFactory::class => $langConverterFactoryMock
|
|
|
|
|
] );
|
2023-07-31 16:20:42 +00:00
|
|
|
$variantsForZh = $config->variantsFor( new Bcp47CodeValue( 'zh-Hans' ) );
|
|
|
|
|
$this->assertIsArray( $variantsForZh );
|
|
|
|
|
$this->assertArrayHasKey( 'base', $variantsForZh );
|
|
|
|
|
$this->assertEquals( 'zh', $variantsForZh['base']->getCode() );
|
|
|
|
|
$this->assertArrayHasKey( 'fallbacks', $variantsForZh );
|
|
|
|
|
$fallbacks = $variantsForZh['fallbacks'];
|
|
|
|
|
$this->assertIsArray( $fallbacks );
|
|
|
|
|
$this->assertCount( 1, $fallbacks );
|
|
|
|
|
$this->assertEquals( 'zh-fallback', $fallbacks[0]->getCode() );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::widthOption
|
|
|
|
|
*/
|
|
|
|
|
public function testWithOption() {
|
|
|
|
|
$optionsLookupMock = $this->createMock( UserOptionsLookup::class );
|
|
|
|
|
$optionsLookupMock
|
|
|
|
|
->method( 'getDefaultOption' )
|
|
|
|
|
->with( 'thumbsize' )
|
|
|
|
|
->willReturn( 'small' );
|
|
|
|
|
$config = $this->createSiteConfig( [
|
2022-08-17 20:33:06 +00:00
|
|
|
MainConfigNames::ThumbLimits => [ 'small' => 42 ]
|
2022-03-25 20:34:04 +00:00
|
|
|
], [], [
|
|
|
|
|
UserOptionsLookup::class => $optionsLookupMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame( 42, $config->widthOption() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getMagicWordMatcher
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMagicWordMatcher() {
|
|
|
|
|
$magicWordMock = $this->createMock( MagicWord::class );
|
|
|
|
|
$magicWordMock
|
|
|
|
|
->expects( $this->once() )
|
|
|
|
|
->method( 'getRegexStartToEnd' )
|
|
|
|
|
->willReturn( 'blabla' );
|
|
|
|
|
$magicWordFactoryMock = $this->createMock( MagicWordFactory::class );
|
|
|
|
|
$magicWordFactoryMock
|
|
|
|
|
->expects( $this->once() )
|
|
|
|
|
->method( 'get' )
|
|
|
|
|
->with( 'blabla_id' )
|
|
|
|
|
->willReturn( $magicWordMock );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
MagicWordFactory::class => $magicWordFactoryMock
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame( 'blabla', $config->getMagicWordMatcher( 'blabla_id' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getParameterizedAliasMatcher
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParameterizedAliasMatcher() {
|
|
|
|
|
$langMock = $this->createMock( Language::class );
|
|
|
|
|
$magicWordFactoryMock = $this->createMock( MagicWordFactory::class );
|
|
|
|
|
$magicWordFactoryMock
|
|
|
|
|
->method( 'newArray' )
|
|
|
|
|
->willReturn( new MagicWordArray( [ 'test' ], $magicWordFactoryMock ) );
|
|
|
|
|
$magicWordFactoryMock
|
|
|
|
|
->method( 'get' )
|
|
|
|
|
->willReturn( new MagicWord( 'blabla_id', [ 'blabla_alias:$1' ], true, $langMock ) );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
MagicWordFactory::class => $magicWordFactoryMock
|
|
|
|
|
] );
|
|
|
|
|
$matcher = $config->getParameterizedAliasMatcher( [ 'blabla' ] );
|
|
|
|
|
$this->assertSame( [ 'k' => 'test', 'v' => 'blabla' ], $matcher( 'blabla_alias:blabla' ) );
|
|
|
|
|
$this->assertNull( $matcher( 'Blabla_alias:blabla' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getSpecialNSAliases
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSpecialNSAliases() {
|
|
|
|
|
$mockLang = $this->createMock( Language::class );
|
|
|
|
|
$mockLang
|
|
|
|
|
->method( 'getNsText' )
|
|
|
|
|
->willReturn( 'Special_Special' );
|
|
|
|
|
$mockLang
|
|
|
|
|
->method( 'getNamespaceAliases' )
|
|
|
|
|
->willReturn( [
|
|
|
|
|
'From Language' => NS_SPECIAL,
|
|
|
|
|
'Whatever' => NS_MAIN
|
|
|
|
|
] );
|
|
|
|
|
$config = $this->createSiteConfig( [
|
2022-08-17 20:33:06 +00:00
|
|
|
MainConfigNames::NamespaceAliases => [
|
2022-03-25 20:34:04 +00:00
|
|
|
'From Config' => NS_SPECIAL,
|
|
|
|
|
'Whatever' => NS_MAIN
|
|
|
|
|
]
|
|
|
|
|
], [], [
|
|
|
|
|
Language::class => $mockLang
|
|
|
|
|
] );
|
|
|
|
|
$config = TestingAccessWrapper::newFromObject( $config );
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
[ 'Special', 'Special[ _]Special', 'From[ _]Language', 'From[ _]Config' ],
|
|
|
|
|
$config->getSpecialNSAliases()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getSpecialPageAliases
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSpecialPageAliases() {
|
|
|
|
|
$mockLang = $this->createMock( Language::class );
|
|
|
|
|
$mockLang
|
|
|
|
|
->method( 'getSpecialPageAliases' )
|
|
|
|
|
->willReturn( [
|
|
|
|
|
'Page1' => [ 'Alias1', 'Alias2' ]
|
|
|
|
|
] );
|
|
|
|
|
$config = $this->createSiteConfig( [], [], [
|
|
|
|
|
Language::class => $mockLang
|
|
|
|
|
] );
|
|
|
|
|
$config = TestingAccessWrapper::newFromObject( $config );
|
|
|
|
|
$this->assertSame( [ 'Page1', 'Alias1', 'Alias2' ], $config->getSpecialPageAliases( 'Page1' ) );
|
|
|
|
|
$this->assertSame( [ 'Page2' ], $config->getSpecialPageAliases( 'Page2' ) );
|
|
|
|
|
}
|
2023-03-07 06:15:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::getMWConfigValue
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMWConfigValue() {
|
|
|
|
|
$config = $this->createSiteConfig( [
|
|
|
|
|
'CiteResponsiveReferences' => true,
|
|
|
|
|
'CiteResponsiveReferencesThreshold' => 10,
|
|
|
|
|
], [], [] );
|
|
|
|
|
$config = TestingAccessWrapper::newFromObject( $config );
|
|
|
|
|
$this->assertSame( true, $config->getMWConfigValue( 'CiteResponsiveReferences' ) );
|
|
|
|
|
$this->assertSame( 10, $config->getMWConfigValue( 'CiteResponsiveReferencesThreshold' ) );
|
|
|
|
|
$this->assertSame( null, $config->getMWConfigValue( 'CiteUnknownConfig' ) );
|
|
|
|
|
}
|
2024-07-08 13:43:18 +00:00
|
|
|
|
|
|
|
|
public function provideMetricsData(): iterable {
|
|
|
|
|
return [ [
|
|
|
|
|
"metric_name",
|
|
|
|
|
[
|
|
|
|
|
[ "label1" => "value1", "label2" => "value2" ],
|
|
|
|
|
[ "label1" => "value1", "label2" => "value3" ]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[ "value1", "value2" ],
|
|
|
|
|
[ "value1", "value3" ] ]
|
|
|
|
|
] ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::incrementCounter
|
|
|
|
|
* @dataProvider provideMetricsData
|
|
|
|
|
*/
|
|
|
|
|
public function testIncrementCounter( $name, $labels, $expectedValues ) {
|
|
|
|
|
$config = $this->createSiteConfig();
|
|
|
|
|
$config->incrementCounter( $name, $labels[0] );
|
|
|
|
|
$config->incrementCounter( $name, $labels[1] );
|
|
|
|
|
$counter = $this->statsFactory->withComponent( "Parsoid" )->getCounter( $name );
|
|
|
|
|
$this->assertSame( 2, $counter->getSampleCount() );
|
|
|
|
|
$this->assertSame( $expectedValues[0], $counter->getSamples()[0]->getLabelValues() );
|
|
|
|
|
$this->assertSame( 1.0, $counter->getSamples()[0]->getValue() );
|
|
|
|
|
$this->assertSame( $expectedValues[1], $counter->getSamples()[1]->getLabelValues() );
|
|
|
|
|
$this->assertSame( 1.0, $counter->getSamples()[1]->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\Config\SiteConfig::observeTiming
|
|
|
|
|
* @dataProvider provideMetricsData
|
|
|
|
|
*/
|
|
|
|
|
public function testObserveTiming( $name, $labels, $expectedValues ) {
|
|
|
|
|
$config = $this->createSiteConfig();
|
|
|
|
|
$config->observeTiming( $name, 1500.1, $labels[0] );
|
|
|
|
|
$config->observeTiming( $name, 2500.2, $labels[1] );
|
|
|
|
|
$counter = $this->statsFactory->withComponent( "Parsoid" )->getTiming( $name );
|
|
|
|
|
$this->assertSame( 2, $counter->getSampleCount() );
|
|
|
|
|
$this->assertSame( $expectedValues[0], $counter->getSamples()[0]->getLabelValues() );
|
|
|
|
|
$this->assertSame( 1500.1, $counter->getSamples()[0]->getValue() );
|
|
|
|
|
$this->assertSame( $expectedValues[1], $counter->getSamples()[1]->getLabelValues() );
|
|
|
|
|
$this->assertSame( 2500.2, $counter->getSamples()[1]->getValue() );
|
|
|
|
|
}
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|