Merge "Button group adjustments"
[lhc/web/wiklou.git] / tests / phpunit / includes / MessageTest.php
1 <?php
2
3 class MessageTest extends MediaWikiLangTestCase {
4 protected function setUp() {
5 parent::setUp();
6
7 $this->setMwGlobals( array(
8 'wgLang' => Language::factory( 'en' ),
9 'wgForceUIMsgAsContentMsg' => array(),
10 ) );
11 }
12
13 public function provideTestParams() {
14 return array(
15 array( array() ),
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' ) ),
24 );
25 }
26
27 /**
28 * @covers Message::params
29 * @dataProvider provideTestParams
30 */
31 public function testParams( $expected ) {
32 $msg = new Message( 'imasomething' );
33
34 $returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
35
36 $this->assertSame( $msg, $returned );
37 $this->assertEquals( $expected, $msg->getParams() );
38 }
39
40 /**
41 * @covers Message::exists
42 */
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() );
50 }
51
52 /**
53 * @covers Message::__construct
54 */
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( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->text() );
60 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
61 $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
62 }
63
64 /**
65 * @covers Message::inLanguage
66 */
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() );
72 }
73
74 /**
75 * @covers Message::__construct
76 */
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() );
82 }
83
84 /**
85 * @covers Message::__construct
86 * @covers Message::rawParams
87 */
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() );
93 }
94
95 /**
96 * @covers Message::__construct
97 * @covers Message::params
98 */
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' );
104 }
105
106 /**
107 * @covers Message::numParams
108 */
109 public function testMessageNumParams() {
110 $lang = Language::factory( 'en' );
111 $msg = new RawMessage( '$1' );
112
113 $this->assertEquals(
114 $lang->formatNum( 123456.789 ),
115 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
116 'numParams is handled correctly'
117 );
118 }
119
120 /**
121 * @covers Message::durationParams
122 */
123 public function testMessageDurationParams() {
124 $lang = Language::factory( 'en' );
125 $msg = new RawMessage( '$1' );
126
127 $this->assertEquals(
128 $lang->formatDuration( 1234 ),
129 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
130 'durationParams is handled correctly'
131 );
132 }
133
134 /**
135 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
136 * @group Database
137 * @covers Message::expiryParams
138 */
139 public function testMessageExpiryParams() {
140 $lang = Language::factory( 'en' );
141 $msg = new RawMessage( '$1' );
142
143 $this->assertEquals(
144 $lang->formatExpiry( wfTimestampNow() ),
145 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
146 'expiryParams is handled correctly'
147 );
148 }
149
150 /**
151 * @covers Message::timeperiodParams
152 */
153 public function testMessageTimeperiodParams() {
154 $lang = Language::factory( 'en' );
155 $msg = new RawMessage( '$1' );
156
157 $this->assertEquals(
158 $lang->formatTimePeriod( 1234 ),
159 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
160 'timeperiodParams is handled correctly'
161 );
162 }
163
164 /**
165 * @covers Message::sizeParams
166 */
167 public function testMessageSizeParams() {
168 $lang = Language::factory( 'en' );
169 $msg = new RawMessage( '$1' );
170
171 $this->assertEquals(
172 $lang->formatSize( 123456 ),
173 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
174 'sizeParams is handled correctly'
175 );
176 }
177
178 /**
179 * @covers Message::bitrateParams
180 */
181 public function testMessageBitrateParams() {
182 $lang = Language::factory( 'en' );
183 $msg = new RawMessage( '$1' );
184
185 $this->assertEquals(
186 $lang->formatBitrate( 123456 ),
187 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
188 'bitrateParams is handled correctly'
189 );
190 }
191
192 /**
193 * @covers Message::inContentLanguage
194 */
195 public function testInContentLanguageDisabled() {
196 $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
197
198 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg disabled' );
199 }
200
201 /**
202 * @covers Message::inContentLanguage
203 */
204 public function testInContentLanguageEnabled() {
205 $this->setMwGlobals( array(
206 'wgLang' => Language::factory( 'fr' ),
207 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
208 ) );
209
210 $this->assertEquals( 'Accueil', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg enabled' );
211 }
212
213 /**
214 * @expectedException MWException
215 * @covers Message::inLanguage
216 */
217 public function testInLanguageThrows() {
218 wfMessage( 'foo' )->inLanguage( 123 );
219 }
220 }