From: aude Date: Wed, 26 Mar 2014 17:36:36 +0000 (+0100) Subject: Allow to optionally set language in Message constructor X-Git-Tag: 1.31.0-rc.0~16439^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/?a=commitdiff_plain;h=3df8366e9ae939e68ebb3246ec28df438630b7e1;p=lhc%2Fweb%2Fwiklou.git Allow to optionally set language in Message constructor This helps with testability to not have to rely on $wgLang and setting globals in tests. This also provides convenience, so one does not necessarily need to call Message::inLanguage() if language is known at time of constructing the message object. Also added tests to cover this change. Change-Id: I14ee98972c7be954e04398ece9e6103f96ab60dd --- diff --git a/includes/Message.php b/includes/Message.php index 7c9b095a29..9ffd9aada6 100644 --- a/includes/Message.php +++ b/includes/Message.php @@ -223,12 +223,14 @@ class Message { * @param string|string[] $key Message key or array of message keys to try and use the first * non-empty message for. * @param array $params Message parameters. + * @param Language $language Optional language of the message, defaults to $wgLang. */ - public function __construct( $key, $params = array() ) { + public function __construct( $key, $params = array(), Language $language = null ) { global $wgLang; + $this->key = $key; $this->parameters = array_values( $params ); - $this->language = $wgLang; + $this->language = $language ? $language : $wgLang; } /** diff --git a/tests/phpunit/includes/MessageTest.php b/tests/phpunit/includes/MessageTest.php index 930a7f6ac1..914f2358e3 100644 --- a/tests/phpunit/includes/MessageTest.php +++ b/tests/phpunit/includes/MessageTest.php @@ -1,6 +1,7 @@ getProperty( 'key' ); + $keyProperty->setAccessible( true ); + + $paramsProperty = $reflection->getProperty( 'parameters' ); + $paramsProperty->setAccessible( true ); + + $langProperty = $reflection->getProperty( 'language' ); + $langProperty->setAccessible( true ); + + $message = new Message( $key, $params, $language ); + + $this->assertEquals( $key, $keyProperty->getValue( $message ) ); + $this->assertEquals( $params, $paramsProperty->getValue( $message ) ); + $this->assertEquals( $expectedLang, $langProperty->getValue( $message ) ); + } + + public function provideConstructor() { + $langDe = Language::factory( 'de' ); + $langEn = Language::factory( 'en' ); + + return array( + array( $langDe, 'foo', array(), $langDe ), + array( $langDe, 'foo', array( 'bar' ), $langDe ), + array( $langEn, 'foo', array( 'bar' ), null ) + ); + } + public function provideTestParams() { return array( array( array() ),