MediaWiki REL1_31
ApiMessageTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
9
10 private function compareMessages( Message $msg, Message $msg2 ) {
11 $this->assertSame( $msg->getKey(), $msg2->getKey(), 'getKey' );
12 $this->assertSame( $msg->getKeysToTry(), $msg2->getKeysToTry(), 'getKeysToTry' );
13 $this->assertSame( $msg->getParams(), $msg2->getParams(), 'getParams' );
14 $this->assertSame( $msg->getLanguage(), $msg2->getLanguage(), 'getLanguage' );
15
16 $msg = TestingAccessWrapper::newFromObject( $msg );
17 $msg2 = TestingAccessWrapper::newFromObject( $msg2 );
18 $this->assertSame( $msg->interface, $msg2->interface, 'interface' );
19 $this->assertSame( $msg->useDatabase, $msg2->useDatabase, 'useDatabase' );
20 $this->assertSame( $msg->format, $msg2->format, 'format' );
21 $this->assertSame(
22 $msg->title ? $msg->title->getFullText() : null,
23 $msg2->title ? $msg2->title->getFullText() : null,
24 'title'
25 );
26 }
27
31 public function testCodeDefaults() {
32 $msg = new ApiMessage( 'foo' );
33 $this->assertSame( 'foo', $msg->getApiCode() );
34
35 $msg = new ApiMessage( 'apierror-bar' );
36 $this->assertSame( 'bar', $msg->getApiCode() );
37
38 $msg = new ApiMessage( 'apiwarn-baz' );
39 $this->assertSame( 'baz', $msg->getApiCode() );
40
41 // BC case
42 $msg = new ApiMessage( 'actionthrottledtext' );
43 $this->assertSame( 'ratelimited', $msg->getApiCode() );
44
45 $msg = new ApiMessage( [ 'apierror-missingparam', 'param' ] );
46 $this->assertSame( 'noparam', $msg->getApiCode() );
47 }
48
54 public function testInvalidCode( $code ) {
55 $msg = new ApiMessage( 'foo' );
56 try {
57 $msg->setApiCode( $code );
58 $this->fail( 'Expected exception not thrown' );
59 } catch ( InvalidArgumentException $ex ) {
60 $this->assertTrue( true );
61 }
62
63 try {
64 new ApiMessage( 'foo', $code );
65 $this->fail( 'Expected exception not thrown' );
66 } catch ( InvalidArgumentException $ex ) {
67 $this->assertTrue( true );
68 }
69 }
70
71 public static function provideInvalidCode() {
72 return [
73 [ '' ],
74 [ 42 ],
75 ];
76 }
77
82 public function testApiMessage() {
83 $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
84 $msg->inLanguage( 'de' )->title( Title::newMainPage() );
85 $msg2 = new ApiMessage( $msg, 'code', [ 'data' ] );
86 $this->compareMessages( $msg, $msg2 );
87 $this->assertEquals( 'code', $msg2->getApiCode() );
88 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
89
90 $msg2 = unserialize( serialize( $msg2 ) );
91 $this->compareMessages( $msg, $msg2 );
92 $this->assertEquals( 'code', $msg2->getApiCode() );
93 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
94
95 $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
96 $msg2 = new ApiMessage( [ [ 'foo', 'bar' ], 'baz' ], 'code', [ 'data' ] );
97 $this->compareMessages( $msg, $msg2 );
98 $this->assertEquals( 'code', $msg2->getApiCode() );
99 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
100
101 $msg = new Message( 'foo' );
102 $msg2 = new ApiMessage( 'foo' );
103 $this->compareMessages( $msg, $msg2 );
104 $this->assertEquals( 'foo', $msg2->getApiCode() );
105 $this->assertEquals( [], $msg2->getApiData() );
106
107 $msg2->setApiCode( 'code', [ 'data' ] );
108 $this->assertEquals( 'code', $msg2->getApiCode() );
109 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
110 $msg2->setApiCode( null );
111 $this->assertEquals( 'foo', $msg2->getApiCode() );
112 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
113 $msg2->setApiData( [ 'data2' ] );
114 $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
115 }
116
121 public function testApiRawMessage() {
122 $msg = new RawMessage( 'foo', [ 'baz' ] );
123 $msg->inLanguage( 'de' )->title( Title::newMainPage() );
124 $msg2 = new ApiRawMessage( $msg, 'code', [ 'data' ] );
125 $this->compareMessages( $msg, $msg2 );
126 $this->assertEquals( 'code', $msg2->getApiCode() );
127 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
128
129 $msg2 = unserialize( serialize( $msg2 ) );
130 $this->compareMessages( $msg, $msg2 );
131 $this->assertEquals( 'code', $msg2->getApiCode() );
132 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
133
134 $msg = new RawMessage( 'foo', [ 'baz' ] );
135 $msg2 = new ApiRawMessage( [ 'foo', 'baz' ], 'code', [ 'data' ] );
136 $this->compareMessages( $msg, $msg2 );
137 $this->assertEquals( 'code', $msg2->getApiCode() );
138 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
139
140 $msg = new RawMessage( 'foo' );
141 $msg2 = new ApiRawMessage( 'foo', 'code', [ 'data' ] );
142 $this->compareMessages( $msg, $msg2 );
143 $this->assertEquals( 'code', $msg2->getApiCode() );
144 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
145
146 $msg2->setApiCode( 'code', [ 'data' ] );
147 $this->assertEquals( 'code', $msg2->getApiCode() );
148 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
149 $msg2->setApiCode( null );
150 $this->assertEquals( 'foo', $msg2->getApiCode() );
151 $this->assertEquals( [ 'data' ], $msg2->getApiData() );
152 $msg2->setApiData( [ 'data2' ] );
153 $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
154 }
155
159 public function testApiMessageCreate() {
160 $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( new Message( 'mainpage' ) ) );
161 $this->assertInstanceOf(
162 ApiRawMessage::class, ApiMessage::create( new RawMessage( 'mainpage' ) )
163 );
164 $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( 'mainpage' ) );
165
166 $msg = new ApiMessage( [ 'parentheses', 'foobar' ] );
167 $msg2 = new Message( 'parentheses', [ 'foobar' ] );
168
169 $this->assertSame( $msg, ApiMessage::create( $msg ) );
170 $this->assertEquals( $msg, ApiMessage::create( $msg2 ) );
171 $this->assertEquals( $msg, ApiMessage::create( [ 'parentheses', 'foobar' ] ) );
172 $this->assertEquals( $msg,
173 ApiMessage::create( [ 'message' => 'parentheses', 'params' => [ 'foobar' ] ] )
174 );
175 $this->assertSame( $msg,
176 ApiMessage::create( [ 'message' => $msg, 'params' => [ 'xxx' ] ] )
177 );
178 $this->assertEquals( $msg,
179 ApiMessage::create( [ 'message' => $msg2, 'params' => [ 'xxx' ] ] )
180 );
181 $this->assertSame( $msg,
182 ApiMessage::create( [ 'message' => $msg ] )
183 );
184
185 $msg = new ApiRawMessage( [ 'parentheses', 'foobar' ] );
186 $this->assertSame( $msg, ApiMessage::create( $msg ) );
187 }
188
189}
serialize()
unserialize( $serialized)
testApiMessageCreate()
ApiMessage::create.
testApiRawMessage()
ApiRawMessage ApiMessageTrait.
testApiMessage()
ApiMessage ApiMessageTrait.
testCodeDefaults()
ApiMessageTrait.
static provideInvalidCode()
testInvalidCode( $code)
ApiMessageTrait provideInvalidCode.
compareMessages(Message $msg, Message $msg2)
Extension of Message implementing IApiMessage.
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Extension of RawMessage implementing IApiMessage.
The Message class provides methods which fulfil two basic services:
Definition Message.php:159
Variant of the Message class.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:865