3 class MessageTest
extends MediaWikiLangTestCase
{
4 protected function setUp() {
7 $this->setMwGlobals( array(
8 'wgLang' => Language
::factory( 'en' ),
9 'wgForceUIMsgAsContentMsg' => array(),
13 public function provideTestParams() {
16 array( array( 'foo' ), 'foo' ),
17 array( array( 'foo', 'bar' ), 'foo', 'bar' ),
18 array( array( 'baz' ), array( 'baz' ) ),
19 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ) ),
20 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh' ),
21 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
22 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
23 array( array( 'baz' ), array( 'baz' ), array( 'ahahahahha' ) ),
28 * @covers Message::params
29 * @dataProvider provideTestParams
31 public function testParams( $expected ) {
32 $msg = new Message( 'imasomething' );
34 $returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
36 $this->assertSame( $msg, $returned );
37 $this->assertEquals( $expected, $msg->getParams() );
41 * @covers Message::exists
43 public function testExists() {
44 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
45 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
46 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
47 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
48 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
49 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
53 * @covers Message::__construct
55 public function testKey() {
56 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
57 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
58 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
59 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
60 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
61 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() );
65 * @covers Message::inLanguage
67 public function testInLanguage() {
68 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
69 $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
70 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( Language
::factory( 'en' ) )->text() );
71 $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( Language
::factory( 'ru' ) )->text() );
75 * @covers Message::__construct
77 public function testMessageParams() {
78 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
79 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
80 $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text() );
81 $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text() );
85 * @covers Message::__construct
86 * @covers Message::rawParams
88 public function testMessageParamSubstitution() {
89 $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses', 'Заглавная страница' )->plain() );
90 $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses', 'Заглавная страница $1' )->plain() );
91 $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain() );
92 $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain() );
96 * @covers Message::__construct
97 * @covers Message::params
99 public function testDeliciouslyManyParams() {
100 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
101 // One less than above has placeholders
102 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
103 $this->assertEquals( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' );
107 * @covers Message::numParams
109 public function testMessageNumParams() {
110 $lang = Language
::factory( 'en' );
111 $msg = new RawMessage( '$1' );
114 $lang->formatNum( 123456.789 ),
115 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
116 'numParams is handled correctly'
121 * @covers Message::durationParams
123 public function testMessageDurationParams() {
124 $lang = Language
::factory( 'en' );
125 $msg = new RawMessage( '$1' );
128 $lang->formatDuration( 1234 ),
129 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
130 'durationParams is handled correctly'
135 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
137 * @covers Message::expiryParams
139 public function testMessageExpiryParams() {
140 $lang = Language
::factory( 'en' );
141 $msg = new RawMessage( '$1' );
144 $lang->formatExpiry( wfTimestampNow() ),
145 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
146 'expiryParams is handled correctly'
151 * @covers Message::timeperiodParams
153 public function testMessageTimeperiodParams() {
154 $lang = Language
::factory( 'en' );
155 $msg = new RawMessage( '$1' );
158 $lang->formatTimePeriod( 1234 ),
159 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
160 'timeperiodParams is handled correctly'
165 * @covers Message::sizeParams
167 public function testMessageSizeParams() {
168 $lang = Language
::factory( 'en' );
169 $msg = new RawMessage( '$1' );
172 $lang->formatSize( 123456 ),
173 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
174 'sizeParams is handled correctly'
179 * @covers Message::bitrateParams
181 public function testMessageBitrateParams() {
182 $lang = Language
::factory( 'en' );
183 $msg = new RawMessage( '$1' );
186 $lang->formatBitrate( 123456 ),
187 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
188 'bitrateParams is handled correctly'
193 * @covers Message::inContentLanguage
195 public function testInContentLanguageDisabled() {
196 $this->setMwGlobals( 'wgLang', Language
::factory( 'fr' ) );
198 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg disabled' );
202 * @covers Message::inContentLanguage
204 public function testInContentLanguageEnabled() {
205 $this->setMwGlobals( array(
206 'wgLang' => Language
::factory( 'fr' ),
207 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
210 $this->assertEquals( 'Accueil', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg enabled' );
214 * @expectedException MWException
215 * @covers Message::inLanguage
217 public function testInLanguageThrows() {
218 wfMessage( 'foo' )->inLanguage( 123 );