MediaWiki REL1_31
ApiModuleManagerTest.php
Go to the documentation of this file.
1<?php
2
11
12 private function getModuleManager() {
13 $request = new FauxRequest();
14 $main = new ApiMain( $request );
15 return new ApiModuleManager( $main );
16 }
17
18 public function newApiLogin( $main, $action ) {
19 return new ApiLogin( $main, $action );
20 }
21
22 public function addModuleProvider() {
23 return [
24 'plain class' => [
25 'login',
26 'action',
27 ApiLogin::class,
28 null,
29 ],
30
31 'with factory' => [
32 'login',
33 'action',
34 ApiLogin::class,
35 [ $this, 'newApiLogin' ],
36 ],
37
38 'with closure' => [
39 'logout',
40 'action',
41 ApiLogout::class,
42 function ( ApiMain $main, $action ) {
43 return new ApiLogout( $main, $action );
44 },
45 ],
46 ];
47 }
48
52 public function testAddModule( $name, $group, $class, $factory = null ) {
53 $moduleManager = $this->getModuleManager();
54 $moduleManager->addModule( $name, $group, $class, $factory );
55
56 $this->assertTrue( $moduleManager->isDefined( $name, $group ), 'isDefined' );
57 $this->assertNotNull( $moduleManager->getModule( $name, $group, true ), 'getModule' );
58 }
59
60 public function addModulesProvider() {
61 return [
62 'empty' => [
63 [],
64 'action',
65 ],
66
67 'simple' => [
68 [
69 'login' => ApiLogin::class,
70 'logout' => ApiLogout::class,
71 ],
72 'action',
73 ],
74
75 'with factories' => [
76 [
77 'login' => [
78 'class' => ApiLogin::class,
79 'factory' => [ $this, 'newApiLogin' ],
80 ],
81 'logout' => [
82 'class' => ApiLogout::class,
83 'factory' => function ( ApiMain $main, $action ) {
84 return new ApiLogout( $main, $action );
85 },
86 ],
87 ],
88 'action',
89 ],
90 ];
91 }
92
96 public function testAddModules( array $modules, $group ) {
97 $moduleManager = $this->getModuleManager();
98 $moduleManager->addModules( $modules, $group );
99
100 foreach ( array_keys( $modules ) as $name ) {
101 $this->assertTrue( $moduleManager->isDefined( $name, $group ), 'isDefined' );
102 $this->assertNotNull( $moduleManager->getModule( $name, $group, true ), 'getModule' );
103 }
104
105 $this->assertTrue( true ); // Don't mark the test as risky if $modules is empty
106 }
107
108 public function getModuleProvider() {
109 $modules = [
110 'feedrecentchanges' => ApiFeedRecentChanges::class,
111 'feedcontributions' => [ 'class' => ApiFeedContributions::class ],
112 'login' => [
113 'class' => ApiLogin::class,
114 'factory' => [ $this, 'newApiLogin' ],
115 ],
116 'logout' => [
117 'class' => ApiLogout::class,
118 'factory' => function ( ApiMain $main, $action ) {
119 return new ApiLogout( $main, $action );
120 },
121 ],
122 ];
123
124 return [
125 'legacy entry' => [
126 $modules,
127 'feedrecentchanges',
128 ApiFeedRecentChanges::class,
129 ],
130
131 'just a class' => [
132 $modules,
133 'feedcontributions',
134 ApiFeedContributions::class,
135 ],
136
137 'with factory' => [
138 $modules,
139 'login',
140 ApiLogin::class,
141 ],
142
143 'with closure' => [
144 $modules,
145 'logout',
146 ApiLogout::class,
147 ],
148 ];
149 }
150
155 public function testGetModule( $modules, $name, $expectedClass ) {
156 $moduleManager = $this->getModuleManager();
157 $moduleManager->addModules( $modules, 'test' );
158
159 // should return the right module
160 $module1 = $moduleManager->getModule( $name, null, false );
161 $this->assertInstanceOf( $expectedClass, $module1 );
162
163 // should pass group check (with caching disabled)
164 $module2 = $moduleManager->getModule( $name, 'test', true );
165 $this->assertNotNull( $module2 );
166
167 // should use cached instance
168 $module3 = $moduleManager->getModule( $name, null, false );
169 $this->assertSame( $module1, $module3 );
170
171 // should not use cached instance if caching is disabled
172 $module4 = $moduleManager->getModule( $name, null, true );
173 $this->assertNotSame( $module1, $module4 );
174 }
175
179 public function testGetModule_null() {
180 $modules = [
181 'login' => ApiLogin::class,
182 'logout' => ApiLogout::class,
183 ];
184
185 $moduleManager = $this->getModuleManager();
186 $moduleManager->addModules( $modules, 'test' );
187
188 $this->assertNull( $moduleManager->getModule( 'quux' ), 'unknown name' );
189 $this->assertNull( $moduleManager->getModule( 'login', 'bla' ), 'wrong group' );
190 }
191
195 public function testGetNames() {
196 $fooModules = [
197 'login' => ApiLogin::class,
198 'logout' => ApiLogout::class,
199 ];
200
201 $barModules = [
202 'feedcontributions' => [ 'class' => ApiFeedContributions::class ],
203 'feedrecentchanges' => [ 'class' => ApiFeedRecentChanges::class ],
204 ];
205
206 $moduleManager = $this->getModuleManager();
207 $moduleManager->addModules( $fooModules, 'foo' );
208 $moduleManager->addModules( $barModules, 'bar' );
209
210 $fooNames = $moduleManager->getNames( 'foo' );
211 $this->assertArrayEquals( array_keys( $fooModules ), $fooNames );
212
213 $allNames = $moduleManager->getNames();
214 $allModules = array_merge( $fooModules, $barModules );
215 $this->assertArrayEquals( array_keys( $allModules ), $allNames );
216 }
217
221 public function testGetNamesWithClasses() {
222 $fooModules = [
223 'login' => ApiLogin::class,
224 'logout' => ApiLogout::class,
225 ];
226
227 $barModules = [
228 'feedcontributions' => [ 'class' => ApiFeedContributions::class ],
229 'feedrecentchanges' => [ 'class' => ApiFeedRecentChanges::class ],
230 ];
231
232 $moduleManager = $this->getModuleManager();
233 $moduleManager->addModules( $fooModules, 'foo' );
234 $moduleManager->addModules( $barModules, 'bar' );
235
236 $fooNamesWithClasses = $moduleManager->getNamesWithClasses( 'foo' );
237 $this->assertArrayEquals( $fooModules, $fooNamesWithClasses );
238
239 $allNamesWithClasses = $moduleManager->getNamesWithClasses();
240 $allModules = array_merge( $fooModules, [
241 'feedcontributions' => ApiFeedContributions::class,
242 'feedrecentchanges' => ApiFeedRecentChanges::class,
243 ] );
244 $this->assertArrayEquals( $allModules, $allNamesWithClasses );
245 }
246
250 public function testGetModuleGroup() {
251 $fooModules = [
252 'login' => ApiLogin::class,
253 'logout' => ApiLogout::class,
254 ];
255
256 $barModules = [
257 'feedcontributions' => [ 'class' => ApiFeedContributions::class ],
258 'feedrecentchanges' => [ 'class' => ApiFeedRecentChanges::class ],
259 ];
260
261 $moduleManager = $this->getModuleManager();
262 $moduleManager->addModules( $fooModules, 'foo' );
263 $moduleManager->addModules( $barModules, 'bar' );
264
265 $this->assertEquals( 'foo', $moduleManager->getModuleGroup( 'login' ) );
266 $this->assertEquals( 'bar', $moduleManager->getModuleGroup( 'feedrecentchanges' ) );
267 $this->assertNull( $moduleManager->getModuleGroup( 'quux' ) );
268 }
269
273 public function testGetGroups() {
274 $fooModules = [
275 'login' => ApiLogin::class,
276 'logout' => ApiLogout::class,
277 ];
278
279 $barModules = [
280 'feedcontributions' => [ 'class' => ApiFeedContributions::class ],
281 'feedrecentchanges' => [ 'class' => ApiFeedRecentChanges::class ],
282 ];
283
284 $moduleManager = $this->getModuleManager();
285 $moduleManager->addModules( $fooModules, 'foo' );
286 $moduleManager->addModules( $barModules, 'bar' );
287
288 $groups = $moduleManager->getGroups();
289 $this->assertArrayEquals( [ 'foo', 'bar' ], $groups );
290 }
291
295 public function testGetClassName() {
296 $fooModules = [
297 'login' => ApiLogin::class,
298 'logout' => ApiLogout::class,
299 ];
300
301 $barModules = [
302 'feedcontributions' => [ 'class' => ApiFeedContributions::class ],
303 'feedrecentchanges' => [ 'class' => ApiFeedRecentChanges::class ],
304 ];
305
306 $moduleManager = $this->getModuleManager();
307 $moduleManager->addModules( $fooModules, 'foo' );
308 $moduleManager->addModules( $barModules, 'bar' );
309
310 $this->assertEquals(
311 ApiLogin::class,
312 $moduleManager->getClassName( 'login' )
313 );
314 $this->assertEquals(
315 ApiLogout::class,
316 $moduleManager->getClassName( 'logout' )
317 );
318 $this->assertEquals(
319 ApiFeedContributions::class,
320 $moduleManager->getClassName( 'feedcontributions' )
321 );
322 $this->assertEquals(
323 ApiFeedRecentChanges::class,
324 $moduleManager->getClassName( 'feedrecentchanges' )
325 );
326 $this->assertFalse(
327 $moduleManager->getClassName( 'nonexistentmodule' )
328 );
329 }
330}
Unit to authenticate log-in attempts to the current wiki.
Definition ApiLogin.php:34
API module to allow users to log out of the wiki.
Definition ApiLogout.php:31
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:43
testGetModule_null()
ApiModuleManager::getModule.
testGetModule( $modules, $name, $expectedClass)
ApiModuleManager::getModule getModuleProvider.
testGetModuleGroup()
ApiModuleManager::getModuleGroup.
testAddModule( $name, $group, $class, $factory=null)
addModuleProvider
testGetNamesWithClasses()
ApiModuleManager::getNamesWithClasses.
testGetNames()
ApiModuleManager::getNames.
testGetClassName()
ApiModuleManager::getClassName.
testAddModules(array $modules, $group)
addModulesProvider
testGetGroups()
ApiModuleManager::getGroups.
This class holds a list of modules and handles instantiation.
WebRequest clone which takes values from a provided array.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806