3 class MessageTest
extends MediaWikiLangTestCase
{
5 protected function setUp() {
8 $this->setMwGlobals( array(
9 'wgLang' => Language
::factory( 'en' ),
10 'wgForceUIMsgAsContentMsg' => array(),
15 * @covers Message::__construct
16 * @dataProvider provideConstructor
18 public function testConstructor( $expectedLang, $key, $params, $language ) {
19 $reflection = new ReflectionClass( 'Message' );
21 $keyProperty = $reflection->getProperty( 'key' );
22 $keyProperty->setAccessible( true );
24 $paramsProperty = $reflection->getProperty( 'parameters' );
25 $paramsProperty->setAccessible( true );
27 $langProperty = $reflection->getProperty( 'language' );
28 $langProperty->setAccessible( true );
30 $message = new Message( $key, $params, $language );
32 $this->assertEquals( $key, $keyProperty->getValue( $message ) );
33 $this->assertEquals( $params, $paramsProperty->getValue( $message ) );
34 $this->assertEquals( $expectedLang, $langProperty->getValue( $message ) );
37 public function provideConstructor() {
38 $langDe = Language
::factory( 'de' );
39 $langEn = Language
::factory( 'en' );
42 array( $langDe, 'foo', array(), $langDe ),
43 array( $langDe, 'foo', array( 'bar' ), $langDe ),
44 array( $langEn, 'foo', array( 'bar' ), null )
48 public function provideTestParams() {
51 array( array( 'foo' ), 'foo' ),
52 array( array( 'foo', 'bar' ), 'foo', 'bar' ),
53 array( array( 'baz' ), array( 'baz' ) ),
54 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ) ),
55 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh' ),
56 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
57 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
58 array( array( 'baz' ), array( 'baz' ), array( 'ahahahahha' ) ),
62 public function getLanguageProvider() {
64 array( 'foo', array( 'bar' ), 'en' ),
65 array( 'foo', array( 'bar' ), 'de' )
70 * @covers Message::getLanguage
71 * @dataProvider getLanguageProvider
73 public function testGetLanguageCode( $key, $params, $languageCode ) {
74 $language = Language
::factory( $languageCode );
75 $message = new Message( $key, $params, $language );
77 $this->assertEquals( $language, $message->getLanguage() );
81 * @covers Message::params
82 * @dataProvider provideTestParams
84 public function testParams( $expected ) {
85 $msg = new Message( 'imasomething' );
87 $returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
89 $this->assertSame( $msg, $returned );
90 $this->assertEquals( $expected, $msg->getParams() );
94 * @covers Message::exists
96 public function testExists() {
97 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
98 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
99 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
100 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
101 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
102 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
106 * @covers Message::__construct
108 public function testKey() {
109 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
110 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
111 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
112 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
113 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
114 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() );
118 * @covers Message::inLanguage
120 public function testInLanguage() {
121 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
122 $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
123 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( Language
::factory( 'en' ) )->text() );
124 $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( Language
::factory( 'ru' ) )->text() );
128 * @covers Message::__construct
130 public function testMessageParams() {
131 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
132 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
133 $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text() );
134 $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text() );
138 * @covers Message::__construct
139 * @covers Message::rawParams
141 public function testMessageParamSubstitution() {
142 $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses', 'Заглавная страница' )->plain() );
143 $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses', 'Заглавная страница $1' )->plain() );
144 $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain() );
145 $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain() );
149 * @covers Message::__construct
150 * @covers Message::params
152 public function testDeliciouslyManyParams() {
153 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
154 // One less than above has placeholders
155 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
156 $this->assertEquals( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' );
160 * @covers Message::numParams
162 public function testMessageNumParams() {
163 $lang = Language
::factory( 'en' );
164 $msg = new RawMessage( '$1' );
167 $lang->formatNum( 123456.789 ),
168 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
169 'numParams is handled correctly'
174 * @covers Message::durationParams
176 public function testMessageDurationParams() {
177 $lang = Language
::factory( 'en' );
178 $msg = new RawMessage( '$1' );
181 $lang->formatDuration( 1234 ),
182 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
183 'durationParams is handled correctly'
188 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
190 * @covers Message::expiryParams
192 public function testMessageExpiryParams() {
193 $lang = Language
::factory( 'en' );
194 $msg = new RawMessage( '$1' );
197 $lang->formatExpiry( wfTimestampNow() ),
198 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
199 'expiryParams is handled correctly'
204 * @covers Message::timeperiodParams
206 public function testMessageTimeperiodParams() {
207 $lang = Language
::factory( 'en' );
208 $msg = new RawMessage( '$1' );
211 $lang->formatTimePeriod( 1234 ),
212 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
213 'timeperiodParams is handled correctly'
218 * @covers Message::sizeParams
220 public function testMessageSizeParams() {
221 $lang = Language
::factory( 'en' );
222 $msg = new RawMessage( '$1' );
225 $lang->formatSize( 123456 ),
226 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
227 'sizeParams is handled correctly'
232 * @covers Message::bitrateParams
234 public function testMessageBitrateParams() {
235 $lang = Language
::factory( 'en' );
236 $msg = new RawMessage( '$1' );
239 $lang->formatBitrate( 123456 ),
240 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
241 'bitrateParams is handled correctly'
246 * @covers Message::inContentLanguage
248 public function testInContentLanguageDisabled() {
249 $this->setMwGlobals( 'wgLang', Language
::factory( 'fr' ) );
251 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg disabled' );
255 * @covers Message::inContentLanguage
257 public function testInContentLanguageEnabled() {
258 $this->setMwGlobals( array(
259 'wgLang' => Language
::factory( 'fr' ),
260 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
263 $this->assertEquals( 'Accueil', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg enabled' );
267 * @expectedException MWException
268 * @covers Message::inLanguage
270 public function testInLanguageThrows() {
271 wfMessage( 'foo' )->inLanguage( 123 );