aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit
diff options
context:
space:
mode:
authorbpirkle <bpirkle@wikimedia.org>2025-03-13 14:34:58 -0500
committerbpirkle <bpirkle@wikimedia.org>2025-03-21 11:54:50 -0500
commitbf5f59a1e9f94f4a7c612d657e0e455fde60dd3c (patch)
treee0c2493316a81d13a78d07f537d75cbe828eefc0 /tests/phpunit/unit
parent8e343d805b16b893bde6ec6c61f4d5c519c0ba51 (diff)
downloadmediawikicore-bf5f59a1e9f94f4a7c612d657e0e455fde60dd3c.tar.gz
mediawikicore-bf5f59a1e9f94f4a7c612d657e0e455fde60dd3c.zip
REST: Make OpenAPI spec info strings translatable
OpenAPI specs include an "info" section that includes strings such as "title" and "description" that are intended to be human-readable. Make all such strings translatable. Bug: T385855 Change-Id: I15285be6d196c0e7fd7e922f23058d7c09b6b31a
Diffstat (limited to 'tests/phpunit/unit')
-rw-r--r--tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php6
-rw-r--r--tests/phpunit/unit/includes/Rest/JsonLocalizerTest.php138
2 files changed, 144 insertions, 0 deletions
diff --git a/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php b/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php
index 3a64f2a7794d..351ae9e7ae8d 100644
--- a/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php
+++ b/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php
@@ -91,6 +91,12 @@ trait HandlerTestTrait {
$hooks instanceof HookContainer ? $hooks : $this->createHookContainer( $hooks );
$session ??= $this->getSession( true );
+
+ // TODO: Even if we are given a Router (either directly or via a Module), we aren't using
+ // its formatter, authority, or session to initialize the handler. We can individually
+ // override authority and session by passing them as parameters, but not formatter.
+ // Consider either adding a formatter parameter, or using these values from any supplied
+ // Router. (Router does not currently provide accessors, making this inconvenient.)
$handler->initContext( $module, $config['path'] ?? 'test', $config );
$handler->initServices( $authority, $responseFactory, $hookContainer );
$handler->initSession( $session );
diff --git a/tests/phpunit/unit/includes/Rest/JsonLocalizerTest.php b/tests/phpunit/unit/includes/Rest/JsonLocalizerTest.php
new file mode 100644
index 000000000000..84f9232a84bf
--- /dev/null
+++ b/tests/phpunit/unit/includes/Rest/JsonLocalizerTest.php
@@ -0,0 +1,138 @@
+<?php
+
+namespace MediaWiki\Tests\Rest;
+
+use MediaWiki\Rest\JsonLocalizer;
+use MediaWiki\Rest\ResponseFactory;
+use MediaWikiUnitTestCase;
+use Wikimedia\Message\MessageValue;
+
+/**
+ * @covers \MediaWiki\Rest\JsonLocalizer
+ */
+class JsonLocalizerTest extends MediaWikiUnitTestCase {
+ private function createMockResponseFactory(): ResponseFactory {
+ $responseFactory = $this->createNoOpMock( ResponseFactory::class, [ 'getFormattedMessage' ] );
+ $responseFactory->method( 'getFormattedMessage' )->willReturn( 'translated' );
+ return $responseFactory;
+ }
+
+ public static function providelocalizeJsonObjects() {
+ return [
+ 'empty object' => [
+ [],
+ []
+ ],
+ 'no matching pair' => [
+ [
+ 'properties' => [
+ [ 'my-property-name' => [ 'type' => 'integer' ] ],
+ ],
+ ],
+ [
+ 'properties' => [
+ [ 'my-property-name' => [ 'type' => 'integer' ] ],
+ ],
+ ]
+ ],
+ 'nontranslatable pair' => [
+ [
+ 'description' => 'foo',
+ ],
+ [
+ 'description' => 'foo',
+ ]
+ ],
+ 'translatable pair' => [
+ [
+ 'x-i18n-description' => 'foo',
+ ],
+ [
+ 'description' => 'translated',
+ ]
+ ],
+ 'pair override' => [
+ [
+ 'x-i18n-description' => 'foo',
+ 'description' => 'bar',
+ ],
+ [
+ 'description' => 'translated',
+ ]
+ ],
+ 'nested translatable pair' => [
+ [
+ 'x-i18n-description' => 'foo',
+ 'properties' => [
+ [
+ 'my-property-name' => [
+ 'type' => 'integer',
+ 'x-i18n-description' => 'bar'
+ ]
+ ],
+ ],
+ ],
+ [
+ 'description' => 'translated',
+ 'properties' => [
+ [
+ 'my-property-name' => [
+ 'type' => 'integer',
+ 'description' => 'translated'
+ ]
+ ],
+ ],
+ ]
+ ],
+ 'translatable title, no description' => [
+ [
+ 'x-i18n-title' => 'foo',
+ ],
+ [
+ 'title' => 'translated',
+ ]
+ ],
+ 'translatable description and title' => [
+ [
+ 'x-i18n-title' => 'foo',
+ 'x-i18n-description' => 'bar',
+ ],
+ [
+ 'title' => 'translated',
+ 'description' => 'translated',
+ ]
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideLocalizeJsonObjects
+ */
+ public function testLocalizeJson( $inputObj, $expectedObj ) {
+ $util = new JsonLocalizer( $this->createMockResponseFactory() );
+ $adjustedObj = $util->localizeJson( $inputObj );
+ $this->assertEquals( $expectedObj, $adjustedObj );
+ }
+
+ public static function provideGetFormattedMessage() {
+ return [
+ 'message key' => [
+ 'foo',
+ 'translated'
+ ],
+ 'message value' => [
+ new MessageValue( 'foo' ),
+ 'translated'
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider provideGetFormattedMessage
+ */
+ public function testGetFormattedMessage( $message, $expectedString ) {
+ $util = new JsonLocalizer( $this->createMockResponseFactory() );
+ $ret = $util->getFormattedMessage( $message );
+ $this->assertEquals( $expectedString, $ret );
+ }
+}