MediaWiki  1.23.16
MessageTest.php
Go to the documentation of this file.
1 <?php
2 
3 class MessageTest extends MediaWikiLangTestCase {
4 
5  protected function setUp() {
6  parent::setUp();
7 
8  $this->setMwGlobals( array(
9  'wgLang' => Language::factory( 'en' ),
10  'wgForceUIMsgAsContentMsg' => array(),
11  ) );
12  }
13 
18  public function testConstructor( $expectedLang, $key, $params, $language ) {
19  $reflection = new ReflectionClass( 'Message' );
20 
21  $keyProperty = $reflection->getProperty( 'key' );
22  $keyProperty->setAccessible( true );
23 
24  $paramsProperty = $reflection->getProperty( 'parameters' );
25  $paramsProperty->setAccessible( true );
26 
27  $langProperty = $reflection->getProperty( 'language' );
28  $langProperty->setAccessible( true );
29 
30  $message = new Message( $key, $params, $language );
31 
32  $this->assertEquals( $key, $keyProperty->getValue( $message ) );
33  $this->assertEquals( $params, $paramsProperty->getValue( $message ) );
34  $this->assertEquals( $expectedLang, $langProperty->getValue( $message ) );
35  }
36 
37  public function provideConstructor() {
38  $langDe = Language::factory( 'de' );
39  $langEn = Language::factory( 'en' );
40 
41  return array(
42  array( $langDe, 'foo', array(), $langDe ),
43  array( $langDe, 'foo', array( 'bar' ), $langDe ),
44  array( $langEn, 'foo', array( 'bar' ), null )
45  );
46  }
47 
48  public function provideTestParams() {
49  return array(
50  array( array() ),
51  array( array( 'foo' ), 'foo' ),
52  array( array( 'foo', 'bar' ), 'foo', 'bar' ),
53  array( array( 'baz' ), array( 'baz' ) ),
54  array( array( 'baz', 'foo' ), array( 'baz', 'foo' ) ),
55  array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh' ),
56  array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
57  array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
58  array( array( 'baz' ), array( 'baz' ), array( 'ahahahahha' ) ),
59  );
60  }
61 
62  public function getLanguageProvider() {
63  return array(
64  array( 'foo', array( 'bar' ), 'en' ),
65  array( 'foo', array( 'bar' ), 'de' )
66  );
67  }
68 
73  public function testGetLanguageCode( $key, $params, $languageCode ) {
74  $language = Language::factory( $languageCode );
75  $message = new Message( $key, $params, $language );
76 
77  $this->assertEquals( $language, $message->getLanguage() );
78  }
79 
84  public function testParams( $expected ) {
85  $msg = new Message( 'imasomething' );
86 
87  $returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
88 
89  $this->assertSame( $msg, $returned );
90  $this->assertEquals( $expected, $msg->getParams() );
91  }
92 
96  public function testExists() {
97  $this->assertTrue( wfMessage( 'mainpage' )->exists() );
98  $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
99  $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
100  $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
101  $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
102  $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
103  }
104 
108  public function testKey() {
109  $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
110  $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
111  $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
112  $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->text() );
113  $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
114  $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
115  }
116 
120  public function testInLanguage() {
121  $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
122  $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
123  $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( Language::factory( 'en' ) )->text() );
124  $this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( Language::factory( 'ru' ) )->text() );
125  }
126 
130  public function testMessageParams() {
131  $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
132  $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
133  $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text() );
134  $this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text() );
135  }
136 
141  public function testMessageParamSubstitution() {
142  $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses', 'Заглавная страница' )->plain() );
143  $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses', 'Заглавная страница $1' )->plain() );
144  $this->assertEquals( '(Заглавная страница)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain() );
145  $this->assertEquals( '(Заглавная страница $1)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain() );
146  }
147 
148  public function testRawHtmlInMsg() {
149  global $wgParserConf;
150  $this->setMwGlobals( 'wgRawHtml', true );
151  // We have to reset the core hook registration.
152  // to register the html hook
154  $this->setMwGlobals( 'wgParser',
155  new $wgParserConf['class']( array( $wgParserConf ) )
156  );
157 
158  $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
159  $txt = '<span class="error">&lt;html&gt; tags cannot be' .
160  ' used outside of normal pages.</span>';
161  $this->assertSame( $txt, $msg->parse() );
162  }
163 
168  public function testDeliciouslyManyParams() {
169  $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
170  // One less than above has placeholders
171  $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
172  $this->assertEquals( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' );
173  }
174 
178  public function testMessageNumParams() {
179  $lang = Language::factory( 'en' );
180  $msg = new RawMessage( '$1' );
181 
182  $this->assertEquals(
183  $lang->formatNum( 123456.789 ),
184  $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
185  'numParams is handled correctly'
186  );
187  }
188 
192  public function testMessageDurationParams() {
193  $lang = Language::factory( 'en' );
194  $msg = new RawMessage( '$1' );
195 
196  $this->assertEquals(
197  $lang->formatDuration( 1234 ),
198  $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
199  'durationParams is handled correctly'
200  );
201  }
202 
208  public function testMessageExpiryParams() {
209  $lang = Language::factory( 'en' );
210  $msg = new RawMessage( '$1' );
211 
212  $this->assertEquals(
213  $lang->formatExpiry( wfTimestampNow() ),
214  $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
215  'expiryParams is handled correctly'
216  );
217  }
218 
222  public function testMessageTimeperiodParams() {
223  $lang = Language::factory( 'en' );
224  $msg = new RawMessage( '$1' );
225 
226  $this->assertEquals(
227  $lang->formatTimePeriod( 1234 ),
228  $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
229  'timeperiodParams is handled correctly'
230  );
231  }
232 
236  public function testMessageSizeParams() {
237  $lang = Language::factory( 'en' );
238  $msg = new RawMessage( '$1' );
239 
240  $this->assertEquals(
241  $lang->formatSize( 123456 ),
242  $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
243  'sizeParams is handled correctly'
244  );
245  }
246 
250  public function testMessageBitrateParams() {
251  $lang = Language::factory( 'en' );
252  $msg = new RawMessage( '$1' );
253 
254  $this->assertEquals(
255  $lang->formatBitrate( 123456 ),
256  $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
257  'bitrateParams is handled correctly'
258  );
259  }
260 
264  public function testInContentLanguageDisabled() {
265  $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
266 
267  $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg disabled' );
268  }
269 
273  public function testInContentLanguageEnabled() {
274  $this->setMwGlobals( array(
275  'wgLang' => Language::factory( 'fr' ),
276  'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
277  ) );
278 
279  $this->assertEquals( 'Accueil', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg enabled' );
280  }
281 
286  public function testInLanguageThrows() {
287  wfMessage( 'foo' )->inLanguage( 123 );
288  }
289 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
$params
$params
Definition: styleTest.css.php:40
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:2561
MediaWikiLangTestCase\setUp
setUp()
Definition: MediaWikiLangTestCase.php:8
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:184
MessageCache\destroyInstance
static destroyInstance()
Destroy the singleton instance.
Definition: MessageCache.php:119