| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Message; |
| 4 |
|
| 5 |
use MediaWiki\Message\Message; |
| 6 |
use MediaWiki\Message\TextFormatter; |
| 7 |
use MediaWiki\Message\UserGroupMembershipParam; |
| 8 |
use MediaWiki\User\UserIdentityValue; |
| 9 |
use MediaWikiIntegrationTestCase; |
| 10 |
use Wikimedia\Message\MessageValue; |
| 11 |
use Wikimedia\Message\ParamType; |
| 12 |
use Wikimedia\Message\ScalarParam; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class TextFormatterTest extends MediaWikiIntegrationTestCase { |
| 22 |
private function createTextFormatter( $langCode, |
| 23 |
$includeWikitext = false, |
| 24 |
$format = Message::FORMAT_TEXT |
| 25 |
) { |
| 26 |
$formatter = $this->getMockBuilder( TextFormatter::class ) |
| 27 |
->onlyMethods( [ 'createMessage' ] ) |
| 28 |
->setConstructorArgs( [ $langCode, $format ] ) |
| 29 |
->getMock(); |
| 30 |
$formatter->method( 'createMessage' ) |
| 31 |
->willReturnCallback( function ( $spec ) use ( $includeWikitext ) { |
| 32 |
$message = $this->getMockBuilder( Message::class ) |
| 33 |
->setConstructorArgs( [ $spec ] ) |
| 34 |
->onlyMethods( [ 'fetchMessage' ] ) |
| 35 |
->getMock(); |
| 36 |
|
| 37 |
$message->method( 'fetchMessage' ) |
| 38 |
->willReturnCallback( static function () use ( $message, $includeWikitext ) { |
| 39 |
|
| 40 |
$result = "{$message->getKey()} $1 $2"; |
| 41 |
if ( $includeWikitext ) { |
| 42 |
$result .= " {{SITENAME}}"; |
| 43 |
} |
| 44 |
return $result; |
| 45 |
} ); |
| 46 |
|
| 47 |
return $message; |
| 48 |
} ); |
| 49 |
|
| 50 |
return $formatter; |
| 51 |
} |
| 52 |
|
| 53 |
public function testGetLangCode() { |
| 54 |
$formatter = new TextFormatter( 'fr' ); |
| 55 |
$this->assertSame( 'fr', $formatter->getLangCode() ); |
| 56 |
} |
| 57 |
|
| 58 |
public function testFormatBitrate() { |
| 59 |
$formatter = $this->createTextFormatter( 'en' ); |
| 60 |
$mv = ( new MessageValue( 'test' ) )->bitrateParams( 100, 200 ); |
| 61 |
$result = $formatter->format( $mv ); |
| 62 |
$this->assertSame( 'test 100 bps 200 bps', $result ); |
| 63 |
} |
| 64 |
|
| 65 |
public function testFormatShortDuration() { |
| 66 |
$formatter = $this->createTextFormatter( 'en' ); |
| 67 |
$mv = ( new MessageValue( 'test' ) )->shortDurationParams( 100, 200 ); |
| 68 |
$result = $formatter->format( $mv ); |
| 69 |
$this->assertSame( 'test 1 min 40 s 3 min 20 s', $result ); |
| 70 |
} |
| 71 |
|
| 72 |
public function testFormatList() { |
| 73 |
$formatter = $this->createTextFormatter( 'en' ); |
| 74 |
$mv = ( new MessageValue( 'test' ) )->commaListParams( [ |
| 75 |
'a', |
| 76 |
new ScalarParam( ParamType::BITRATE, 100 ), |
| 77 |
] ); |
| 78 |
$result = $formatter->format( $mv ); |
| 79 |
$this->assertSame( 'test a, 100 bps $2', $result ); |
| 80 |
} |
| 81 |
|
| 82 |
public static function provideTestFormatMessage() { |
| 83 |
yield [ ( new MessageValue( 'test' ) ) |
| 84 |
->params( new MessageValue( 'test2', [ 'a', 'b' ] ) ) |
| 85 |
->commaListParams( [ |
| 86 |
'x', |
| 87 |
new ScalarParam( ParamType::BITRATE, 100 ), |
| 88 |
new MessageValue( 'test3', [ 'c', new MessageValue( 'test4', [ 'd', 'e' ] ) ] ) |
| 89 |
] ), |
| 90 |
'test (test2: a, b) x(comma-separator)(bitrate-bits)(comma-separator)(test3: c, (test4: d, e))' |
| 91 |
]; |
| 92 |
|
| 93 |
yield [ ( new MessageValue( 'test' ) ) |
| 94 |
->userGroupParams( 'bot' ), |
| 95 |
'test (group-bot) $2' |
| 96 |
]; |
| 97 |
|
| 98 |
|
| 99 |
@yield [ ( new MessageValue( 'test' ) ) |
| 100 |
->objectParams( |
| 101 |
new UserGroupMembershipParam( 'bot', new UserIdentityValue( 1, 'user' ) ) |
| 102 |
), |
| 103 |
'test (group-bot-member: user) $2' |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function testFormatMessage( $message, $expected ) { |
| 111 |
$formatter = $this->createTextFormatter( 'qqx' ); |
| 112 |
$result = $formatter->format( $message ); |
| 113 |
$this->assertSame( $expected, $result ); |
| 114 |
} |
| 115 |
|
| 116 |
public function testFormatMessageFormatsWikitext() { |
| 117 |
global $wgSitename; |
| 118 |
$formatter = $this->createTextFormatter( 'en', true ); |
| 119 |
$mv = MessageValue::new( 'test' ) |
| 120 |
->plaintextParams( '1', '2' ); |
| 121 |
$this->assertSame( "test 1 2 $wgSitename", $formatter->format( $mv ) ); |
| 122 |
} |
| 123 |
|
| 124 |
public function testFormatMessageNotWikitext() { |
| 125 |
$formatter = $this->createTextFormatter( 'en', true, Message::FORMAT_PLAIN ); |
| 126 |
$mv = MessageValue::new( 'test' ) |
| 127 |
->plaintextParams( '1', '2' ); |
| 128 |
$this->assertSame( "test 1 2 {{SITENAME}}", $formatter->format( $mv ) ); |
| 129 |
} |
| 130 |
} |