Nikerabbit has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/82115


Change subject: (bug 53644) Incorrect plural flattening in RubyYamlFFS
......................................................................

(bug 53644) Incorrect plural flattening in RubyYamlFFS

Happened when only the last keyword in array was a reserved plural keyword.

Change-Id: Id98f442541341569e6858123502919e5585657e3
---
M ffs/RubyYamlFFS.php
M tests/ffs/RubyYamlFFSTest.php
2 files changed, 88 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate 
refs/changes/15/82115/1

diff --git a/ffs/RubyYamlFFS.php b/ffs/RubyYamlFFS.php
index ac4cc97..525b449 100644
--- a/ffs/RubyYamlFFS.php
+++ b/ffs/RubyYamlFFS.php
@@ -23,24 +23,38 @@
        /**
         * Flattens ruby plural arrays into special plural syntax.
         *
-        * @param $messages array
+        * @param array $messages Array of keys and values
         *
         * @throws MWException
         * @return bool|string
         */
        public function flattenPlural( $messages ) {
 
-               $plurals = false;
-               foreach ( array_keys( $messages ) as $key ) {
+               $pluralKeys = false;
+               $nonPluralKeys = false;
+               foreach ( $messages as $key => $value ) {
+                       if ( is_array( $value ) ) {
+                               # Plurals can only happen in the lowest level 
of the structure
+                               return false;
+                       }
+
+                       # Check if we find any reserved plural keyword
                        if ( isset( self::$pluralWords[$key] ) ) {
-                               $plurals = true;
-                       } elseif ( $plurals ) {
-                               throw new MWException( "Reserved plural 
keywords mixed with other keys: $key." );
+                               $pluralKeys = true;
+                       } else {
+                               $nonPluralKeys = true;
                        }
                }
 
-               if ( !$plurals ) {
+               # No plural keys at all, we can skip
+               if ( !$pluralKeys ) {
                        return false;
+               }
+
+               # Mixed plural keys with other keys, should not happen
+               if ( $nonPluralKeys ) {
+                       $keys = implode( ', ', array_keys( $messages ) );
+                       throw new MWException( "Reserved plural keywords mixed 
with other keys: $keys." );
                }
 
                $pls = '{{PLURAL';
@@ -62,8 +76,8 @@
        /**
         * Converts the special plural syntax to array or ruby style plurals
         *
-        * @param $key string
-        * @param $message string
+        * @param string $key Message key prefix
+        * @param string $message The plural string
         *
         * @return bool|array
         */
diff --git a/tests/ffs/RubyYamlFFSTest.php b/tests/ffs/RubyYamlFFSTest.php
index dbe66d4..d756782 100644
--- a/tests/ffs/RubyYamlFFSTest.php
+++ b/tests/ffs/RubyYamlFFSTest.php
@@ -21,25 +21,81 @@
 
        protected function setUp() {
                parent::setUp();
-               $this->group = MessageGroupBase::factory( 
$this->groupConfiguration );
+               $group = MessageGroupBase::factory( $this->groupConfiguration );
+               /** @var YamlFFS $ffs */
+               $this->ffs = $group->getFFS();
        }
 
-       protected function tearDown() {
-               unset( $this->group );
-               parent::tearDown();
+       public function testFlattenPluralWithNoPlurals() {
+               $input = array(
+                       'much' => 'a lot',
+                       'less' => 'not so much',
+               );
+               $output = false;
+               $this->assertEquals( $output, $this->ffs->flattenPlural( $input 
) );
+       }
+
+       public function testFlattenPluralWithPlurals() {
+               $input = array(
+                       'one' => 'just a tiny bit',
+                       'two' => 'not so much',
+                       'other' => 'maybe a lot',
+               );
+               $output = '{{PLURAL|one=just a tiny bit|two=not so much|maybe a 
lot}}';
+               $this->assertEquals( $output, $this->ffs->flattenPlural( $input 
) );
+       }
+
+       public function testFlattenPluralWithArrays() {
+               $input = array(
+                       'one' => array(
+                               'multi' => 'he lives in a multistorey house',
+                               'row' => 'he lives in a row house',
+                       ),
+                       'other' => array(
+                               'multi' => 'he lives in mountain cave',
+                               'row' => 'he lives in a cave near the river',
+                       ),
+               );
+               $output = false;
+               $this->assertEquals( $output, $this->ffs->flattenPlural( $input 
) );
+       }
+
+       /**
+        * @expectedException MWException
+        * @expectedExceptionMessage Reserved plural keywords mixed with other 
keys
+        * @dataProvider flattenPluralsWithMixedKeywordsProvider
+        */
+
+       public function testFlattenPluralsWithMixedKeywords( $input, $comment ) 
{
+               $this->ffs->flattenPlural( $input );
+       }
+
+       public function flattenPluralsWithMixedKeywordsProvider() {
+               return array(
+                       array(
+                               array(
+                                       'carrot' => 'I like carrots',
+                                       'other' =>  'I like milk',
+                               ),
+                               'reserved keyword at the end',
+                       ),
+                       array(
+                               array(
+                                       'one' => 'I am the one leader',
+                                       'club' =>  'I am the club leader',
+                               ),
+                               'reserved keyword at the beginning',
+                       )
+               );
        }
 
        /**
         * @dataProvider unflattenDataProvider
         */
        public function testUnflattenPural( $key, $value, $result ) {
-               /**
-                * @var YamlFFS $ffs
-                */
-               $ffs = $this->group->getFFS();
                $this->assertEquals(
                        $result,
-                       $ffs->unflattenPlural( $key, $value )
+                       $this->ffs->unflattenPlural( $key, $value )
                );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id98f442541341569e6858123502919e5585657e3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>

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

Reply via email to