2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2011-05-01 19:32:49 +00:00
|
|
|
class MessageTest extends MediaWikiLangTestCase {
|
2014-03-26 17:36:36 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgLang' => Language::factory( 'en' ),
|
|
|
|
|
'wgForceUIMsgAsContentMsg' => array(),
|
|
|
|
|
) );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2014-03-26 17:36:36 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::__construct
|
|
|
|
|
* @dataProvider provideConstructor
|
|
|
|
|
*/
|
|
|
|
|
public function testConstructor( $expectedLang, $key, $params, $language ) {
|
|
|
|
|
$reflection = new ReflectionClass( 'Message' );
|
|
|
|
|
|
|
|
|
|
$keyProperty = $reflection->getProperty( 'key' );
|
|
|
|
|
$keyProperty->setAccessible( true );
|
|
|
|
|
|
|
|
|
|
$paramsProperty = $reflection->getProperty( 'parameters' );
|
|
|
|
|
$paramsProperty->setAccessible( true );
|
|
|
|
|
|
|
|
|
|
$langProperty = $reflection->getProperty( 'language' );
|
|
|
|
|
$langProperty->setAccessible( true );
|
|
|
|
|
|
|
|
|
|
$message = new Message( $key, $params, $language );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $key, $keyProperty->getValue( $message ) );
|
|
|
|
|
$this->assertEquals( $params, $paramsProperty->getValue( $message ) );
|
|
|
|
|
$this->assertEquals( $expectedLang, $langProperty->getValue( $message ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideConstructor() {
|
|
|
|
|
$langDe = Language::factory( 'de' );
|
|
|
|
|
$langEn = Language::factory( 'en' );
|
|
|
|
|
|
|
|
|
|
return array(
|
|
|
|
|
array( $langDe, 'foo', array(), $langDe ),
|
|
|
|
|
array( $langDe, 'foo', array( 'bar' ), $langDe ),
|
|
|
|
|
array( $langEn, 'foo', array( 'bar' ), null )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-06 19:23:22 +00:00
|
|
|
public function provideTestParams() {
|
|
|
|
|
return array(
|
|
|
|
|
array( array() ),
|
|
|
|
|
array( array( 'foo' ), 'foo' ),
|
|
|
|
|
array( array( 'foo', 'bar' ), 'foo', 'bar' ),
|
|
|
|
|
array( array( 'baz' ), array( 'baz' ) ),
|
|
|
|
|
array( array( 'baz', 'foo' ), array( 'baz', 'foo' ) ),
|
|
|
|
|
array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh' ),
|
|
|
|
|
array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
|
|
|
|
|
array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
|
|
|
|
|
array( array( 'baz' ), array( 'baz' ), array( 'ahahahahha' ) ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-26 17:59:30 +00:00
|
|
|
public function getLanguageProvider() {
|
|
|
|
|
return array(
|
|
|
|
|
array( 'foo', array( 'bar' ), 'en' ),
|
|
|
|
|
array( 'foo', array( 'bar' ), 'de' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Message::getLanguage
|
|
|
|
|
* @dataProvider getLanguageProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testGetLanguageCode( $key, $params, $languageCode ) {
|
|
|
|
|
$language = Language::factory( $languageCode );
|
|
|
|
|
$message = new Message( $key, $params, $language );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $language, $message->getLanguage() );
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-06 19:23:22 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::params
|
|
|
|
|
* @dataProvider provideTestParams
|
|
|
|
|
*/
|
|
|
|
|
public function testParams( $expected ) {
|
|
|
|
|
$msg = new Message( 'imasomething' );
|
|
|
|
|
|
2014-03-20 18:59:20 +00:00
|
|
|
$returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
|
2014-03-06 19:23:22 +00:00
|
|
|
|
|
|
|
|
$this->assertSame( $msg, $returned );
|
|
|
|
|
$this->assertEquals( $expected, $msg->getParams() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::exists
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testExists() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertTrue( wfMessage( 'mainpage' )->exists() );
|
|
|
|
|
$this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
|
|
|
|
|
$this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
|
|
|
|
|
$this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
|
|
|
|
|
$this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
|
|
|
|
|
$this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::__construct
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testKey() {
|
2010-12-28 15:13:42 +00:00
|
|
|
$this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
|
|
|
|
|
$this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
|
|
|
|
|
$this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
|
2012-03-28 10:06:23 +00:00
|
|
|
$this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
|
|
|
|
|
$this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::inLanguage
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testInLanguage() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
|
2014-07-24 17:03:17 +00:00
|
|
|
$this->assertEquals( 'Заглавная страница',
|
|
|
|
|
wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
|
2014-04-11 10:19:25 +00:00
|
|
|
|
|
|
|
|
// NOTE: make sure internal caching of the message text is reset appropriately
|
|
|
|
|
$msg = wfMessage( 'mainpage' );
|
|
|
|
|
$this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'Заглавная страница',
|
|
|
|
|
$msg->inLanguage( Language::factory( 'ru' ) )->text()
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::__construct
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testMessageParams() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
|
|
|
|
|
$this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'You have foo (bar).',
|
|
|
|
|
wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'You have foo (bar).',
|
|
|
|
|
wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::__construct
|
|
|
|
|
* @covers Message::rawParams
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testMessageParamSubstitution() {
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'(Заглавная страница)',
|
|
|
|
|
wfMessage( 'parentheses', 'Заглавная страница' )->plain()
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'(Заглавная страница $1)',
|
|
|
|
|
wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'(Заглавная страница)',
|
|
|
|
|
wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'(Заглавная страница $1)',
|
|
|
|
|
wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
|
|
|
|
|
);
|
2011-06-06 17:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::__construct
|
|
|
|
|
* @covers Message::params
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testDeliciouslyManyParams() {
|
2013-01-02 17:06:23 +00:00
|
|
|
$msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
|
|
|
|
|
// One less than above has placeholders
|
|
|
|
|
$params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'abcdefghijka2',
|
|
|
|
|
$msg->params( $params )->plain(),
|
|
|
|
|
'Params > 9 are replaced correctly'
|
|
|
|
|
);
|
2013-01-02 17:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-23 20:32:43 +00:00
|
|
|
/**
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers Message::numParams
|
2013-08-23 20:32:43 +00:00
|
|
|
*/
|
2014-03-06 19:36:29 +00:00
|
|
|
public function testMessageNumParams() {
|
2013-08-23 20:32:43 +00:00
|
|
|
$lang = Language::factory( 'en' );
|
|
|
|
|
$msg = new RawMessage( '$1' );
|
2014-03-06 19:36:29 +00:00
|
|
|
|
2013-08-23 20:32:43 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$lang->formatNum( 123456.789 ),
|
|
|
|
|
$msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
|
|
|
|
|
'numParams is handled correctly'
|
|
|
|
|
);
|
2014-03-06 19:36:29 +00:00
|
|
|
}
|
2013-08-23 20:32:43 +00:00
|
|
|
|
2014-03-06 19:36:29 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::durationParams
|
|
|
|
|
*/
|
|
|
|
|
public function testMessageDurationParams() {
|
|
|
|
|
$lang = Language::factory( 'en' );
|
2013-08-23 20:32:43 +00:00
|
|
|
$msg = new RawMessage( '$1' );
|
2014-03-06 19:36:29 +00:00
|
|
|
|
2013-08-23 20:32:43 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$lang->formatDuration( 1234 ),
|
|
|
|
|
$msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
|
|
|
|
|
'durationParams is handled correctly'
|
|
|
|
|
);
|
2014-03-06 19:36:29 +00:00
|
|
|
}
|
2013-08-23 20:32:43 +00:00
|
|
|
|
2014-03-06 19:36:29 +00:00
|
|
|
/**
|
|
|
|
|
* FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
|
|
|
|
|
* @group Database
|
|
|
|
|
* @covers Message::expiryParams
|
|
|
|
|
*/
|
|
|
|
|
public function testMessageExpiryParams() {
|
|
|
|
|
$lang = Language::factory( 'en' );
|
2013-08-23 20:32:43 +00:00
|
|
|
$msg = new RawMessage( '$1' );
|
2014-03-06 19:36:29 +00:00
|
|
|
|
2013-08-23 20:32:43 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$lang->formatExpiry( wfTimestampNow() ),
|
|
|
|
|
$msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
|
|
|
|
|
'expiryParams is handled correctly'
|
|
|
|
|
);
|
2014-03-06 19:36:29 +00:00
|
|
|
}
|
2013-08-23 20:32:43 +00:00
|
|
|
|
2014-03-06 19:36:29 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::timeperiodParams
|
|
|
|
|
*/
|
|
|
|
|
public function testMessageTimeperiodParams() {
|
|
|
|
|
$lang = Language::factory( 'en' );
|
2013-08-23 20:32:43 +00:00
|
|
|
$msg = new RawMessage( '$1' );
|
2014-03-06 19:36:29 +00:00
|
|
|
|
2013-08-23 20:32:43 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$lang->formatTimePeriod( 1234 ),
|
|
|
|
|
$msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
|
|
|
|
|
'timeperiodParams is handled correctly'
|
|
|
|
|
);
|
2014-03-06 19:36:29 +00:00
|
|
|
}
|
2013-08-23 20:32:43 +00:00
|
|
|
|
2014-03-06 19:36:29 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::sizeParams
|
|
|
|
|
*/
|
|
|
|
|
public function testMessageSizeParams() {
|
|
|
|
|
$lang = Language::factory( 'en' );
|
2013-08-23 20:32:43 +00:00
|
|
|
$msg = new RawMessage( '$1' );
|
2014-03-06 19:36:29 +00:00
|
|
|
|
2013-08-23 20:32:43 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$lang->formatSize( 123456 ),
|
|
|
|
|
$msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
|
|
|
|
|
'sizeParams is handled correctly'
|
|
|
|
|
);
|
2014-03-06 19:36:29 +00:00
|
|
|
}
|
2013-08-23 20:32:43 +00:00
|
|
|
|
2014-03-06 19:36:29 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::bitrateParams
|
|
|
|
|
*/
|
|
|
|
|
public function testMessageBitrateParams() {
|
|
|
|
|
$lang = Language::factory( 'en' );
|
2013-08-23 20:32:43 +00:00
|
|
|
$msg = new RawMessage( '$1' );
|
2014-03-06 19:36:29 +00:00
|
|
|
|
2013-08-23 20:32:43 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$lang->formatBitrate( 123456 ),
|
|
|
|
|
$msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
|
|
|
|
|
'bitrateParams is handled correctly'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::inContentLanguage
|
|
|
|
|
*/
|
2014-04-11 10:19:25 +00:00
|
|
|
public function testInContentLanguage() {
|
2013-03-21 19:05:39 +00:00
|
|
|
$this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
|
2011-02-10 10:10:44 +00:00
|
|
|
|
2014-04-11 10:19:25 +00:00
|
|
|
// NOTE: make sure internal caching of the message text is reset appropriately
|
|
|
|
|
$msg = wfMessage( 'mainpage' );
|
|
|
|
|
$this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
|
|
|
|
|
$this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
|
|
|
|
|
$this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
|
2013-03-21 19:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Message::inContentLanguage
|
|
|
|
|
*/
|
2014-04-11 10:19:25 +00:00
|
|
|
public function testInContentLanguageOverride() {
|
2013-03-21 19:05:39 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgLang' => Language::factory( 'fr' ),
|
|
|
|
|
'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
|
|
|
|
|
) );
|
|
|
|
|
|
2014-04-11 10:19:25 +00:00
|
|
|
// NOTE: make sure internal caching of the message text is reset appropriately.
|
|
|
|
|
// NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
|
|
|
|
|
$msg = wfMessage( 'mainpage' );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'Accueil',
|
|
|
|
|
$msg->inContentLanguage()->plain(),
|
|
|
|
|
'inContentLanguage() with ForceUIMsg override enabled'
|
|
|
|
|
);
|
2014-04-11 10:19:25 +00:00
|
|
|
$this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'Main Page',
|
|
|
|
|
$msg->inContentLanguage()->plain(),
|
|
|
|
|
'inContentLanguage() with ForceUIMsg override enabled'
|
|
|
|
|
);
|
2014-04-11 10:19:25 +00:00
|
|
|
$this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
|
2011-02-10 10:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* @expectedException MWException
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers Message::inLanguage
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testInLanguageThrows() {
|
2010-12-14 16:26:35 +00:00
|
|
|
wfMessage( 'foo' )->inLanguage( 123 );
|
|
|
|
|
}
|
2014-04-17 09:01:36 +00:00
|
|
|
|
|
|
|
|
public function keyProvider() {
|
|
|
|
|
return array(
|
|
|
|
|
'string' => array(
|
|
|
|
|
'key' => 'mainpage',
|
|
|
|
|
'expected' => array( 'mainpage' ),
|
|
|
|
|
),
|
|
|
|
|
'single' => array(
|
|
|
|
|
'key' => array( 'mainpage' ),
|
|
|
|
|
'expected' => array( 'mainpage' ),
|
|
|
|
|
),
|
|
|
|
|
'multi' => array(
|
|
|
|
|
'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
|
|
|
|
|
'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
|
|
|
|
|
),
|
|
|
|
|
'empty' => array(
|
|
|
|
|
'key' => array(),
|
|
|
|
|
'expected' => null,
|
|
|
|
|
'exception' => 'InvalidArgumentException',
|
|
|
|
|
),
|
|
|
|
|
'null' => array(
|
|
|
|
|
'key' => null,
|
|
|
|
|
'expected' => null,
|
|
|
|
|
'exception' => 'InvalidArgumentException',
|
|
|
|
|
),
|
|
|
|
|
'bad type' => array(
|
|
|
|
|
'key' => 17,
|
|
|
|
|
'expected' => null,
|
|
|
|
|
'exception' => 'InvalidArgumentException',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider keyProvider()
|
|
|
|
|
*
|
|
|
|
|
* @covers Message::getKey
|
|
|
|
|
*/
|
|
|
|
|
public function testGetKey( $key, $expected, $exception = null ) {
|
|
|
|
|
if ( $exception ) {
|
|
|
|
|
$this->setExpectedException( $exception );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$msg = new Message( $key );
|
|
|
|
|
$this->assertEquals( $expected, $msg->getKeysToTry() );
|
|
|
|
|
$this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
|
|
|
|
|
$this->assertContains( $msg->getKey(), $expected );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|