Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / api / ApiMessageTest.php
blobc54197921e00f6d9a31e06c49f5a688aac5a39df
1 <?php
3 namespace MediaWiki\Tests\Api;
5 use InvalidArgumentException;
6 use MediaWiki\Api\ApiMessage;
7 use MediaWiki\Api\ApiRawMessage;
8 use MediaWiki\Language\RawMessage;
9 use MediaWiki\Message\Message;
10 use MediaWiki\Page\PageReferenceValue;
11 use MediaWikiIntegrationTestCase;
12 use Wikimedia\TestingAccessWrapper;
14 /**
15 * @group API
17 class ApiMessageTest extends MediaWikiIntegrationTestCase {
19 private function compareMessages( Message $msg, Message $msg2 ) {
20 $this->assertSame( $msg->getKey(), $msg2->getKey(), 'getKey' );
21 $this->assertSame( $msg->getKeysToTry(), $msg2->getKeysToTry(), 'getKeysToTry' );
22 $this->assertSame( $msg->getParams(), $msg2->getParams(), 'getParams' );
23 $this->assertSame( $msg->getLanguage(), $msg2->getLanguage(), 'getLanguage' );
25 $msg = TestingAccessWrapper::newFromObject( $msg );
26 $msg2 = TestingAccessWrapper::newFromObject( $msg2 );
27 $this->assertSame( $msg->isInterface, $msg2->isInterface, 'interface' );
28 $this->assertSame( $msg->useDatabase, $msg2->useDatabase, 'useDatabase' );
29 $this->assertSame(
30 $msg->contextPage ? "{$msg->contextPage->getNamespace()}:{$msg->contextPage->getDbKey()}" : null,
31 $msg2->contextPage ? "{$msg->contextPage->getNamespace()}:{$msg->contextPage->getDbKey()}" : null,
32 'title'
36 /**
37 * @covers \MediaWiki\Api\ApiMessageTrait
38 * @dataProvider provideCodeDefaults
40 public function testCodeDefaults( $msg, $expectedCode ) {
41 $apiMessage = new ApiMessage( $msg );
42 $this->assertSame( $expectedCode, $apiMessage->getApiCode() );
45 public static function provideCodeDefaults() {
46 // $msg, $expectedCode
47 yield 'foo' => [ 'foo', 'foo' ];
48 yield 'apierror prefix' => [ 'apierror-bar', 'bar' ];
49 yield 'apiwarn prefix' => [ 'apiwarn-baz', 'baz' ];
50 yield 'Weird "message key"' => [ "<foo> bar\nbaz", '_foo__bar_baz' ];
51 yield 'BC string' => [ 'actionthrottledtext', 'ratelimited' ];
52 yield 'array' => [ [ 'apierror-missingparam', 'param' ], 'noparam' ];
55 /**
56 * @covers \MediaWiki\Api\ApiMessageTrait
57 * @dataProvider provideInvalidCode
59 public function testInvalidCode( $code ) {
60 $msg = new ApiMessage( 'foo' );
61 try {
62 $msg->setApiCode( $code );
63 $this->fail( 'Expected exception not thrown' );
64 } catch ( InvalidArgumentException $ex ) {
65 $this->assertTrue( true );
68 try {
69 new ApiMessage( 'foo', $code );
70 $this->fail( 'Expected exception not thrown' );
71 } catch ( InvalidArgumentException $ex ) {
72 $this->assertTrue( true );
76 public static function provideInvalidCode() {
77 return [
78 [ '' ],
79 [ 42 ],
80 [ 'A bad code' ],
81 [ 'Project:A_page_title' ],
82 [ "WTF\nnewlines" ],
86 /**
87 * @covers \MediaWiki\Api\ApiMessage
88 * @covers \MediaWiki\Api\ApiMessageTrait
90 public function testApiMessage() {
91 $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
92 $msg->inLanguage( 'de' )
93 ->page(
94 PageReferenceValue::localReference( NS_MAIN, 'Main_Page' )
96 $msg2 = new ApiMessage( $msg, 'code', [ 'data' ] );
97 $this->compareMessages( $msg, $msg2 );
98 $this->assertEquals( 'code', $msg2->getApiCode() );
99 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
101 $msg2 = unserialize( serialize( $msg2 ) );
102 $this->compareMessages( $msg, $msg2 );
103 $this->assertEquals( 'code', $msg2->getApiCode() );
104 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
106 $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
107 $msg2 = new ApiMessage( [ [ 'foo', 'bar' ], 'baz' ], 'code', [ 'data' ] );
108 $this->compareMessages( $msg, $msg2 );
109 $this->assertEquals( 'code', $msg2->getApiCode() );
110 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
112 $msg = new Message( 'foo' );
113 $msg2 = new ApiMessage( 'foo' );
114 $this->compareMessages( $msg, $msg2 );
115 $this->assertEquals( 'foo', $msg2->getApiCode() );
116 $this->assertEquals( [], $msg2->getApiData() );
118 $msg2->setApiCode( 'code', [ 'data' ] );
119 $this->assertEquals( 'code', $msg2->getApiCode() );
120 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
121 $msg2->setApiCode( null );
122 $this->assertEquals( 'foo', $msg2->getApiCode() );
123 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
124 $msg2->setApiData( [ 'data2' ] );
125 $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
129 * @covers \MediaWiki\Api\ApiRawMessage
130 * @covers \MediaWiki\Api\ApiMessageTrait
132 public function testApiRawMessage() {
133 $msg = new RawMessage( 'foo', [ 'baz' ] );
134 $msg->inLanguage( 'de' )->page(
135 PageReferenceValue::localReference( NS_MAIN, 'Main_Page' )
137 $msg2 = new ApiRawMessage( $msg, 'code', [ 'data' ] );
138 $this->compareMessages( $msg, $msg2 );
139 $this->assertEquals( 'code', $msg2->getApiCode() );
140 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
142 $msg2 = unserialize( serialize( $msg2 ) );
143 $this->compareMessages( $msg, $msg2 );
144 $this->assertEquals( 'code', $msg2->getApiCode() );
145 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
147 $msg = new RawMessage( 'foo', [ 'baz' ] );
148 $msg2 = new ApiRawMessage( [ 'foo', 'baz' ], 'code', [ 'data' ] );
149 $this->compareMessages( $msg, $msg2 );
150 $this->assertEquals( 'code', $msg2->getApiCode() );
151 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
153 $msg = new RawMessage( 'foo' );
154 $msg2 = new ApiRawMessage( 'foo', 'code', [ 'data' ] );
155 $this->compareMessages( $msg, $msg2 );
156 $this->assertEquals( 'code', $msg2->getApiCode() );
157 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
159 $msg2->setApiCode( 'code', [ 'data' ] );
160 $this->assertEquals( 'code', $msg2->getApiCode() );
161 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
162 $msg2->setApiCode( null );
163 $this->assertEquals( 'foo', $msg2->getApiCode() );
164 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
165 $msg2->setApiData( [ 'data2' ] );
166 $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
170 * @covers \MediaWiki\Api\ApiMessage::create
172 public function testApiMessageCreate() {
173 $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( new Message( 'mainpage' ) ) );
174 $this->assertInstanceOf(
175 ApiRawMessage::class, ApiMessage::create( new RawMessage( 'mainpage' ) )
177 $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( 'mainpage' ) );
179 $msg = new ApiMessage( [ 'parentheses', 'foobar' ] );
180 $msg2 = new Message( 'parentheses', [ 'foobar' ] );
182 $this->assertSame( $msg, ApiMessage::create( $msg ) );
183 $this->assertEquals( $msg, ApiMessage::create( $msg2 ) );
184 $this->assertEquals( $msg, ApiMessage::create( [ 'parentheses', 'foobar' ] ) );
185 $this->assertEquals( $msg,
186 ApiMessage::create( [ 'message' => 'parentheses', 'params' => [ 'foobar' ] ] )
188 $this->assertSame( $msg,
189 ApiMessage::create( [ 'message' => $msg, 'params' => [ 'xxx' ] ] )
191 $this->assertEquals( $msg,
192 ApiMessage::create( [ 'message' => $msg2, 'params' => [ 'xxx' ] ] )
194 $this->assertSame( $msg,
195 ApiMessage::create( [ 'message' => $msg ] )
198 $msg = new ApiRawMessage( [ 'parentheses', 'foobar' ] );
199 $this->assertSame( $msg, ApiMessage::create( $msg ) );