MediaWiki  1.33.0
ApiMessageTest.php
Go to the documentation of this file.
1 <?php
2 
3 use 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  // Weird "message key"
42  $msg = new ApiMessage( "<foo> bar\nbaz" );
43  $this->assertSame( '_foo__bar_baz', $msg->getApiCode() );
44 
45  // BC case
46  $msg = new ApiMessage( 'actionthrottledtext' );
47  $this->assertSame( 'ratelimited', $msg->getApiCode() );
48 
49  $msg = new ApiMessage( [ 'apierror-missingparam', 'param' ] );
50  $this->assertSame( 'noparam', $msg->getApiCode() );
51  }
52 
58  public function testInvalidCode( $code ) {
59  $msg = new ApiMessage( 'foo' );
60  try {
61  $msg->setApiCode( $code );
62  $this->fail( 'Expected exception not thrown' );
63  } catch ( InvalidArgumentException $ex ) {
64  $this->assertTrue( true );
65  }
66 
67  try {
68  new ApiMessage( 'foo', $code );
69  $this->fail( 'Expected exception not thrown' );
70  } catch ( InvalidArgumentException $ex ) {
71  $this->assertTrue( true );
72  }
73  }
74 
75  public static function provideInvalidCode() {
76  return [
77  [ '' ],
78  [ 42 ],
79  [ 'A bad code' ],
80  [ 'Project:A_page_title' ],
81  [ "WTF\nnewlines" ],
82  ];
83  }
84 
89  public function testApiMessage() {
90  $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
91  $msg->inLanguage( 'de' )->title( Title::newMainPage() );
92  $msg2 = new ApiMessage( $msg, 'code', [ 'data' ] );
93  $this->compareMessages( $msg, $msg2 );
94  $this->assertEquals( 'code', $msg2->getApiCode() );
95  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
96 
97  $msg2 = unserialize( serialize( $msg2 ) );
98  $this->compareMessages( $msg, $msg2 );
99  $this->assertEquals( 'code', $msg2->getApiCode() );
100  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
101 
102  $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
103  $msg2 = new ApiMessage( [ [ 'foo', 'bar' ], 'baz' ], 'code', [ 'data' ] );
104  $this->compareMessages( $msg, $msg2 );
105  $this->assertEquals( 'code', $msg2->getApiCode() );
106  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
107 
108  $msg = new Message( 'foo' );
109  $msg2 = new ApiMessage( 'foo' );
110  $this->compareMessages( $msg, $msg2 );
111  $this->assertEquals( 'foo', $msg2->getApiCode() );
112  $this->assertEquals( [], $msg2->getApiData() );
113 
114  $msg2->setApiCode( 'code', [ 'data' ] );
115  $this->assertEquals( 'code', $msg2->getApiCode() );
116  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
117  $msg2->setApiCode( null );
118  $this->assertEquals( 'foo', $msg2->getApiCode() );
119  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
120  $msg2->setApiData( [ 'data2' ] );
121  $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
122  }
123 
128  public function testApiRawMessage() {
129  $msg = new RawMessage( 'foo', [ 'baz' ] );
130  $msg->inLanguage( 'de' )->title( Title::newMainPage() );
131  $msg2 = new ApiRawMessage( $msg, 'code', [ 'data' ] );
132  $this->compareMessages( $msg, $msg2 );
133  $this->assertEquals( 'code', $msg2->getApiCode() );
134  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
135 
136  $msg2 = unserialize( serialize( $msg2 ) );
137  $this->compareMessages( $msg, $msg2 );
138  $this->assertEquals( 'code', $msg2->getApiCode() );
139  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
140 
141  $msg = new RawMessage( 'foo', [ 'baz' ] );
142  $msg2 = new ApiRawMessage( [ 'foo', 'baz' ], 'code', [ 'data' ] );
143  $this->compareMessages( $msg, $msg2 );
144  $this->assertEquals( 'code', $msg2->getApiCode() );
145  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
146 
147  $msg = new RawMessage( 'foo' );
148  $msg2 = new ApiRawMessage( 'foo', 'code', [ 'data' ] );
149  $this->compareMessages( $msg, $msg2 );
150  $this->assertEquals( 'code', $msg2->getApiCode() );
151  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
152 
153  $msg2->setApiCode( 'code', [ 'data' ] );
154  $this->assertEquals( 'code', $msg2->getApiCode() );
155  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
156  $msg2->setApiCode( null );
157  $this->assertEquals( 'foo', $msg2->getApiCode() );
158  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
159  $msg2->setApiData( [ 'data2' ] );
160  $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
161  }
162 
166  public function testApiMessageCreate() {
167  $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( new Message( 'mainpage' ) ) );
168  $this->assertInstanceOf(
170  );
171  $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( 'mainpage' ) );
172 
173  $msg = new ApiMessage( [ 'parentheses', 'foobar' ] );
174  $msg2 = new Message( 'parentheses', [ 'foobar' ] );
175 
176  $this->assertSame( $msg, ApiMessage::create( $msg ) );
177  $this->assertEquals( $msg, ApiMessage::create( $msg2 ) );
178  $this->assertEquals( $msg, ApiMessage::create( [ 'parentheses', 'foobar' ] ) );
179  $this->assertEquals( $msg,
180  ApiMessage::create( [ 'message' => 'parentheses', 'params' => [ 'foobar' ] ] )
181  );
182  $this->assertSame( $msg,
183  ApiMessage::create( [ 'message' => $msg, 'params' => [ 'xxx' ] ] )
184  );
185  $this->assertEquals( $msg,
186  ApiMessage::create( [ 'message' => $msg2, 'params' => [ 'xxx' ] ] )
187  );
188  $this->assertSame( $msg,
189  ApiMessage::create( [ 'message' => $msg ] )
190  );
191 
192  $msg = new ApiRawMessage( [ 'parentheses', 'foobar' ] );
193  $this->assertSame( $msg, ApiMessage::create( $msg ) );
194  }
195 
196 }
ApiMessageTest
API.
Definition: ApiMessageTest.php:8
ApiMessageTest\testInvalidCode
testInvalidCode( $code)
ApiMessageTrait provideInvalidCode.
Definition: ApiMessageTest.php:58
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:632
serialize
serialize()
Definition: ApiMessageTrait.php:134
ApiMessageTest\testApiMessageCreate
testApiMessageCreate()
ApiMessage::create.
Definition: ApiMessageTest.php:166
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiMessageTest\testCodeDefaults
testCodeDefaults()
ApiMessageTrait.
Definition: ApiMessageTest.php:31
ApiRawMessage
Extension of RawMessage implementing IApiMessage.
Definition: ApiRawMessage.php:26
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:26
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:780
ApiMessageTest\testApiMessage
testApiMessage()
ApiMessage ApiMessageTrait.
Definition: ApiMessageTest.php:89
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40
ApiMessageTest\testApiRawMessage
testApiRawMessage()
ApiRawMessage ApiMessageTrait.
Definition: ApiMessageTest.php:128
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
ApiMessageTest\provideInvalidCode
static provideInvalidCode()
Definition: ApiMessageTest.php:75
ApiMessageTest\compareMessages
compareMessages(Message $msg, Message $msg2)
Definition: ApiMessageTest.php:10