3 namespace MediaWiki\Tests\Message
;
5 use MediaWiki\Message\Message
;
6 use MediaWiki\Message\TextFormatter
;
7 use MediaWikiIntegrationTestCase
;
8 use Wikimedia\Message\MessageValue
;
9 use Wikimedia\Message\ParamType
;
10 use Wikimedia\Message\ScalarParam
;
13 * @covers \MediaWiki\Message\TextFormatter
14 * @covers \Wikimedia\Message\MessageValue
15 * @covers \Wikimedia\Message\ListParam
16 * @covers \Wikimedia\Message\ScalarParam
17 * @covers \Wikimedia\Message\MessageParam
19 class TextFormatterTest
extends MediaWikiIntegrationTestCase
{
20 private function createTextFormatter( $langCode,
21 $includeWikitext = false,
22 $format = Message
::FORMAT_TEXT
24 $formatter = $this->getMockBuilder( TextFormatter
::class )
25 ->onlyMethods( [ 'createMessage' ] )
26 ->setConstructorArgs( [ $langCode, $format ] )
28 $formatter->method( 'createMessage' )
29 ->willReturnCallback( function ( $spec ) use ( $includeWikitext ) {
30 $message = $this->getMockBuilder( Message
::class )
31 ->setConstructorArgs( [ $spec ] )
32 ->onlyMethods( [ 'fetchMessage' ] )
35 $message->method( 'fetchMessage' )
36 ->willReturnCallback( static function () use ( $message, $includeWikitext ) {
37 /** @var Message $message */
38 $result = "{$message->getKey()} $1 $2";
39 if ( $includeWikitext ) {
40 $result .= " {{SITENAME}}";
51 public function testGetLangCode() {
52 $formatter = new TextFormatter( 'fr' );
53 $this->assertSame( 'fr', $formatter->getLangCode() );
56 public function testFormatBitrate() {
57 $formatter = $this->createTextFormatter( 'en' );
58 $mv = ( new MessageValue( 'test' ) )->bitrateParams( 100, 200 );
59 $result = $formatter->format( $mv );
60 $this->assertSame( 'test 100 bps 200 bps', $result );
63 public function testFormatShortDuration() {
64 $formatter = $this->createTextFormatter( 'en' );
65 $mv = ( new MessageValue( 'test' ) )->shortDurationParams( 100, 200 );
66 $result = $formatter->format( $mv );
67 $this->assertSame( 'test 1 min 40 s 3 min 20 s', $result );
70 public function testFormatList() {
71 $formatter = $this->createTextFormatter( 'en' );
72 $mv = ( new MessageValue( 'test' ) )->commaListParams( [
74 new ScalarParam( ParamType
::BITRATE
, 100 ),
76 $result = $formatter->format( $mv );
77 $this->assertSame( 'test a, 100 bps $2', $result );
80 public static function provideTestFormatMessage() {
81 yield
[ ( new MessageValue( 'test' ) )
82 ->params( new MessageValue( 'test2', [ 'a', 'b' ] ) )
85 new ScalarParam( ParamType
::BITRATE
, 100 ),
86 new MessageValue( 'test3', [ 'c', new MessageValue( 'test4', [ 'd', 'e' ] ) ] )
88 'test (test2: a, b) x(comma-separator)(bitrate-bits)(comma-separator)(test3: c, (test4: d, e))'
91 yield
[ ( new MessageValue( 'test' ) )
92 ->userGroupParams( 'bot' ),
98 * @dataProvider provideTestFormatMessage
100 public function testFormatMessage( $message, $expected ) {
101 $formatter = $this->createTextFormatter( 'qqx' );
102 $result = $formatter->format( $message );
103 $this->assertSame( $expected, $result );
106 public function testFormatMessageFormatsWikitext() {
108 $formatter = $this->createTextFormatter( 'en', true );
109 $mv = MessageValue
::new( 'test' )
110 ->plaintextParams( '1', '2' );
111 $this->assertSame( "test 1 2 $wgSitename", $formatter->format( $mv ) );
114 public function testFormatMessageNotWikitext() {
115 $formatter = $this->createTextFormatter( 'en', true, Message
::FORMAT_PLAIN
);
116 $mv = MessageValue
::new( 'test' )
117 ->plaintextParams( '1', '2' );
118 $this->assertSame( "test 1 2 {{SITENAME}}", $formatter->format( $mv ) );