Consistent handling of multiple keys in Message
[lhc/web/wiklou.git] / tests / phpunit / includes / MessageTest.php
index 25b0805..c2acec0 100644 (file)
@@ -119,12 +119,16 @@ class MessageTest extends MediaWikiLangTestCase {
         */
        public function testInLanguage() {
                $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
-               $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
+               $this->assertEquals( 'Заглавная страница',
+                       wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
 
                // NOTE: make sure internal caching of the message text is reset appropriately
                $msg = wfMessage( 'mainpage' );
                $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
-               $this->assertEquals( 'Заглавная страница', $msg->inLanguage( Language::factory( 'ru' ) )->text() );
+               $this->assertEquals(
+                       'Заглавная страница',
+                       $msg->inLanguage( Language::factory( 'ru' ) )->text()
+               );
        }
 
        /**
@@ -133,8 +137,14 @@ class MessageTest extends MediaWikiLangTestCase {
        public function testMessageParams() {
                $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
                $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
-               $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text() );
-               $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text() );
+               $this->assertEquals(
+                       'You have foo (bar).',
+                       wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
+               );
+               $this->assertEquals(
+                       'You have foo (bar).',
+                       wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
+               );
        }
 
        /**
@@ -142,10 +152,22 @@ class MessageTest extends MediaWikiLangTestCase {
         * @covers Message::rawParams
         */
        public function testMessageParamSubstitution() {
-               $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses', 'Заглавная страница' )->plain() );
-               $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses', 'Заглавная страница $1' )->plain() );
-               $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain() );
-               $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain() );
+               $this->assertEquals(
+                       '(Заглавная страница)',
+                       wfMessage( 'parentheses', 'Заглавная страница' )->plain()
+               );
+               $this->assertEquals(
+                       '(Заглавная страница $1)',
+                       wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
+               );
+               $this->assertEquals(
+                       '(Заглавная страница)',
+                       wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
+               );
+               $this->assertEquals(
+                       '(Заглавная страница $1)',
+                       wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
+               );
        }
 
        /**
@@ -156,7 +178,11 @@ class MessageTest extends MediaWikiLangTestCase {
                $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
                // One less than above has placeholders
                $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
-               $this->assertEquals( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' );
+               $this->assertEquals(
+                       'abcdefghijka2',
+                       $msg->params( $params )->plain(),
+                       'Params > 9 are replaced correctly'
+               );
        }
 
        /**
@@ -270,9 +296,17 @@ class MessageTest extends MediaWikiLangTestCase {
                // NOTE: make sure internal caching of the message text is reset appropriately.
                // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
                $msg = wfMessage( 'mainpage' );
-               $this->assertEquals( 'Accueil', $msg->inContentLanguage()->plain(), 'inContentLanguage() with ForceUIMsg override enabled' );
+               $this->assertEquals(
+                       'Accueil',
+                       $msg->inContentLanguage()->plain(),
+                       'inContentLanguage() with ForceUIMsg override enabled'
+               );
                $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
-               $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), 'inContentLanguage() with ForceUIMsg override enabled' );
+               $this->assertEquals(
+                       'Main Page',
+                       $msg->inContentLanguage()->plain(),
+                       'inContentLanguage() with ForceUIMsg override enabled'
+               );
                $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
        }
 
@@ -283,4 +317,52 @@ class MessageTest extends MediaWikiLangTestCase {
        public function testInLanguageThrows() {
                wfMessage( 'foo' )->inLanguage( 123 );
        }
+
+       public function keyProvider() {
+               return array(
+                       'string' => array(
+                               'key' => 'mainpage',
+                               'expected' => array( 'mainpage' ),
+                       ),
+                       'single' => array(
+                               'key' => array( 'mainpage' ),
+                               'expected' => array( 'mainpage' ),
+                       ),
+                       'multi' => array(
+                               'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
+                               'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
+                       ),
+                       'empty' => array(
+                               'key' => array(),
+                               'expected' => null,
+                               'exception' => 'InvalidArgumentException',
+                       ),
+                       'null' => array(
+                               'key' => null,
+                               'expected' => null,
+                               'exception' => 'InvalidArgumentException',
+                       ),
+                       'bad type' => array(
+                               'key' => 17,
+                               'expected' => null,
+                               'exception' => 'InvalidArgumentException',
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider keyProvider()
+        *
+        * @covers Message::getKey
+        */
+       public function testGetKey( $key, $expected, $exception = null ) {
+               if ( $exception ) {
+                       $this->setExpectedException( $exception );
+               }
+
+               $msg = new Message( $key );
+               $this->assertEquals( $expected, $msg->getKeysToTry() );
+               $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
+               $this->assertContains( $msg->getKey(), $expected );
+       }
 }