jenkins-bot has submitted this change and it was merged.

Change subject: Do not "unlocalize" numeric months
......................................................................


Do not "unlocalize" numeric months

Some translations localize the genitive month names as "1", "2" and so
on. These become integer array keys in PHP.

Related to: T139509

Change-Id: Iade477971735c5bcc8b13d76c28ef73fbdf740c0
---
M repo/includes/Parsers/TimeParserFactory.php
M repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
2 files changed, 35 insertions(+), 9 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/includes/Parsers/TimeParserFactory.php 
b/repo/includes/Parsers/TimeParserFactory.php
index 2cd8d75..259c6d8 100644
--- a/repo/includes/Parsers/TimeParserFactory.php
+++ b/repo/includes/Parsers/TimeParserFactory.php
@@ -105,9 +105,15 @@
                        $canonicalMonthNames = 
$this->monthNameProvider->getLocalizedMonthNames(
                                self::CANONICAL_LANGUAGE_CODE
                        );
-                       $replacements = array_map( function( $i ) use ( 
$canonicalMonthNames ) {
-                               return $canonicalMonthNames[$i];
-                       }, $this->monthNameProvider->getMonthNumbers( 
$languageCode ) );
+                       $replacements = 
$this->monthNameProvider->getMonthNumbers( $languageCode );
+
+                       foreach ( $replacements as $localizedMonthName => &$i ) 
{
+                               if ( !is_string( $localizedMonthName ) ) {
+                                       unset( 
$replacements[$localizedMonthName] );
+                               } else {
+                                       $i = $canonicalMonthNames[$i];
+                               }
+                       }
                }
 
                return new MonthNameUnlocalizer( $replacements );
diff --git a/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php 
b/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
index e6e3766..b6ed55f 100644
--- a/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
+++ b/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
@@ -54,11 +54,17 @@
                return $monthNameProvider;
        }
 
-       private function newTimeParserFactory( $languageCode ) {
+       private function newTimeParserFactory(
+               $languageCode,
+               MonthNameProvider $monthNameProvider = null
+       ) {
                $options = new ParserOptions();
                $options->setOption( ValueParser::OPT_LANG, $languageCode );
 
-               return new TimeParserFactory( $options, 
$this->newMonthNameProvider() );
+               return new TimeParserFactory(
+                       $options,
+                       $monthNameProvider ?: $this->newMonthNameProvider()
+               );
        }
 
        public function testGetTimeParser() {
@@ -76,7 +82,7 @@
                $parser = $factory->getTimeParser();
                $actual = $parser->parse( $value );
 
-               $this->assertEquals( $expected->getArrayValue(), 
$actual->getArrayValue() );
+               $this->assertSame( $expected->getArrayValue(), 
$actual->getArrayValue() );
        }
 
        public function validInputProvider() {
@@ -287,7 +293,7 @@
                );
                $actual = $factory->getTimeParser()->parse( $value );
 
-               $this->assertEquals( $expected->getArrayValue(), 
$actual->getArrayValue() );
+               $this->assertSame( $expected->getArrayValue(), 
$actual->getArrayValue() );
        }
 
        public function parserOptionsProvider() {
@@ -373,7 +379,7 @@
                $factory = $this->newTimeParserFactory( $languageCode );
                $unlocalizer = $factory->getMonthNameUnlocalizer();
 
-               $this->assertEquals( $expected, $unlocalizer->unlocalize( $date 
) );
+               $this->assertSame( $expected, $unlocalizer->unlocalize( $date ) 
);
        }
 
        public function localizedMonthNameProvider() {
@@ -457,7 +463,7 @@
                        $date = $unlocalizer->unlocalize( $date );
                }
 
-               $this->assertEquals( $expected, $date );
+               $this->assertSame( $expected, $date );
        }
 
        public function localizedMonthName_withLanguageChainProvider() {
@@ -476,4 +482,18 @@
                );
        }
 
+       public function testMonthNameUnlocalizer_withUnlocalizedMonthNumbers() {
+               $monthNameProvider = $this->getMock( MonthNameProvider::class );
+               $monthNameProvider->expects( $this->any() )
+                       ->method( 'getLocalizedMonthNames' )
+                       ->will( $this->returnValue( [ 2 => 'Localized' ] ) );
+               $monthNameProvider->expects( $this->any() )
+                       ->method( 'getMonthNumbers' )
+                       ->will( $this->returnValue( [ '2' => 2 ] ) );
+
+               $factory = $this->newTimeParserFactory( 'ko', 
$monthNameProvider );
+               $unlocalizer = $factory->getMonthNameUnlocalizer();
+               $this->assertSame( '2000', $unlocalizer->unlocalize( '2000' ) );
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/297756
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iade477971735c5bcc8b13d76c28ef73fbdf740c0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to