MediaWiki  1.29.1
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  // 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 }
ApiMessageTest
API.
Definition: ApiMessageTest.php:8
ApiMessageTest\testInvalidCode
testInvalidCode( $code)
ApiMessageTrait provideInvalidCode.
Definition: ApiMessageTest.php:54
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:559
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
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
serialize
serialize()
Definition: ApiMessage.php:177
ApiMessageTest\testApiMessageCreate
testApiMessageCreate()
ApiMessage::create.
Definition: ApiMessageTest.php:159
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: ApiMessage.php:268
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:198
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
ApiMessageTest\testApiMessage
testApiMessage()
ApiMessage ApiMessageTrait.
Definition: ApiMessageTest.php:82
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:212
ApiMessageTest\testApiRawMessage
testApiRawMessage()
ApiRawMessage ApiMessageTrait.
Definition: ApiMessageTest.php:121
$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 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:783
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
ApiMessageTest\provideInvalidCode
static provideInvalidCode()
Definition: ApiMessageTest.php:71
ApiMessageTest\compareMessages
compareMessages(Message $msg, Message $msg2)
Definition: ApiMessageTest.php:10