Merge "tipsy: using user class borks positioning of tip"
[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 /**
14 * @covers Message::exists
15 */
16 public function testExists() {
17 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
18 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
19 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
20 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
21 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
22 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
23 }
24
25 /**
26 * @covers Message::__construct
27 */
28 public function testKey() {
29 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
30 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
31 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
32 $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->text() );
33 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
34 $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
35 }
36
37 /**
38 * @covers Message::inLanguage
39 */
40 public function testInLanguage() {
41 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
42 $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
43 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( Language::factory( 'en' ) )->text() );
44 $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( Language::factory( 'ru' ) )->text() );
45 }
46
47 /**
48 * @covers Message::__construct
49 */
50 public function testMessageParams() {
51 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
52 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
53 $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text() );
54 $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text() );
55 }
56
57 /**
58 * @covers Message::__construct
59 * @covers Message::rawParams
60 */
61 public function testMessageParamSubstitution() {
62 $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses', 'Заглавная страница' )->plain() );
63 $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses', 'Заглавная страница $1' )->plain() );
64 $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain() );
65 $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain() );
66 }
67
68 /**
69 * @covers Message::__construct
70 * @covers Message::params
71 */
72 public function testDeliciouslyManyParams() {
73 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
74 // One less than above has placeholders
75 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
76 $this->assertEquals( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' );
77 }
78
79 /**
80 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
81 * @group Database
82 * @todo this should be split up into multiple test methods
83 * @covers Message::numParams
84 * @covers Message::durationParams
85 * @covers Message::expiryParams
86 * @covers Message::timeperiodParams
87 * @covers Message::sizeParams
88 * @covers Message::bitrateParams
89 */
90 public function testMessageParamTypes() {
91 $lang = Language::factory( 'en' );
92
93 $msg = new RawMessage( '$1' );
94 $this->assertEquals(
95 $lang->formatNum( 123456.789 ),
96 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
97 'numParams is handled correctly'
98 );
99
100 $msg = new RawMessage( '$1' );
101 $this->assertEquals(
102 $lang->formatDuration( 1234 ),
103 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
104 'durationParams is handled correctly'
105 );
106
107 $msg = new RawMessage( '$1' );
108 $this->assertEquals(
109 $lang->formatExpiry( wfTimestampNow() ),
110 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
111 'expiryParams is handled correctly'
112 );
113
114 $msg = new RawMessage( '$1' );
115 $this->assertEquals(
116 $lang->formatTimePeriod( 1234 ),
117 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
118 'timeperiodParams is handled correctly'
119 );
120
121 $msg = new RawMessage( '$1' );
122 $this->assertEquals(
123 $lang->formatSize( 123456 ),
124 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
125 'sizeParams is handled correctly'
126 );
127
128 $msg = new RawMessage( '$1' );
129 $this->assertEquals(
130 $lang->formatBitrate( 123456 ),
131 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
132 'bitrateParams is handled correctly'
133 );
134 }
135
136 /**
137 * @covers Message::inContentLanguage
138 */
139 public function testInContentLanguageDisabled() {
140 $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
141
142 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg disabled' );
143 }
144
145 /**
146 * @covers Message::inContentLanguage
147 */
148 public function testInContentLanguageEnabled() {
149 $this->setMwGlobals( array(
150 'wgLang' => Language::factory( 'fr' ),
151 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
152 ) );
153
154 $this->assertEquals( 'Accueil', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg enabled' );
155 }
156
157 /**
158 * @expectedException MWException
159 * @covers Message::inLanguage
160 */
161 public function testInLanguageThrows() {
162 wfMessage( 'foo' )->inLanguage( 123 );
163 }
164 }