From fb8494b4dd3706b6ca71cb177c5a6faef179d105 Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Mon, 4 Dec 2023 17:51:38 +0100 Subject: [PATCH] Relax support for error/warning message keys with dashes This is a direct follow up for I6f05842 where we already started supporting dashes, but converted them to underscores. The only change in this patch is that the CSS class will use the message key as it is, without the dashes being converted to underscores. I added a test case specifically for this. Bug: T352676 Change-Id: Ic22e897c27b8371e3b1ed63932b619e7af71bd5c --- src/ErrorReporter.php | 6 +++++- tests/phpunit/unit/ErrorReporterTest.php | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ErrorReporter.php b/src/ErrorReporter.php index 4097ab62f..425cccf87 100644 --- a/src/ErrorReporter.php +++ b/src/ErrorReporter.php @@ -2,6 +2,7 @@ namespace Cite; +use InvalidArgumentException; use Language; use MediaWiki\Html\Html; use MediaWiki\Message\Message; @@ -119,7 +120,10 @@ class ErrorReporter { * @return string[] Two elements, e.g. "error" and "ref_numeric_key" */ private function parseTypeAndIdFromMessageKey( string $messageKey ): array { - return array_slice( explode( '_', str_replace( '-', '_', $messageKey ), 3 ), 1 ); + if ( !preg_match( '/^cite.(error|warning).(.+)/i', $messageKey, $matches ) ) { + throw new InvalidArgumentException( 'Unexpected message key' ); + } + return array_slice( $matches, 1 ); } } diff --git a/tests/phpunit/unit/ErrorReporterTest.php b/tests/phpunit/unit/ErrorReporterTest.php index ed3452eff..b1f524fec 100644 --- a/tests/phpunit/unit/ErrorReporterTest.php +++ b/tests/phpunit/unit/ErrorReporterTest.php @@ -66,6 +66,13 @@ class ErrorReporterTest extends \MediaWikiUnitTestCase { 'dir="rtl">(cite_warning|(cite_warning_example|first param))', 'expectedCategory' => null, ], + 'Optional support for messages with dashes' => [ + 'key' => 'cite-warning-with-dashes', + 'expectedHtml' => '' . + '(cite_warning|(cite-warning-with-dashes|first param))', + 'expectedCategory' => null, + ], ]; }