X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FMessageTest.php;h=4c689abb041050e552c59f3ed09bc9f0225d3aaf;hb=af614d2c3bebbf35e383e02bb957e41a295c159c;hp=8aa136144da5ebd2604575742228f9464a01af45;hpb=1f675de5971be2454b4ff89d1a01596fb3d38e06;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/MessageTest.php b/tests/phpunit/includes/MessageTest.php index 8aa136144d..1de9c23a36 100644 --- a/tests/phpunit/includes/MessageTest.php +++ b/tests/phpunit/includes/MessageTest.php @@ -18,8 +18,8 @@ class MessageTest extends MediaWikiLangTestCase { public function testConstructor( $expectedLang, $key, $params, $language ) { $message = new Message( $key, $params, $language ); - $this->assertEquals( $key, $message->getKey() ); - $this->assertEquals( $params, $message->getParams() ); + $this->assertSame( $key, $message->getKey() ); + $this->assertSame( $params, $message->getParams() ); $this->assertEquals( $expectedLang, $message->getLanguage() ); $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' ); @@ -29,8 +29,8 @@ class MessageTest extends MediaWikiLangTestCase { ->method( 'getParams' )->will( $this->returnValue( $params ) ); $message = new Message( $messageSpecifier, [], $language ); - $this->assertEquals( $key, $message->getKey() ); - $this->assertEquals( $params, $message->getParams() ); + $this->assertSame( $key, $message->getKey() ); + $this->assertSame( $params, $message->getParams() ); $this->assertEquals( $expectedLang, $message->getLanguage() ); } @@ -51,6 +51,10 @@ class MessageTest extends MediaWikiLangTestCase { [], [], ], + [ + [], + [ [] ], + ], [ [ 'foo' ], [ 'foo' ], @@ -68,19 +72,37 @@ class MessageTest extends MediaWikiLangTestCase { [ [ 'baz', 'foo' ] ], ], [ - [ 'baz', 'foo' ], + [ Message::rawParam( 'baz' ) ], + [ Message::rawParam( 'baz' ) ], + ], + [ + [ Message::rawParam( 'baz' ), 'foo' ], + [ Message::rawParam( 'baz' ), 'foo' ], + ], + [ + [ Message::rawParam( 'baz' ) ], + [ [ Message::rawParam( 'baz' ) ] ], + ], + [ + [ Message::rawParam( 'baz' ), 'foo' ], + [ [ Message::rawParam( 'baz' ), 'foo' ] ], + ], + + // Test handling of erroneous input, to detect if it changes + [ + [ [ 'baz', 'foo' ], 'hhh' ], [ [ 'baz', 'foo' ], 'hhh' ], ], [ - [ 'baz', 'foo' ], + [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ], [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ], ], [ - [ 'baz', 'foo' ], + [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ], [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ], ], [ - [ 'baz' ], + [ [ 'baz' ], [ 'ahahahahha' ] ], [ [ 'baz' ], [ 'ahahahahha' ] ], ], ]; @@ -97,7 +119,7 @@ class MessageTest extends MediaWikiLangTestCase { $returned = call_user_func_array( [ $msg, 'params' ], $args ); $this->assertSame( $msg, $returned ); - $this->assertEquals( $expected, $msg->getParams() ); + $this->assertSame( $expected, $msg->getParams() ); } public static function provideConstructorLanguage() { @@ -165,8 +187,8 @@ class MessageTest extends MediaWikiLangTestCase { $msg = new Message( $key ); $this->assertContains( $msg->getKey(), $expected ); - $this->assertEquals( $expected, $msg->getKeysToTry() ); - $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() ); + $this->assertSame( $expected, $msg->getKeysToTry() ); + $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() ); } /** @@ -190,16 +212,45 @@ class MessageTest extends MediaWikiLangTestCase { * @covers Message::__construct */ public function testWfMessageParams() { - $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() ); - $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', [] )->text() ); - $this->assertEquals( + $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() ); + $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() ); + $this->assertSame( + 'Return to 1,024.', + wfMessage( 'returnto', Message::numParam( 1024 ) )->text() + ); + $this->assertSame( + 'Return to 1,024.', + wfMessage( 'returnto', [ Message::numParam( 1024 ) ] )->text() + ); + $this->assertSame( 'You have foo (bar).', wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text() ); - $this->assertEquals( + $this->assertSame( 'You have foo (bar).', wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text() ); + $this->assertSame( + 'You have 1,024 (bar).', + wfMessage( + 'youhavenewmessages', + Message::numParam( 1024 ), 'bar' + )->text() + ); + $this->assertSame( + 'You have foo (2,048).', + wfMessage( + 'youhavenewmessages', + 'foo', Message::numParam( 2048 ) + )->text() + ); + $this->assertSame( + 'You have 1,024 (2,048).', + wfMessage( + 'youhavenewmessages', + [ Message::numParam( 1024 ), Message::numParam( 2048 ) ] + )->text() + ); } /** @@ -222,23 +273,28 @@ class MessageTest extends MediaWikiLangTestCase { * @covers Message::toString */ public function testToStringKey() { - $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() ); - $this->assertEquals( '', wfMessage( 'i-dont-exist-evar' )->text() ); - $this->assertEquals( 'exist-evar>', wfMessage( 'iexist-evar' )->text() ); - $this->assertEquals( '', wfMessage( 'i-dont-exist-evar' )->plain() ); - $this->assertEquals( 'exist-evar>', wfMessage( 'iexist-evar' )->plain() ); - $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() ); - $this->assertEquals( - '<i<dont>exist-evar>', + $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() ); + $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() ); + $this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'iexist-evar' )->text() ); + $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() ); + $this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'iexist-evar' )->plain() ); + $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() ); + $this->assertSame( + '⧼i<dont>exist-evar⧽', wfMessage( 'iexist-evar' )->escaped() ); } public static function provideToString() { return [ - [ 'mainpage', 'Main Page' ], - [ 'i-dont-exist-evar', '' ], - [ 'i-dont-exist-evar', '<i-dont-exist-evar>', 'escaped' ], + // key, transformation, transformed, transformed implicitly + [ 'mainpage', 'plain', 'Main Page', 'Main Page' ], + [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ], + [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ], + [ 'script>alert(1)alert(1)$format(); - $this->assertEquals( $expect, $msg->toString() ); - $this->assertEquals( $expect, $msg->__toString() ); + $this->assertSame( $expect, $msg->$format() ); + $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' ); + $this->assertSame( $expectImplicit, $msg->__toString() ); + $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' ); + } + + public static function provideToString_raw() { + return [ + [ 'foo', 'parse', 'foo', 'foo' ], + [ 'foo', 'escaped', '<span>foo</span>', + 'foo' ], + [ 'foo', 'plain', 'foo', 'foo' ], + [ '', 'parse', '<script>alert(1)</script>', + '<script>alert(1)</script>' ], + [ '', 'escaped', '<script>alert(1)</script>', + '<script>alert(1)</script>' ], + [ '', 'plain', '', + '<script>alert(1)</script>' ], + ]; + } + + /** + * @covers Message::toString + * @covers Message::__toString + * @dataProvider provideToString_raw + */ + public function testToString_raw( $message, $format, $expect, $expectImplicit ) { + // make the message behave like RawMessage and use the key as-is + $msg = $this->getMockBuilder( Message::class )->setMethods( [ 'fetchMessage' ] ) + ->disableOriginalConstructor() + ->getMock(); + $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message ); + /** @var Message $msg */ + $this->assertSame( $expect, $msg->$format() ); + $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' ); + $this->assertSame( $expectImplicit, $msg->__toString() ); + $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' ); } /** * @covers Message::inLanguage */ public function testInLanguage() { - $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() ); - $this->assertEquals( 'Заглавная страница', + $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() ); + $this->assertSame( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() ); // 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() ); - $this->assertEquals( + $this->assertSame( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() ); + $this->assertSame( 'Заглавная страница', $msg->inLanguage( Language::factory( 'ru' ) )->text() ); @@ -276,19 +366,19 @@ class MessageTest extends MediaWikiLangTestCase { * @covers Message::rawParams */ public function testRawParams() { - $this->assertEquals( + $this->assertSame( '(Заглавная страница)', wfMessage( 'parentheses', 'Заглавная страница' )->plain() ); - $this->assertEquals( + $this->assertSame( '(Заглавная страница $1)', wfMessage( 'parentheses', 'Заглавная страница $1' )->plain() ); - $this->assertEquals( + $this->assertSame( '(Заглавная страница)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain() ); - $this->assertEquals( + $this->assertSame( '(Заглавная страница $1)', wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain() ); @@ -300,8 +390,8 @@ class MessageTest extends MediaWikiLangTestCase { */ public function testRawMessage() { $msg = new RawMessage( 'example &' ); - $this->assertEquals( 'example &', $msg->plain() ); - $this->assertEquals( 'example &', $msg->escaped() ); + $this->assertSame( 'example &', $msg->plain() ); + $this->assertSame( 'example &', $msg->escaped() ); } /** @@ -313,7 +403,7 @@ class MessageTest extends MediaWikiLangTestCase { $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' ); // One less than above has placeholders $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ]; - $this->assertEquals( + $this->assertSame( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' @@ -321,7 +411,7 @@ class MessageTest extends MediaWikiLangTestCase { $msg = new RawMessage( 'Params$*' ); $params = [ 'ab', 'bc', 'cd' ]; - $this->assertEquals( + $this->assertSame( 'Params: ab, bc, cd', $msg->params( $params )->text() ); @@ -335,7 +425,7 @@ class MessageTest extends MediaWikiLangTestCase { $lang = Language::factory( 'en' ); $msg = new RawMessage( '$1' ); - $this->assertEquals( + $this->assertSame( $lang->formatNum( 123456.789 ), $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(), 'numParams is handled correctly' @@ -350,7 +440,7 @@ class MessageTest extends MediaWikiLangTestCase { $lang = Language::factory( 'en' ); $msg = new RawMessage( '$1' ); - $this->assertEquals( + $this->assertSame( $lang->formatDuration( 1234 ), $msg->inLanguage( $lang )->durationParams( 1234 )->plain(), 'durationParams is handled correctly' @@ -367,7 +457,7 @@ class MessageTest extends MediaWikiLangTestCase { $lang = Language::factory( 'en' ); $msg = new RawMessage( '$1' ); - $this->assertEquals( + $this->assertSame( $lang->formatExpiry( wfTimestampNow() ), $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(), 'expiryParams is handled correctly' @@ -382,7 +472,7 @@ class MessageTest extends MediaWikiLangTestCase { $lang = Language::factory( 'en' ); $msg = new RawMessage( '$1' ); - $this->assertEquals( + $this->assertSame( $lang->formatTimePeriod( 1234 ), $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(), 'timeperiodParams is handled correctly' @@ -397,7 +487,7 @@ class MessageTest extends MediaWikiLangTestCase { $lang = Language::factory( 'en' ); $msg = new RawMessage( '$1' ); - $this->assertEquals( + $this->assertSame( $lang->formatSize( 123456 ), $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(), 'sizeParams is handled correctly' @@ -412,7 +502,7 @@ class MessageTest extends MediaWikiLangTestCase { $lang = Language::factory( 'en' ); $msg = new RawMessage( '$1' ); - $this->assertEquals( + $this->assertSame( $lang->formatBitrate( 123456 ), $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(), 'bitrateParams is handled correctly' @@ -466,13 +556,143 @@ class MessageTest extends MediaWikiLangTestCase { 'one $2', '
foo
[[Bar]] {{Baz}} <', ]; - $this->assertEquals( + $this->assertSame( $expect, $msg->inLanguage( $lang )->plaintextParams( $params )->$format(), "Fail formatting for $format" ); } + public static function provideListParam() { + $lang = Language::factory( 'de' ); + $msg1 = new Message( 'mainpage', [], $lang ); + $msg2 = new RawMessage( "''link''", [], $lang ); + + return [ + 'Simple comma list' => [ + [ 'a', 'b', 'c' ], + 'comma', + 'text', + 'a, b, c' + ], + + 'Simple semicolon list' => [ + [ 'a', 'b', 'c' ], + 'semicolon', + 'text', + 'a; b; c' + ], + + 'Simple pipe list' => [ + [ 'a', 'b', 'c' ], + 'pipe', + 'text', + 'a | b | c' + ], + + 'Simple text list' => [ + [ 'a', 'b', 'c' ], + 'text', + 'text', + 'a, b and c' + ], + + 'Empty list' => [ + [], + 'comma', + 'text', + '' + ], + + 'List with all "before" params, ->text()' => [ + [ "''link''", Message::numParam( 12345678 ) ], + 'semicolon', + 'text', + '\'\'link\'\'; 12,345,678' + ], + + 'List with all "before" params, ->parse()' => [ + [ "''link''", Message::numParam( 12345678 ) ], + 'semicolon', + 'parse', + 'link; 12,345,678' + ], + + 'List with all "after" params, ->text()' => [ + [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ], + 'semicolon', + 'text', + 'Main Page; \'\'link\'\'; [[foo]]' + ], + + 'List with all "after" params, ->parse()' => [ + [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ], + 'semicolon', + 'parse', + 'Main Page; link; [[foo]]' + ], + + 'List with both "before" and "after" params, ->text()' => [ + [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ], + 'semicolon', + 'text', + 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678' + ], + + 'List with both "before" and "after" params, ->parse()' => [ + [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ], + 'semicolon', + 'parse', + 'Main Page; link; [[foo]]; link; 12,345,678' + ], + ]; + } + + /** + * @covers Message::listParam + * @covers Message::extractParam + * @covers Message::formatListParam + * @dataProvider provideListParam + */ + public function testListParam( $list, $type, $format, $expect ) { + $lang = Language::factory( 'en' ); + + $msg = new RawMessage( '$1' ); + $msg->params( [ Message::listParam( $list, $type ) ] ); + $this->assertEquals( + $expect, + $msg->inLanguage( $lang )->$format() + ); + } + + /** + * @covers Message::extractParam + */ + public function testMessageAsParam() { + $this->setMwGlobals( [ + 'wgScript' => '/wiki/index.php', + 'wgArticlePath' => '/wiki/$1', + ] ); + + $msg = new Message( 'returnto', [ + new Message( 'apihelp-link', [ + 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) ) + ], Language::factory( 'de' ) ) + ], Language::factory( 'es' ) ); + + $this->assertEquals( + 'Volver a [[Special:ApiHelp/foo|Página principal]].', + $msg->text(), + 'Process with ->text()' + ); + $this->assertEquals( + '

Volver a Página ' + . "principal.\n

", + $msg->parseAsBlock(), + 'Process with ->parseAsBlock()' + ); + } + public static function provideParser() { return [ [ @@ -507,7 +727,7 @@ class MessageTest extends MediaWikiLangTestCase { */ public function testParser( $expect, $format ) { $msg = new RawMessage( "''&'' " ); - $this->assertEquals( + $this->assertSame( $expect, $msg->inLanguage( 'en' )->$format() ); @@ -521,9 +741,9 @@ class MessageTest extends MediaWikiLangTestCase { // 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' )" ); + $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" ); + $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" ); + $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" ); } /** @@ -538,18 +758,18 @@ class MessageTest extends MediaWikiLangTestCase { // 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' ); - $this->assertEquals( + $this->assertSame( 'Accueil', $msg->inContentLanguage()->plain(), 'inContentLanguage() with ForceUIMsg override enabled' ); - $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" ); - $this->assertEquals( + $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" ); + $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), 'inContentLanguage() with ForceUIMsg override enabled' ); - $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" ); + $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" ); } /** @@ -568,18 +788,18 @@ class MessageTest extends MediaWikiLangTestCase { $msg = new Message( 'parentheses' ); $msg->rawParams( 'foo' ); $msg->title( Title::newFromText( 'Testing' ) ); - $this->assertEquals( '(foo)', $msg->parse(), 'Sanity check' ); + $this->assertSame( '(foo)', $msg->parse(), 'Sanity check' ); $msg = unserialize( serialize( $msg ) ); - $this->assertEquals( '(foo)', $msg->parse() ); + $this->assertSame( '(foo)', $msg->parse() ); $title = TestingAccessWrapper::newFromObject( $msg )->title; $this->assertInstanceOf( 'Title', $title ); - $this->assertEquals( 'Testing', $title->getFullText() ); + $this->assertSame( 'Testing', $title->getFullText() ); $msg = new Message( 'mainpage' ); $msg->inLanguage( 'de' ); - $this->assertEquals( 'Hauptseite', $msg->plain(), 'Sanity check' ); + $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' ); $msg = unserialize( serialize( $msg ) ); - $this->assertEquals( 'Hauptseite', $msg->plain() ); + $this->assertSame( 'Hauptseite', $msg->plain() ); } /** @@ -589,6 +809,10 @@ class MessageTest extends MediaWikiLangTestCase { public function testNewFromSpecifier( $value, $expectedText ) { $message = Message::newFromSpecifier( $value ); $this->assertInstanceOf( Message::class, $message ); + if ( $value instanceof Message ) { + $this->assertInstanceOf( get_class( $value ), $message ); + $this->assertEquals( $value, $message ); + } $this->assertSame( $expectedText, $message->text() ); } @@ -602,9 +826,9 @@ class MessageTest extends MediaWikiLangTestCase { 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ], 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ], 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ], + 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ], 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ], 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ], ]; } } -