Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / api / ApiMessageTest.php
blobe405b3b89592ad57d7e69446857465c9a529b8e1
1 <?php
3 /**
4 * @group API
5 */
6 class ApiMessageTest extends MediaWikiTestCase {
8 private function compareMessages( Message $msg, Message $msg2 ) {
9 $this->assertSame( $msg->getKey(), $msg2->getKey(), 'getKey' );
10 $this->assertSame( $msg->getKeysToTry(), $msg2->getKeysToTry(), 'getKeysToTry' );
11 $this->assertSame( $msg->getParams(), $msg2->getParams(), 'getParams' );
12 $this->assertSame( $msg->getLanguage(), $msg2->getLanguage(), 'getLanguage' );
14 $msg = TestingAccessWrapper::newFromObject( $msg );
15 $msg2 = TestingAccessWrapper::newFromObject( $msg2 );
16 $this->assertSame( $msg->interface, $msg2->interface, 'interface' );
17 $this->assertSame( $msg->useDatabase, $msg2->useDatabase, 'useDatabase' );
18 $this->assertSame( $msg->format, $msg2->format, 'format' );
19 $this->assertSame(
20 $msg->title ? $msg->title->getFullText() : null,
21 $msg2->title ? $msg2->title->getFullText() : null,
22 'title'
26 /**
27 * @covers ApiMessageTrait
29 public function testCodeDefaults() {
30 $msg = new ApiMessage( 'foo' );
31 $this->assertSame( 'foo', $msg->getApiCode() );
33 $msg = new ApiMessage( 'apierror-bar' );
34 $this->assertSame( 'bar', $msg->getApiCode() );
36 $msg = new ApiMessage( 'apiwarn-baz' );
37 $this->assertSame( 'baz', $msg->getApiCode() );
39 // BC case
40 $msg = new ApiMessage( 'actionthrottledtext' );
41 $this->assertSame( 'ratelimited', $msg->getApiCode() );
43 $msg = new ApiMessage( [ 'apierror-missingparam', 'param' ] );
44 $this->assertSame( 'noparam', $msg->getApiCode() );
47 /**
48 * @covers ApiMessageTrait
49 * @dataProvider provideInvalidCode
50 * @param mixed $code
52 public function testInvalidCode( $code ) {
53 $msg = new ApiMessage( 'foo' );
54 try {
55 $msg->setApiCode( $code );
56 $this->fail( 'Expected exception not thrown' );
57 } catch ( InvalidArgumentException $ex ) {
58 $this->assertTrue( true );
61 try {
62 new ApiMessage( 'foo', $code );
63 $this->fail( 'Expected exception not thrown' );
64 } catch ( InvalidArgumentException $ex ) {
65 $this->assertTrue( true );
69 public static function provideInvalidCode() {
70 return [
71 [ '' ],
72 [ 42 ],
76 /**
77 * @covers ApiMessage
78 * @covers ApiMessageTrait
80 public function testApiMessage() {
81 $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
82 $msg->inLanguage( 'de' )->title( Title::newMainPage() );
83 $msg2 = new ApiMessage( $msg, 'code', [ 'data' ] );
84 $this->compareMessages( $msg, $msg2 );
85 $this->assertEquals( 'code', $msg2->getApiCode() );
86 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
88 $msg2 = unserialize( serialize( $msg2 ) );
89 $this->compareMessages( $msg, $msg2 );
90 $this->assertEquals( 'code', $msg2->getApiCode() );
91 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
93 $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
94 $msg2 = new ApiMessage( [ [ 'foo', 'bar' ], 'baz' ], 'code', [ 'data' ] );
95 $this->compareMessages( $msg, $msg2 );
96 $this->assertEquals( 'code', $msg2->getApiCode() );
97 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
99 $msg = new Message( 'foo' );
100 $msg2 = new ApiMessage( 'foo' );
101 $this->compareMessages( $msg, $msg2 );
102 $this->assertEquals( 'foo', $msg2->getApiCode() );
103 $this->assertEquals( [], $msg2->getApiData() );
105 $msg2->setApiCode( 'code', [ 'data' ] );
106 $this->assertEquals( 'code', $msg2->getApiCode() );
107 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
108 $msg2->setApiCode( null );
109 $this->assertEquals( 'foo', $msg2->getApiCode() );
110 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
111 $msg2->setApiData( [ 'data2' ] );
112 $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
116 * @covers ApiRawMessage
117 * @covers ApiMessageTrait
119 public function testApiRawMessage() {
120 $msg = new RawMessage( 'foo', [ 'baz' ] );
121 $msg->inLanguage( 'de' )->title( Title::newMainPage() );
122 $msg2 = new ApiRawMessage( $msg, 'code', [ 'data' ] );
123 $this->compareMessages( $msg, $msg2 );
124 $this->assertEquals( 'code', $msg2->getApiCode() );
125 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
127 $msg2 = unserialize( serialize( $msg2 ) );
128 $this->compareMessages( $msg, $msg2 );
129 $this->assertEquals( 'code', $msg2->getApiCode() );
130 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
132 $msg = new RawMessage( 'foo', [ 'baz' ] );
133 $msg2 = new ApiRawMessage( [ 'foo', 'baz' ], 'code', [ 'data' ] );
134 $this->compareMessages( $msg, $msg2 );
135 $this->assertEquals( 'code', $msg2->getApiCode() );
136 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
138 $msg = new RawMessage( 'foo' );
139 $msg2 = new ApiRawMessage( 'foo', 'code', [ 'data' ] );
140 $this->compareMessages( $msg, $msg2 );
141 $this->assertEquals( 'code', $msg2->getApiCode() );
142 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
144 $msg2->setApiCode( 'code', [ 'data' ] );
145 $this->assertEquals( 'code', $msg2->getApiCode() );
146 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
147 $msg2->setApiCode( null );
148 $this->assertEquals( 'foo', $msg2->getApiCode() );
149 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
150 $msg2->setApiData( [ 'data2' ] );
151 $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
155 * @covers ApiMessage::create
157 public function testApiMessageCreate() {
158 $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( new Message( 'mainpage' ) ) );
159 $this->assertInstanceOf(
160 ApiRawMessage::class, ApiMessage::create( new RawMessage( 'mainpage' ) )
162 $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( 'mainpage' ) );
164 $msg = new ApiMessage( [ 'parentheses', 'foobar' ] );
165 $msg2 = new Message( 'parentheses', [ 'foobar' ] );
167 $this->assertSame( $msg, ApiMessage::create( $msg ) );
168 $this->assertEquals( $msg, ApiMessage::create( $msg2 ) );
169 $this->assertEquals( $msg, ApiMessage::create( [ 'parentheses', 'foobar' ] ) );
170 $this->assertEquals( $msg,
171 ApiMessage::create( [ 'message' => 'parentheses', 'params' => [ 'foobar' ] ] )
173 $this->assertSame( $msg,
174 ApiMessage::create( [ 'message' => $msg, 'params' => [ 'xxx' ] ] )
176 $this->assertEquals( $msg,
177 ApiMessage::create( [ 'message' => $msg2, 'params' => [ 'xxx' ] ] )
179 $this->assertSame( $msg,
180 ApiMessage::create( [ 'message' => $msg ] )
183 $msg = new ApiRawMessage( [ 'parentheses', 'foobar' ] );
184 $this->assertSame( $msg, ApiMessage::create( $msg ) );