Avoid global RequestContext in GadgetRep::validationWarnings()

This already returns safe Message objects, which allow Special:Gadgets
to localise it accordingly.

Avoid formatting the list ahead of time with a hardcoded user language,
and instead use Message::listParam() so that the caller controls
the Language object. This makes it safe to use in jobs, CLI, tests,
load.php and elsewhere instead of previously where RequestContext would
cause an exception or use something the caller doesn't control.

Bug: T298334
Change-Id: I77dd3c7fef86b4c8c41837bfb276b019f30baeb0
This commit is contained in:
Timo Tijhof 2024-01-09 20:02:52 +00:00 committed by Krinkle
parent cc4efec689
commit 1448500c2a
2 changed files with 75 additions and 3 deletions

View file

@ -7,7 +7,6 @@ use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use Message; use Message;
use RequestContext;
abstract class GadgetRepo { abstract class GadgetRepo {
@ -211,7 +210,6 @@ abstract class GadgetRepo {
* @param string $message * @param string $message
*/ */
private function maybeAddWarnings( array $entries, callable $isInvalid, array &$warnings, string $message ) { private function maybeAddWarnings( array $entries, callable $isInvalid, array &$warnings, string $message ) {
$lang = RequestContext::getMain()->getLanguage();
$invalidEntries = []; $invalidEntries = [];
foreach ( $entries as $entry ) { foreach ( $entries as $entry ) {
if ( $isInvalid( $entry ) ) { if ( $isInvalid( $entry ) ) {
@ -219,7 +217,9 @@ abstract class GadgetRepo {
} }
} }
if ( count( $invalidEntries ) ) { if ( count( $invalidEntries ) ) {
$warnings[] = wfMessage( $message, $lang->commaList( $invalidEntries ), count( $invalidEntries ) ); $warnings[] = wfMessage( $message,
Message::listParam( $invalidEntries, 'comma' ),
count( $invalidEntries ) );
} }
} }

View file

@ -0,0 +1,72 @@
<?php
use MediaWiki\Extension\Gadgets\Gadget;
use MediaWiki\Extension\Gadgets\StaticGadgetRepo;
/**
* @covers \MediaWiki\Extension\Gadgets\GadgetRepo
* @group Gadgets
* @group Database
*/
class GadgetRepoTest extends MediaWikiIntegrationTestCase {
public function setUp(): void {
parent::setUp();
$this->editPage( 'MediaWiki:Gadget-x.css', '' );
$this->editPage( 'MediaWiki:Gadget-x.js', '' );
}
/**
* @see GadgetTest::testGadgetWarnings
* @dataProvider provideValidationWarnings
*/
public function testValidationWarnings( array $options, array $expected ) {
$gadget = new Gadget( $options + [ 'name' => 'example' ] );
$repo = new StaticGadgetRepo( [] );
$warnings = $repo->validationWarnings( $gadget );
foreach ( $warnings as &$message ) {
$message = $message->inLanguage( 'qqx' )->plain();
}
$this->assertArrayEquals( $expected, $warnings );
}
public static function provideValidationWarnings() {
yield 'simple' => [
[ 'pages' => [ 'MediaWiki:Gadget-x.css', 'MediaWiki:Gadget-x.js' ] ],
[]
];
yield 'missing style' => [
[ 'pages' => [ 'MediaWiki:Gadget-test.css' ] ],
[
'(gadgets-validate-nopage: MediaWiki:Gadget-test.css)'
]
];
yield 'missing script' => [
[ 'pages' => [ 'MediaWiki:Gadget-test.js' ] ],
[
'(gadgets-validate-nopage: MediaWiki:Gadget-test.js)'
]
];
yield 'valid model' => [
[ 'pages' => [ 'MediaWiki:Gadget-x.css' ], 'requiredContentModels' => [ 'wikitext' ] ],
[]
];
yield 'invalid model' => [
[ 'pages' => [ 'MediaWiki:Gadget-x.css' ], 'requiredContentModels' => [ 'wat' ] ],
[
'(gadgets-validate-invalidcontentmodels: wat, 1)'
]
];
yield 'valid ns' => [
[ 'pages' => [ 'MediaWiki:Gadget-x.css' ], 'requiredNamespaces' => [ '1' ] ],
[]
];
yield 'invalid ns' => [
[ 'pages' => [ 'MediaWiki:Gadget-x.css' ], 'requiredNamespaces' => [ '99999', '1', '88888' ] ],
[
'(gadgets-validate-invalidnamespaces: 99999(comma-separator)88888, 2)'
]
];
}
}