wiki.techinc.nl/tests/phpunit/includes/MessageTest.php

131 lines
5.1 KiB
PHP
Raw Normal View History

<?php
class MessageTest extends MediaWikiLangTestCase {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
parent::setUp();
$this->setMwGlobals( array(
'wgLang' => Language::factory( 'en' ),
'wgForceUIMsgAsContentMsg' => array(),
) );
}
function testExists() {
$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() );
}
function testKey() {
$this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
$this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
$this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
$this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->text() );
$this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
$this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
}
function testInLanguage() {
$this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
$this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
$this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( Language::factory( 'en' ) )->text() );
$this->assertEquals( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( Language::factory( 'ru' ) )->text() );
}
2011-02-10 10:10:44 +00:00
function testMessageParams() {
$this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
$this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
$this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text() );
$this->assertEquals( 'You have foo (bar).', wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text() );
}
2011-02-10 10:10:44 +00:00
function testMessageParamSubstitution() {
$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() );
}
function testDeliciouslyManyParams() {
$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' );
$this->assertEquals( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' );
}
/**
* FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
* @group Database
*/
function testMessageParamTypes() {
$lang = Language::factory( 'en' );
$msg = new RawMessage( '$1' );
$this->assertEquals(
$lang->formatNum( 123456.789 ),
$msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
'numParams is handled correctly'
);
$msg = new RawMessage( '$1' );
$this->assertEquals(
$lang->formatDuration( 1234 ),
$msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
'durationParams is handled correctly'
);
$msg = new RawMessage( '$1' );
$this->assertEquals(
$lang->formatExpiry( wfTimestampNow() ),
$msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
'expiryParams is handled correctly'
);
$msg = new RawMessage( '$1' );
$this->assertEquals(
$lang->formatTimePeriod( 1234 ),
$msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
'timeperiodParams is handled correctly'
);
$msg = new RawMessage( '$1' );
$this->assertEquals(
$lang->formatSize( 123456 ),
$msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
'sizeParams is handled correctly'
);
$msg = new RawMessage( '$1' );
$this->assertEquals(
$lang->formatBitrate( 123456 ),
$msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
'bitrateParams is handled correctly'
);
}
function testInContentLanguageDisabled() {
$this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
2011-02-10 10:10:44 +00:00
$this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg disabled' );
}
function testInContentLanguageEnabled() {
$this->setMwGlobals( array(
'wgLang' => Language::factory( 'fr' ),
'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
) );
$this->assertEquals( 'Accueil', wfMessage( 'mainpage' )->inContentLanguage()->plain(), 'ForceUIMsg enabled' );
2011-02-10 10:10:44 +00:00
}
/**
* @expectedException MWException
*/
function testInLanguageThrows() {
wfMessage( 'foo' )->inLanguage( 123 );
}
}