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