3 class MessageTest
extends MediaWikiLangTestCase
{
4 protected function setUp() {
7 $this->setMwGlobals( array(
8 'wgLang' => Language
::factory( 'en' ),
9 'wgForceUIMsgAsContentMsg' => array(),
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() );
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( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
27 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
28 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() );
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() );
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() );
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() );
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' );
60 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
63 function testMessageParamTypes() {
64 $lang = Language
::factory( 'en' );
66 $msg = new RawMessage( '$1' );
68 $lang->formatNum( 123456.789 ),
69 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
70 'numParams is handled correctly'
73 $msg = new RawMessage( '$1' );
75 $lang->formatDuration( 1234 ),
76 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
77 'durationParams is handled correctly'
80 $msg = new RawMessage( '$1' );
82 $lang->formatExpiry( wfTimestampNow() ),
83 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
84 'expiryParams is handled correctly'
87 $msg = new RawMessage( '$1' );
89 $lang->formatTimePeriod( 1234 ),
90 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
91 'timeperiodParams is handled correctly'
94 $msg = new RawMessage( '$1' );
96 $lang->formatSize( 123456 ),
97 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
98 'sizeParams is handled correctly'
101 $msg = new RawMessage( '$1' );
103 $lang->formatBitrate( 123456 ),
104 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
105 'bitrateParams is handled correctly'
109 function testInContentLanguageDisabled() {
110 $this->setMwGlobals( 'wgLang', Language
::factory( 'fr' ) );
112 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg disabled' );
115 function testInContentLanguageEnabled() {
116 $this->setMwGlobals( array(
117 'wgLang' => Language
::factory( 'fr' ),
118 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
121 $this->assertEquals( 'Accueil', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg enabled' );
125 * @expectedException MWException
127 function testInLanguageThrows() {
128 wfMessage( 'foo' )->inLanguage( 123 );